Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux rsync Command

Linux rsync Command

Linux Command Reference Linux Command Reference


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 and remote systems. rsync is known for its "incremental transfer" algorithm, which transfers only the differences between source and target files, greatly improving file transfer efficiency.

Core features of rsync

  1. Incremental synchronization: Transfer only the changed parts of files, saving bandwidth and time
  2. Preserve attributes: Can preserve metadata such as file permissions and timestamps
  3. Compressed transfer: Supports compression during data transfer, reducing network load
  4. Flexible exclusion: Can exclude specific files or directories
  5. Remote support: Securely synchronize files on remote servers via SSH

Basic rsync 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; preserve 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 times
-g --group Preserve file group ownership
-o --owner Preserve file owner
-D --devices Preserve device files (superuser only)
-h --human-readable Output numbers in a human-readable format
--progress Show 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

Practical rsync examples

Example 1: Local directory synchronization

Example

Synchronize /home/user/docs to /backup/docs, preserving all attributes

rsync

-av

/

home

/

user

/

docs

/

/

backup

/

docs

/

Note: The trailing slash after the source directory / is important:

Example 2: Remote server backup

Example

Synchronize a local directory to a remote server, using compression

rsync

-avz

/

data

/

backups

/

user

@

example.com:

/

remote

/

backups

/

Example 3: Synchronization with exclusion options

Example

Synchronize but exclude .tmp files and the log directory

rsync

-av

--exclude

=

'*.tmp'

--exclude

=

'log/'

/

source

/

/

destination

/

Example 4: Keep both sides exactly identical (delete extra files on the destination)

Example

rsync

-av

--delete

/

source

/

/

destination

/


Advanced rsync 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?

Q2: How to resume an interrupted rsync transfer?

Use --partial option 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 -z option to enable compression
  2. Use --compress-level=N Adjust the compression level (1-9)
  3. Disable checksum calculation (trusted networks only):--no-checksum

Best Practice Recommendations

  1. Test first: When using -n option to perform a dry run first
  2. Logging: Add the --log-file=rsync.log Log operations
  3. Permission management: Consider using --chmod Unify file permissions
  4. Scheduled Tasks: Combine with crontab for automated backups
  5. Secure transfer: Always transfer remotely over SSH

Summary

rsync is an essential tool for Linux system administrators and developers. Mastering it allows you to:

Through this article, you should have mastered the basic usage and advanced techniques of rsync. It is recommended to practice more in real work and gradually explore more of rsync's powerful features.

Linux Command Reference Linux Command Reference