#!/usr/bin/env bash set -euo pipefail # Safe sync: development -> production code, plus optional archive transfer. # # Example: # scripts/ops/netgescon-sync-prod-update.sh \ # --src /home/michele/netgescon/netgescon-laravel \ # --dst /var/www/netgescon \ # --archive-src /home/michele/backup-completo \ # --archive-dst /mnt/nas-backup-produzione \ # --post-deploy SRC_DIR="/home/michele/netgescon/netgescon-laravel" DST_DIR="/var/www/netgescon" ARCHIVE_SRC="" ARCHIVE_DST="" POST_DEPLOY="no" DRY_RUN="no" while [[ $# -gt 0 ]]; do case "$1" in --src) SRC_DIR="$2" shift 2 ;; --dst) DST_DIR="$2" shift 2 ;; --archive-src) ARCHIVE_SRC="$2" shift 2 ;; --archive-dst) ARCHIVE_DST="$2" shift 2 ;; --post-deploy) POST_DEPLOY="yes" shift 1 ;; --dry-run) DRY_RUN="yes" shift 1 ;; *) echo "Unknown option: $1" echo "Usage: $0 [--src ] [--dst ] [--archive-src ] [--archive-dst ] [--post-deploy] [--dry-run]" exit 1 ;; esac done if [[ ! -d "$SRC_DIR" || ! -f "$SRC_DIR/artisan" ]]; then echo "Error: source app not valid: $SRC_DIR" exit 1 fi if [[ ! -d "$DST_DIR" || ! -f "$DST_DIR/artisan" ]]; then echo "Error: destination app not valid: $DST_DIR" exit 1 fi RSYNC_ARGS=( -rlvh --update --checksum --no-perms --no-owner --no-group --omit-dir-times --no-times --exclude=.git/ --exclude=.env --exclude=storage/ --exclude=node_modules/ --exclude=vendor/ --exclude=bootstrap/cache/ --exclude=public/build/ ) if [[ "$DRY_RUN" == "yes" ]]; then RSYNC_ARGS+=(--dry-run) fi echo "Syncing app updates: $SRC_DIR -> $DST_DIR" rsync "${RSYNC_ARGS[@]}" "$SRC_DIR/" "$DST_DIR/" if [[ -n "$ARCHIVE_SRC" || -n "$ARCHIVE_DST" ]]; then if [[ -z "$ARCHIVE_SRC" || -z "$ARCHIVE_DST" ]]; then echo "Error: use both --archive-src and --archive-dst together" exit 1 fi if [[ ! -d "$ARCHIVE_SRC" ]]; then echo "Error: archive source does not exist: $ARCHIVE_SRC" exit 1 fi mkdir -p "$ARCHIVE_DST" echo "Syncing archives (non-destructive): $ARCHIVE_SRC -> $ARCHIVE_DST" ARCHIVE_ARGS=(-avh --ignore-existing) if [[ "$DRY_RUN" == "yes" ]]; then ARCHIVE_ARGS+=(--dry-run) fi rsync "${ARCHIVE_ARGS[@]}" "$ARCHIVE_SRC/" "$ARCHIVE_DST/" fi if [[ "$POST_DEPLOY" == "yes" && "$DRY_RUN" == "no" ]]; then echo "Running post-deploy commands in $DST_DIR" pushd "$DST_DIR" >/dev/null composer install --no-dev --optimize-autoloader php artisan optimize:clear php artisan config:cache || true php artisan event:cache || true php artisan view:cache || true popd >/dev/null fi echo "Sync completed." if [[ "$DRY_RUN" == "yes" ]]; then echo "Dry-run mode: no file changed." fi