rsync
Overview
The rsync
command is a fast and versatile file synchronization tool that efficiently transfers and synchronizes files between locations, locally or over a network.
Syntax
rsync [options] source destination
rsync [options] source [source...] destination
Common Options
Option | Description |
---|---|
-a |
Archive mode (preserves permissions, times, etc.) |
-v |
Verbose output |
-r |
Recursive |
-u |
Update (skip newer files) |
-n |
Dry run (show what would be done) |
-z |
Compress during transfer |
-P |
Show progress and keep partial files |
--delete |
Delete files not in source |
--exclude=pattern |
Exclude files matching pattern |
--include=pattern |
Include files matching pattern |
-e ssh |
Use SSH for remote transfers |
Archive Mode Components
The -a
flag includes: - -r
(recursive) - -l
(copy symlinks as symlinks) - -p
(preserve permissions) - -t
(preserve modification times) - -g
(preserve group) - -o
(preserve owner) - -D
(preserve device files and special files)
Key Use Cases
- Backup files and directories
- Synchronize directories
- Mirror websites
- Transfer files over network
- Incremental backups
Examples with Explanations
Example 1: Basic Local Sync
rsync -av source/ destination/
Synchronizes source directory to destination with archive mode
Example 2: Remote Sync via SSH
rsync -avz -e ssh local/ user@server:/remote/path/
Syncs local directory to remote server with compression
Example 3: Dry Run
rsync -avn --delete source/ destination/
Shows what would be synchronized without making changes
Remote Synchronization
Push to remote:
rsync -av local/ user@host:/remote/
Pull from remote:
rsync -av user@host:/remote/ local/
Between remote hosts:
rsync -av host1:/path/ host2:/path/
Advanced Options
Option | Description |
---|---|
--bwlimit=rate |
Limit bandwidth |
--timeout=seconds |
Set timeout |
--partial |
Keep partial files |
--inplace |
Update files in place |
--backup |
Make backups |
--backup-dir=dir |
Backup directory |
--log-file=file |
Log to file |
--stats |
Show transfer statistics |
Common Usage Patterns
Incremental backup:
rsync -av --delete /home/user/ /backup/user/
Exclude patterns:
rsync -av --exclude='*.tmp' --exclude='cache/' source/ dest/
Bandwidth limited transfer:
rsync -av --bwlimit=1000 large_files/ remote:/backup/
Include/Exclude Patterns
Exclude temporary files:
rsync -av --exclude='*.tmp' --exclude='*.log' source/ dest/
Include only specific types:
rsync -av --include='*.txt' --exclude='*' source/ dest/
Complex patterns:
rsync -av --exclude-from=exclude.txt source/ dest/
Performance Analysis
- Delta-sync algorithm (only transfers differences)
- Compression reduces network usage
- Efficient for large files with small changes
- Minimal memory usage
- Good for slow connections
Backup Strategies
Daily incremental:
rsync -av --delete --backup --backup-dir=../backup-$(date +%Y%m%d) source/ dest/
Snapshot backups:
rsync -av --link-dest=../previous source/ current/
Rotating backups:
rsync -av --delete source/ backup/current/
Additional Resources
Best Practices
- Always test with dry run first
- Use archive mode for complete sync
- Implement proper exclude patterns
- Monitor transfer progress
- Verify sync completion
Security Considerations
- Use SSH for remote transfers
- Verify host keys
- Use key-based authentication
- Limit rsync access with restricted shells
- Monitor transfer logs
Troubleshooting
- Permission denied errors
- Network connectivity issues
- Disk space problems
- SSH authentication failures
- Pattern matching issues
Integration Examples
With cron for automated backups:
0 2 * * * rsync -av --delete /home/ /backup/
With find for selective sync:
find source/ -name "*.txt" -print0 | rsync -av --files-from=- --from0 source/ dest/
Monitoring script:
rsync -av --stats source/ dest/ | tee sync.log
Common Patterns
Website deployment:
rsync -avz --delete local_site/ user@server:/var/www/html/
Database backup sync:
rsync -av --compress-level=9 db_backups/ remote:/backups/db/
Media file sync:
rsync -av --progress --partial media/ backup:/media/