Complete Guide to rsync Commands in Linux. rsync is an essential tool for Linux administrators and developers.
With its help, you can perform quick backups, file transfers via SSH, automated synchronizations and many more, all with minimal resource consumption.
Understanding rsync commands and integrating them into your daily processes can significantly improve security and efficiency of data management.
What is rsync?
rsync is a command-line tool in Linux used for synchronizing files and directories between two locations, either locally or remotely.
It is appreciated for its efficiency, as it only transfers modified files, saving time and bandwidth.
Advantages of using rsync
- Incremental file transfer
- Local or SSH synchronization
- File integrity verification through checksum
- Ability to preserve permissions, access times and directory structure
- Ideal for automatic or manual backups
Complete Guide to rsync Commands
1. General syntax of rsync command
rsync [options] source destination
Simple example:
rsync -avh /home/user/documents/ /media/backup/documents/
2. Most commonly used rsync options
-a: archive (preserves permissions, symbolic links, times, etc.)-v: verbose (displays process details)-h: human-readable (displays sizes in easy-to-read format)--progress: shows progress of each file--delete: deletes files from destination that no longer exist in source
3. Local synchronization
Command for synchronizing one local folder with another:
rsync -avh --delete /var/www/html/ /backup/html/
4. Transfer via SSH
To copy files between a local server and a remote one:
rsync -avz -e ssh /home/user/ user@192.168.1.10:/backup/
Explanations:
-z: enables data compression for faster transfer-e ssh: specifies the use of SSH protocol
5. Excluding certain files or directories
rsync -av --exclude 'node_modules' --exclude '*.log' /project/ /backup/project/
Files and directories can be excluded using the --exclude syntax.
6. Bidirectional synchronization?
rsync is unidirectional: it synchronizes from source to destination.
For bidirectional synchronization, custom scripts or tools like unison must be used.
7. Scheduling backups with cron
Example of daily backup at 2:00 AM:
0 2 * * * rsync -a /etc/ /backup/etc/
The line is added to the crontab file with the crontab -e command.
8. Checking without making modifications (dry-run)
rsync -avh --dry-run /home/user/ /backup/user/
The --dry-run option shows you what would happen, without actually copying files.
9. Logging rsync operations
rsync -av --log-file=/var/log/rsync.log /data/ /backup/data/
Logs are useful for debugging or auditing.
10. Synchronizing only recently modified files
find /source -type f -mtime -1 -print0 | rsync -av --files-from=- --from0 / /destination/
This example synchronizes only files modified in the last 24 hours.

Comments (0)