Linux rsync Command

Linux 命令大全Complete Linux Command List


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

  1. Incremental synchronization: Only transfers the changed portions of files, saving bandwidth and time
  2. Preserve attributes: Can preserve metadata such as file permissions and timestamps
  3. Compressed transfer: Supports data compression during transfer, reducing network load
  4. Flexible exclusion: Can exclude specific files or directories
  5. 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

# Sync /home/user/docs to /backup/docs, preserving all attributes
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

# Sync the local directory to a remote server using compression
rsync -avz /data/backups/ user@example.com:/remote/backups/

Example 3: Synchronization with exclude options

Example

# Sync but exclude .tmp files and the log directory
rsync -av --exclude='*.tmp' --exclude='log/' /source/ /destination/

Example 4: Keep both ends completely consistent (delete excess files on the target)

Example

rsync -av --delete /source/ /destination/

rsync advanced usage

1. Using a custom SSH port

Example

rsync -avz -e 'ssh -p 2222' /local/path/ user@host:/remote/path/

2. Bandwidth limit (limited to 500KB/s)

Example

rsync -avz --bwlimit=500 /source/ /destination/

3. Partial transfer (only transfer files larger than 100KB)

Example

rsync -av --min-size=100K /source/ /destination/

4. Scheduled backup script

Example

#!/bin/bash
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

rsync -av --partial /source/ /destination/

Q3: How to see what rsync will do without actually executing it?

Use-n(dry-run) option:

Example

rsync -avn /source/ /destination/

Q4: How to improve rsync transfer speed?

  1. Use-zoption to enable compression
  2. Use--compress-level=Nadjust the compression level (1-9)
  3. Disable checksum calculation (trusted networks only):--no-checksum

Best practice recommendations

  1. Test first: Use-noption to do a dry run first
  2. Logging: Add--log-file=rsync.logto record operations
  3. Permission management: Consider using--chmodto unify file permissions
  4. Scheduled tasks: Combine with crontab for automatic backups
  5. 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.

Linux 命令大全Complete Linux Command List

Other extensions