Linux rsync Command
What is the rsync command
rsync (Remote Sync) is a powerful file synchronization tool in Linux systems. It can efficiently synchronize files and directories between local or remote systems. rsync is known for its "incremental transfer" algorithm, which only transfers the differences between the source and destination files, greatly improving file transfer efficiency.
Core features of rsync
- Incremental synchronization: Only transfers the changed portions of files, saving bandwidth and time
- Preserve attributes: Can preserve metadata such as file permissions and timestamps
- Compressed transfer: Supports data compression during transfer, reducing network load
- Flexible exclusion: Can exclude specific files or directories
- Remote support: Securely synchronize remote server files via SSH
rsync basic syntax
rsync [选项] 源文件 目标文件
Common command forms
Local synchronization:
rsync -av /path/to/source/ /path/to/destination/
Synchronize from local to remote:
rsync -avz /local/path/ username@remote_host:/remote/path/
Synchronize from remote to local:
rsync -avz username@remote_host:/remote/path/ /local/path/
Detailed explanation of common rsync options
| Option | Full name | Description |
|---|---|---|
| -a | --archive | Archive mode, preserves all file attributes (equivalent to -rlptgoD) |
| -v | --verbose | Display detailed transfer information |
| -z | --compress | Compress data during transfer |
| -r | --recursive | Recursively copy directories |
| -l | --links | Preserve symbolic links |
| -p | --perms | Preserve file permissions |
| -t | --times | Preserve file modification time |
| -g | --group | Preserve file group |
| -o | --owner | Preserve file owner |
| -D | --devices | Preserve device files (superuser only) |
| -h | --human-readable | Output numbers in human-readable format |
| --progress | Display transfer progress | |
| --delete | Delete files in the destination that are not in the source | |
| --exclude=PATTERN | Exclude files matching PATTERN | |
| --include=PATTERN | Include files matching PATTERN |
rsync practical application examples
Example 1: Local directory synchronization
Example
rsync -av /home/user/docs/ /backup/docs/
Note: The slash after the source directory/is very important:
- With slash: sync the directory contents
- Without slash: sync the directory itself
Example 2: Remote server backup
Example
rsync -avz /data/backups/ user@example.com:/remote/backups/
Example 3: Synchronization with exclude options
Example
rsync -av --exclude='*.tmp' --exclude='log/' /source/ /destination/
Example 4: Keep both ends completely consistent (delete excess files on the target)
Example
rsync advanced usage
1. Using a custom SSH port
Example
2. Bandwidth limit (limited to 500KB/s)
Example
3. Partial transfer (only transfer files larger than 100KB)
Example
4. Scheduled backup script
Example
rsync -avz --delete /important/data/ backup@server:/backups/data/
echo "Backup completed at $(date)" >> /var/log/backup.log
rsync FAQ
Q1: What is the difference between rsync and scp?
- rsync: Incremental sync, only transfers the differences, suitable for regular backups
- scp: Full transfer every time, suitable for one-time file transfers
Q2: How to resume an interrupted rsync transfer?
Use--partialoption to keep partially transferred files:
Example
Q3: How to see what rsync will do without actually executing it?
Use-n(dry-run) option:
Example
Q4: How to improve rsync transfer speed?
- Use
-zoption to enable compression - Use
--compress-level=Nadjust the compression level (1-9) - Disable checksum calculation (trusted networks only):
--no-checksum
Best practice recommendations
- Test first: Use
-noption to do a dry run first - Logging: Add
--log-file=rsync.logto record operations - Permission management: Consider using
--chmodto unify file permissions - Scheduled tasks: Combine with crontab for automatic backups
- Secure transfer: Always perform remote transfers via SSH
Summary
rsync is an essential tool for Linux system administrators and developers. Mastering it allows you to:
- Efficiently complete local and remote file synchronization
- Implement automated backup solutions
- Optimize network data transfer
- Maintain file system consistency
Through this article, you should have mastered the basic usage and advanced techniques of rsync. It is recommended to practice more in your actual work and gradually explore more of rsync's powerful features.
Other extensions
Complete Linux Command List