1.安装软件包
# yum install inotify-tools
# yum -y install rsync

2.同步机器相互添加信任

[root@host-10-0-100-106 ~]# ssh-keygen  #一路回车
[root@host-10-0-100-106 ~]# ssh-copy-id -i /root/.ssh/
[root@host-10-0-100-106 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.100.139

3.两台机器上分别放置同步脚本

vim /shell/inotify.sh
#!/bin/bash
watch_dir=/data/wwwroot/www.ikmak.com/data/attachment
push_to=10.0.100.104 #另一台机器放置修改ip地址
inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" |\
while read line;do
# logging some files which has been deleted and moved out
if echo $line | grep -i -E "delete|moved_from";then
echo "$line" >> /etc/inotify_away.log
fi
# from here, start rsync's function
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/data/wwwroot/www.ikmak.com/data/
if [ $? -eq 0 ];then
echo "sent $watch_dir success"
else
echo "sent $watch_dir failed"
fi
done

优化脚本

[root@xuexi tmp]# cat ~/inotify.sh
#!/bin/bash
watch_dir=/www
push_to=172.16.10.5 # First to do is initial sync
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" >>/etc/inotifywait.log & while true;do
if [ -s "/etc/inotifywait.log" ];then
grep -i -E "delete|moved_from" /etc/inotifywait.log >> /etc/inotify_away.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp
if [ $? -ne 0 ];then
echo "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |\
mail -s "inotify+Rsync error has occurred" root@localhost
fi
cat /dev/null > /etc/inotifywait.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/tmp
else
sleep 1
fi
done

4.配置后台执行脚本

# nohup /shell/inotify.sh &
# crontab -e
* * * * * nohup /shell/inotify.sh > /dev/null 2>&1 &

  

  

 

 

Rsync+Inotify 搭建实时同步数据的更多相关文章

  1. rsync简介与rsync+inotify配置实时同步数据

    rsync简介 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. rsync特性 rsync ...

  2. rsync+inotify实现实时同步案例--转

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  3. linux rsync +inotify 实现 实时同步

    前言:     rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rs ...

  4. rsync+inotify实现实时同步案例【转】

    1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...

  5. rsync+inotify实现实时同步案例

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  6. Rsync+inotify实现实时同步

    1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...

  7. CentOS 7 rsync+inotify实现实时同步

    测试环境如下: inotify-slave IP : 172.16.0.222 inotify-master IP : 172.16.0.233 对两台机的要求: 安装依赖包gcc: yum inst ...

  8. rsync+inotify实现实时同步,自动触发同步文件

    本文参考来自:http://chocolee.blog.51cto.com/8158455/1400596 我的需求和他的略有不同,同时做了一下更改,如下: 需求:两台机器相互为主备,搭建相同的两个服 ...

  9. rsync+inotify 实现实时同步

    inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件 inotify的特性需要linux内核2.6.13以上的支持 [root@test1 inotify-tools-3.13]# u ...

随机推荐

  1. NYOJ 745 dp

    蚂蚁的难题(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 下雨了,下雨了,蚂蚁搬家了. 已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种 ...

  2. JN5169 --------- zigbee代码

    队列: 需要创建3个标准队列(只创建不使用): ------Queue with handle zps_msgMlmeDcfmInd to receive IEEE 802.15.4 MACcomma ...

  3. Nginx与Apache工作方式

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://going.blog.51cto.com/7876557/1304204 Ngin ...

  4. 痛苦之旅——安装Eric4

    因为想做桌面程序,所以在学PyQt4, 顺便装了下Eric4,这Eric4装起来可不简单,活活花了一个星期..... 网上有很多装Eric4的教程,详细我就不说了,大概步骤是: 1.安装SIP (需要 ...

  5. 动态规划:LIS

    题目中的严格二字,表示的意思是不允许≥或者是≤的情况出现,只允许>的情况以及<的情况 经典问题是NOIP合唱队形,在这个题目中,既求了最长上升子序列,也求了最长下降子序列 其最终的结果由两 ...

  6. GitLab 迁移与升级

    参考: [ 博客园 BigBao ] 环境说明: OS: CentOS 7.x gitlab-ce 初始版本: 8.8.5 gitlab-ce 升级到版本: 11.2.3 升级方式: rpm 安装升级 ...

  7. UIToolBar的半透明属性设置

      UIToolBar的半透明属性设置style:Translucent(Ps:长得很像翻译translation)   https://www.evernote.com/shard/s227/sh/ ...

  8. c语言目录操作总结

    =================================================== char *getcwd( char *buffer, int maxlen ); (获取当前目 ...

  9. vue双向绑定原理源码解析

    当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理. 简易vue源码地址:https://github.com/maxlove123 ...

  10. python基础===tkinter学习链接

    http://effbot.org/tkinterbook/tkinter-classes.htm