rsync
is one of the most powerful and efficient tools for synchronizing files and directories between local and remote systems in Linux. It’s used widely by system administrators, developers, and DevOps engineers for backup, replication, and deployment tasks.
📌 What is rsync
?
rsync
(Remote Sync) is a fast and versatile file-copying tool. It only copies differences between source and destination, making it bandwidth-efficient and smart.
⚙️ Installing rsync
On most Linux systems, it’s pre-installed. If not:
# Ubuntu/Debian
sudo apt install rsync
# RHEL/CentOS/Fedora
sudo dnf install rsync
🧪 Basic Syntax
rsync [options] source destination
🚀 Local File Sync Examples
1. Sync a directory to another local folder
rsync -av /home/user/docs/ /backup/docs/
✅ Explanation:
-a
: Archive mode (preserves permissions, timestamps, etc.)-v
: Verbose- The trailing
/
on source ensures contents are copied, not the folder itself
🌐 Remote Sync (via SSH)
2. Sync local to remote
rsync -avz /home/user/projects/ user@remote-server:/home/user/backup/
3. Sync remote to local
rsync -avz user@remote-server:/home/user/backup/ ./local-backup/
✅ Extra flag:
-z
: Compress data during transfer (faster over slow connections)
🔁 Dry Run Before Real Sync
Use --dry-run
to preview changes without actually copying:
rsync -av --dry-run /source/ /dest/
🚫 Deleting Extra Files in Destination
Use --delete
to delete files in the destination that no longer exist in the source:
rsync -av --delete /source/ /dest/
⚠️ Be careful — this is destructive!
🧹 Exclude Files or Folders
rsync -av --exclude 'node_modules/' --exclude '*.log' /src/ /dest/
Or use an exclude file:
rsync -av --exclude-from='exclude-list.txt' /src/ /dest/
Contents of exclude-list.txt
:
*.log
tmp/
cache/
🕰️ Sync with Progress and Stats
rsync -avh --progress --stats /src/ /dest/
-h
: Human-readable sizes--progress
: Shows transfer progress--stats
: Shows summary
🔒 Secure with SSH (Custom Port)
If your SSH server is on a non-standard port:
rsync -av -e "ssh -p 2222" /src/ user@host:/dest/
📦 Rsync for Backups (with Timestamps)
Backup with date appended:
rsync -a /data/ /backups/data-$(date +%F)/
🔁 Using Rsync in Cron Jobs
Example daily backup at 2 AM:
0 2 * * * rsync -az /data/ /mnt/backup/ >> /var/log/rsync.log 2>&1
Use crontab -e
to add it.
🧪 Compare Directories (Without Copying)
rsync -avnc /dir1/ /dir2/
Flags:
-n
: Dry run-c
: Use checksums to compare files
🧰 Common Options Cheat Sheet
Option | Description |
---|---|
-a |
Archive mode (recursive + metadata) |
-v |
Verbose output |
-z |
Compress data during transfer |
-h |
Human-readable sizes |
--progress |
Show progress of transfer |
--delete |
Delete files in dest not in source |
--exclude |
Exclude files or patterns |
-e "ssh ..." |
Specify remote shell |
-n |
Dry-run (simulation only) |
-c |
Compare by checksum (not just timestamp) |
🛑 Gotchas & Tips
- Always test with
--dry-run
before syncing to production. - Watch out for trailing slashes:
/src/
vs/src
- Use
--partial
if large transfers may get interrupted - Pair with
cron
for automated backups - Use
--checksum
if you care about byte-level differences
✅ Summary
rsync
is your go-to tool for fast, reliable file synchronization on Linux. With a solid understanding of the basic and advanced options, you can automate everything from local backups to cloud server deployments.