本文参考来自:http://chocolee.blog.51cto.com/8158455/1400596

我的需求和他的略有不同,同时做了一下更改,如下:

需求:两台机器相互为主备,搭建相同的两个服务,实现数据同步,并长期运行脚本,不受hup影响

1.安装Rsync

http://www.cnblogs.com/liuquan/p/5413132.html

2.安装 inotify-slave部署

inotify是rsync客户端安装和执行的

企业场景压力测试200-300个同步限制,受网卡,磁盘,带宽等的制约。

1.查看当前系统是否支持inotify
[root@JD-BJ-747 ~]#ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_queued_events
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_user_instances
-rw-r--r-- 1 root root 0 Apr 26 14:20 max_user_watches

  

2.下载inotify源码包并编译安装
http://pan.baidu.com/s/1jIRLiei
tar -zxvf inotify-tools-3.14.tar.gz
./configure --prefix=/usr/local/inotify-3.14 make && make install

  

3.常用参数详解
[root@inotify-master inotify-tools-3.14]# cd /usr/local/inotify-3.14/
[root@inotify-master inotify-3.14]# ./bin/inotifywait --help
-r|--recursive Watch directories recursively. #递归查询目录
-q|--quiet Print less (only print events). #打印监控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始终保持事件监听状态
--excludei <pattern> Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。
--timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定时间输出的格式
--format <fmt> Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. #通过此参数可以指定需要监控的事件,如下所示:
Events:
access file or directory contents were read #文件或目录被读取。
modify file or directory contents were written #文件或目录内容被修改。
attrib file or directory attributes changed #文件或目录属性被改变。
close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式。
open file or directory opened #文件或目录被打开。
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。
move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。
create file or directory created within watched directory #文件或目录被创建在当前目录
delete file or directory deleted within watched directory #文件或目录被删除
unmount file system containing file or directory unmounted #文件系统被卸载

  

4.编写监控脚本并加载到后台执行
#/bin/bash
host01=$IP //写其他机器IP
src=/letv/fe
t/rsync
dst=/test/rsync
user=root
inotify_home=/usr/local/inotify
if [ ! -e "$src" ] \
||[ ! -e "${inotify_home}/bin/inotifywait" ] \
||[ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H/%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read files;
do
#rsync -aczP --delete --timeout=100 $src $user@$host01:$dst >/dev/null 2>&1
cd $src && /usr/local/bin/rsync -vrtapogL -R --delete ./ --timeout=100 $user@$host01:$dst
done
exit 0

  

5. 脚本后台执行,并开机自启,并且不能受到HUP影响而中断服务。

[root@cdn ~]# nohup sh inotify.sh &
[1] 4907
[root@cdn ~]# nohup: ignoring input and appending output to `nohup.out' [root@cdn ~]# echo "nohup sh inotify.sh &" >>/etc/rc.local
[root@cdn ~]#

  

我们知道,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程。因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为不属于此终端的子进程。

nohup 示例
[root@pvcent107 ~]# nohup ping www.ibm.com &
[1] 3059
nohup: appending output to `nohup.out'
[root@pvcent107 ~]# ps -ef |grep 3059
root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.com
root 3067 984 0 21:06 pts/3 00:00:00 grep 3059
[root@pvcent107 ~]#

6.测试

去/TEST/RSYNC目录下面创建文件,看看是否能自动触发同步过去。

另外一台机器操作与上方一致既可以实现互为主备,哪台机器备份目录优先有改动哪台即为主,就会执行同步脚本。

rsync+inotify实现实时同步,自动触发同步文件的更多相关文章

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

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

  2. rsync+inotify 实现实时同步

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

  3. rsync + inotify 数据实时同步

    一.rsync介绍 rsync英文全称为Remote synchronization,从软件的名称就可以看出来,Rsync具有可是本地和远程两台主机之间的数据快速复制同步镜像.远程备份的功能,这个功能 ...

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

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

  5. centos 配置rsync+inotify数据实时同步

    何为rsync? 定义: rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,非常适用于异地备份 何为源端和发起端? 在远程同步过程中,负责发起rs ...

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

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

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

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

  8. centos 配置rsync+inotify数据实时同步2

    一.Rsync服务简介 1. 什么是Rsync 它是一个远程数据同步工具,它在同步文件的同时,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“rsync算法”来使本地和远程两个主机 ...

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

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

随机推荐

  1. 竞赛题解 - Broken Tree(CF-758E)

    Broken Tree(CF-758E) - 竞赛题解 贪心复习~(好像暴露了什么算法--) 标签:贪心 / DFS / Codeforces 『题意』 给出一棵以1为根的树,每条边有两个值:p-强度 ...

  2. sourcetree .git 强制忽略指定文件不提交

    在公司写项目,大部分都会用到 svn 或 git 提交代码到服务器.我们公司用的GIT,每个程序员有自己的独立分支,各写各的代码互不冲突,最终合并到主分支再解决相同代码冲突问题.这时候会遇到一些配置文 ...

  3. Manjaro Linux KDE个人的一些安装配置

    安装manjaro kde linux的个人步骤 1 换源 1.1 自动寻找最快的源 sudo pacman-mirrors -i -c China -m rank 1.2 修改源文件 sudo ge ...

  4. Asp.net core静态文件目录访问

    Asp.net core静态文件目录访问 如果使用Asp.net core来实现一个能够访问其它电脑上的资源 新建工程 选择项目框架 如何将静态文件注入到项目中 在startup.cs文件的Confi ...

  5. Zookeeper原理和实战开发经典视频教程 百度云网盘下载

    Zookeeper原理和实战开发 经典视频教程 百度云网盘下载 资源下载地址:http://pan.baidu.com/s/1o7ZjPeM   密码:r5yf   

  6. IDEA阿里Java规范插件的安装

    本文参考自阿飞博客:http://www.cnblogs.com/aflyun/p/7668306.html 官方使用教程:https://zhuanlan.zhihu.com/p/30191998? ...

  7. PHP实现识别带emoji表情的字符串

    function have_special_char($str) { $length = mb_strlen($str); $array = []; for ($i=0; $i<$length; ...

  8. 利尔达NB-IOT模块烧写固件的步骤

    1. NB-IOT是3个内核,用户开发的是A核,就是应用核,用Eclipse软件打开软件SDK之后,会生成一个bin文件(应用核的bin),现在就是要把bin文件合并到包里面(3个内核的bin),一起 ...

  9. 绝地求生大逃杀BE启动失败,应用程序无法正常启动

    今日更新绝地求生大逃杀后部分客户反馈绝地求生点击启动提示BE安装,应用程序无法启动 问题原因:经过排查发现,客户开启过超级工作站运行过游戏,在系统镜像包中保留了旧版的BE服务,致使新版BE无法安装,冲 ...

  10. 前端图片转base64,转格式,转blob,上传的总结

    1. 图片文件转base64 <input accept="image/gif,image/jpeg,image/jpg,image/png" type="file ...