Linux命令之rsync增量传输文件
Tags: LinuxOSrsync传输工具命令介绍增量大文件传输文件同步
一、简介
rsync 是一个常用的 Linux 应用程序,用于文件同步。
它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp
和mv
命令。
它名称里面的r
指的是 remote,rsync 其实就是"远程同步"(remote sync)的意思。与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)。
二、安装
如果本机或者远程计算机没有安装 rsync,可以用下面的命令安装。
1 2 3 4 5 6 7 8 | # Debian $ sudo apt-get install rsync # Red Hat $ sudo yum install rsync # Arch Linux $ sudo pacman -S rsync |
注意,传输的双方都必须安装 rsync。
三、基本用法
3.1 -r 参数
本机使用 rsync 命令时,可以作为cp
和mv
命令的替代方法,将源目录同步到目标目录。
1 | $ rsync -r source destination |
上面命令中,-r
表示递归,即包含子目录。注意,-r
是必须的,否则 rsync 运行不会成功。source
目录表示源目录,destination
表示目标目录。
如果有多个文件或目录需要同步,可以写成下面这样。
1 | $ rsync -r source1 source2 destination |
上面命令中,source1
、source2
都会被同步到destination
目录。
3.2 -a 参数
-a
参数可以替代-r
,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a
比-r
更有用。下面的用法才是常见的写法。
1 | $ rsync -a source destination |
目标目录destination
如果不存在,rsync 会自动创建。执行上面的命令后,源目录source
被完整地复制到了目标目录destination
下面,即形成了destination/source
的目录结构。
如果只想同步源目录source
里面的内容到目标目录destination
,则需要在源目录后面加上斜杠。
1 | $ rsync -a source/ destination |
上面命令执行后,source
目录里面的内容,就都被复制到了destination
目录里面,并不会在destination
下面创建一个source
子目录。
3.3 -n 参数
如果不确定 rsync 执行后会产生什么结果,可以先用-n
或--dry-run
参数模拟执行的结果。
1 | $ rsync -anv source/ destination |
上面命令中,-n
参数模拟命令执行的结果,并不真的执行命令。-v
参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。
3.4 --delete 参数
默认情况下,rsync 只确保源目录的所有内容(明确排除的文件除外)都复制到目标目录。它不会使两个目录保持相同,并且不会删除文件。如果要使得目标目录成为源目录的镜像副本,则必须使用--delete
参数,这将删除只存在于目标目录、不存在于源目录的文件。
1 | $ rsync -av --delete source/ destination |
上面命令中,--delete
参数会使得destination
成为source
的一个镜像。
四、排除文件
4.1 --exclude 参数
有时,我们希望同步时排除某些文件或目录,这时可以用--exclude
参数指定排除模式。
1 2 3 | $ rsync -av --exclude='*.txt' source/ destination > # 或者 $ rsync -av --exclude '*.txt' source/ destination |
上面命令排除了所有 TXT 文件。
注意,rsync 会同步以"点"开头的隐藏文件,如果要排除隐藏文件,可以这样写--exclude=".*"
。
如果要排除某个目录里面的所有文件,但不希望排除目录本身,可以写成下面这样。
1 | $ rsync -av --exclude 'dir1/*' source/ destination |
多个排除模式,可以用多个--exclude
参数。
1 | $ rsync -av --exclude 'file1.txt' --exclude 'dir1/*' source/ destination |
多个排除模式也可以利用 Bash 的大扩号的扩展功能,只用一个--exclude
参数。
1 | $ rsync -av --exclude={'file1.txt','dir1/*'} source/ destination |
如果排除模式很多,可以将它们写入一个文件,每个模式一行,然后用--exclude-from
参数指定这个文件。
1 | $ rsync -av --exclude-from='exclude-file.txt' source/ destination |
4.2 --include 参数
--include
参数用来指定必须同步的文件模式,往往与--exclude
结合使用。
1 | $ rsync -av --include="*.txt" --exclude='*' source/ destination |
上面命令指定同步时,排除所有文件,但是会包括 TXT 文件。
五、远程同步
5.1 SSH 协议
rsync 除了支持本地两个目录之间的同步,也支持远程同步。它可以将本地内容,同步到远程服务器。
1 | $ rsync -av source/ username@remote_host:destination |
也可以将远程内容同步到本地。
1 | $ rsync -av username@remote_host:source/ destination |
rsync 默认使用 SSH 进行远程登录和数据传输。
由于早期 rsync 不使用 SSH 协议,需要用-e
参数指定协议,后来才改的。所以,下面-e ssh
可以省略。
1 | $ rsync -av -e ssh source/ user@remote_host:/destination |
但是,如果 ssh 命令有附加的参数,则必须使用-e
参数指定所要执行的 SSH 命令。
1 | $ rsync -av -e 'ssh -p 2234' source/ user@remote_host:/destination |
上面命令中,-e
参数指定 SSH 使用2234端口。
5.2 rsync 协议
除了使用 SSH,如果另一台服务器安装并运行了 rsync 守护程序,则也可以用rsync://
协议(默认端口873)进行传输。具体写法是服务器与目标目录之间使用双冒号分隔::
。
1 | $ rsync -av source/ 192.168.122.32::module/destination |
注意,上面地址中的module
并不是实际路径名,而是 rsync 守护程序指定的一个资源名,由管理员分配。
如果想知道 rsync 守护程序分配的所有 module 列表,可以执行下面命令。
1 | $ rsync rsync://192.168.122.32 |
rsync 协议除了使用双冒号,也可以直接用rsync://
协议指定地址。
1 | $ rsync -av source/ rsync://192.168.122.32/module/destination |
六、增量备份
rsync 的最大特点就是它可以完成增量备份,也就是默认只复制有变动的文件。
除了源目录与目标目录直接比较,rsync 还支持使用基准目录,即将源目录与基准目录之间变动的部分,同步到目标目录。
具体做法是,第一次同步是全量备份,所有文件在基准目录里面同步一份。以后每一次同步都是增量备份,只同步源目录与基准目录之间有变动的部分,将这部分保存在一个新的目标目录。这个新的目标目录之中,也是包含所有文件,但实际上,只有那些变动过的文件是存在于该目录,其他没有变动的文件都是指向基准目录文件的硬链接。
--link-dest
参数用来指定同步时的基准目录。
1 | $ rsync -a --delete --link-dest /compare/path /source/path /target/path |
上面命令中,--link-dest
参数指定基准目录/compare/path
,然后源目录/source/path
跟基准目录进行比较,找出变动的文件,将它们拷贝到目标目录/target/path
。那些没变动的文件则会生成硬链接。这个命令的第一次备份时是全量备份,后面就都是增量备份了。
下面是一个脚本示例,备份用户的主目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/bin/bash # A script to perform incremental backups using rsync set -o errexit set -o nounset set -o pipefail readonly SOURCE_DIR="${HOME}" readonly BACKUP_DIR="/mnt/data/backups" readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" readonly LATEST_LINK="${BACKUP_DIR}/latest" mkdir -p "${BACKUP_DIR}" rsync -av --delete \ "${SOURCE_DIR}/" \ --link-dest "${LATEST_LINK}" \ --exclude=".cache" \ "${BACKUP_PATH}" rm -rf "${LATEST_LINK}" ln -s "${BACKUP_PATH}" "${LATEST_LINK}" |
上面脚本中,每一次同步都会生成一个新目录${BACKUP_DIR}/${DATETIME}
,并将软链接${BACKUP_DIR}/latest
指向这个目录。下一次备份时,就将${BACKUP_DIR}/latest
作为基准目录,生成新的备份目录。最后,再将软链接${BACKUP_DIR}/latest
指向新的备份目录。
七、配置项
-a
、--archive
参数表示存档模式,保存所有的元数据,比如修改时间(modification time)、权限、所有者等,并且软链接也会同步过去。
--append
参数指定文件接着上次中断的地方,继续传输。
--append-verify
参数跟--append
参数类似,但会对传输完成后的文件进行一次校验。如果校验失败,将重新发送整个文件。
-b
、--backup
参数指定在删除或更新目标目录已经存在的文件时,将该文件更名后进行备份,默认行为是删除。更名规则是添加由--suffix
参数指定的文件后缀名,默认是~
。
--backup-dir
参数指定文件备份时存放的目录,比如--backup-dir=/path/to/backups
。
--bwlimit
参数指定带宽限制,默认单位是 KB/s,比如--bwlimit=100
。
-c
、--checksum
参数改变rsync
的校验方式。默认情况下,rsync 只检查文件的大小和最后修改日期是否发生变化,如果发生变化,就重新传输;使用这个参数以后,则通过判断文件内容的校验和,决定是否重新传输。
--delete
参数删除只存在于目标目录、不存在于源目标的文件,即保证目标目录是源目标的镜像。
-e
参数指定使用 SSH 协议传输数据。
--exclude
参数指定排除不进行同步的文件,比如--exclude="*.iso"
。
--exclude-from
参数指定一个本地文件,里面是需要排除的文件模式,每个模式一行。
--existing
、--ignore-non-existing
参数表示不同步目标目录中不存在的文件和目录。
-h
参数表示以人类可读的格式输出。
-h
、--help
参数返回帮助信息。
-i
参数表示输出源目录与目标目录之间文件差异的详细情况。
--ignore-existing
参数表示只要该文件在目标目录中已经存在,就跳过去,不再同步这些文件。
--include
参数指定同步时要包括的文件,一般与--exclude
结合使用。
--link-dest
参数指定增量备份的基准目录。
-m
参数指定不同步空目录。
--max-size
参数设置传输的最大文件的大小限制,比如不超过200KB(--max-size='200k'
)。
--min-size
参数设置传输的最小文件的大小限制,比如不小于10KB(--min-size=10k
)。
-n
参数或--dry-run
参数模拟将要执行的操作,而并不真的执行。配合-v
参数使用,可以看到哪些内容会被同步过去。
-P
参数是--progress
和--partial
这两个参数的结合。
--partial
参数允许恢复中断的传输。不使用该参数时,rsync
会删除传输到一半被打断的文件;使用该参数后,传输到一半的文件也会同步到目标目录,下次同步时再恢复中断的传输。一般需要与--append
或--append-verify
配合使用。
--partial-dir
参数指定将传输到一半的文件保存到一个临时目录,比如--partial-dir=.rsync-partial
。一般需要与--append
或--append-verify
配合使用。
--progress
参数表示显示进展。
-r
参数表示递归,即包含子目录。
--remove-source-files
参数表示传输成功后,删除发送方的文件。
--size-only
参数表示只同步大小有变化的文件,不考虑文件修改时间的差异。
--suffix
参数指定文件名备份时,对文件名添加的后缀,默认是~
。
-u
、--update
参数表示同步时跳过目标目录中修改时间更新的文件,即不同步这些有更新的时间戳的文件。
-v
参数表示输出细节。-vv
表示输出更详细的信息,-vvv
表示输出最详细的信息。
--version
参数返回 rsync 的版本。
-z
参数指定同步时压缩数据。
八、参考链接
https://www.ruanyifeng.com/blog/2020/08/rsync.html
我的示例
本地复制
1 2 3 4 5 6 | [root@docker35 tmp]# rsync -av /etc/passwd /tmp/1.txt sending incremental file list sent 45 bytes received 12 bytes 114.00 bytes/sec total size is 3,441 speedup is 60.37 [root@docker35 tmp]# |
此例中,通过执行 rsync 命令,实现了将 /etc/passwd 文件本地同步到 /tmp/ 目录下,并改名为 1.txt。
远程复制
rsync 命令还支持远程同步数据,也就是将本地的数据备份到远程机器上。比如说,我们知道远程机器的 IP 地址为 192.168.1.36,则使用 rsync 命令备份 passwd 文件的执行命令为:
1 2 3 4 5 6 7 | [root@docker35 tmp]# rsync -av /etc/passwd 192.168.1.36:/tmp/1.txt root@192.168.1.36's password: <-- 输入密码 sending incremental file list passwd sent 3,527 bytes received 34 bytes 1,424.40 bytes/sec total size is 3,441 speedup is 0.97 |
注意,首次远程连接时,会提示是否要继续连接,输入 yes 即可。另外,当成功建立连接后,需要输入目标系统的 root 密码。
文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [root@docker35 tmp]# rsync -av /tmp/ 192.168.1.36:/tmp/ root@192.168.1.36's password: sending incremental file list ./ .X10-lock a.sql dotnet-diagnostic-1142-2317-socket lhrdb.sql new.sql npc_vkey.txt screenlog_google.log sys.sql todeskd.sock.1025 .ICE-unix/ .ICE-unix/234679 .Test-unix/ .X11-unix/ .X11-unix/X10 .XIM-unix/ .font-unix/ hsperfdata_root/ ssh-8YnkBxb7eJdM/ ssh-8YnkBxb7eJdM/agent.234444 systemd-private-13afe749df91415ca1030a296d5403ed-chronyd.service-QH7s9W/ systemd-private-13afe749df91415ca1030a296d5403ed-chronyd.service-QH7s9W/tmp/ systemd-private-13afe749df91415ca1030a296d5403ed-rtkit-daemon.service-lD2Px3/ systemd-private-13afe749df91415ca1030a296d5403ed-rtkit-daemon.service-lD2Px3/tmp/ sent 1,733,786 bytes received 302 bytes 693,635.20 bytes/sec total size is 1,735,596 speedup is 1.00 |
增量同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [oracle@oracle-01 tmp]$ fallocate -l 10G 10g_file [oracle@oracle-01 tmp]$ ll -h 10g_file -rw-r--r-- 1 oracle oinstall 10G Jul 28 17:19 10g_file [oracle@oracle-01 tmp]$ rsync -av 10g_file oracle@hwy-oracle-02:/tmp/ sending incremental file list 10g_file sent 10,740,039,786 bytes received 35 bytes 154,532,947.06 bytes/sec total size is 10,737,418,240 speedup is 1.00 [oracle@oracle-01 tmp]$ rsync -av 10g_file oracle@oracle-02:/tmp/ sending incremental file list sent 67 bytes received 12 bytes 158.00 bytes/sec total size is 10,737,418,240 speedup is 135,916,686.58 |
第2次传递特别快,说明只传递变化了的文件。
若目标端修改文件后,则rsync会重新同步,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | -- 目标端修改 [oracle@oracle-02 tmp]$ ll total 10485768 -rw-r--r-- 1 oracle oinstall 10737418240 Jul 28 17:19 10g_file prw-r--r-- 1 root root 0 Jul 28 17:20 wrapper-859-1-in prw-r--r-- 1 root root 0 Jul 28 17:20 wrapper-859-1-out [oracle@oracle-02 tmp]$ echo '1' >> 10g_file [oracle@oracle-02 tmp]$ -- 源端传递 [oracle@oracle-01 tmp]$ rsync -av 10g_file oracle@hwy-oracle-02:/tmp/ sending incremental file list 10g_file sent 417,654 bytes received 829,107 bytes 5,894.85 bytes/sec total size is 10,737,418,240 speedup is 8,612.25 [oracle@oracle-01 tmp]$ echo '2' >> 10g_file [oracle@oracle-01 tmp]$ rsync -av 10g_file oracle@hwy-oracle-02:/tmp/ sending incremental file list 10g_file sent 417,655 bytes received 829,107 bytes 5,922.86 bytes/sec total size is 10,737,418,242 speedup is 8,612.24 |
从传输的字节数上可以看到,对同一个文件的确是增量同步,但是传输时间上感觉没有变化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | [oracle@oracle-01 tmp]$ time rsync -av 10g_file oracle@oracle-02:/tmp/ sending incremental file list 10g_file sent 10,740,039,794 bytes received 35 bytes 156,788,902.61 bytes/sec total size is 10,737,418,244 speedup is 1.00 real 1m7.809s user 1m2.669s sys 0m14.811s [oracle@oracle-01 tmp]$ echo '3' >> 10g_file [oracle@oracle-01 tmp]$ time rsync -av 10g_file oracle@oracle-02:/tmp/ sending incremental file list 10g_file sent 417,660 bytes received 829,107 bytes 5,922.88 bytes/sec total size is 10,737,418,246 speedup is 8,612.21 real 3m30.220s user 0m35.150s sys 0m4.518s [oracle@oracle-01 tmp]$ echo '4' >> 10g_file [oracle@oracle-01 tmp]$ time rsync -av 10g_file --append oracle@oracle-02:/tmp/ sending incremental file list 10g_file sent 116 bytes received 35 bytes 302.00 bytes/sec total size is 10,737,418,252 speedup is 71,108,730.15 real 0m0.129s user 0m0.010s sys 0m0.002s [oracle@oracle-02 tmp]$ tail -n 1 10g_file 4 [oracle@oracle-02 tmp]$ |
配置密码
服务端
- 创建rsyncd.conf配置文件,sudo vim /etc/rsyncd.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | uid = ai #运行进程用户 gid = ai #运行用户组 port = 873 #监听端口 fake super = yes #无需rsync以root身份运行,允许接受数据 use chroot = no #禁锢推送数据至某个目录 max connections = 0 #最大连接数 timeout = 600 #超时时间 log file = /var/log/rsyncd.log #定义日志文件路径 ##################################### [backup] #模块名称 path = /backup #定义接收备份数据目录 ignore errors #忽略错误信息 read only = false #对备份数据可读写 list = false #不允许查看模块信息 auth users = test #定义虚拟用户,认证使用 secrets file = /etc/rsync.passwd #认证密码路径 |
- 创建rsync.passwd
1 2 | echo "test:123456" > /etc/rsync.passwd chmod 600 /etc/rsync.passwd |
- 启动rsync
1 | `rsync --daemon` |
客户端
- 创建rsync.passwd
1 2 3 4 5 6 7 8 | echo "123456" > /etc/rsync.password chmod 600 /etc/rsync.password #### 实例(拉取) rsync -avz test@192.168.174.128::backup --password-file=/etc/rsyncd.password /tmp/backup/ #### 定时拉取 把拉取指令配置在客户端crontab即可 |
总结
1、rsync在文件传递完成后,才能在目标端看到文件;在传递未完成之前,在目标端是看不到文件的
2、rsync只会传递变化的文件,未变化的文件不会传递
3、若源文件很大,但是增加的数据不多,则可以使用--append
参数快速同步增量数据:
1 | rsync -av 10g_file --append oracle@oracle-02:/tmp/ |
但是,若文件内容被删除或修改过,那么使用该参数是不能将最新的数据同步的。