讲解 rsync 用法之前,为了让大家对此命令有一个整体的认识,这里先举个例子:在系统学习 rsync 命令之前,请确认你的 Linux 系统中已经安装有此命令,如果没有,可以直接使用 yum install -y 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
通过以上 2 个实例,读者应该能对“rsync既支持本地备份数据,还支持远程备份数据”有了直观的认识。那么,rsync 命令要怎样使用呢?注意,首次远程连接时,会提示是否要继续连接,输入 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
另外,以上几种格式中各个参数的含义如下:ssh 协议和 rsync 协议的区别在于,rsync 协议在使用时需要额外配置,增加了工作量,但优势是更加安全;反之,ssh 协议使用方便,无需进行配置,但有泄漏服务器密码的风险。
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]#
[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
sent 200 bytes received 72 bytes 544.00 bytes/sec
total size is 13 speedup is 0.05
#拷贝 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 命令其他选项的用法,本节不再给出具体实例,有兴趣的读者可自行编写代码进行测试。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有