Skip to content

Instantly share code, notes, and snippets.

@filipnet
Created October 31, 2024 19:55
Show Gist options
  • Select an option

  • Save filipnet/8b24157c959debc6f4ced44d05b368be to your computer and use it in GitHub Desktop.

Select an option

Save filipnet/8b24157c959debc6f4ced44d05b368be to your computer and use it in GitHub Desktop.
This Bash script automates the process of synchronizing files from a remote server to a local directory using rsync. It includes options for preserving file permissions, ownership, and group ownership, while also providing options for compression, deletion of old files, and recursive copying of directories.
#!/bin/bash
# Parameters
REMOTE_PATH="/home" # Source path on the remote server
LOCAL_PATH="/mnt/sdb/" # Destination path on the local system "DO NOT REPEAT" destination folder
CERT_PATH="/root/id_rsa_root_hostname.priv.crt" # Path to the SSH certificate
REMOTE_SERVER="host.domain.com" # Hostname or IP address of the remote server
CHECK_MODE="--dry-run" # Use "--dry-run" for a dry run, leave empty for normal operation
# File permission options
PERMISSION_OPTIONS="--no-perms --no-owner --no-group" # Do not transfer permissions, ownership, or group ownership
# Additional options
DELETE_OPTION="--delete" # Delete files in the destination that are no longer present in the source
COMPRESSION_OPTION="--compress" # Enable compression during transfer
RECURSIVE_OPTION="-r" # Enable recursive copying of directories
# Execute the Rsync command
rsync -avz $CHECK_MODE -e "ssh -i $CERT_PATH" $PERMISSION_OPTIONS $DELETE_OPTION $COMPRESSION_OPTION $RECURSIVE_OPTION "$REMOTE_SERVER:$REMOTE_PATH" "$LOCAL_PATH"
# Provide feedback
if [ "$CHECK_MODE" == "--dry-run" ]; then
echo "Dry-run completed. No files were actually transferred."
else
echo "Sync completed successfully."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment