10.28 rsync工具介绍
rsync是一个实用的同步工具
需求:A目录的数据实时更新,将A的数据拷贝到B目录,每隔一小时拷贝一次
使用cp命令:
1 在A目录中找出最近1小时新增的内容拷贝到B(比较麻烦)
2 直接拷贝A目录的所有内容将B目录覆盖(浪费时间和磁盘IO)
cp命令不适用
rsync可以实现增量更新(rsync只会同步新增和有内容变更的文件);
支持远程同步(将数据同步到不同ip的另一台机器上);
安装rsync
[root@hyc-01-01 ~]# yum install -y rsync
将passwd同步到tmp目录下并改名为1.txt
[root@hyc-01-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 938 bytes received 35 bytes 1,946.00 bytes/sec
total size is 846 speedup is 0.87
-a 包含多个选项
-v 可视化,可以看到拷贝的过程
将文件同步到远程机器的特定目录
[root@hyc-01-01 ~]# rsync -av /etc/passwd root@192.168.31.128:/tmp/1234.txt
The authenticity of host '192.168.31.128 (192.168.31.128)' can't be established.
ECDSA key fingerprint is SHA256:0SErfGbbc3AfFcxC92Tav9X/T/bOn8wfnvum/wnw5Xs.
ECDSA key fingerprint is MD5:b7:d4:e4:4a:4a:33:29:99:1a:2e:45:94:d9:40:17:fb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.31.128' (ECDSA) to the list of known hosts.
root@192.168.31.128's password:
sending incremental file list
passwd
sent 938 bytes received 35 bytes 67.10 bytes/sec
total size is 846 speedup is 0.87
其中root@可以省略,此时使用执行当前命令的用户作为登录用户
将远程机器的某文件拷贝到当前机器某个目录下
[root@hyc-01-01 ~]# rsync -av root@192.168.31.128:/etc/passwd /tmp/passwddd
root@192.168.31.128's password:
receiving incremental file list
passwd
sent 43 bytes received 938 bytes 130.80 bytes/sec
total size is 846 speedup is 0.86
[root@hyc-01-01 ~]# ls /tmp/passwddd
/tmp/passwddd
10.29 rsync常用选项(上)
-a 包含-rtplgoD
-r 类似cp –r,同步目录时使用
-v 显示拷贝文件的一些信息,包括拷贝用时,拷贝字节数,拷贝了哪些文件,传输的时间和速度等信息
-l 将软链接文件原封不动拷贝到对应位置
-L 假如将某个软链接从一台机器拷贝到另一台机器,另一台机器可能没有这个软链接对应的源文件,导致软链接文件标红并闪烁,为避免这种情况,加上-L参数会把要拷贝的软链接文件对应的源文件一起拷贝到另一台机器
-p 保持文件的权限属性
-o 保持文件的属主,同步文件所有者不变,若另一台机器没有该属主,则该文件属主信息在另一台机器上会被显示为PUID(若hyc对应1001则显示属主为hyc或1001)
-g 保持文件属组信息不变,若另一台机器没有该属组,则属组会被显示为GUID(hyc组对应1001则属组为hyc或1001)
-D 保持设备文件信息
-t 保持文件的时间信息(mtime、ctime、atime等时间信息保持不变)
--delete 在目标中删除源中没有的项
--exclude 在同步时排除某些项不执行同步
-P 显示比-v更详细的同步过程
-z 传输时压缩,传输过程中压缩,传到对端后自动解压
10.30 rsync常用选项(下)
将目录aaa同步到/tmp下并改名为aaa_dest
[root@hyc-01-01 aaa]# rsync -av /root/aaa/ /tmp/aaa_dest/
sending incremental file list
created directory /tmp/aaa_dest
./
1.txt
2.txt
3.txt
ipt.txt.l -> /tmp/ipt.txt
sent 271 bytes received 115 bytes 772.00 bytes/sec
total size is 12 speedup is 0.03
[root@hyc-01-01 aaa]# ls /tmp/aaa_dest
1.txt 2.txt 3.txt ipt.txt.l
[root@hyc-01-01 aaa]# ls -l ipt.txt.l
lrwxrwxrwx. 1 root root 12 7月 19 22:34 ipt.txt.l -> /tmp/ipt.txt
拷贝目录时最好源目录和目标目录均写完整的/,否则拷贝会有问题
[root@hyc-01-01 tmp]# rsync -av /root/aaa /tmp/bbb_dest
sending incremental file list
bbb/
bbb/1.txt
bbb/2.txt
bbb/3.txt
bbb/ipt.txt.l -> /tmp/ipt.txt
sent 286 bytes received 80 bytes 732.00 bytes/sec
total size is 12 speedup is 0.03
[root@hyc-01-01 tmp]# ls bbb_dest
aaa
此时会将aaa目录同步到/tmp/aaa_dest目录下而不是将aaa目录改名为aaa_dest目录
l
拷贝软链接文件
L
[root@hyc-01-01 tmp]# rsync -av /root/aaa/ /tmp/aaa_dest/
sending incremental file list
created directory /tmp/aaa_dest
./
1.txt
2.txt
3.txt
ipt.txt.l -> /tmp/ipt.txt
sent 271 bytes received 115 bytes 772.00 bytes/sec
total size is 12 speedup is 0.03
[root@hyc-01-01 tmp]# ls /tmp/aaa_dest
1.txt 2.txt 3.txt ipt.txt.l
[root@hyc-01-01 tmp]# ls /root/aaa
1.txt 2.txt 3.txt ipt.txt.l
L参数将软链接指向的源文件同步到目录下
[root@hyc-01-01 tmp]# rsync -avL /root/aaa/ /tmp/aaa_dest/
sending incremental file list
ipt.txt.l
sent 3,591 bytes received 35 bytes 7,252.00 bytes/sec
total size is 3,417 speedup is 0.94
[root@hyc-01-01 tmp]# ls /tmp/aaa_dest
1.txt 2.txt 3.txt ipt.txt.l
--delete
[root@hyc-01-01 aaa_dest]# touch new.txt 在aaa_dest目录创建new.txt,此时/root/aaa下没有new.txt
[root@hyc-01-01 aaa_dest]# ls
1.txt 2.txt 3.txt ipt.txt.l new.txt
[root@hyc-01-01 aaa_dest]# pwd
/tmp/aaa_dest
[root@hyc-01-01 aaa_dest]# rsync -avl --delete /root/aaa/ /tmp/aaa_dest/ 同步文件
sending incremental file list
deleting new.txt
./
ipt.txt.l -> /tmp/ipt.txt
sent 154 bytes received 33 bytes 374.00 bytes/sec
total size is 12 speedup is 0.06
[root@hyc-01-01 aaa_dest]# ls /tmp/aaa_dest 同步完成后/tmp/aaa_dest下的new.txt消失
1.txt 2.txt 3.txt ipt.txt.l
另:同步时使用了l参数,可以看到原来使用L参数产生的文本文件ipt.txt.l变成了软链接ipt.txt.l
--exclude
[root@hyc-01-01 aaa_dest]# rm -rf *
[root@hyc-01-01 aaa_dest]# ls
[root@hyc-01-01 aaa_dest]# rsync -avl --exclude "*.txt" /root/aaa/ /tmp/aaa_dest/
sending incremental file list
./
ipt.txt.l -> /tmp/ipt.txt
sent 91 bytes received 22 bytes 226.00 bytes/sec
total size is 12 speedup is 0.11
[root@hyc-01-01 aaa_dest]# ls /root/aaa
1.txt 2.txt 3.txt ipt.txt.l
[root@hyc-01-01 aaa_dest]# ls /tmp/aaa_dest
ipt.txt.l
aaa目录下的*.txt文件都没有同步到aaa_dest目录下;
--exclude参数不支持跟多个项,要匹配多个项只能写多个—exclude;
[root@hyc-01-01 aaa_dest]# ls /root/aaa
1.txt 2.txt 3.txt ddd.doc hyc.doc ipt.txt.l
[root@hyc-01-01 aaa_dest]# rsync -avl --exclude "*.txt" --exclude "*.doc" /root/aaa/ /tmp/aaa_dest/
sending incremental file list
./
sent 87 bytes received 19 bytes 212.00 bytes/sec
total size is 12 speedup is 0.11
[root@hyc-01-01 aaa_dest]# ls /tmp/aaa_dest
ipt.txt.l
过滤了*.doc和*.txt,同步的目标目录中只有软链接ipt.txt.l
-P 将传输过程中的信息打印在屏幕上,显示的信息比-v更详细
[root@hyc-01-01 aaa_dest]# rsync -avLP /root/aaa/ /tmp/aaa_dest/
sending incremental file list
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=5/7)
2.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/7)
3.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/7)
ddd.doc
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=2/7)
hyc.doc
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=1/7)
ipt.txt.l
3,417 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=0/7)
sent 3,830 bytes received 133 bytes 7,926.00 bytes/sec
total size is 3,417 speedup is 0.86
-u 若被同步的目录中的文件比源目录中对应文件更新时间更晚,则不同步该文件
[root@hyc-01-01 aaa_dest]# cat 3.txt 同步前被同步目录中3.txt内容
;sjdojojqioejfqjfojdfio
qajenrjqer[ognnf[qjio
[root@hyc-01-01 aaa_dest]# rsync -avLu /root/aaa/ /tmp/aaa_dest/
sending incremental file list
./
sent 176 bytes received 19 bytes 390.00 bytes/sec
total size is 3,441 speedup is 17.65
[root@hyc-01-01 aaa_dest]# cat 3.txt 同步后文件内容未改变
;sjdojojqioejfqjfojdfio
qajenrjqer[ognnf[qjio
[root@hyc-01-01 aaa_dest]# cat /root/aaa/3.txt 同步源目录中的3.txt文件
;sjdojojqioejfqjfojdfio
[root@hyc-01-01 aaa_dest]# ls -l 3.txt 被同步目录中的3.txt更新时间更晚
-rw-r--r--. 1 root root 46 7月 19 23:38 3.txt
[root@hyc-01-01 aaa_dest]# ls -l /root/aaa/3.txt
-rw-r--r--. 1 root root 24 7月 19 23:37 /root/aaa/3.txt
-z 传输时将文件压缩传输,同步后的文件为解压缩后的文件或目录,这样做可以节省带宽
10.31 rsync通过ssh同步
将本机文件同步到远程机器的指定目录下并改名(推文件)
[root@hyc-01-01 aaa_dest]# rsync /etc/passwd 192.168.31.128:/tmp/hyc.txt
root@192.168.31.128's password:
不仅本机需要装rsync,远程机器也需要安装
将远程主机的某文件同步到本机指定目录下(拉文件)
[root@hyc-01-01 aaa_dest]# rsync -avP 192.168.31.128:/tmp/hyc.txt /root/123.txt
root@192.168.31.128's password:
receiving incremental file list
hyc.txt
883 100% 862.30kB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 43 bytes received 976 bytes 135.87 bytes/sec
total size is 883 speedup is 0.87
[root@hyc-01-01 aaa_dest]# ls /root/123.txt
/root/123.txt
假如对方机器的ssh端口不是22
[root@hyc-01-01 aaa_dest]# rsync -avP -e "ssh -p 22" 192.168.31.128:/tmp/hyc.txt /root/123.txt
root@192.168.31.128's password:
receiving incremental file list
sent 20 bytes received 46 bytes 10.15 bytes/sec
total size is 883 speedup is 13.38
使用-e后跟“ssh –p 22”指定从哪个端口进行ssh登录
[root@hyc-01-01 aaa_dest]# ssh -p 22 192.168.31.128 使用ssh指定端口登录31.128
root@192.168.31.128's password:
Last login: Fri Jul 20 00:00:51 2018 from 192.168.31.1