本文参考来自: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目录下面创建文件,看看是否能自动触发同步过去。