三分薄地,认真耕耘

导航

 

1、简介

rsync 就是远程同步的意思remote sync.
rsync 被用在UNIX / Linux执行备份操作操作.
rsync 工具包被用来从一个位置到另一个位置高效地同步文件和文件夹. rsync可以实现在同一台机器的不同文件直接备份,也可以跨服务器备份.

2、特性

  • 速度快: 初次同步时, rsync会全量拷贝从源文件或目录到目标位置. 第二次往后同步时, rsync 仅仅会拷贝变化的数据块或字节到目标位置这将使得文件传输非常迅速.
  • 安全: rsync 可以使用ssh协议加密传输.
  • 占用带宽少: rsync 在发送时会压缩数据块, 接收后再解压缩数据块. 所以和其他文件传输协议比起来, rsync在跨主机传输文件时会占用较小的带宽.
  • 不需要特殊权限: 安装和运行rsync 不需要特殊权限.

3、使用方法

rsync语法如下:


rsync [OPTION]… SRC [SRC]… [USER@]HOST:DEST]
# 将本地数据拷贝到远程主机上

rsync [OPTION]… [USER@]HOST:SRC DEST]
# 将远程数据拷贝到本地

rsync [OPTION]… SRC [SRC]… DEST]
# 将本地数据拷贝到本地其他路径

rsync [OPTION]… [USER@]HOST::SRC [DEST]
# 从远程rsync服务器拷贝数据到本地

rsync [OPTION]… SRC [SRC]… [USER@]HOST::DEST]
# 将本地数据拷贝到远程rsync服务器

rsync [OPTION]… rsync://[USER@]HOST[:PORT]/SRC [DEST]

从语法结构我们可以看出, 源和目标即可以在本地也可以在远端. 如果是远端的话,需要指明登录用户名, 远端服务器名, 和远端文件或目录. 同时源可以是多个, 目标位置只能是一个.

rsync的工作模式如下:


1、拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号”:“分隔符时就启动这种工作模式。

2、使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST地址包含单个冒号”:“分隔符时启动该模式。

3、使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC路径包含单个冒号”:“分隔符时启动该模式。

4、从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含”::“分隔符时启动该模式。

5、从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含”::“分隔符时启动该模式。

6、列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。

4、常用选项

-z: –compress 使用压缩机制
-v: –verbose 打印详细信息
-r: –recursive 以递归模式同步子目录
-l, –links 将符号链接当作符号链接文件拷贝, 不拷贝符合链接指向的文件内容.
-L 会拷贝链接指向的文件内容
-p, –perms: 保留文件权限
-t, –times: 保留修改时间戳
-g, –group: 保留用户组信息
-o, –owner: 保留用户信息(需要超级用户权限)
-D 相当于 –devices –specials 的组合, 保留设备文件, 保留特殊文件
-a –archive  归档模式选项. -a 选项相当于7个选项的组合 -rlptgoD
-e, –rsh=COMMAND 指定远端使用的shell命令
-h, --human-readable 以易读模式显示文件大小,如K,M,G,T等单位
-u, --update 忽略接收方上更新时间更新的同名文件
-d, --dirs 只传输目录结构,不传输文件
--delete 在传输时,如果目的端有源端不存在的文件,则在目的端删除
--delete-excluded 指定需要在目的端删除时排除的文件
--delete-after 默认情况下,rsync是先清理目的端的文件再开始数据同步;如果使用此选项,则rsync会先数据同步,完成后在删除目的端文件。
--partial 断点续传选项。默认情况下,rsync会删除传输中中断的文件,然后重新传输。使用该选项就不会重新传输,而是续传。
--partial-dir=DIR 指定断点传输的目录,示例:rsync -av --partial --partial-dir=.rsync-partials/ --progress /local/data/ user@192.168.1.100:/remote/backup/
--max-size=SIZE 不传输大于size的文件,单位支持支持 K(千字节)、M(兆字节)、G(吉字节),也支持小数(如 --max-size=1.5M
--min-size=SIZE 不传输小于size的文件,单位支持支持 K(千字节)、M(兆字节)、G(吉字节),也支持小数(如 --max-size=1.5M
--progress 显示出传输进度信息
-p 相当于--progress--partial的组合
--exclude=PATTERN 同步时不包含匹配的文件
--exclude-from=FILE 同步时不包含file中的文件

5、示例

5.1 rsync同步数据时,源目录后带/和不带/的区别

使用rsync进行文件传输时:

  • 目录后带/:这种情况下,rsync会递归复制整个目录,但不包括目录本身。 例如,rsync -a ./source/ ./destination会将source目录下的所有内容复制到destination目录中,但不会将source目录本身复制过去,最终的结构类似于/destination/...‌。
  • 目录后不带/‌:这种情况下,rsync会递归复制整个目录包括目录本身。例如,rsync -a ./source ./destination会将source目录及其所有内容复制到destination目录中,最终的结构类似于/destination/source/...‌。

[root@entos7 ~]# ll /opt/bak_dir/shellscript/
total 4
drwxr-xr-x 2 root root   6 May 29 14:40 awk_test
drwxr-xr-x 2 root root   6 May 24 20:45 cut_test
drwxr-xr-x 2 root root  66 May 22 14:11 data
drwxr-xr-x 2 root root  38 May 26 14:02 eval_test
drwxr-xr-x 2 root root  54 May 27 17:01 expr_test
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root  86 May 23 13:20 sort_test
drwxr-xr-x 2 root root 220 May 25 19:52 split_paste_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
drwxr-xr-x 2 root root  23 Jun 23 15:08 tac_rev_test
-rw-r--r-- 1 root root  53 May 23 13:35 testfile1
drwxr-xr-x 2 root root  22 May 23 17:45 tr_test
drwxr-xr-x 2 root root   6 May 23 13:31 uniq_test
[root@entos7 ~]# ll /root/tmp
total 0

# 不带/,将shellscript目录及其下面的所有目录、文件都递归复制过去
[root@entos7 ~]# rsync -avh /opt/bak_dir/shellscript /root/tmp
sending incremental file list
shellscript/
shellscript/testfile1
......
shellscript/tr_test/
shellscript/tr_test/testfile
shellscript/uniq_test/

sent 13.11M bytes  received 939 bytes  8.74M bytes/sec
total size is 13.10M  speedup is 1.00
[root@entos7 ~]# ll /root/tmp
total 0
drwxr-xr-x 14 root root 229 Jun 23 14:56 shellscript

# 带/,不拷贝shellscript目录,但是递归拷贝其下所有目录和文件
[root@entos7 ~]# rsync -avh /opt/bak_dir/shellscript/ /root/tmp
sending incremental file list
./
testfile1
awk_test/
cut_test/
......
tr_test/testfile
uniq_test/

sent 13.11M bytes  received 938 bytes  26.22M bytes/sec
total size is 13.10M  speedup is 1.00
[root@entos7 ~]# ll /root/tmp/
total 4
drwxr-xr-x 2 root root   6 May 29 14:40 awk_test
drwxr-xr-x 2 root root   6 May 24 20:45 cut_test
drwxr-xr-x 2 root root  66 May 22 14:11 data
drwxr-xr-x 2 root root  38 May 26 14:02 eval_test
drwxr-xr-x 2 root root  54 May 27 17:01 expr_test
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root  86 May 23 13:20 sort_test
drwxr-xr-x 2 root root 220 May 25 19:52 split_paste_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
drwxr-xr-x 2 root root  23 Jun 23 15:08 tac_rev_test
-rw-r--r-- 1 root root  53 May 23 13:35 testfile1
drwxr-xr-x 2 root root  22 May 23 17:45 tr_test
drwxr-xr-x 2 root root   6 May 23 13:31 uniq_test


5.1 同步同一个主机上的两个目录


[root@entos7 ~]# ll /opt/bak_dir/shellscript/
total 4
drwxr-xr-x 2 root root   6 May 29 14:40 awk_test
drwxr-xr-x 2 root root   6 May 24 20:45 cut_test
drwxr-xr-x 2 root root  66 May 22 14:11 data
drwxr-xr-x 2 root root  38 May 26 14:02 eval_test
drwxr-xr-x 2 root root  54 May 27 17:01 expr_test
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root  86 May 23 13:20 sort_test
drwxr-xr-x 2 root root 220 May 25 19:52 split_paste_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
drwxr-xr-x 2 root root  23 Jun 23 15:08 tac_rev_test
-rw-r--r-- 1 root root  53 May 23 13:35 testfile1
drwxr-xr-x 2 root root  22 May 23 17:45 tr_test
drwxr-xr-x 2 root root   6 May 23 13:31 uniq_test
[root@entos7 ~]# ll /opt/tmp
total 0
[root@entos7 ~]# rsync -rzv /opt/bak_dir/shellscript /opt/tmp/
sending incremental file list
shellscript/
......
shellscript/tr_test/testfile
shellscript/uniq_test/

sent 1,001,703 bytes  received 935 bytes  2,005,276.00 bytes/sec
total size is 13,104,145  speedup is 13.07

[root@entos7 ~]# ll /opt/tmp
total 0
drwxr-xr-x 14 root root 229 Jun 26 14:09 shellscript
[root@entos7 ~]# ll /opt/bak_dir/shellscript -d
drwxr-xr-x 14 root root 229 Jun 23 14:56 /opt/bak_dir/shellscript

说明:

-z: –compress 使用压缩机制

-v: –verbose 打印详细信息

-r: –recursive 以递归模式同步子目录

注意: 同步完成后, 我们会发现文件的时间戳timestamps发生了改变.


[root@entos7 ~]# ll /opt/tmp/shellscript/awk_test/ -d
drwxr-xr-x 2 root root 6 Jun 26 14:09 /opt/tmp/shellscript/awk_test/
[root@entos7 ~]# ll /opt/bak_dir/shellscript/awk_test/ -d
drwxr-xr-x 2 root root 6 May 29 14:40 /opt/bak_dir/shellscript/awk_test/

5.3 保留文件的时间戳等属性

有时我们希望拷贝或同步时, 时间戳不要发生变化, 源文件是什么时间戳,目标文件就是什么时间戳, 这时我们需要使用 -a –archive 归档模式选项. -a 选项相当于7个选项的组合 -rlptgoD

-r, –recursive: 递归模式Recursive mode
-l, –links: 将符号链接当作符号链接文件拷贝, 不拷贝符合链接指向的文件内容.
-p, –perms: 保留文件权限
-t, –times: 保留修改时间戳
-g, –group: 保留用户组信息
-o, –owner: 保留用户信息(需要超级用户权限)
-D, 相当于 –devices –specials 的组合, 保留设备文件, 保留特殊文件.

[root@entos7 ~]# ll /opt/bak_dir/shellscript/
total 4
drwxr-xr-x 2 root root   6 May 29 14:40 awk_test
drwxr-xr-x 2 root root   6 May 24 20:45 cut_test
drwxr-xr-x 2 root root  66 May 22 14:11 data
drwxr-xr-x 2 root root  38 May 26 14:02 eval_test
drwxr-xr-x 2 root root  54 May 27 17:01 expr_test
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root  86 May 23 13:20 sort_test
drwxr-xr-x 2 root root 220 May 25 19:52 split_paste_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
drwxr-xr-x 2 root root  23 Jun 23 15:08 tac_rev_test
-rw-r--r-- 1 root root  53 May 23 13:35 testfile1
drwxr-xr-x 2 root root  22 May 23 17:45 tr_test
drwxr-xr-x 2 root root   6 May 23 13:31 uniq_test
[root@entos7 ~]# ll /opt/tmp
total 0
[root@entos7 ~]# rsync -azvh /opt/bak_dir/shellscript /opt/tmp/
sending incremental file list
shellscript/
shellscript/testfile1
......
shellscript/tr_test/testfile
shellscript/uniq_test/

sent 1.00M bytes  received 935 bytes  2.01M bytes/sec
total size is 13.10M  speedup is 13.07
[root@entos7 ~]# ll /opt/tmp/shellscript/awk_test/ -d
drwxr-xr-x 2 root root 6 May 29 14:40 /opt/tmp/shellscript/awk_test/
[root@entos7 ~]# ll /opt/bak_dir/shellscript/awk_test/ -d
drwxr-xr-x 2 root root 6 May 29 14:40 /opt/bak_dir/shellscript/awk_test/


5.4 拷贝单个文件


[root@entos7 ~]# ll /opt/bak_dir/shellscript/sed_test/book.txt
-rw-r--r-- 1 root root 268 Jun 18 14:28 /opt/bak_dir/shellscript/sed_test/book.txt

[root@entos7 ~]# rsync -av /opt/bak_dir/shellscript/sed_test/book.txt /opt/tmp
sending incremental file list
book.txt

sent 362 bytes  received 35 bytes  794.00 bytes/sec
total size is 268  speedup is 0.68

[root@entos7 ~]# ll /opt/tmp/book.txt
-rw-r--r-- 1 root root 268 Jun 18 14:28 /opt/tmp/book.txt

5.5 从本地拷贝多个文件到远端


[root@entos7 ~]# rsync -avzh /opt/tmp/shellscript/sed_test /opt/tmp/shellscript/substr_test root@192.168.16.11:/opt/11tmp/
The authenticity of host '192.168.16.11 (192.168.16.11)' can't be established.
ECDSA key fingerprint is SHA256:wKbHkdwmpfqDeHqqdXMwzONwjYdqDp3V8q0JL2rzRW0.
ECDSA key fingerprint is MD5:75:b3:69:28:3f:93:8c:74:90:99:c2:73:06:1c:cb:81.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.16.11' (ECDSA) to the list of known hosts.
root@192.168.16.11's password:
sending incremental file list
sed_test/
sed_test/0
...
sed_test/testfile.txt
substr_test/

sent 3.54K bytes  received 328 bytes  61.87 bytes/sec
total size is 7.35K  speedup is 1.90

[root@centos7 /]# ll /opt/11tmp
total 0
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test

说明:

-z: –compress 使用压缩机制

-v: –verbose 打印详细信息

-a: -a –archive 归档模式选项, 详见示例3

-h: 以易读模式打印

注意:

与本地文件拷贝不同的地方在于, 当拷贝文件到远程服务器时, 我们指定远程主机上的用户名, 服务器地址, 路径等信息, 类似于使用scp命令拷贝,
与scp不同的地方在于, rsync更加强大, 支持各种选项, 最重要的一点是它支持断点续传.

如果没有设置ssh免密码登录我们还需要提供远程用户的密码等信息.有时你不想频繁输入密码, 或者rsync运行在一个无人执守的脚本里面, 这是需要预先设置ssh免密登录, 或者使用结合expect命令,自动输入密码, 但是出于安全考虑, 密码需要加密. 所以在条件许可的情况下, 还是推荐设置ssh免密登录.

5.6 从远端拷贝数据至本地


[root@entos7 ~]# ll /opt/10tmp #本地目录
total 0
[root@centos7 /]# ll /opt/11tmp #远端目录
total 0
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
[root@entos7 ~]# rsync -avzh root@192.168.16.11:/opt/11tmp/ /opt/10tmp/
root@192.168.16.11's password:
receiving incremental file list
./
sed_test/
sed_test/0
sed_test/book.txt
......
sed_test/testdata_new1.txt
sed_test/testfile.txt
substr_test/

sent 343 bytes  received 3.56K bytes  600.15 bytes/sec
total size is 7.35K  speedup is 1.88
[root@entos7 ~]# ll /opt/10tmp/
total 0
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test

5.7 拷贝时不覆盖目标位置已经修改过(同绝对路径同名文件,但是mtime晚于源目录文件)的文件

# 目标目录的文件
[root@entos7 sed_test]# ll /opt/10tmp/sed_test/book.txt
-rw-r--r-- 1 root root 296 Jun 26 14:42 /opt/10tmp/sed_test/book.txt

# 原目录的文件
[root@centos7 /]# ll /opt/11tmp/sed_test/book.txt
-rw-r--r-- 1 root root 268 Jun 18 14:28 /opt/11tmp/sed_test/book.txt

[root@entos7 sed_test]# rsync -avzhu root@192.168.16.11:/opt/11tmp/ /opt/10tmp/
root@192.168.16.11's password:
receiving incremental file list

sent 22 bytes  received 469 bytes  42.70 bytes/sec
total size is 7.35K  speedup is 14.97
[root@entos7 sed_test]# ll /opt/10tmp/sed_test/book.txt
-rw-r--r-- 1 root root 296 Jun 26 14:42 /opt/10tmp/sed_test/book.txt

# 可以看到目的目录中的/opt/10tmp/sed_test/book.txt文件因为mtime晚于源目录中的该文件,所有没有被同步

5.8 只拷贝目录结构不拷贝文件

在某些特殊场景中, 我们只需要远程服务器上的目录结构, 而不希望花大量时间, 带宽拷贝文件内容, 这时我们可以使用 -d, –dirs选项来达到目的


[root@entos7 opt]# rsync -v -d root@192.168.16.11:/opt/11tmp/ /opt/10tmp/
root@192.168.16.11's password:
receiving file list ... done
sed_test/
substr_test/

sent 26 bytes  received 90 bytes  6.63 bytes/sec
total size is 0  speedup is 0.00
[root@entos7 opt]# ll /opt/10tmp/sed_test/
total 0
[root@entos7 opt]# ll /opt/10tmp/
total 0
drwxr-xr-x 2 root root 6 Jun 27 16:58 sed_test
drwxr-xr-x 2 root root 6 Jun 27 16:58 substr_test


5.9 文件传输时显示进度

有时我们希望拷贝文件时, 能实时的显示拷贝进度, 以及传输速率等信息. 尤其是拷贝大文件时, 程序不输出信息, 用户往往无法区分程序是在响应中, 还是已经挂起, 在这种情况下如果使用 –progress 就会非常有帮助.


$ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/  
Password:  
receiving file list ...  
19 files to consider  
./  
Basenames  
     5357568 100%   14.98MB/s    0:00:00 (xfer#1, to-check=17/19)  
Conflictname  
       12288 100%   35.09kB/s    0:00:00 (xfer#2, to-check=16/19)  
  
sent 406 bytes  received 15810211 bytes  2108082.27 bytes/sec  
total size is 45305958  speedup is 2.87

5.10 同时删除目标位置多余的文件或目录

如果希望目标文件和源文件保持严格地一致, 不要多文件也不要少文件, 这是我们可能需要使用 -delete 选项来达到目的. 如果使用 -delete 选项, rsync将删除目标位置多余的文件或文件夹. 此选项还可以结合–delete-excluded 选项一起使用, 添加一些例外的文件.


[root@entos7 10tmp]# touch {1..10}.txt
[root@entos7 10tmp]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  sed_test  substr_test
[root@entos7 10tmp]# rsync -avzh -P --delete --exclude=4.txt root@192.168.16.11:/opt/11tmp/ /opt/10tmp
# 同步前删除目的目录中比原目录多的文件或目录,但是不包含4.txt
root@192.168.16.11's password:
receiving incremental file list
deleting 9.txt
deleting 8.txt
deleting 7.txt
deleting 6.txt
deleting 5.txt
deleting 3.txt
deleting 2.txt
deleting 10.txt
deleting 1.txt
./

sent 40 bytes  received 476 bytes  79.38 bytes/sec
total size is 7.35K  speedup is 14.25
[root@entos7 10tmp]# ls
4.txt  sed_test  substr_test

5.11 在目的端不创建新文件,只更新已存在的目录

只想更新, 目标位置已经存在的文件或目录, 而不关心源位置的新文件, 这时我们可以使用-existing 选项仅仅更新已经存在的文件.


[root@centos7 ~]# touch /opt/11tmp/{10..19}.txt
[root@centos7 ~]# ll /opt/11tmp/
total 0
-rw-r--r-- 1 root root   0 Jun 27 17:20 10.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 11.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 12.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 13.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 14.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 15.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 16.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 17.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 18.txt
-rw-r--r-- 1 root root   0 Jun 27 17:20 19.txt
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test
[root@entos7 10tmp]# ls
4.txt  sed_test  substr_test
[root@entos7 10tmp]# rsync -avhz -P --existing root@192.168.16.11:/opt/11tmp/  /opt/10tmp
root@192.168.16.11's password:
receiving incremental file list
./

sent 29 bytes  received 650 bytes  104.46 bytes/sec
total size is 7.35K  speedup is 10.83
[root@entos7 10tmp]# ll /opt/10tmp
total 0
-rw-r--r-- 1 root root   0 Jun 27 17:12 4.txt
drwxr-xr-x 2 root root 302 Jun 23 17:44 sed_test
drwxr-xr-x 2 root root   6 May 25 13:31 substr_test

可以发现没有将源端/opt/11tmp的新文件或目录拷贝过来, 只是更新了目的端的/tmp/10tmp下已经存在的文件或目录

5.12 查看目标位置和源位置之间的差异

选项-i, –itemize-changes 非常有用, 当我们想了解目标位置和源位置的文件差异时. 此选项可以用在拷贝前或拷贝后比较差异.

在源端:


$ ls -l /var/lib/rpm  
-rw-r--r-- 1 root root  5357568 2010-06-24 08:57 Basenames  
-rw-r--r-- 1 root root    12288 2008-05-28 22:03 Conflictname  
-rw-r--r-- 1 root root  1179648 2010-06-24 08:57 Dirnames

在目标端:


$ ls -l /root/temp  
-rw-r--r-- 1 root root    12288 May 28  2008 Conflictname  
-rw-r--r-- 1 bin  bin   1179648 Jun 24 05:27 Dirnames  
-rw-r--r-- 1 root root        0 Sep  3 06:39 Basenames

注意: 在上面的例子中, 源位置和目标位置有两处差异. 第一, 源文件Basenames的所有者和组与目标文件不同, 第一Dirnames文件大小也不一样.
现在让我们来看看rsync会怎样显示这些差异


$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/  
Password:  
receiving file list ... done  
>f.st.... Basenames  
.f....og. Dirnames  
  
sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec  
total size is 45305958  speedup is 20.76

输出信息只在相应文件前面显示了9个字母来标识改变, 这些字母具体是什么意思呢? 请参考以下详细说明

>表示文件已经被拷贝到了本地
f 代表该项目是一个文件.
s 代表文件大小发生了变化.
t 代表时间戳有差异.
o 所有者有差异
g 所属组有差异.

5.13 使用通配符过滤文件

rsync 可以使用–include 和 –exclude 选项结合通配符进行文件或文件夹过滤


$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/  
Password:  
receiving file list ... done  
./  
Packages  
Providename  
Provideversion  
Pubkeys  
  
sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec  
total size is 32768000  speedup is 3.19

在上面的示例中, 仅仅以P打头的文件和文件夹被包含了进来, 其他的文件都被过滤掉, 在拷贝的过程中被排除在外了.

5.14 过滤小文件和大文件


rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ 

## 不传输大小超过100K的文件和目录

rsync -avz --min-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/

## 不传输大小小于100K的文件和目录



5.15 拷贝整个文件

rsync 有个重要优点就是, 可以做到在拷贝的过程中, 只拷贝发生变化了的部分, 而不是发送整个文件. 但是在某些场景中, 比如文件较少, 文件size较小时, 我们的带宽又足够大, cpu资源相对又贫乏, 我们不希望它这样做, 因为毕竟计算源端和目标端的checksum, 并做对比, 也需要额外cpu开销. 这时我们可以使用 -W, –whole-file 选项, 让rsync不用计算那么多, 一上来就直接开始传送文件. 我们可以像下面这么做.(用带宽换使用率)


#  rsync -avzW  thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp  
Password:  
receiving file list ... done  
./  
Basenames  
Conflictname  
Dirnames  
Filemd5s  
Group  
Installtid  
Name  
  
sent 406 bytes  received 15810211 bytes  2874657.64 bytes/sec  
total size is 45305958  speedup is 2.87


5.16 断点传续

默认的情况下,当rsync传输中断后,新的rsync传输将删除所有的未完成的残余文件片段,然后开始新的传输。
而使用–partial后,将会进行我们所说的断点续传。
值得注意的是-P这个参数是综合了–partial –progress两个参数,所以rsync的断点续传可以用下面的例子来说明:

rsync -avzP /tmp/bigfile cnangel@10.1.6.160:/tmp/bigfile

5.17 使用--delete选项和空目录快速删除目录

rsync -av --delete  --exclude-from=/tmp/filelist.txt /path/to/source/ /path/to/destination/

# /path/to/source/ 为空目录
# /path/to/destination/ 为需要删除的目录
# --exclude-from=/tmp/filelist.txt 为指定不需要删除的文件列表

5.18rsync使用多线程

5.18.1 通过xargs



find /source -type d -print0 | xargs -0 -P 4 -I {} rsync -av {} user@remote:/dest/{}

5.18.2 通过parallel



ls file_part_* | parallel -j 4 rsync -av --files-from={} /source/ user@remote:/dest/

posted on 2025-10-11 17:44  平复心态  阅读(7)  评论(0)    收藏  举报