2024-08-31
At its heart, rsync synchronizes files and directories between a source and a destination. It intelligently identifies only the differences between the two, transferring only the necessary data. This makes it remarkably efficient, especially for large files or frequent backups.
The basic syntax is:
rsync [OPTIONS] source destinationLet’s start with a simple local backup. Suppose you want to back up your /home/user/Documents directory to /mnt/backup/documents:
rsync -avz /home/user/Documents /mnt/backup/documents-a: Archive mode. This recursively copies directories, preserves symbolic links, permissions, timestamps, etc.-v: Verbose mode. Shows detailed progress information.-z: Compression. Compresses the data during transfer, saving space and bandwidth.This command creates a complete backup of your Documents directory. Subsequent backups can use rsync’s incremental nature:
rsync -avz --delete /home/user/Documents /mnt/backup/documentsThe --delete option is crucial. It ensures that files deleted from the source are also deleted from the destination, keeping your backup perfectly synchronized.
Rsync shines when backing up to remote servers. To back up /home/user/Documents to a remote server at user@remote_server:/backup/documents, use:
rsync -avz /home/user/Documents user@remote_server:/backup/documentsThis uses SSH to securely transfer the data. Make sure you have SSH access configured correctly.
For enhanced security, consider using SSH keys instead of passwords:
ssh-keygen~/.ssh/authorized_keys file.rsync command will use key-based authentication without prompting for a password.Rsync allows fine-grained control over what gets backed up. You can exclude specific files or directories using the --exclude option:
rsync -avz --exclude "*.tmp" --exclude "cache/*" /home/user/Documents user@remote_server:/backup/documentsThis excludes all .tmp files and the entire cache directory from the backup. Multiple --exclude options can be used.
Rsync automatically handles incremental backups. Only the changed files and directories are transferred during subsequent runs. Furthermore, if a transfer is interrupted (e.g., due to network issues), rsync can resume from where it left off without re-transferring already copied data. This robustness is a significant advantage over simpler copy methods.
For automated backups, schedule rsync using cron. Create a cron job (e.g., using crontab -e) to run your backup command at specific intervals. For example, to run the remote backup daily at 3 AM:
0 3 * * * rsync -avz /home/user/Documents user@remote_server:/backup/documents >> /var/log/rsync_backup.log 2>&1
This logs the output to /var/log/rsync_backup.log, allowing you to monitor the backup process.
Restoring from a rsync backup is straightforward. Simply reverse the source and destination in your rsync command, ensuring you use the appropriate options to preserve file attributes during the restoration.