一、inotif机制
- Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象。
此种机制的出现的目的是当内核空间发生某种事件之后,可以立即通知到用户空间。方便用户做出具体的操作
二、inotify部署
1.部署前提:
- rsync daemon服务配置成功,可以在客户端推送拉取数据。
2.下载inotify包
[root@rsync-B ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
3.编译安装inotify
[root@rsync-B ~]# tar zxf inotify-tools-3.14.tar.gz
[root@rsync-B ~]# cd inotify-tools-3.14
[root@rsync-B inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14(编辑报错,查看是否安装有gcc)
[root@rsync-B inotify-tools-3.14]# make && make install
[root@rsync-B inotify-tools-3.14]# ln -s /usr/local/inotify-3.14 /usr/local/inotify
[root@rsync-B inotify]# ll
total 16
drwxr-xr-x 2 root root 4096 Jul 23 05:15 bin #inotify命令(二进制)
drwxr-xr-x 3 root root 4096 Jul 23 05:15 include #程序所需的头文件
drwxr-xr-x 2 root root 4096 Jul 23 05:15 lib #动态链接库文件
drwxr-xr-x 4 root root 4096 Jul 23 05:15 share #帮助文档
4.inotify命令详解
[root@rsync-B inotify]# bin/inotifywait --help
-r|--recursive #递归查询目录
-q|--quiet #打印监控时间的信息
-m|--monitor #始终保持时间监听状态
--timefmt <fmt> #指定时间输出的格式
--format <fmt> #打印使用指定的输出类似格式字符创
--excludei #排除文件或目录是,不区分大小写
-e|--event #可以指定需要监控的时间
Events:
access #文件或目录被读取
modify #文件或目录被修改
attrib #文件或目录属性被改变
close #文件或目录封闭,无论读/写模式
open #文件或目录被打开
moved_to #文件或目录被移动另一个目录
move #文件或目录被移动,从另一个目录移动至当前目录
create #文件或目录被创建在当前目录
delete #文件或目录被删除
unmount #文件系统被卸载
三、测试
1.监听/data1/目录,创建事件
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e create /data1/
打开新的窗口
[root@rsync-B data1]# touch aaa
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e create /data1/
230718 05:%M /data1/%F
2.测试删除
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e delete /data1/
[root@rsync-B data1]# rm -f aaa
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e delete /data1/
230718 06:%M /data1/%F
3.同时监控多个操作
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e create,delete /data1/
[root@rsync-B data1]# touch aaa
[root@rsync-B data1]# rm -f aaa
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%%M' --format '%T %w%F' -e create,delete /data1/
230718 06:%M /data1/%F
230718 06:%M /data1/%F
4.简化输出
[root@rsync-B data1]# touch aa
[root@rsync-B ~]# /usr/local/inotify/bin/inotifywait -mrq --format '%w%f' -e create /data1/
/data1/aa
四、编写脚本监控
1.脚本内容(判断是否有新文件创建,文件被删除,文件写入内容操作,凡是有则执行一次无差异同步)
[root@rsync-B ~]# cat /server/scritps/2018-07-23/inotify.sh
#!/bin/bash
cmd=/usr/local/inotify/bin/inotifywait
Dir1=/data1/
User=rsync_liang
File_passwd=/etc/rsync.password
Ip=10.0.0.129
$cmd -mrq --format '%w%f' -e create,delete,close_write ${Dir1}|\
while read line
do
if [ ! -z "$line" ];then
rsync -av --delete ${Dir1} ${User}@${Ip}::liang2 --password-file=${File_passwd}>/dev/null 2>&1
rsync -av --delete ${Dir1} ${User}@${Ip}::liang3 --password-file=${File_passwd}>/dev/null 2>&1
fi
done
2.测试
2.1执行脚本
[root@rsync-B ~]# bash /server/scritps/2018-07-23/inotify.sh
2.2创建文件
[root@rsync-B data1]# touch {1..100}
2.3查看模块liang2与liang3目录
[root@rsync-A liang2]# ll
total 0
[root@rsync-A liang2]# ls
1 14 2 25 30 36 41 47 52 58 63 69 74 8 85 90 96
10 15 20 26 31 37 42 48 53 59 64 7 75 80 86 91 97
100 16 21 27 32 38 43 49 54 6 65 70 76 81 87 92 98
11 17 22 28 33 39 44 5 55 60 66 71 77 82 88 93 99
12 18 23 29 34 4 45 50 56 61 67 72 78 83 89 94
13 19 24 3 35 40 46 51 57 62 68 73 79 84 9 95
[root@rsync-A liang3]# ll
total 0
[root@rsync-A liang3]# ls
1 14 2 25 30 36 41 47 52 58 63 69 74 8 85 90 96
10 15 20 26 31 37 42 48 53 59 64 7 75 80 86 91 97
100 16 21 27 32 38 43 49 54 6 65 70 76 81 87 92 98
11 17 22 28 33 39 44 5 55 60 66 71 77 82 88 93 99
12 18 23 29 34 4 45 50 56 61 67 72 78 83 89 94
13 19 24 3 35 40 46 51 57 62 68 73 79 84 9 95
2.4删除文件
[root@rsync-B data1]# rm -rf *
2.5查看模块liang2与liang3目录
[root@rsync-A liang2]# ll
total 0
[root@rsync-A liang3]# ll
total 0
本文介绍Inotify机制,一种用于检测文件系统变化的高效方法。通过部署Inotify并结合rsync服务,实现文件创建、删除及修改的实时监控与同步。详细展示了Inotify命令的使用,包括递归监控、事件类型选择等,并提供了脚本示例,用于自动化无差异文件同步。
892

被折叠的 条评论
为什么被折叠?



