多语言网站制作,工商企业网,做好网站,中国互联网信息中心官网引言 使用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!