仙居网站制作,多媒体网页设计,银行系统网站模板,申请免费网站建设引言 使用inotify通知接口#xff0c;可以用来监控文件系统的各种变化情况#xff0c;如文件存取、删除、移动、修改等。利用这一机制#xff0c;可以非常方便地实现文件异动告警、增量备份#xff0c;并针对目录或文件的变化及时作出响应。 将inotify机制与rsync工具相结合…引言 使用inotify通知接口可以用来监控文件系统的各种变化情况如文件存取、删除、移动、修改等。利用这一机制可以非常方便地实现文件异动告警、增量备份并针对目录或文件的变化及时作出响应。 将inotify机制与rsync工具相结合可以实现触发式备份实时同步即只要原始位置的文档发生变化则立即启动增量备份操作否则处于静默等待状态。这样就避免了按固定周期备份时存在的延迟性、周期过密等问题。因为 inotify 通知机制由 Linux 内核提供因此主要做本机监控在触发式备份中应用时更适合上行同步。
1、修改 rsync 源服务器配置文件rsync
vim /etc/rsyncd.conf #修改 read only 为 noread only nokill $(cat /var/run/rsyncd.pid)rm -rf /var/run/rsyncd.pidrsync --daemonnetstat -natp | grep rsyncchmod 777 /var/www/html/txy 2、调整 inotify 内核参数backuper
在 Linux 内核中默认的 inotify 机制提供了三个调控参数
max_queued_events 监控事件队列默认值为 16384
max_user_instances 最多监控实例数默认值为 128
max_user_watches 每个实例最多监控文件数默认值为 8192
当要监控的目录、文件数较多或者变化较频繁时建议加大这三个参数的值 cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
可通过 /etc/sysctl.conf 文件进行配置设定参数应符合服务器性能以及服务需要
vim /etc/sysctl.conf
#添加如下配置
fs.inotify.max_queued_events 16384
fs.inotify.max_user_instances 1024
fs.inotify.max_user_watches 1048576 sysctl -p 3、安装 inotify-toolsbackuper
用 inotify 机制需要安装 inotify-tools以便提供 inotifywait、inotifywatch 辅助工具程序用来监控、汇总改动情况。
inotifywait可监控modify修改、create创建、move移动、delete删除、attrib属性更改等各种事件一旦变动立即输出结果。
inotifywatch可用来收集文件系统变动情况并在运行结果后输出汇总的变化情况。
[rootlocalhost ~]# cd /opt[rootlocalhost opt]# rz -E#传入 inotify-tools 安装包rz waiting to receive.tar zxvf inotify-tools-3.14.tar.gz -C /optcd inotify-tools-3.14/./configuremake -j 2 make install
可以先执行 inotifywait 命令然后另外再开启一个新终端向 /var/www/html 目录下添加文件、移动文件在原来的终端上跟踪屏幕输出结果。
新终端操作
[rootlocalhost ~]# cd /var/www/html/txy[rootlocalhost txy]# touch test.php[rootlocalhost txy]# mv test.php test.txt[rootlocalhost txy]# echo test test.txt[rootlocalhost txy]# rm -rf test.txt
原终端操作
[rootlocalhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/txy 4、实验验证
在新终端的/var/www/html/txy的目录下新建文件夹 在原终端上显示该内容 inotifywait 选项说明如下
常用选项 说明
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息
5、另一个终端的触发式同步脚本
[rootlocalhost txy]# vim /opt/inotify.sh#!/bin/bashINOTIFY_CMDinotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/txyRSYNC_CMDrsync -azH --delete --password-file/etc/server.pass /var/www/html/txy backuper192.168.10.23::rsync/$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录doif [ $(pgrep rsync | wc -l) -le 0 ] ; then$RSYNC_CMDfidone
[rootlocalhost opt]# chmod x inotify.sh[rootlocalhost opt]# chmod 777 /var/www/html/[rootlocalhost opt]# chmod x /etc/rc.d/rc.local[rootlocalhost opt]# echo /opt/inotify.sh /etc/rc.d/rc.local
##加入开机自动执行
上述脚本用来检测本机 /var/www/html/txy 目录的变动情况一旦有更新触发 rsync 同步操作上传备份至服务器 192.168.10.23 的 rsync 共享目录下。
6、验证
6.1 在本机运行 /opt/inotify_rsync.sh 脚本程序
[rootbackuper opt]# ./inotify.sh //后台运行该脚本
6.2 切换到本机的 /var/www/html 目录执行增加、删除、修改文件等操作
[rootlocalhost opt]# cd /var/www/html/txy[rootlocalhost txy]# lstest.html test.txt[rootlocalhost txy]# rm -rf *[rootlocalhost txy]# touch test.html[rootlocalhost txy]# echo this is inotify_rsync test! test.html 6.3 查看远端服务器中的 rsync 目录下的变化情况
[rootrsync ~]# cd /data[rootrsync data]# lstest.html[rootrsync data]# cat test.htmlthis is inotify_rsync test!
文章转载自: http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.zkbxx.cn.gov.cn.zkbxx.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.zlgth.cn.gov.cn.zlgth.cn