参考资料
目录
- [一. 简介](#一. 简介)
- [二. 基本参数](#二. 基本参数)
- [三. 配置项](#三. 配置项)
-
- [3.1 `-r` 递归同步](#3.1
-r递归同步) - [3.2 `-a` 元信息递归同步](#3.2
-a元信息递归同步) - [3.3 `-n` 模拟执行结果](#3.3
-n模拟执行结果) - [3.4 `--delete`](#3.4
--delete) -
- [3.4.1 注意事项](#3.4.1 注意事项)
- [3.4.2 正确使用方式](#3.4.2 正确使用方式)
- [3.5 `--include` 与 `--exclude` 参数](#3.5
--include与--exclude参数) -
- [3.5.1 `--exclude`](#3.5.1
--exclude) - [3.5.2 `--include`](#3.5.2
--include)
- [3.5.1 `--exclude`](#3.5.1
- [3.6 其他配置项](#3.6 其他配置项)
-
- [3.6.1 `-P` 断点续传](#3.6.1
-P断点续传) - [3.6.2 `--bwlimit` 限制同步速度](#3.6.2
--bwlimit限制同步速度)
- [3.6.1 `-P` 断点续传](#3.6.1
- [3.1 `-r` 递归同步](#3.1
- [四. ssh协议远程同步](#四. ssh协议远程同步)
-
- [4.1 基础使用](#4.1 基础使用)
- [4.2 免密同步](#4.2 免密同步)
一. 简介

从字面意思上,rsync 可以理解为 remote sync(远程同步),但它不仅可以远程同步数据(类似于 scp 命令),还可以本地同步数据(类似于 cp 命令)。不同于 cp 或 scp 的一点是,使用 rsync 命令备份数据时,如果数据已经存在的话,不会直接覆盖以前的数据,而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
总结:rsync 是一个非常强大的文件同步和备份工具,支持本地和远程同步,还可以增量传输,只传输变化的部分。
二. 基本参数
| OPTION选项 | 功能 |
|---|---|
| -a | 这是归档模式,表示以递归方式传输文件,并保持所有属性。 |
| -r | 表示以递归模式处理子目录,它主要是针对目录来说的,如果单独传一个文件不需要加 -r 选项,但是传输目录时必须加。 |
| -v | 表示打印一些信息,比如文件列表、文件数量等。 |
| -l | 表示保留软连接。 |
| -L | 表示像对待常规文件一样处理软连接。如果是 SRC 中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到 DEST。 |
| -p | 表示保持文件权限。 |
| -o | 表示保持文件属主信息。 |
| -g | 表示保持文件属组信息。 |
| -D | 表示保持设备文件信息。 |
| -t | 表示保持文件时间信息。 |
--delete |
表示删除 DEST 中 SRC 没有的文件。 |
--exclude |
表示指定排除不需要传输的文件,等号后面跟文件名,可以是通配符模式(如 *.txt)。 |
--progress |
表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、 同步的文件传输速度等。 |
| -u | 表示把 DEST 中比 SRC 还新的文件排除掉,不会覆盖。 |
| -z | 加上该选项,将会在传输过程中压缩。 |
三. 配置项
3.1 -r 递归同步
本机使用 rsync 命令时,可以作为cp和mv命令的替代方法,将源目录同步到目标目录。
-r表示递归,即包含子目录。-r是必须的,否则 rsync 运行不会成功。source目录表示源目录,destination表示目标目录。
bash
$ rsync -r source destination
- 如果有多个文件或目录需要同步,可以写成下面这样。
bash
$ rsync -r source1 source2 destination
3.2 -a 元信息递归同步
⏹-a参数可以替代-r,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。
由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a比-r更有用。
⏹-a参数等同于-r、-l、-p、-t、-g、-o、-D 选项。
-a 选项后面可以跟一个 --no-OPTION,表示关闭 -r、-l、-p、-t、-g、-o、-D 中的某一个。
比如-a --no-l 等同于 -r、-p、-t、-g、-o、-D 选项。
- 目标目录
destination如果不存在,rsync会自动创建。 - 需要注意的是
source后面没有/,会导致源目录source被完整地复制到了目标目录destination下面。- 即形成了
destination/source的目录结构。
bash
$ rsync -a source destination
- 如果只想同步源目录
source里面的内容到目标目录destination,则需要在源目录后面加上斜杠/。
bash
$ rsync -a source/ destination
3.3 -n 模拟执行结果
⏹如果不确定 rsync 执行后会产生什么结果,可以先用-n或--dry-run参数模拟执行的结果。
bash
apluser@FengYeHong-HP:20251123$ rsync -anv /home/apluser/work/20251123/0[0-3].txt /home/apluser/
sending incremental file list
00.txt
01.txt
02.txt
03.txt
sent 127 bytes received 28 bytes 310.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
3.4 --delete
默认情况下,rsync 只确保源目录的所有内容(明确排除的文件除外)都复制到目标目录。它不会使两个目录保持相同,并且不会删除文件。
如果要使得目标目录成为源目录的镜像副本,则必须使用--delete参数,这将会删除只存在于目标目录、不存在于源目录的文件。
3.4.1 注意事项
💥注意,下面这种写法并不会删除目标目录中的任何文件
delete只会在源是一个目录的情况下才会删除目标目录中多余的文件- 当源是多个文件时,
rsync不会把目标目录当成镜像目录,因此不会删除其他文件。

3.4.2 正确使用方式
⏹源一定要是一个目录,而不能是包含正则表达式在内的多个文件。

⏹如果我们就是想要将指定的多个文件同步到目标目录后,删除其他文件的话,可以使用下面这种方式
bash
rsync -av --delete --delete-excluded --include='0[0-3].txt' --exclude='*' ./ /home/apluser/work/test/

3.5 --include 与 --exclude 参数
3.5.1 --exclude
⏹如果希望同步时排除 某些文件或目录,这时可以用--exclude参数指定排除模式。
bash
# 排除所有的.txt文件
$ rsync -av --exclude='*.txt' source/ destination
$ rsync -av --exclude '*.txt' source/ destination
# 排除所有的隐藏文件
$ rsync -av --exclude '*.*' source/ destination
⏹多个排除模式,可以用多个--exclude参数。
bash
$ rsync -av --exclude 'file1.txt' --exclude 'dir1/*' source/ destination
⏹多个排除模式也可以利用 Bash 的大扩号的扩展功能,只用一个--exclude参数。
bash
$ rsync -av --exclude={'file1.txt','dir1/*'} source/ destination
3.5.2 --include
⏹--include参数用来指定必须同步的文件模式,往往与--exclude结合使用。
- 下面命令指定同步时,排除所有文件,但是会包括
txt文件。
bash
$ rsync -av --include="*.txt" --exclude='*' source/ destination
💥注意,下面这两条命令的作用并不相同🙄
- include 不会阻止其它文件同步
include的作用是即使这个文件符合exclude,也强行包含,- 但没有
exclude的话,则include实际上什么也不干,include必须要和exclude结合使用。
exclude会排除所有文件(除非被include覆盖)
bash
# 只同步 .txt 文件,其他文件不会被复制
rsync -av --include="*.txt" --exclude='*' source/ destination
# 同步所有文件(因为你没有排除其他文件,include 不会阻止其它文件被复制)
rsync -av --include="*.txt" source/ destination
3.6 其他配置项
3.6.1 -P 断点续传
⏹在传输大文件的时候,如果遇到网络波动,可能会在传输到一半的时候失败,使用-P配置项可以断点续传。
bash
rsync -avzP ./large_file.iso user@192.168.1.100:/data/
3.6.2 --bwlimit 限制同步速度
⏹通过 --bwlimit=RATE 限制传输速率(单位:KB/s)
bash
# 限制同步速率为 100KB/s
rsync -avzP --bwlimit=100 /home/user/big_dir user@192.168.1.100:/data/
四. ssh协议远程同步
4.1 基础使用
⏹rsync 除了支持本地两个目录之间的同步,也支持远程同步。它可以将本地与远程服务器之前的同步。
bash
# 本地文件同步到远程服务器
$ rsync -av /local/path/ user@remote_host:/remote/path/
# 远程服务器内容同步到本地
$ rsync -av user@remote_host:/remote/path/ /local/path/
⏹rsync 默认使用 SSH 进行远程登录和数据传输,默认情况下并不需要特别指定传输协议。
但如果 ssh 命令有附加的参数,则必须使用-e参数指定所要执行的 SSH 命令。
bash
$ rsync -av -e 'ssh -p 2234' source/ user@remote_host:/destination
4.2 免密同步
😑每次使用rsync命令同步本地文件到远程服务器的时候,都需要手动输入密码很麻烦,下面介绍免密同步的方法
⏹使用ssh-keygen命令生成一对密钥
ssh-keygen -t rsa -b 4096- 密钥默认会生成到
/home/apluser/.ssh/文件夹下,但可以手动指定其他的存放路径
bash
apluser@FengYeHong-HP:key$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/apluser/.ssh/id_rsa): /home/apluser/key/rsync_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/apluser/key/rsync_rsa
Your public key has been saved in /home/apluser/key/rsync_rsa.pub
The key fingerprint is:
SHA256:Bw77/Rb3dn21FcSyQYWbZJq6J7F8o6ForVbWODcIAoI apluser@FengYeHong-HP
The key's randomart image is:
+---[RSA 4096]----+
|. ..+. |
|E. * o |
|. . . . = B |
| . . + . o + . |
| . o S o .|
| B O . . o|
| + =.* o .=|
| o...=.=. .*|
| oo.. .=.o .o|
+----[SHA256]-----+
apluser@FengYeHong-HP:key$
apluser@FengYeHong-HP:key$ ls -l /home/apluser/key/
-rw------- 1 apluser apluser 3389 Nov 24 09:04 rsync_rsa
-rw-r--r-- 1 apluser apluser 747 Nov 24 09:04 rsync_rsa.pub
⏹复制生成的密钥中的公钥到目标服务器
ssh-copy-id -i /home/apluser/key/rsync_rsa.pub apluser@192.168.137.129ssh-copy-id命令的-i配置项目指定要复制的公钥全路径
bash
apluser@FengYeHong-HP:key$ ssh-copy-id -i /home/apluser/key/rsync_rsa.pub apluser@192.168.137.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/apluser/key/rsync_rsa.pub"
The authenticity of host '192.168.137.129 (192.168.137.129)' can't be established.
ED25519 key fingerprint is SHA256:vW4uUy6GP4C8BTInJGMSK6r5viWx2vw9nTM2uAq/Mgg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
apluser@192.168.137.129's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'apluser@192.168.137.129'"
and check to make sure that only the key(s) you wanted were added.
apluser@FengYeHong-HP:key$
⏹之后就可以通过指定私钥的方式去无密码连接远程服务器了
bash
ssh -i /home/apluser/key/rsync_rsa apluser@192.168.137.129
⏹通过密钥的方式无密码同步文件到远程服务器
- ssh 的
-i配置项指定连接远程服务器的私钥 - ssh 的
-p配置项指定连接远程服务器的端口号
bash
rsync -av -e "ssh -i /home/apluser/key/rsync_rsa -p 22" /home/apluser/work/20251123/ apluser@192.168.137.129:/home/apluser/work/test/
