Inotify监测部署

1.确保rsync服务端配置完成,客户端nfs可以推送
rsync -avz /data/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
2.检查系统是否支持inotify
uname -r
3.是否存在下面三个文件
[root@nfs01 ~]$ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_queued_events
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_user_instances
-rw-r--r-- 1 root root 0 Nov 14 21:28 max_user_watches
4.inotify-tools工具安装过程
yum install epel-release -y
yum install inotify-tools -y
[root@nfs01 ~]$rpm -ql inotify-tools|head -2
/usr/bin/inotifywait    #监控软件命令
/usr/bin/inotifywatch

inotifywait命令参数
-m 始终保持事件监听状态*****
-d 类似于-m *****
-r 递归监控根目录数据信息变化*****
-e 监听的事件
5.事件状态
6.实践
#测试create事件
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
#再开一个窗口测试touch {1..5}.txt#

测试delete
inotifywait -mrq -e delete /data

#多个参数监控
inotifywait -mrq -e delete,create,close_write /data
inotifywait -mrq -e delete,close,move /data
7.inotify-tools软件部署项目实战
inotifywait -mrq --format '%w%f' -e close_write,delete /data
inotifywait -mrq -e close_write,delete /data
8.生产脚本nfs01创建执行执行
[root@nfs01 scripts]$cat monitor.sh 
#!/bin/bash
cmd="/usr/bin/inotifywait"
$cmd -mrq -e --format '%w%f' close_write,delete /data | \
while read line
do
 cd /data &&\
 rsync -az --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
done
#测试
sh -x monitor.sh 

#bacup服务器检查
ls /backup
#放到/etc/rc.local
/bin/sh /server/scripts/monitor.sh &>/dev/null &
9.inotify-tools优化
#放到/etc/rc.local下
echo "50000000" > /proc/sys/fs/inotify/max_user_watches #单进程可以监控的文件数量
echo "50000000" > /proc/sys/fs/inotify/max_queued_events #队列容纳的事件数量


分享到:
关键词:Linux运维

网友留言(0 条)

发表评论