本文参考来自: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. Python-调用系统指令小记

    import subprocess def exec_command(cmd, log_path, **kwargs): with open(log_path, 'w') as f: p = subp ...

  2. yii 后台配置独立子域名方法

    我这里安装的是宝塔面板集成的环境WNMP,官网上虽然也有,但是写的并不明确,对我这种用YII的新手来说也很头疼,折腾了半天终于弄好,记录一下. 首先解析一个子域名:back.domain.com: 用 ...

  3. APSC4xSeries_Ver32.exe在win764位提示缺少DLL错误解决办法

    APSC4xSeries_Ver32.exe在win764位提示缺少DLL错误解决办法 从网上下载oatime_epson-me1清零软件,Stylus4xProgram_Ver32的 解决办法:还是 ...

  4. opencv3 学习四 - 图像减色

    程序如下 #include "opencv2/opencv.hpp" using namespace cv; int main() { // 灰度图 Mat original = ...

  5. 20190118-利用Python实现Pig Latin游戏

    1.利用Python实现Pig Latin字母游戏 “Pig Latin”是一个英语儿童文字改写游戏,整个游戏遵从下述规则:a. 元音字母是‘a’.‘e’.‘i’.‘o’.‘u’.字母‘y’在不是第一 ...

  6. FPGA学习之路——PLL的使用

    锁相环(PLL)主要用于频率综合,使用一个 PLL 可以从一个输入时钟信号生成多个时钟信号. PLL 内部的功能框图如下图所示: 在ISE中新建一个PLL的IP核,设置四个输出时钟,分别为25MHz. ...

  7. 北京Uber优步司机奖励政策(3月22日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 北京Uber优步司机奖励政策(1月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 宁波Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 2038: [2009国家集训队]小Z的袜子(hose)

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 9472  Solved: 4344 Desc ...