在系统学习 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
[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 密码。
[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 种不同的工作模式:
- 第一种用于仅在本地备份数据;
- 第二种用于将本地数据备份到远程机器上;
- 第三种用于将远程机器上的数据备份到本地机器上;
- 第四种和第三种是相对的,同样第五种和第二种是相对的,它们各自之间的区别在于登陆认证时使用的验证方式不同。
ssh 协议和 rsync 协议的区别在于,rsync 协议在使用时需要额外配置,增加了工作量,但优势是更加安全;反之,ssh 协议使用方便,无需进行配置,但有泄漏服务器密码的风险。
另外,以上几种格式中各个参数的含义如下:
- SRC:用来表示要备份的目标数据所在的位置(路径);
- DEST:用于表示将数据备份到什么位置;
- USER@:当做远程同步操作时,需指明系统登录的用户名,如果不显示指定,默认为以 root 身份登录系统并完成同步操作。
| 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 提供的所有选项,可直接执行 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
[root@localhost rsync]#rm -rf test2 [root@localhost rsync]# rsync -a test1/ test2/ [root@localhost rsync]# ls test2/ 1 123.txt 2 3
[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
这里使用 -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
[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
[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 命令其他选项的用法,本节不再给出具体实例,有兴趣的读者可自行编写代码进行测试。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178705.html原文链接:https://javaforall.net
