Rsync (Remote Sync) is a powerful utility for efficiently synchronizing files and directories between different locations, whether on the same machine or across remote systems. This guide covers essential rsync commands and best practices.
What is Rsync?
Rsync is a versatile file synchronization and transfer tool that offers several advantages:
- Performs incremental transfers by copying only changed portions of files
- Supports compression during transfer to reduce bandwidth usage
- Preserves file permissions, ownership, timestamps, and other attributes
- Provides built-in error checking and verification
- Works over SSH for secure remote file transfers
- Supports both local and remote synchronization
Basic Syntax
The basic syntax for rsync follows this pattern:
rsync [options] source destination
For remote transfers:
rsync [options] source [user@]host:destination
rsync [options] [user@]host:source destination
Essential Options
Common rsync options and their meanings:
-a
(archive): Preserves permissions, ownership, timestamps, and recursively copies directories-v
(verbose): Shows detailed output of the transfer process-z
(compress): Compresses data during transfer-h
(human-readable): Displays file sizes in human-readable format-P
(progress): Shows progress during transfer and allows transfer resumption--delete
: Removes files in destination that don’t exist in source-n
or--dry-run
: Simulates the transfer without making changes
Common Usage Examples
1. Basic Local Directory Sync
rsync -avh /source/directory/ /destination/directory/
2. Remote Directory Sync with Progress
rsync -avzh --progress /local/directory/ user@remote:/remote/directory/
3. Sync from Remote to Local
rsync -avzh user@remote:/remote/directory/ /local/directory/
4. Excluding Files and Directories
Create an exclude file (exclude.txt):
*.tmp
temp/
.git/
node_modules/
*.log
Use the exclude file:
rsync -avzh --exclude-from='exclude.txt' source/ destination/
5. Mirror Sync with Deletion
rsync -avzh --delete source/ destination/
6. Bandwidth-Limited Transfer
rsync -avzh --bwlimit=1000 source/ destination/ # Limits to 1000 KB/s
Advanced Features
1. Backup with Timestamp
rsync -avb --backup-dir=/backup/$(date +%Y-%m-%d) source/ destination/
2. Include/Exclude Patterns
rsync -avz --include='*.txt' --exclude='*' source/ destination/
3. Resume Interrupted Transfer
rsync -avzh --partial --progress source/ destination/
Best Practices
- Always Test First: Use
--dry-run
before performing critical synchronizationsrsync -avzhn source/ destination/
- Use Trailing Slashes Correctly:
- With slash
/source/
: Copies contents of the directory - Without slash
/source
: Copies the directory itself
- With slash
- Secure Remote Transfers: Use SSH for secure remote transfers
rsync -avz -e "ssh -p 2222" source/ user@remote:/destination/
- Handle Special Files:
-D
: Preserve device files-l
: Copy symlinks as symlinks-H
: Preserve hard links
Troubleshooting
Common issues and solutions:
- Permission Denied:
- Use sudo when necessary
- Check file permissions
- Verify SSH key permissions
- Network Issues:
- Use
-P
to enable resume capability - Add
--timeout=X
to handle unstable connections
- Use
- Space Issues:
- Use
--max-size
to limit file sizes - Check destination disk space before transfer
- Use
Safety Tips
- Always verify source and destination paths
- Use
--dry-run
before critical operations - Create backups before large synchronizations
- Be careful with
--delete
option - Test restore procedures regularly
Monitoring and Logging
Add logging to your rsync commands:
rsync -avzh --log-file=rsync.log source/ destination/
Performance Optimization
- Use
-z
only for text files over slow connections - Consider using
-W
for local transfers - Adjust
--checksum
usage based on needs - Use
--partial-dir
for large file transfers
Remember to always verify your backups and test restore procedures regularly to ensure data integrity and availability.