目录
一、rsync工具介绍
二、rsync常用选项
三、rsync通过ssh同步
一、rsync工具介绍
rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于cp或scp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
系统中没有rsync,执行yum install -y rsync命令安装rsync。
- rsync 的命令格式
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
举例:
把/etc/passwd同步到本地/tmp/目录下,并改名为a.txt。
[root@minglinux-01 ~]# rsync -av /etc/passwd /tmp/a.txt
sending incremental file list
passwd
sent 1,184 bytes received 35 bytes 2,438.00 bytes/sec
total size is 1,092 speedup is 0.90
[root@minglinux-01 ~]# ll !$
ll /tmp/a.txt
-rw-r--r-- 1 root root 1092 10月 24 23:26 /tmp/a.txt
把/etc/passwd远程数据备份,形式为:用户名[@IP]:路径,比如root@192.168.162.132:/root/,对方机器也需要安装rsync,示例命令如下:
[root@minglinux-01 ~]# rsync -av /etc/passwd root@192.168.162.132:/root/a.txt
root@192.168.162.132's password:
sending incremental file list
passwd
sent 1,184 bytes received 35 bytes 221.64 bytes/sec
total size is 1,092 speedup is 0.90
[root@minglinux-02 ~]# ls //minglinux-02IP地址即192.168.162.132
anaconda-ks.cfg a.txt
[root@minglinux-02 ~]# ll a.txt
-rw-r--r--. 1 root root 1092 10月 24 23:26 a.txt
二、rsync常用选项
rsync命令常用选项:
-a:这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-rlptgoD。-a选项后面可以跟一个--no-OPTION,表示关闭-rlptgoD中的某一个,比如-a--no-l等同于-rptgoD。
-r:表示以递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。
-v:表示打印一些信息,比如文件列表、文件数量等。
-l:表示保留软连接。
-L:表示像对待常规文件一样处理软连接。如果是SRC中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到DST。
-p:表示保持文件权限。
-o:表示保持文件属主信息。
-g:表示保持文件属组信息。
-D:表示保持设备文件信息。
-t:表示保持文件时间信息。
--delete:表示删除DST中SRC没有的文件。
--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)。
--progress:表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。
-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。
-z:加上该选项,将会在传输过程中压缩
选项使用练习:
- 建立练习用的目录和文件
[root@minglinux-01 ~]# mkdir rsync
[root@minglinux-01 ~]# cd rsync/
[root@minglinux-01 rsync]# mkdir test1
[root@minglinux-01 rsync]# cd test1/
[root@minglinux-01 test1]# touch 1 2 3 /root/123.tzr
[root@minglinux-01 test1]# touch 1 2 3 /root/123.txt
[root@minglinux-01 test1]# ln -s /root/123.txt ./123.txt
[root@minglinux-01 test1]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
- 使用-a选项
使用rsync备份目录时要在源目录和目标目录后面加上斜杠/,否则达不到复制效果。
示例命令如下:
[root@minglinux-01 rsync]# rsync -a test1/ test2/
[root@minglinux-01 rsync]# ll test2/
总用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
-a选项等同于-rlptgoD,且-a还可以和--no-OPTIN一并使用。
使用--no-l不备份链接文件,示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2
[root@minglinux-01 rsync]# ls
test1
[root@minglinux-01 rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3
sent 229 bytes received 144 bytes 746.00 bytes/sec
total size is 13 speedup is 0.03
- 使用-L选项
加上-L选项就可以把SRC中软连接的目标文件复制到DST。示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2
[root@minglinux-01 rsync]# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3
sent 260 bytes received 123 bytes 766.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ll test2/
总用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
-rw-r--r-- 1 root root 0 10月 31 22:47 123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
- 使用-u选项
首先查看test1/1和test2/1的创建时间是一样的:
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1
面修改test2/1的创建时间,然后使用rsync不加-u同步:
[root@minglinux-01 rsync]# echo "1" > test2/1
[root@minglinux-01 rsync]# ll !$
ll test2/1
-rw-r--r-- 1 root root 2 10月 31 23:09 test2/1
[root@minglinux-01 rsync]# rsync -a test1/1 test2/
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1
上面test2/1的创建时间还是和test1/1一样。下面加上-u选项:
[root@minglinux-01 rsync]# echo "123" > test2/1
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
[root@minglinux-01 rsync]# rsy
rsync rsyslogd rsyslog-recover-qi.pl
[root@minglinux-01 rsync]# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt
sent 129 bytes received 22 bytes 302.00 bytes/sec
total size is 13 speedup is 0.09
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
加上-u选项后,不会再把test1/1同步为test2/1了。
- 使用--delete选项
首先删除test1/123.txt,然后把test1/目录同步到test2/目录下:
[root@minglinux-01 rsync]# rm -f test1/123.txt
[root@minglinux-01 rsync]# ls test1/
1 2 3
[root@minglinux-01 rsync]# rsync -av test1/ test2/
sending incremental file list
./
1
sent 127 bytes received 38 bytes 330.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1 123.txt 2 3
上例中,test2/目录并没有删除test1目录中没有的123.txt。下面加上--delete选项,示例如下:
[root@minglinux-01 rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt
sent 81 bytes received 23 bytes 208.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1 2 3
这样test2/目录下的123.txt也被删除了。
如果在DST中增加文件了,而SRC当中没有这些文件,同步时加上
--delete选项后同样会删除新增的文件。如下所示:
[root@minglinux-01 rsync]# ls test2/
1 2 3
[root@minglinux-01 rsync]# touch test2/4
[root@minglinux-01 rsync]# ls test1/
1 2 3
[root@minglinux-01 rsync]# ls test2/
1 2 3 4
[root@minglinux-01 rsync]# rsync -a --delete test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1 2 3
test2/:
1 2 3
- 使用--exclude选项
[root@minglinux-01 rsync]# touch test1/4
[root@minglinux-01 rsync]# rsync -a --exclude="4" test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1 2 3 4
test2/:
1 2 3
三、rsync通过ssh同步
前面介绍过的rsync的5种命令格式中如下两种就属于通过ssh的方式备份数据。这种方式其实就是让用户登录到远程机器,然后执行rsync的任务。
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
第一种方式已经实现过了,下面试试第二种方式:
[root@minglinux-01 rsync]# rsync -avL 192.168.162.132:/root/a.txt ./test3
root@192.168.162.132's password:
receiving incremental file list
a.txt
sent 43 bytes received 1,183 bytes 272.44 bytes/sec
total size is 1,092 speedup is 0.89