Linux rsync命令用法详解

 
在 Linux 系统下,数据备份的工具很多,除了前面介绍了 tar、cpio、dd 命令之外,本节再介绍一个用来备份数据的命令,就是 rsync

从字面意思上,rsync 可以理解为 remote sync(远程同步),但它不仅可以远程同步数据(类似于 scp 命令),还可以本地同步数据(类似于 cp 命令)。不同于 cp 或 scp 的一点是,使用 rsync 命令备份数据时,不会直接覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。

在系统学习 rsync 命令之前,请确认你的 Linux 系统中已经安装有此命令,如果没有,可以直接使用 yum install -y rsync 命令安装。

讲解 rsync 用法之前,为了让大家对此命令有一个整体的认识,这里先举个例子:

[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list

sent 34 bytes  received 15 bytes  98.00 bytes/sec
total size is 1432  speedup is 29.22

此例中,通过执行 rsync 命令,实现了将 /etc/passwd 文件本地同步到 /tmp/ 目录下,并改名为 1.txt。

除此之外,rsync 命令还支持远程同步数据,也就是将本地的数据备份到远程机器上。比如说,我们知道远程机器的 IP 地址为 192.168.188.128,则使用 rsync 命令备份 passwd 文件的执行命令为:

[root@localhost ~]# rsync -av /etc/passwd 192.168.188.128:/tmp/1.txt
The authenticity of host '192.168.188.128 (192.168.188.128)' can't be established.
ECDSA key fingerprint is 26:e3:97:e7:bb:ae:17:33:ea:aa:Oc:5f:37:Oe:9e:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.l68.l88.l28' (ECDSA) to the list of known hosts.
root@192.168.188.128's password: <-- 输入密码
sending incremental file list

sent 31 bytes received 12 bytes 7.82 bytes/sec
total size is 1432 speedup is 54.91

注意,首次远程连接时,会提示是否要继续连接,输入 yes 即可。另外,当成功建立连接后,需要输入目标系统的 root 密码。

通过以上 2 个实例,读者应该能对“rsync既支持本地备份数据,还支持远程备份数据”有了直观的认识。那么,rsync 命令要怎样使用呢?

rsync 命令的基本格式有多种,分别是:

[root@localhost ~]# rsync [OPTION] SRC DEST
[root@localhost ~]# rsync [OPTION] SRC [USER@]HOST:DEST
[root@localhost ~]# rsync [OPTION] [USER@]HOST:SRC DEST
[root@localhost ~]# rsync [OPTION] [USER@]HOST::SRC DEST
[root@localhost ~]# rsync [OPTION] SRC [USER@]HOST::DEST

针对以上 5 种命令格式,rsync 有 5 种不同的工作模式:
  • 第一种用于仅在本地备份数据;
  • 第二种用于将本地数据备份到远程机器上;
  • 第三种用于将远程机器上的数据备份到本地机器上;
  • 第四种和第三种是相对的,同样第五种和第二种是相对的,它们各自之间的区别在于登陆认证时使用的验证方式不同。

要知道,使用 rsync 在远程传输数据(备份数据)前,是需要进行登陆认证的,这个过程需要借助 ssh 协议或者 rsync 协议才能完成。在 rsync 命令中,如果使用单个冒号(:),则默认使用 ssh 协议;反之,如果使用两个冒号(::),则使用 rsync 协议。

ssh 协议和 rsync 协议的区别在于,rsync 协议在使用时需要额外配置,增加了工作量,但优势是更加安全;反之,ssh 协议使用方便,无需进行配置,但有泄漏服务器密码的风险。

另外,以上几种格式中各个参数的含义如下:
  • SRC:用来表示要备份的目标数据所在的位置(路径);
  • DEST:用于表示将数据备份到什么位置;
  • USER@:当做远程同步操作时,需指明系统登录的用户名,如果不显示指定,默认为以 root 身份登录系统并完成同步操作。

rsync 命令提供使用的 OPTION 及功能如表 1 所示。

表 1 rsync 选项及功能
OPTION选项 功能
-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 选项。
-r 表示以递归模式处理子目录,它主要是针对目录来说的,如果单独传一个文件不需要加 -r 选项,但是传输目录时必须加。
-v 表示打印一些信息,比如文件列表、文件数量等。
-l 表示保留软连接。
-L 表示像对待常规文件一样处理软连接。如果是 SRC 中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到 DEST。
-p 表示保持文件权限。
-o 表示保持文件属主信息。
-g 表示保持文件属组信息。
-D 表示保持设备文件信息。
-t 表示保持文件时间信息。
--delete 表示删除 DEST 中 SRC 没有的文件。
--exclude=PATTERN 表示指定排除不需要传输的文件,等号后面跟文件名,可以是通配符模式(如 *.txt)。
--progress 表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、 同步的文件传输速度等。
-u 表示把 DEST 中比 SRC 还新的文件排除掉,不会覆盖。
-z 加上该选项,将会在传输过程中压缩。

以上也仅是列出了 async 命令常用的一些选项,对于初学者来说,记住最常用的几个即可,比如 -a、-v、-z、--delete 和 --exclude。

如果想查看 async 提供的所有选项,可直接执行 async 命令。

为了更好的演示各个选项的功能,需要做一些准备工作,执行如下命令:

#新建rsync目录
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
#在rsync目录中,创建test1目录
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# cd test1
#在test1目录中,分别创建名为 1、2、3、/root.123.txt 文件
[root@localhost test1]# touch 1 2 3 /root/123.txt
[root@localhost test1]# ln -s /root/123.txt ./123.txt
[root@localhost test1]# ls -l
total 0
-rw-r--r--. 1 root root 0 0ct 23 07:34 1
lrwxrwxrwx. 1 root root 13 0ct 23 08:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 0ct 23 07:34 2
-rw-r--r--. 1 root root 0 0ct 23 07:34 3
[root@localhost test1]# cd ..
#回到rsync目录
[root@localhost rsync]#

在此基础上,下面挑选了几个常用的 OPTION 选项,给大家举例说明它们的用法。

rsync -a 选项

首先来看看 -a 选项的用法,如下所示:

[root@localhost rsync]# rsync -a test1 test2
[root@localhost rsync]# ls test2
test1
[root@localhost rsync]# ls test2/test1/
1  123.txt  2  3

这里有一个问题,我们本来是想把 test1 目录中的内容直接放到 test2 目录中,可结果 rsync 命令却新建了 test2 目录,然后把 test1 放到 test2 中。

如果想要实现将 test1 目录中的内容直接备份到 test2 目录中,需修改上面的命令为:

[root@localhost rsync]#rm -rf test2
[root@localhost rsync]# rsync -a test1/ test2/
[root@localhost rsync]# ls test2/
1  123.txt  2  3

可以看到,只需给 test1 和 test2 目录后添加 / 斜杠即可。

前面讲过,使用 -a 选项,等同于同时使用 -r、-l、-p、-t、-g、-o、-D 选项,且 -a 还可以和 --no-OPTION 一并使用。下面再来看看 -l 选项的作用:

[root@localhost rsync]# rm -rf test2
[root@localhost rsync]# rsync -av test1/ test2/
sending incremental file list
created directory test2
./
1
skipping non-regular file "123.txt"
2
3

sent 200 bytes received 72 bytes 544.00 bytes/sec
total size is 13  speedup is 0.05

这里使用 -v 选项,可以看到,拷贝过程中跳过了非普通文件 123.txt,其实 123.txt 是一个软链接文件,如果不使用 -l 选项,系统将不理会软链接文件。

rsync --delete选项

通过表 1 可以看到,--delete 选项用来--delete 删除 DEST 中 SRC 没有的文件。例如:

#拷贝 test1 目录下的数据
[root@localhost rsync]# rsync -a test1/ test2
#删除 test1/123.txt 文件
[root@localhost rsync]# rm -f test1/123.txt
[root@localhost rsync]# ls test1/
1 2 3
[root@localhost rsync]# rsync -av test1/ test2/
sending incremental file list
./

sent 55 bytes  received 15 bytes 140.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost rsync]# ls test2/
1 123.txt 2 3

可以看到,当对 test1 目录删除了 123.txt 文件之后,再次备份并没有对 test2 目录中的 123.txt 文件产生任何影响。

下面使用 --delete 选项,再次执行拷贝命令,如下所示:

[root@localhost rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt

sent 52 bytes  received 12 bytes 128.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost rsync]# ls test2/
1 2 3

可以看到,使用 --delete 选项进行备份数据时,test1 目录一旦做了改变,那么 test2 也会做相应改变。

不仅如此,如果在 DEST 中增加文件,而 SRC 中不包含这些文件,那么在使用 --delete 选项做同步备份操作时,DEST 新增的这些文件会被删除。例如:

[root@localhost rsync]# touch test2/4
[root@localhost rsync]# ls test1/
1 2 3
[root@localhost rsync]# ls test2/
1 2 3 4
[root@localhost rsync]# rsync -a --delete test1/ test2/
[root@localhost rsync]# ls test2/
1 2 3

受到篇幅的限制,有关 rsync 命令其他选项的用法,本节不再给出具体实例,有兴趣的读者可自行编写代码进行测试。