Introduction rsync (Remote Sync) is a powerful and efficient file-copying tool used for local and remote data transfer. Unlike cp or scp, rsync minimizes data transfer by only copying the differences between source and destination files, making it an excellent choice for backups and synchronization.
In this mega guide, we will cover:
- Basic syntax of
rsync - Common options and their use cases
- Examples of local and remote file transfer
- Advanced features like compression, deletion, and bandwidth limits
- Automating
rsyncwith cron jobs - Security and performance optimizations
- Troubleshooting common
rsyncissues
1. Understanding rsync and How It Works
rsync works by comparing the source and destination directories, copying only the differences using a delta-transfer algorithm. This makes rsync highly efficient for synchronizing large datasets over a network.
Why Use rsync?
- Efficient synchronization by copying only changes
- Works both locally and remotely
- Preserves file attributes like timestamps, permissions, and symbolic links
- Supports bandwidth limiting and compression
- Secure transfer via SSH
- Can be automated with scripts and cron jobs
2. Basic Syntax of rsync
The general syntax of rsync is:
rsync [options] source destination
Examples:
- Copy a file locally:
rsync file.txt /home/user/backup/ - Copy a directory recursively:
rsync -r /home/user/docs/ /backup/docs/ - Copy a file to a remote system:
rsync file.txt user@remote:/home/user/
3. Commonly Used rsync Options
rsync has many options that control its behavior. Here are the most commonly used:
a) Preserving Attributes (-a)
The -a option (archive mode) preserves symbolic links, permissions, timestamps, and ownership.
rsync -a /source/ /destination/
b) Verbose Output (-v)
Show detailed progress:
rsync -av /source/ /destination/
c) Deleting Extra Files (--delete)
Synchronizes destination by removing extra files not present in the source:
rsync -av --delete /source/ /destination/
d) Compression (-z)
Reduce bandwidth usage:
rsync -avz /source/ user@remote:/destination/
e) Progress Output (--progress)
Shows real-time file transfer progress:
rsync -avz --progress /source/ /destination/
f) Bandwidth Limit (--bwlimit=KB/s)
Limits transfer speed to avoid network congestion:
rsync -avz --bwlimit=1000 /source/ /destination/
g) Partial Transfers (--partial)
Allows resuming interrupted transfers:
rsync -avz --partial /source/ /destination/
h) Dry Run (--dry-run)
Simulates the transfer without making actual changes:
rsync -av --dry-run /source/ /destination/
4. Local File and Directory Sync
a) Copying Files Locally
rsync -av /home/user/documents/ /backup/documents/
b) Mirroring a Directory with Deletion
rsync -av --delete /home/user/docs/ /backup/docs/
5. Remote File Transfer with rsync
a) Copy from Local to Remote
rsync -avz /source/ user@remote:/destination/
b) Copy from Remote to Local
rsync -avz user@remote:/source/ /destination/
c) Using SSH for Secure Transfers (-e ssh)
rsync -avz -e ssh /source/ user@remote:/destination/
d) Specifying a Different SSH Port
rsync -avz -e "ssh -p 2222" /source/ user@remote:/destination/
6. Automating rsync with Cron Jobs
To schedule a daily backup at 2 AM, add this to crontab:
0 2 * * * rsync -avz /source/ user@remote:/destination/
7. Security and Performance Optimizations
a) Secure rsync Transfers with SSH Keys
To avoid password prompts, use SSH key authentication:
ssh-keygen -t rsa
ssh-copy-id user@remote
Now, rsync can run without asking for a password.
b) Using Checksum-Based Sync (-c)
If timestamps are unreliable, use checksum-based comparison:
rsync -avz -c /source/ /destination/
c) Speeding Up Transfers with --ignore-existing
Skip files that already exist at the destination:
rsync -av --ignore-existing /source/ /destination/
8. Troubleshooting Common rsync Issues
a) Permission Denied Errors
Use sudo if needed:
sudo rsync -av /source/ /destination/
b) Connection Timeout Issues
Increase SSH timeout:
rsync -avz -e "ssh -o ConnectTimeout=30" /source/ user@remote:/destination/
c) Debugging rsync Transfers
Use -vv for more detailed output:
rsync -avvv /source/ /destination/
9. Conclusion
rsync is a robust and efficient tool for file synchronization and backups. With its various options, it provides flexibility for both simple file transfers and advanced automation. By mastering rsync, sysadmins can efficiently manage data across local and remote systems with minimal overhead.