inotify实践

2019-04-19  本文已影响0人  马帅琦

作者:马帅琦
归档:day36
时间:2019/4/19

前提:backup rsync服务端部署好。

部署NFS客户端

[root@nfs01 ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[root@nfs01 ~]# source /etc/bashrc
[root@nfs01 ~]# echo $RSYNC_PASSWORD
oldboy

测试推送

[root@nfs01 ~]# rsync -avz /data rsync_backup@172.16.1.41::backup/
sending incremental file list

sent 164 bytes  received 25 bytes  126.00 bytes/sec
total size is 0  speedup is 0.00

2)查看inotify支持情况

[root@nfs01 ~]# uname -r
3.10.0-957.5.1.el7.x86_64
[root@nfs01 ~]#  ls -l /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 4月  19 09:45 max_queued_events
-rw-r--r-- 1 root root 0 4月  19 09:45 max_user_instances
-rw-r--r-- 1 root root 0 4月  19 09:45 max_user_watches

3)安装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

[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-8.el7.x86_64

4)命令参数和事件知识

image.png image.png

5)测试实践

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data

6)思考:实现数据复制
监控哪些事件?
增 改 删 需要监控

[root@nfs01 ~]# inotifywait -mrq --format '%w%f' -e close_write,delete /data
/data/a.txt
/data/a.txt

7)编写脚本

[root@nfs01 ~]#  mkdir /server/scripts -p
[root@nfs01 ~]# cd /server/scripts/
[root@nfs01 /server/scripts]# vim monitor.sh 
#!/bin/sh
cmd="/usr/bin/inotifywait"
$cmd -mrq  --format '%w%f' -e close_write,delete /data|\
while read line
do
  cd /data&&\
  rsync -az --delete ./ rsync_backup@172.16.1.41::backup
done

[root@nfs01 /server/scripts]# /bin/sh /server/scripts/monitor1.sh &
[2] 9199

8)放入开机自启动程序里( vim /etc/rc.local)

[root@nfs01 /server/scripts]# vim /etc/rc.local 
###############
/bin/sh /server/scripts/monitor1.sh 
[root@nfs01 /server/scripts]# tail -2 /etc/rc.local
###############
/bin/sh /server/scripts/monitor1.sh &
上一篇下一篇

猜你喜欢

热点阅读