目录
1 rsync工具介绍
rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于cp或scp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖
- 安装rsync软件包(同步文件的机器都需要安装)
[root@linux02 ~]# yum install -y rsync
- 把/etc/passwd同步到/tmp/目录下,并改名为1.txt
[root@linux02 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 926 bytes received 35 bytes 1,922.00 bytes/sec
total size is 834 speedup is 0.87
[root@linux02 ~]# ls -l /tmp/
total 8
-rw-r--r--. 1 root root 834 Oct 25 09:28 1.txt
-rwx------. 1 root root 836 Oct 25 08:52 ks-script-wgZ0hw
-rw-------. 1 root root 0 Oct 25 08:45 yum.log
- 若远程复制,数据备份就是这样的形式——IP:path,比如192.168.139.112:/tmp/
- 在linux01(129.168.139.111)上同步文件到linux02(192.168.139.112)
[root@linux01 ~]# rsync -av /etc/passwd 192.168.139.112:/tmp/1.txt
sending incremental file list
passwd
sent 964 bytes received 35 bytes 1,998.00 bytes/sec
total size is 873 speedup is 0.87
[root@linux02 ~]# ls -l /tmp/
total 8
-rw-r--r--. 1 root root 873 Oct 28 17:05 1.txt
-rwx------. 1 root root 836 Oct 25 08:52 ks-script-wgZ0hw
-rw-------. 1 root root 0 Oct 25 08:45 yum.log
rsync格式
- rsync [OPTION] … SRC DEST
- rsync [OPTION] … SRC [user@]host:DEST
- rsync [OPTION] … [user@]host:SRC DEST
- rsync [OPTION] … SRC [user@]host::DEST
- rsync [OPTION] … [user@]host::SRC DEST
第一个例子为第一种格式,第二个例子为第二种格式。但不同的是,并没有加user@host,如果不加默认指的是root。第三种格式是从远程目录同步数据到本地。第四种和第五种格式使用了两个冒号,这种格式和其他格式的验证方式不同。
2 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 删除DEST中SRC没有的文件
--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
-z 传输时压缩
先创建一些文件和目录
[root@linux01 ~]# mkdir rsync
[root@linux01 ~]# cd rsync/
[root@linux01 rsync]# mkdir test1
[root@linux01 rsync]# cd test1/
[root@linux01 test1]# touch 1.txt 2.txt 3.txt /root/123.txt
[root@linux01 test1]# ln -s /root/123.txt ./123.txt
[root@linux01 test1]# mkdir dir
[root@linux01 test1]# touch dir/4.txt dir/5.txt
[root@linux01 test1]# ls -l
total 0
lrwxrwxrwx 1 root root 13 Oct 28 20:45 123.txt -> /root/123.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 2.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 3.txt
drwxr-xr-x 2 root root 32 Oct 28 20:45 dir
- 带-av参数同步文件
[root@linux01 rsync]# rsync -av test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/4.txt
dir/5.txt
sent 425 bytes received 162 bytes 1,174.00 bytes/sec
total size is 13 speedup is 0.02
[root@linux01 rsync]# ls /tmp/
ks-script-wgZ0hw test2 testcron.log usercron.log yum.log
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 6 files
- 带--delete同步文件
[root@linux01 rsync]# touch /tmp/test2/111.txt
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 111.txt
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 7 files
[root@linux01 rsync]# rsync -av --delete test1/ /tmp/test2/
sending incremental file list
deleting 111.txt
./
sent 216 bytes received 31 bytes 494.00 bytes/sec
total size is 13 speedup is 0.05
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 6 files
- 带--exclude同步文件
[root@linux01 rsync]# rm -rf /tmp/test2/
[root@linux01 rsync]# rsync -av --exclude="*.txt" test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
dir/
sent 80 bytes received 56 bytes 272.00 bytes/sec
total size is 0 speedup is 0.00
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
└── dir
1 directory, 0 files
- 带--exclude同步文件,并指定不同步dir目录下的4.txt和test2目录下的1.txt
[root@linux01 rsync]# rm -rf /tmp/test2/
[root@linux01 rsync]# tree test1/
test1/
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 6 files
[root@linux01 rsync]# rsync -av --exclude="1.txt" --exclude="dir/4.txt" test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/5.txt
sent 310 bytes received 120 bytes 860.00 bytes/sec
total size is 13 speedup is 0.03
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 123.txt -> /root/123.txt
├── 2.txt
├── 3.txt
└── dir
└── 5.txt
1 directory, 4 files
- 带-u同步文件
- 没加-u参数同步文件,test1目录的1.txt和/tmp/test2目录下的1.txt时间一样
[root@linux01 rsync]# rm -rf /tmp/test2/
[root@linux01 rsync]# rsync -av test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/4.txt
dir/5.txt
sent 417 bytes received 158 bytes 1,150.00 bytes/sec
total size is 13 speedup is 0.02
[root@linux01 rsync]# ll test1/1.txt /tmp/test2/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 test1/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 /tmp/test2/1.txt
- 修改/tmp/test2目录下的1.txt的创建时间
[root@linux01 rsync]# echo "123" > /tmp/test2/1.txt
[root@linux01 rsync]# ll test1/1.txt /tmp/test2/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 test1/1.txt
-rw-r--r-- 1 root root 4 Oct 28 21:15 /tmp/test2/1.txt
- 这时时间不一样了,再次同步文件
[root@linux01 rsync]# rsync -av test1/ /tmp/test2/
sending incremental file list
1.txt
sent 252 bytes received 36 bytes 576.00 bytes/sec
total size is 13 speedup is 0.05
[root@linux01 rsync]# ll test1/1.txt /tmp/test2/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 test1/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 /tmp/test2/1.txt
- 两者1.txt时间一样了,这时候加上-u选项
[root@linux01 rsync]# echo "123" > /tmp/test2/1.txt
[root@linux01 rsync]# ll test1/1.txt /tmp/test2/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 test1/1.txt
-rw-r--r-- 1 root root 4 Oct 28 21:18 /tmp/test2/1.txt
[root@linux01 rsync]# rsync -auv test1/ /tmp/test2/
sending incremental file list
sent 209 bytes received 13 bytes 444.00 bytes/sec
total size is 13 speedup is 0.06
[root@linux01 rsync]# ll test1/1.txt /tmp/test2/1.txt
-rw-r--r-- 1 root root 0 Oct 28 20:43 test1/1.txt
-rw-r--r-- 1 root root 4 Oct 28 21:18 /tmp/test2/1.txt
这时候两者的时间不相同了
- 带-P同步文件,显示传输速率
[root@linux01 rsync]# rm -rf /tmp/test2/
[root@linux01 rsync]# rsync -avP test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
123.txt -> /root/123.txt
2.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/8)
3.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/8)
dir/
dir/4.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=1/8)
dir/5.txt
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=0/8)
sent 425 bytes received 162 bytes 1,174.00 bytes/sec
total size is 13 speedup is 0.02
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 6 files
- 带-z同步文件,加快传输速率
[root@linux01 rsync]# rm -rf /tmp/test2/
[root@linux01 rsync]# rsync -avPz test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
123.txt -> /root/123.txt
2.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/8)
3.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/8)
dir/
dir/4.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=1/8)
dir/5.txt
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=0/8)
sent 410 bytes received 162 bytes 1,144.00 bytes/sec
total size is 13 speedup is 0.02
[root@linux01 rsync]# tree /tmp/test2/
/tmp/test2/
├── 123.txt -> /root/123.txt
├── 1.txt
├── 2.txt
├── 3.txt
└── dir
├── 4.txt
└── 5.txt
1 directory, 6 files
3 rsync通过ssh同步
- 传输的机器都要安装rsync
[root@linux01 ~]# yum install -y rsync
- rsync通过ssh方式同步,同步文件到ip为192.168.139.112
[root@linux01 rsync]# rsync -av test1/ 192.168.139.112:/tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/4.txt
dir/5.txt
sent 425 bytes received 162 bytes 391.33 bytes/sec
total size is 13 speedup is 0.02
[root@linux02 ~]# ls /tmp/
1.txt ks-script-wgZ0hw test2 yum.log
- 从ip为192.168.139.112机器上同步文件到本机上
[root@linux01 rsync]# rsync -av 192.168.139.112:/tmp/test2/ /tmp/test3/
receiving incremental file list
created directory /tmp/test3
./
1.txt
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/4.txt
dir/5.txt
sent 133 bytes received 415 bytes 1,096.00 bytes/sec
total size is 13 speedup is 0.02
[root@linux01 rsync]# ls /tmp/
ks-script-wgZ0hw test2 test3 testcron.log usercron.log yum.log
- 指定端口传文件
[root@linux01 rsync]# rsync -av -e "ssh -p 22" test1/ 192.168.139.112:/tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1.txt
123.txt -> /root/123.txt
2.txt
3.txt
dir/
dir/4.txt
dir/5.txt
sent 425 bytes received 162 bytes 1,174.00 bytes/sec
total size is 13 speedup is 0.02
- 指定端口登录到机器上
[root@linux01 rsync]# ssh -p 22 192.168.139.112
Last login: Sun Oct 28 20:15:44 2018 from 192.168.139.1
[root@linux02 ~]#