Inotify
强大的,细粒度的,异步的文件系统事件监控机制
Inotify实现的软件
inotify
sersync
lsyncd
安装Inotify
1.验证系统支持inotify
ls /proc/sys/fs/inotify/
存在三个文件:max_queued_events max_user_instances max_user_watches
2.https://github.com/rvoicilas/inotify-tools/tree/master github下载
3.安装
tar zxvf inotify-tools-3.14.tar.gz
./configure --perfix=/usr/local/inotify && make && make install
4.参数
-r 递归
-q 打印监控事件信息
-m 永久保持时间监听状态
-excludei 不区分大小写排除文件或目录,
--timefmt 指定输出格式
-e 指定监控的事件
events
access 文件或目录被获取
modify 文件或目录被修改
attrib 文件或目录属性被改变
close 文件或目录关闭
open 文件或目录打开
move_to 文件或目录移动至另外一个目录
move 文件或目录被移动到另一个目录或从另一个目录移动至当前目录
create 文件或目录被创建在当前目录
delete 文件或目录被删除
unmount 文件系统被卸载
使用
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,modify,open,close /backup
应用思路
inotify会监听文件或目录的内容和属性变化,并会答应出当前变化的文件名,我们可以写shell,当inotify有输出的时候就执行rsync的同步命令
友情提示:
监听打印信息
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,modify,open,close /backup |\
while read line
do
[ ! -e "$line" ] && continue
rsync -avzP --delete --timeout=100 $line rsync_backup@172.16.17.12:/backup/ --password-file=/etc/rysnc.password >/dev/null 2>&1
done
调优
1.调整调用inotify_init时分配给inotify instance中可排队的event的数目的最大值
/proc/sys/fs/inotify/max_queued_events
修改其数值为50000000
2.调整一个real user ID可创建的inotify instatnces的数量上限
/proc/sys/fs/inotify/max_user_instances
修改其数值为50000000
3.调整同一用户同时可以添加的watch数目(watch一般是针对目录,决定了同时同一用户可以监控的目录数量)
/proc/sys/fs/inotify/max_user_watches
修改其数值为50000000