This script automates the synchronization of files between a local system and a remote system using Rsync. It supports both push (local to remote) and pull (remote to local) operations, as well as incremental or full replication. The script provides robust logging, retention policies, and the ability to customize Rsync behavior with user-defined flags for maximum flexibility.
- Flexible Replication: Supports both local-to-remote (push) and remote-to-local (pull) file synchronization.
- Incremental and Full Backups: Choose between syncing only changed files (incremental) or performing a full copy of the data (mirror).
- Customizable Rsync Flags: Modify short and long Rsync flags to tailor the transfer to your needs.
- Retention Policies: Automatically manage old backups using time, count, or storage-based retention policies to prevent excessive storage usage.
- Log Rotation: Compresses and rotates log files to prevent uncontrolled log growth.
- Automatic Directory Creation: Automatically creates the destination directory if it doesn't exist.
- Concurrency Control: Prevents multiple script instances from running simultaneously using lock files.
- Retry with Exponential Backoff: Automatically retries failed Rsync operations with exponential backoff for transient network or I/O issues.
- Rsync installed on the local system.
- Rsync installed on both the local and remote systems.
- Passwordless SSH access to the remote system.
- Setup Guide: RedHat Passwordless SSH
Edit the following settings in the script to suit your environment:
- Source Directories: Directories to be synchronized.
- Example:
source_directories=("/path/to/source1" "/path/to/source2")
- Example:
- Destination Directory: Directory where the synchronized files will be stored.
- Example:
destination_directory="/path/to/destination"
- Example:
- Rsync Mode: Define the sync direction:
push
(local to remote) orpull
(remote to local).- Example:
rsync_mode="push"
- Example:
- Rsync Type: Define the replication type:
incremental
(sync differences) ormirror
(full replication).- Example:
rsync_type="incremental"
- Example:
rsync_short_args
: Short options like-a
,-v
,-z
.- Example:
local_rsync_short_args="-aH"
- Example:
rsync_long_args
: Long options like--delete
,--checksum
.- Example:
local_rsync_long_args="--delete --numeric-ids --checksum"
- Example:
- Remote User: Username for the remote system.
- Example:
remote_user="username"
- Example:
- Remote Server: IP address or hostname of the remote system.
- Example:
remote_server="192.168.1.100"
- Example:
- Retention Policy: Choose from
time
,count
,storage
, oroff
to manage old backups automatically.- Example:
retention_policy="storage"
- Example:
- Retention Settings:
- Time-Based Retention: Delete backups older than a specified number of days.
- Example:
backup_retention_days=30
- Example:
- Count-Based Retention: Keep only the last X backups.
- Example:
backup_retention_count=7
- Example:
- Storage-Based Retention: Delete old backups when storage exceeds a limit.
- Example:
backup_max_storage="100G"
- Example:
- Time-Based Retention: Delete backups older than a specified number of days.
- Log File Path: Define where logs will be stored.
- Example:
log_file="/path/to/logfile.log"
- Example:
- The script creates a lock file to ensure only one instance of the script runs at a time. The lock file is automatically removed when the script finishes or is interrupted.
The script supports full customization of Rsync’s behavior:
rsync_short_args
: Short Rsync options for efficiency (e.g.,-a
,-v
,-z
).rsync_long_args
: Long Rsync options for granular control (e.g.,--delete
,--checksum
).- Exponential Backoff: Automatically retries Rsync with increasing delays when network issues or transient errors occur.
- Incremental Backups: Uses
--link-dest
to create incremental backups, saving space and bandwidth.
- Push Mode Backup: Sync local files to a remote server.
- Pull Mode Restore: Restore files from a remote server to a local system.
- Incremental Backups: Efficiently back up only the differences between source and destination.
- Full Mirror Backup: Create an exact replica of the source at the destination.
- Automated Backup Retention: Manage storage and prevent accumulation of old backups with retention policies.
-
Create the Script File:
cd /opt/scripts/ sudo nano rsync_replication.sh
Paste the script contents into the file and save.
-
Make the Script Executable:
sudo chmod +x /opt/scripts/rsync_replication.sh
-
Run the Script:
sudo /opt/scripts/rsync_replication.sh
To automate the backup process, configure the script to run as a cron job.
-
Open the cron job configuration for the current user:
crontab -e
-
Add an entry to run the script at the desired interval:
0 0 * * * /path/to/rsync_replication.sh >> /path/to/logfile.log 2>&1
This example runs the script every day at midnight.
Should your backups be needed for any reason restoring data is easy. Simply just adjust the variables of the script as needed to push/pull data in the opposite direction you had originally configured it for. Just make sure you include the timestamped directory as the source like so:
source_directories=("/path/to/backup/source/2023-09-01_0000")
Logs are written to the specified log file with detailed messages, including timestamps, errors, and Rsync operations. Old logs are automatically rotated and compressed to avoid excessive disk usage.
This script is provided under the MIT License.