rsync命令与scp命令,实时文件同步。

rsync命令和scp命令都是用来在两台主机之间传递文件的。但是二者又有不同:
不管是rsync还是scp命令都要做互信(双方都可以免密登录),ssh-Keygen就是其中一种方法:
两台主机:
192.168.1.88
192.168.1.18
首先在192.168.1.88生成秘钥对,并将私钥发送给192.168.1.18

[root@localhost mnt]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
cc:48:f9:fc:9c:11:80:28:8b:ce:79:2e:ac:da:6f:2e root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|     . ..        |
|  . . .. .       |
| . o  o   .      |
|. .  . *   .     |
|o .   . S .      |
| + .     o o     |
|. o       +      |
| +E..            |
|= o=o            |
+-----------------+
[root@localhost mnt]# ssh-copy-id 192.168.1.18

192.168.1.18上做相同的操作,并将私钥发给192.168.1.88
然后远程登录实验互信是否成功:

[root@localhost mnt]# ssh 192.168.1.18
Last login: Sun Nov 26 10:03:18 2017 from 192.168.1.88

不需要密码可以互相远程登录说明简历互信成功。
准备工作做完。

先看scp命令:

[root@localhost mnt]# scp anaconda-ks.cfg  install.log  install.log.syslog 192.168.1.18:/mnt/
anaconda-ks.cfg                                                                                                            100% 3361     3.3KB/s   00:00
install.log                                                                                                                100%   41KB  40.9KB/s   00:00
install.log.syslog                                                                                                         100% 9154     8.9KB/s   00:00

格式是:scp + 需要发送的文件或者目录(相对路径或者绝对路径都可以) + 目标主机:需要存放的位置(绝对路径)
“:”冒号是不能省略的。运行成功的话没有报错,到192.168.1.18的/mnt/下可以看到这写文件。

再看rsync命令:
还是上面的位置文件,(需要将刚才远程拷贝到1.18上的文件删除)

[root@localhost mnt]# rsync -vartopg /mnt/ 192.168.1.18:/mnt/
sending incremental file list
./
anaconda-ks.cfg
install.log
install.log.syslog

sent 54656 bytes  received 72 bytes  9950.55 bytes/sec
total size is 54433  speedup is 0.99

两者达到的效果差不对一样。
但是rsync相对要灵活跟多,可以跟跟很多参数比如:

v:增加冗长
a:归档模式
r:递归的目录
t:保存修改时间
o:保存所有人
p:保存权限
g:保护所有组
Z:对备份的文件在传输时进行压缩处理

还可以有过滤,删除目标主机对应目录下面本身存在的文件这些功能:
比如:
–exclude 过滤掉指定的文件或者目录
–delete 删除目主机的目标目录下原本存在的文件

[root@localhost mnt]# rsync -vartopg /mnt/ --exclude "*.cfg" --exclude "install.log"  192.168.1.18:/mnt/

表示 同步1.88/mnt/的除了以”.cfg”结尾的,和名字是”install.log” 的文件到 1.18的/mnt/下 , 1.18:/mnt/ 本来存在的文件,依然存在。

[root@localhost mnt]# rsync -vartopg  --delete /mnt/ --exclude "*.cfg" --exclude "install.log"  192.168.1.18:/mnt/

执行的结果跟上面的相比,目标主机的/mnt/只存在现在同步过来的文件。其他文件被删除掉了。

应用场景:
结合crontab实现两台主机的文件同步:

/1  * * * /usr/bin/rsync  -vzrtopg --delete 192.168.1.18:/etc/nginx/sites-enabled/ /etc/nginx/sites-enabled/

这就实现了文件同步。

另外scp命令每次从远程拷贝的时间都差不多,比较慢。
rsync第一次远程同步的时间相对慢一点,之后的每次同步时间都非常短。

time命令可以计时,例如:

[root@localhost ~]# time scp /mnt/* 192.168.1.18:/mnt
anaconda-ks.cfg                                                                                                            100% 3361     3.3KB/s   00:00
install.log                                                                                                                100%   41KB  40.9KB/s   00:00
install.log.syslog                                                                                                         100% 9154     8.9KB/s   00:00

real    0m0.296s
user    0m0.009s
sys 0m0.011s
[root@localhost ~]# time rsync -vartopg /mnt/  192.168.1.18:/mnt/
sending incremental file list
./
anaconda-ks.cfg
install.log
install.log.syslog

sent 54656 bytes  received 72 bytes  9950.55 bytes/sec
total size is 54433  speedup is 0.99

real    0m5.294s
user    0m0.005s
sys 0m0.013s

实时同步rsync常见一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值