10.28 rsync工具介绍(同步工具)
进行远程同步时必须远程机和本地机都有rsync工具才可以
rsync -av /etc/passwd /tmp/1.txt
rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt
rsync格式
rsync [OPTION](选项) … SRC(源目录) DEST(目标目录) (本机)
rsync [OPTION] … SRC [user@]IP:DEST 指定目标登陆用户
rsync [OPTION] … SRC IP:DEST 当前登陆用户远程登录目标服务器
rsync [OPTION] … [user@]host:SRC DEST (反向拷贝至本地)
rsync [OPTION] … SRC [user@]host::DEST (目标)
rsync [OPTION] … [user@]host::SRC DEST (源)
本地拷贝至另一台服务器,也可以本机至本机拷贝,增量备份,cp工具只能覆盖拷贝浪费资源
特点:
1.本机目录拷贝目录,并且 /root/1目录数据一直在更新每一小时更新 拷贝至/tmp/2一次(增量拷贝)
2.远程同步
[root@test ~]# rsync -av -e "ssh -p 1110" /etc/passwd root@192.168.1.1:/tmp/1.txt
The authenticity of host '[192.168.1.1]:9910 ([192.168.1.1]:1110)' can't be established.
ECDSA key fingerprint is SHA256:oSh36fWH7QrQ/c+TPVF+pH+BqKcm246gqlg3H3tMPEw.
ECDSA key fingerprint is MD5:c8:11:df:ec:be:ad:92:20:b8:00:a9:fc:08:be:12:2f.
Are you sure you want to continue connecting (yes/no)?
yes
Warning: Permanently added '[192.168.1.1]:1110' (ECDSA) to the list of known hosts.
root@192.168.1.1's password:
sending incremental file list
passwd
sent 2,181 bytes received 35 bytes 80.58 bytes/sec
total size is 2,089 speedup is 0.94
3.拷贝文件
[root@test ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 2,181 bytes received 35 bytes 4,432.00 bytes/sec
total size is 2,089 speedup is 0.94
10.29/10.30 rsync常用选项
假设机器A为192.168.131,B机器为192.168.133.130;A机器为本机。
A机器/tmp/1目录下有1.txt 2.txt 3.txt 4.txt 四个文件;B机器/tmp/1目录下有1.txt 2.txt 3.txt 三个文件
如下操作,会把本机(机器A)/tmp/1目录下的4文件进行删除
rsync -av
--delete
root@192.168.133.130:/tmp/1/ /tmp/1/
是可以实现以远程机器做模板来比对本机目录下文件
rsync常用选项
-a 包含-rtplgoD
-r 同步目录时要加上,类似cp时的-r选项
-v 同步时显示一些信息,让我们知道同步的过程
-l 保留软连接
-L 加上该选项后,
同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息(
dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用吧。)
-t 保持文件的时间属性
--delete 删除DEST目标中SRC源没有的文件
--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步 (
新和旧怎么判断 根据mtime来定)
-z 传输时压缩
1.目录与目录同步并且更改目标目录名字
[root@test ~]# rsync -av /root/1/ /tmp/1-1/
sending incremental file list
created directory /tmp/1-1
./
1.txt
2.txt
2.txt,3.txt,4.txt
[a-c].txt
a.txt
b.txt
passwd
adbsmdbmasbdmasb/
dhaskdhakshdkash/
sent 1,820 bytes received 195 bytes 4,030.00 bytes/sec
total size is 1,245 speedup is 0.62
2.同步软硬件文件 -L将软链接的源文件拷贝
[root@test ~]# ls -l
lrwxrwxrwx 1 root root 11 10月 31 12:40 12312 -> /etc/passwd
[root@test ~]# rsync -avL /root/12312 /tmp/
sending incremental file list
12312
sent 2,180 bytes received 35 bytes 4,430.00 bytes/sec
total size is 2,089 speedup is 0.94
[root@test ~]# ls -l /tmp/
总用量 36
drwxr-xr-x 4 root root 162 10月 11 12:52 1-1
-rw-r--r-- 1 root root 2089 10月 24 11:44 12312
3.--delete 删除DEST目标中SRC源没有的文件
[root@test ~]# ls /tmp/1-1/
1.txt 2.txt 2.txt,3.txt,4.txt [a-c].txt adbsmdbmasbdmasb a.txt b.txt dhaskdhakshdkash new.txr new.txt passwd
[root@test ~]# ls /root/1/
1.txt 2.txt 2.txt,3.txt,4.txt [a-c].txt adbsmdbmasbdmasb a.txt b.txt dhaskdhakshdkash passwd
[root@test ~]# rsync -avL --delete /root/1/ /tmp/1-1/
sending incremental file list
deleting new.txt
deleting new.txr
./
sent 268 bytes received 39 bytes 614.00 bytes/sec
total size is 1,245 speedup is 4.06
4.--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
[root@test ~]# ls /root/1/
1.log 1.txt 2.log 2.txt 2.txt,3.txt,4.txt [a-c].txt adbsmdbmasbdmasb a.txt b.txt dhaskdhakshdkash passwd
[root@test ~]# rsync -avL --exclude "*.txt" --exclude "*.log" /root/1/ /tmp/1-1/
sending incremental file list
./
passwd
adbsmdbmasbdmasb/
dhaskdhakshdkash/
sent 1,239 bytes received 50 bytes 2,578.00 bytes/sec
total size is 1,048 speedup is 0.81
5.-P 显示同步过程,比如速率,比-v更加详细
[root@test ~]# rsync -avP /root/1/ /tmp/1-1/
sending incremental file list
./
1.log
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=10/12)
1.txt
44 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=9/12)
2.log
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=8/12)
2.txt
88 100% 85.94kB/s 0:00:00 (xfr#4, to-chk=7/12)
2.txt,3.txt,4.txt
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=6/12)
[a-c].txt
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=5/12)
a.txt
12 100% 11.72kB/s 0:00:00 (xfr#7, to-chk=4/12)
b.txt
53 100% 51.76kB/s 0:00:00 (xfr#8, to-chk=3/12)
passwd
1,048 100% 1023.44kB/s 0:00:00 (xfr#9, to-chk=2/12)
adbsmdbmasbdmasb/
dhaskdhakshdkash/
sent 1,938 bytes received 202 bytes 4,280.00 bytes/sec
total size is 1,245 speedup is 0.58
6.-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
[root@test ~]# rsync -avPu /root/1/ /tmp/1-1/
sending incremental file list
sent 305 bytes received 14 bytes 638.00 bytes/sec
total size is 1,245 speedup is 3.90
[root@test ~]# cat /root/1/1.log
[root@test ~]# cat /tmp/1-1/1.log
daskdhkashda
daskhdkash
dashkhdk
7.-z 传输时压缩 传输时节约带宽
[root@test ~]# rsync -avPz /root/1/ /tmp/1-1/
sending incremental file list
1.log
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=10/12)
sent 345 bytes received 37 bytes 764.00 bytes/sec
total size is 1,245 speedup is 3.26
10.31 rsync通过ssh同步
rsync通过ssh方式同步
rsync -av test1/ 192.168.133.132:/tmp/test2/
rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
rsync通过服务的方式同步
要编辑配置文件/etc/rsyncd.conf
启动服务rsync --daemon
格式:rsync -av test1/ 192.168.133.130::module/dir/
安装rsync命令(本地与远程服务器都要安装)
[root@test ~]# yum install -y rsync
远程同步
本地同步远端 推文件
[root@test ~]# rsync -av -e "ssh -p 1110" /etc/passwd root@192.168.1.1:/tmp/1.txt
The authenticity of host '[192.168.1.1]:9910 ([192.168.1.1]:1110)' can't be established.
ECDSA key fingerprint is SHA256:oSh36fWH7QrQ/c+TPVF+pH+BqKcm246gqlg3H3tMPEw.
ECDSA key fingerprint is MD5:c8:11:df:ec:be:ad:92:20:b8:00:a9:fc:08:be:12:2f.
Are you sure you want to continue connecting (yes/no)?
yes
Warning: Permanently added '[192.168.1.1]:1110' (ECDSA) to the list of known hosts.
root@192.168.1.1's password:
sending incremental file list
passwd
sent 2,181 bytes received 35 bytes 80.58 bytes/sec
total size is 2,089 speedup is 0.94
远端同步至本地 拉文件
[root@test ~]# rsync -avP -e "ssh -p 1110" root@1192.168.1.1:/etc/passwd /root/1/345.txt
root@192.168.1.1's password:
receiving incremental file list
passwd
1,048 100% 1023.44kB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 43 bytes received 1,139 bytes 214.91 bytes/sec
total size is 1,048 speedup is 0.89