Linux-rsync命令⽤法详解
从字⾯意思上,rsync 可以理解为 remote sync(远程同步),但它不仅可以远程同步数据(类似于 scp 命令),还可以本地同步数据(类似于 cp 命令)。不同于 cp 或 scp 的⼀点是,使⽤ rsync 命令备份数据时,不会直接覆盖以前的数据(如果数据已经存在),⽽是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
在系统学习 rsync 命令之前,请确认你的 Linux 系统中已经安装有此命令,如果没有,可以直接使⽤ yum install -y rsync 命令安装。
讲解 rsync ⽤法之前,为了让⼤家对此命令有⼀个整体的认识,这⾥先举个例⼦:
[root@localhost ~]# rsync -av /etc/passwd /
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:/
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选项功能
这是归档模式,表⽰以递归⽅式传输⽂件,并保持所有属性,它等同于-r、-l、-p、-t、-g、-o、-D 选项。-a 选项后⾯可
-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⽬录linux递归删除目录命令
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# cd test1
#在test1⽬录中,分别创建名为 1、2、3、/ ⽂件
[root@localhost test1]# touch 1 2 3 /
[root@localhost test1]# ln -s / ./
[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: -> /
-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/
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/
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 ""
2
3
sent 200 bytes received 72 bytes 544.00 bytes/sec
total size is 13  speedup is 0.05
这⾥使⽤ -v 选项,可以看到,拷贝过程中跳过了⾮普通⽂件 ,其实 是⼀个软链接⽂件,如果不使⽤ -l 选项,系统将不理会软链接⽂件。
rsync --delete选项
通过表 1 可以看到,--delete 选项⽤来--delete 删除 DEST 中 SRC 没有的⽂件。例如:
#拷贝 test1 ⽬录下的数据
[root@localhost rsync]# rsync -a test1/ test2
#删除 ⽂件
[root@localhost rsync]# rm -f
[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/
2 3
可以看到,当对 test1 ⽬录删除了 ⽂件之后,再次备份并没有对 test2 ⽬录中的 ⽂件产⽣任何影响。
下⾯使⽤ --delete 选项,再次执⾏拷贝命令,如下所⽰:
[root@localhost rsync]# rsync -av --delete test1/ test2/
sending incremental file list
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 命令其他选项的⽤法,本节不再给出具体实例,有兴趣的读者可⾃⾏编写代码进⾏测试。