Table of Contents
- What is rsync? Overview and Key Features
- Basic rsync Syntax and Key Flags
- Common Backup Scenarios with rsync
- Local Backups (e.g., to an External Drive)
- Remote Backups (e.g., to a Server via SSH)
- Mirroring Directories (Keeping Destinations in Sync)
- Advanced rsync Options for Power Users
- Excluding/Including Files
- Creating Incremental Snapshots with
--link-dest - Bandwidth Limiting and Compression
- Automating Backups with cron
- Best Practices for rsync Backups
- Troubleshooting Common rsync Issues
- Conclusion
- References
1. What is rsync? Overview and Key Features
rsync is a command-line utility for synchronizing files and directories between two sources: local paths, remote servers (via SSH, FTP, or rsync daemon), or even across networks. Its core strength lies in efficiency—unlike tools that copy entire files every time, rsync minimizes data transfer by comparing source and destination files and only sending the differences.
Key Features of rsync:
- Incremental Transfers: Only syncs files that have changed since the last backup.
- Delta Encoding: Transfers only the modified parts of files (e.g., updating a 1GB log file by sending only the new lines added).
- Compression: Reduces bandwidth usage with
--compress(-z). - Preserves File Metadata: Maintains permissions, timestamps, ownership, and symbolic links (via flags like
-afor “archive mode”). - Flexible Synchronization: Supports mirroring (deleting files in the destination not present in the source), partial transfers (resuming interrupted jobs), and exclusion/inclusion rules.
- Cross-Platform Support: Works on Linux, macOS, and Windows (via WSL or Cygwin).
2. Basic rsync Syntax and Key Flags
Before diving into backup scenarios, let’s master the basics. The core syntax of rsync is:
rsync [OPTIONS] SOURCE DESTINATION
SOURCE: Path to the files/directories to back up (local or remote, e.g.,/home/user/documentsoruser@remote:/data).DESTINATION: Path to the target location (local or remote).
Critical Flags for Backups
| Flag | Description |
|---|---|
-a | Archive mode: Preserves permissions, ownership, timestamps, symlinks, and recurses into directories. Essential for backups. |
-v | Verbose: Shows detailed output of files being transferred. |
-z | Compress: Reduces data transfer size (useful for remote backups). |
-h | Human-readable: Displays sizes in KB, MB, GB instead of bytes. |
--delete | Mirror source: Deletes files in DESTINATION that no longer exist in SOURCE. Use with caution! |
--dry-run | Simulate the transfer without making changes. Always test with this first! |
--partial | Keep partial files if the transfer is interrupted (resumes later). |
Example: Basic Local Backup
To back up your Documents folder to an external drive mounted at /mnt/backup:
rsync -avh --dry-run /home/user/Documents /mnt/backup
- The
--dry-runflag lets you preview changes. Remove it to execute the backup. - The destination will contain a
Documentssubfolder (e.g.,/mnt/backup/Documents). To sync contents ofDocumentsdirectly into/mnt/backup, add a trailing slash to the source:/home/user/Documents/.
3. Common Backup Scenarios with rsync
rsync adapts to diverse backup needs. Below are three common scenarios:
Scenario 1: Local Backup to an External Drive
External USB drives or internal secondary disks are ideal for on-site backups.
Steps:
- Mount the external drive (e.g., to
/mnt/ext_drive). - Sync your home directory to the drive:
rsync -avh --delete --partial /home/user /mnt/ext_drive/backups
-a: Preserves all metadata (critical for restoring files).--delete: Ensures the backup mirrors the source (deletes old files in the backup).--partial: Resumes interrupted transfers (avoids re-copying large files).
Scenario 2: Remote Backup via SSH
For off-site backups, sync to a remote server (e.g., a VPS or home server) using SSH. rsync uses SSH by default for remote transfers, ensuring encryption.
Syntax:
rsync -avhz --delete /local/source [email protected]:/remote/destination
-z: Compresses data (speeds up transfers over slow networks).- Example: Backup
Picturesto a remote server:
rsync -avhz --dry-run /home/user/Pictures [email protected]:/mnt/server_backups
Note: For passwordless SSH access, set up SSH keys (run ssh-keygen and ssh-copy-id [email protected]).
Scenario 3: Mirroring Directories
To keep two directories in perfect sync (e.g., a local folder and a network share), use --delete to mirror the source:
rsync -avh --delete /source/dir/ /destination/dir/
- Warning:
--deleteis destructive! Always test with--dry-runfirst to avoid accidental data loss.
4. Advanced rsync Options for Power Users
For complex backups, rsync offers advanced flags to refine behavior:
Excluding/Including Files
Use --exclude or --include to filter files. Create an exclude list in a file (e.g., exclude.txt) for simplicity:
exclude.txt:
*.tmp
node_modules/
secret.docx
Command:
rsync -avh --exclude-from=exclude.txt /source /destination
- To include specific files despite exclusion rules, use
--includebefore--exclude:rsync -avh --include="*.pdf" --exclude="*" /source /destination # Only sync PDFs
Incremental Snapshots with --link-dest
To save space, create time-stamped snapshots where unchanged files are hard-linked to the previous snapshot (only new/changed files take up space).
Example: Daily snapshots of /home/user:
BACKUP_DIR="/mnt/backup/snapshots"
TIMESTAMP=$(date +%Y%m%d)
PREVIOUS_SNAPSHOT="$BACKUP_DIR/$(ls -t $BACKUP_DIR | head -1)" # Get latest snapshot
rsync -avh --link-dest=$PREVIOUS_SNAPSHOT /home/user "$BACKUP_DIR/$TIMESTAMP"
- Each snapshot appears as a full backup but shares unchanged files via hard links, reducing disk usage.
Bandwidth Limiting
Avoid saturating your network with --bwlimit=RATE (e.g., --bwlimit=1000 for 1000 KB/s):
rsync -avhz --bwlimit=500 user@remote:/data /local/dest # Limit to 500 KB/s
5. Automating Backups with cron
Manual backups are error-prone. Use cron to schedule rsync jobs automatically.
Step 1: Create a Backup Script
Write a script (e.g., backup_script.sh) to handle the backup, logging, and error checking:
#!/bin/bash
# /home/user/backup_script.sh
SOURCE="/home/user"
DEST="/mnt/ext_drive/backups"
LOG_FILE="/var/log/rsync_backup.log"
DATE=$(date +"%Y-%m-%d %H:%M:%S")
# Check if destination is mounted
if ! mountpoint -q "$DEST"; then
echo "[$DATE] Error: Destination $DEST not mounted. Backup failed." >> "$LOG_FILE"
exit 1
fi
# Run rsync
rsync -avh --delete --partial "$SOURCE" "$DEST" >> "$LOG_FILE" 2>&1
# Check if rsync succeeded
if [ $? -eq 0 ]; then
echo "[$DATE] Backup completed successfully." >> "$LOG_FILE"
else
echo "[$DATE] Backup failed with errors." >> "$LOG_FILE"
fi
Step 2: Make the Script Executable
chmod +x /home/user/backup_script.sh
Step 3: Schedule with cron
Edit the crontab to run the script daily at 2 AM:
crontab -e
Add this line:
0 2 * * * /home/user/backup_script.sh
0 2 * * *= “At 02:00 AM, every day.”- Check logs at
/var/log/rsync_backup.logto verify success.
6. Best Practices for rsync Backups
To ensure your backups are reliable:
- Test Backups Regularly: Restore a file to confirm it works (e.g.,
rsync -avh /mnt/backup/Documents/file.docx /tmp/test_restore/). - Encrypt Sensitive Data: Use SSH for remote backups (encrypts transfers) or encrypt the backup directory with LUKS (for local drives).
- Keep Multiple Copies: Follow the 3-2-1 rule: 3 copies of data, 2 on different media, 1 off-site.
- Monitor Backups: Use tools like
logwatchto email log summaries, or check exit codes in scripts (e.g.,if [ $? -ne 0 ]; then send alert; fi). - Avoid Common Pitfalls:
- Always use
--dry-runbefore--delete. - Never rely on a single backup (e.g., a failed drive could lose all data).
- Back up critical system files (e.g.,
/etc,/var/lib/docker) in addition to user data.
- Always use
7. Troubleshooting Common rsync Issues
Issue 1: “Permission Denied” Errors
Cause: The user running rsync lacks read access to the source or write access to the destination.
Fix: Use sudo for system files, or adjust permissions with chmod/chown.
Issue 2: Slow Remote Transfers
Cause: Unnecessary compression or large files.
Fix:
- Omit
-zif the network is fast (compression adds CPU overhead). - Use
--partialto resume large files.
Issue 3: Files Not Being Deleted with --delete
Cause: The --delete flag is not applied recursively, or DEST has a trailing slash mismatch.
Fix: Ensure the source/destination paths are consistent (e.g., both have trailing slashes or neither).
Issue 4: “Disk Full” Errors
Cause: Insufficient space in DEST.
Fix: Clean up old backups or use --link-dest snapshots to reduce disk usage.
8. Conclusion
rsync is a Swiss Army knife for Linux backups, combining efficiency, flexibility, and reliability. By mastering its syntax, leveraging advanced features like incremental snapshots, and automating with cron, you can build a backup system that protects your data against loss.
Remember: The best backup strategy is one you actually use. Start small (e.g., a daily local backup), test rigorously, and expand to remote/off-site backups as needed. With rsync, you’re in control.
9. References
- rsync Official Documentation (man page)
- Cron Guide (Arch Linux Wiki)
- SSH Key Setup (DigitalOcean)
- rsync Snapshot Tutorial (Arch Linux Wiki)