rsync命令与scp命令,实时文件同步。
来源:互联网 发布:淘宝达人怎么介绍自己 编辑:程序博客网 时间:2024/06/06 02:25
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-keygenGenerating 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.localdomainThe 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.18Last 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:00install.log 100% 41KB 40.9KB/s 00:00install.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.cfginstall.loginstall.log.syslogsent 54656 bytes received 72 bytes 9950.55 bytes/sectotal 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:/mntanaconda-ks.cfg 100% 3361 3.3KB/s 00:00install.log 100% 41KB 40.9KB/s 00:00install.log.syslog 100% 9154 8.9KB/s 00:00real 0m0.296suser 0m0.009ssys 0m0.011s[root@localhost ~]# time rsync -vartopg /mnt/ 192.168.1.18:/mnt/sending incremental file list./anaconda-ks.cfginstall.loginstall.log.syslogsent 54656 bytes received 72 bytes 9950.55 bytes/sectotal size is 54433 speedup is 0.99real 0m5.294suser 0m0.005ssys 0m0.013s
实时同步rsync常见一些。
- rsync命令与scp命令,实时文件同步。
- rsync 文件同步命令 SCP命令
- rsync与scp命令讲解
- linux下ssh远程登录/scp远程复制文件/rsync远程同步命令的自动登录
- linux下ssh远程登录/scp远程复制文件/rsync远程同步命令的自动登录
- linux下ssh远程登录/scp远程复制文件/rsync远程同步命令的自动登录
- rsync同步命令使用
- rsync 同步 命令详解
- rsync同步命令
- rsync+inotify文件实时同步
- Rsync+inotify文件实时同步
- rsync+inotify文件实时同步
- Linux文件同步方法(rsync及scp)
- 常用命令一:文件同步【scp,rsync】
- 双向实时远程同步文件(inotify+rsync与nfs)
- 【实时文件同步】rsync+inotify-tools的安装与配置
- Linux命令之文件实时备份 -incron,rsync,inotify
- linux 拷贝命令 cp ,scp ,rsync
- SQL语句实现多张表的连接查询
- windows下使用hexo和github配置个人博客
- eslint 的一些规则
- html超链接的4种状态
- react+webapck多页面(multipage)程序
- rsync命令与scp命令,实时文件同步。
- Leetcode 547. Friend Circles [Medium]
- oracle12c下简单使用expdp&impdp
- FPGA中LUT设计
- 怎么安装和配置jdk
- 查看MySQL库、表所占磁盘空间大小
- Android studio的build.gradle的dependency的版本号后面的‘+’加号
- 【Freemark】-基本数据类型
- 数据结构学习-线性表-链表的代码实现