#!/bin/bash # Function to display usage information usage() { echo "Usage: $0 [-n|--dry-run] " echo "Options:" echo " -n, --dry-run Perform a trial run with no changes made" exit 1 } # Default dry run flag DRY_RUN=false # Parse command-line arguments ARGS=$(getopt -o n -l dry-run -n "$0" -- "$@") if [ $? -ne 0 ]; then usage fi eval set -- "$ARGS" # Process arguments while true; do case "$1" in -n|--dry-run) DRY_RUN=true shift ;; --) shift break ;; *) usage ;; esac done # Check if source and destination paths are provided if [ $# -ne 2 ]; then usage fi # Assign input arguments to variables SOURCE_DIR="$1" DEST_DIR="$2" # Validate source directory exists if [ ! -d "$SOURCE_DIR" ]; then echo "Error: Source directory does not exist" exit 1 fi # Create destination directory if it doesn't exist (unless dry run) if [ "$DRY_RUN" = false ]; then mkdir -p "$DEST_DIR" fi # Prepare rsync options RSYNC_OPTS="-av" # Add dry run option if specified if [ "$DRY_RUN" = true ]; then RSYNC_OPTS+=" --dry-run" echo "=== PERFORMING DRY RUN (NO CHANGES WILL BE MADE) ===" fi # Add compression and delete options RSYNC_OPTS+=" -z --delete" # Perform rsync rsync $RSYNC_OPTS "$SOURCE_DIR/" "$DEST_DIR" # Check rsync exit status if [ $? -eq 0 ]; then if [ "$DRY_RUN" = true ]; then echo "Dry run completed successfully. No changes were made." else echo "Synchronization completed successfully" fi else echo "Synchronization failed" exit 1 fi