设为首页 收藏本站
查看: 240|回复: 0

[经验分享] rsync远程同步 +inotify实时同步

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-26 08:41:50 | 显示全部楼层 |阅读模式
rsync是一个开源的快速备份工具,支持增量备份,保持链接和权限。负责发起rsync同步操作到客户机称为发起端,而负责响应到服务器称为备份源。
rsync往往默认安装了

1
2
[iyunv@localhost ~]# rpm -qa rsync
rsync-3.0.6-5.el6_0.1.i686



1.SSH备份源,通常需要输入交互验证密码,这一定程度上限制了计划任务的自动执行。所以一般采用SSH备份源到无交互验证,即使用密钥对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@rhel6 ~]# ssh-keygen -t rsa                //创建密钥对
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:
2c:56:1d:bf:af:82:69:4c:19:56:5a:18:7e:af:41:24 root@rhel5
[iyunv@rhel6 ~]# ssh-copy-id root@192.168.130.133    //将公钥发给服务器
root@192.168.130.133's password:            //输入密码
Now try logging into the machine, with "ssh 'root@192.168.130.133'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[iyunv@localhost 桌面]# ssh root@192.168.130.123          //不用密码登录
Last login: Sun Jan 25 10:57:48 2015 from 192.168.130.133
[iyunv@rhel6 ~]#



测试远程备份。

1
2
[iyunv@rhel6 ~]# ll /wwwroot/
total 0



在服务器上创建/var/www/html/test文件。

1
[iyunv@localhost 桌面]# touch /var/www/html/test



回到客户机上同步服务器

1
2
3
4
5
6
7
8
9
[iyunv@rhel6 ~]#rsync -avz root@192.168.130.133:/var/www/html /wwwroot/
receiving incremental file list
./
test
sent 33 bytes received 83 bytes 232.00 bytes/sec
total size is 0  speedup is 0.00
[iyunv@rhel6 ~]#ll /wwwroot/
total 0
-rw-r--r-- 1 root root 0 Jan 25 11:16 test



验证成功,不用输入密码。可写成脚本加入任务计划。

2.rsync备份源的无交互验证:
在服务器上创建test2文件。

1
touch /var/www/html/test2



回到客户端
可以使用环境变量RSYNC_PASSWORD来保存密码。

1
2
3
4
[iyunv@rhel6 ~]#export RSYNC_PASSWORD=pwd123
[iyunv@rhel6 ~]#rsync -avz root@192.168.130.133:/var/www/html/ /wwwroot/
[iyunv@rhel6 ~]#ls /wwwroot/
test test2



验证成功!!
#######rsync 命令选项#######
rsync [选项] 原始位置 目标位置
-r:递归,包含目录及子目录
-l:复制符号链件文件为符号链件文件
-v:显示同步过程详细
-a:归档,保留文件权限,属性
-z:传输文件时压缩
-p:保留文件权限标记
-t:保留文件时间标记
-g:保留文件属组标记
-o:保留文件属主标记
-H:保留硬链文件
-A:保留ACL属性
-D:保留设备文件其他特殊文件
--delete:删除目标位置有而原始位置没有的文件
--checksum:校对决定是否跳过文件


******rsync+inotify实时同步*******

1.调整inotify内核参数

1
2
3
4
[iyunv@localhost ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576



//根据实际调整,监控数大于监控目标总文件数。

2.安装inotify-tools工具

1
2
3
[iyunv@localhost ~]# tar zxf inotify-tools-3.14.tar.gz
[iyunv@localhost ~]# cd inotify-tools-3.14
[iyunv@localhost inotify-tools-3.14]# ./configure && make && make install



以监控/var/www/html为例,先执行inotifywait命令,然后在另一终端在该目录下修改、创建、移动、删除等,查看屏幕输出结果。inotifywait可以监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等。
终端一:

1
2
3
4
[iyunv@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE test3
/var/www/html/ MODIFY test2
/var/www/html/ DELETE test



终端二:

1
2
3
4
5
[iyunv@localhost html]# touch test3
[iyunv@localhost html]# cat >> /var/www/html/test2 << end
> 123
> end
[iyunv@localhost html]# rm -rf test



其中终端一选项-mrq -e为:-e指定监控到事件,-m持续监控,-r递归整个目录,-q简化输出信息。

3.编写触发同步脚本。
使用inotifywait输出到监控结果中,每行记录依次为目录、事件、文件。因此可以识别变动情况。

1
vim /opt/inotify_rsync.sh




1
2
3
4
5
6
7
8
#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,move,delete,attrib /var/www/html/"
rsync_cmd="rsync -avz --delete /var/www/html/  root@192.168.130.132:/var/www/html/"
  
$inotify_cmd | while read DIRECTORY EVENT FILE
do
        $rsync_cmd
done



保存退出
可以编写多个rsync_cmd1、2、3、.....归属不同客户机,在循环do里增加相应变量,就可以在服务器发生变化,多台客户机自动增删改。

1
chmod a+x /opt/inotify_rsync.sh



开机实现监控,并触发同步

1
echo "/opt/inotify_rsync.sh" >>/etc/rc.local



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-41401-1-1.html 上篇帖子: 禁用ubuntu的ipv6 下篇帖子: 大规模部署无人职守Centos 7
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表