netgescon-day0/scripts/ops/netgescon-data-sync-nondestructive.sh

75 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Non-destructive data sync (files only) between environments.
# It updates changed files and copies new files, but never deletes destination files.
#
# Usage:
# scripts/ops/netgescon-data-sync-nondestructive.sh \
# --from /home/michele/netgescon/netgescon-laravel/storage/app/amministratori \
# --to /var/www/netgescon/storage/app/amministratori
#
# Add --dry-run for preview.
FROM_DIR=""
TO_DIR=""
DRY_RUN="no"
while [[ $# -gt 0 ]]; do
case "$1" in
--from)
FROM_DIR="$2"
shift 2
;;
--to)
TO_DIR="$2"
shift 2
;;
--dry-run)
DRY_RUN="yes"
shift 1
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 --from <source-dir> --to <dest-dir> [--dry-run]"
exit 1
;;
esac
done
if [[ -z "$FROM_DIR" || -z "$TO_DIR" ]]; then
echo "Usage: $0 --from <source-dir> --to <dest-dir> [--dry-run]"
exit 1
fi
if [[ ! -d "$FROM_DIR" ]]; then
echo "Error: source directory not found: $FROM_DIR"
exit 1
fi
mkdir -p "$TO_DIR"
RSYNC_ARGS=(
-avh
--update
--checksum
--no-owner
--no-group
--exclude=.git/
--exclude=node_modules/
--exclude=vendor/
--exclude=bootstrap/cache/
)
if [[ "$DRY_RUN" == "yes" ]]; then
RSYNC_ARGS+=(--dry-run)
fi
# Trailing slash on source to copy contents into destination.
rsync "${RSYNC_ARGS[@]}" "$FROM_DIR/" "$TO_DIR/"
echo "Sync completed (non-destructive)."
if [[ "$DRY_RUN" == "yes" ]]; then
echo "Dry-run mode: no file changed."
fi