引言
使用inotify通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。
1、修改 rsync 源服务器配置文件(rsync)
vim /etc/rsyncd.conf
#修改 read only 为 no
read only = no
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
rsync --daemon
netstat -natp | grep rsync
chmod 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-tools(backuper)
用 inotify 机制需要安装 inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。
inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一旦变动立即输出结果。
inotifywatch:可用来收集文件系统变动情况,并在运行结果后输出汇总的变化情况。
[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
#传入 inotify-tools 安装包
rz waiting to receive.
tar zxvf inotify-tools-3.14.tar.gz -C /opt
cd inotify-tools-3.14/
./configure
make -j 2 && make install
可以先执行 inotifywait 命令,然后另外再开启一个新终端向 /var/www/html 目录下添加文件、移动文件,在原来的终端上跟踪屏幕输出结果。
新终端操作
[root@localhost ~]# cd /var/www/html/txy
[root@localhost txy]# touch test.php
[root@localhost txy]# mv test.php test.txt
[root@localhost txy]# echo 'test' > test.txt
[root@localhost txy]# rm -rf test.txt
原终端操作
[root@localhost 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、另一个终端的触发式同步脚本
[root@localhost txy]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/txy"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/txy backuper@192.168.10.23::rsync/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@localhost opt]# chmod +x inotify.sh
[root@localhost opt]# chmod 777 /var/www/html/
[root@localhost opt]# chmod +x /etc/rc.d/rc.local
[root@localhost 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 脚本程序
[root@backuper opt]# ./inotify.sh & //后台运行该脚本
6.2 切换到本机的 /var/www/html 目录,执行增加、删除、修改文件等操作
[root@localhost opt]# cd /var/www/html/txy
[root@localhost txy]# ls
test.html test.txt
[root@localhost txy]# rm -rf *
[root@localhost txy]# touch test.html
[root@localhost txy]# echo 'this is inotify_rsync test!' > test.html
6.3 查看远端服务器中的 rsync 目录下的变化情况
[root@rsync ~]# cd /data
[root@rsync data]# ls
test.html
[root@rsync data]# cat test.html
this is inotify_rsync test!