Rsync over SSH¶
Overview¶
Rsync is the most efficient tool for file synchronization and transfer. When used with SSH, it provides:
- Delta transfers (only changed parts)
- Resume support
- Compression
- Preservation of permissions, ownership, timestamps
- Bandwidth limiting
┌──────────────────────────────────────────────────────────────────────────┐
│ Rsync Delta Transfer │
│ │
│ Source Destination │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ File: 100 MB │ │ File: 100 MB │ │
│ │ │ Only Changed │ (older version) │ │
│ │ ████████░░░░░░░ │───────────────────────▶│ ████████████████ │ │
│ │ │ Blocks (~1 MB) │ │ │
│ │ (10% changed) │ │ (updated) │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ Traditional copy: 100 MB │
│ Rsync delta: ~10 MB │
│ │
└──────────────────────────────────────────────────────────────────────────┘
Basic Syntax¶
Common Options¶
| Option | Description |
|---|---|
-a | Archive mode (preserves everything) |
-v | Verbose |
-z | Compress during transfer |
-P | Progress + partial (resume) |
--delete | Delete extraneous files from dest |
-n | Dry run (show what would happen) |
-e ssh | Use SSH (default on modern rsync) |
Basic Transfers¶
Local to Remote¶
Remote to Local¶
With Progress¶
Important: Trailing Slashes¶
# With trailing slash: copy CONTENTS of dir
rsync -av /source/ /dest/
# Result: /dest/file1, /dest/file2
# Without trailing slash: copy dir ITSELF
rsync -av /source /dest/
# Result: /dest/source/file1, /dest/source/file2
SSH Options¶
Custom Port¶
Specific Key¶
Through Jump Host¶
Multiple SSH Options¶
Synchronization¶
Mirror (Delete Extra Files)¶
Dangerous
--delete removes files from destination that don't exist in source. Use with caution.
Dry Run First¶
One-Way Sync¶
Exclude Files¶
Exclude from File¶
Include/Exclude Patterns¶
# Only sync *.php and *.html
rsync -avz --include='*.php' --include='*.html' --exclude='*' /local/ user@host:/remote/
Resume Transfers¶
Partial Files¶
rsync -avzP /local/largefile.iso user@host:/remote/
# -P = --partial --progress
# If interrupted, run same command to resume
Partial Directory¶
Bandwidth Control¶
Limit Speed¶
Preserve Options¶
Archive Mode (-a)¶
Equivalent to: -rlptgoD - -r Recursive - -l Preserve symlinks - -p Preserve permissions - -t Preserve times - -g Preserve group - -o Preserve owner - -D Preserve devices and specials
Additional Preservation¶
Hard Links¶
Comparison Options¶
Skip Based on Checksum¶
rsync -avzc /local/ user@host:/remote/
# -c uses checksum instead of time/size (slower but accurate)
Update Only (Skip Newer)¶
Ignore Existing¶
Backup Strategies¶
Simple Backup¶
Incremental with Hard Links¶
rsync -avz --link-dest=/backups/latest /data/ user@backup:/backups/$(date +%Y%m%d)/
ln -sfn /backups/$(date +%Y%m%d) /backups/latest
Space-efficient: unchanged files are hard-linked.
Backup with Rotation¶
#!/bin/bash
# backup.sh
DEST="user@backup:/backups"
LINK_DEST="--link-dest=../latest"
# Rotate
ssh user@backup "rm -rf /backups/backup.3"
ssh user@backup "mv /backups/backup.2 /backups/backup.3 2>/dev/null"
ssh user@backup "mv /backups/backup.1 /backups/backup.2 2>/dev/null"
ssh user@backup "mv /backups/latest /backups/backup.1 2>/dev/null"
# New backup
rsync -avz --delete $LINK_DEST /data/ $DEST/latest/
Logging¶
Verbose Output to File¶
Log File Option¶
Statistics¶
Common Use Cases¶
Website Deployment¶
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='.env' \
/local/project/ user@web:/var/www/site/
Database Backup Sync¶
Photo/Media Sync¶
rsync -avzP \
--include='*.jpg' \
--include='*.mp4' \
--include='*/' \
--exclude='*' \
/media/photos/ user@nas:/photos/
Home Directory Backup¶
rsync -avz \
--exclude='.cache' \
--exclude='Downloads' \
--exclude='.local/share/Trash' \
~/ user@backup:/backups/home/
Troubleshooting¶
Permission Errors¶
# Check user has write permission
ssh user@host "ls -la /remote/path"
# Or use --no-perms
rsync -avz --no-perms /local/ user@host:/remote/
Slow Initial Scan¶
Connection Timeout¶
Character Encoding¶
Debugging¶
Rsync Daemon Mode¶
For better performance (no SSH overhead):
Server Setup¶
# /etc/rsyncd.conf
[backup]
path = /data/backup
read only = no
auth users = backupuser
secrets file = /etc/rsyncd.secrets
Client Usage¶
Security
Rsync daemon mode isn't encrypted. Use SSH for security, or only on trusted networks.
Comparison¶
| Feature | rsync | scp | sftp |
|---|---|---|---|
| Delta transfer | ✅ | ❌ | ❌ |
| Resume | ✅ | ❌ | ✅ |
| Progress | ✅ | ✅ | ✅ |
| Sync/delete | ✅ | ❌ | ❌ |
| Bandwidth limit | ✅ | ✅ | ✅ |
| Preserve perms | ✅ | ✅ | ✅ |
| Complexity | Medium | Low | Low |