Created
October 31, 2024 19:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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