inotifywait --help
[root@backup ~]# inotifywait --help inotifywait 3.14 Wait for a particular event on a file or set of files. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern> Like --exclude but case insensitive. -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -r|--recursive Watch directories recursively. --fromfile <file> Read files to watch from <file> or `-\' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events). -qq Print nothing (not even events). --format <fmt> Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt> strftime-compatible format string for use with %T in --format string. -c|--csv Print events in CSV format. -t|--timeout <seconds> When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. Exit status: 0 - An event you asked to watch for was received. 1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred. 2 - The --timeout option was given and no events occurred in the specified interval of time. Events: access file or directory contents were read modify file or directory contents were written attrib file or directory attributes changed close_write file or directory closed, after being opened in writeable mode close_nowrite file or directory closed, after being opened in read-only mode close file or directory closed, regardless of read/write mode open file or directory opened moved_to file or directory moved to watched directory moved_from file or directory moved from watched directory move file or directory moved to or from watched directory **create** file or directory created within watched directory **delete** file or directory deleted within watched directory delete_self file or directory was deleted unmount file system containing file or directory unmounted下面用列表详细解释一下各个参数的含义
inotifywait参数 含义说明-r --recursive 递归查询目录
-q --quiet 打印很少的信息,仅仅打印监控事件的信息
-m,--monitor 始终保持事件监听状态
--exclude 排除文件或目录时,不区分大小写。
--timefmt 指定时间输出的格式
--format 打印使用指定的输出类似格式字符串
-e,--event 通过此参数可以指定需要监控的事件,如下一个列表所示
-e :--event的各种事件含义
Events 含义access 文件或目录被读取
modify 文件或目录内容被修改
attrib 文件或目录属性被改变
close 文件或目录封闭,无论读/写模式
open 文件或目录被打开
moved_to 文件或目录被移动至另外一个目录
move 文件或目录被移动到另一个目录或从另一个目录移动至当前目录
create 文件或目录被创建在当前目录
delete 文件或目录被删除
umount 文件系统被卸载
5.4 人工测试监控事件
开启两个窗口
5.4.1 测试create 在第一个窗口输入如下内容: [root@backup ~]# ls /backup [root@backup ~]# inotifywait -mrq --timefmt \'%y %m %d %H %M\' --format \'%T %w%f\' -e create /backup 在第二个窗口:输入如下内容 [root@backup ~]# cd /backup [root@backup backup]# touch chensiqi 此时回到第一个窗口出现如下内容: 17 03 11 07 19 /backup/chensiqi #命令说明 inotifywait:ionotify的命令工具 -mrq:-q只输入简短信息 -r,递归监控整个目录包括子目录 -m进行不间断持续监听 --timefmt 指定输出的时间格式 --format:指定输出信息的格式 -e create:制定监控的时间类型,监控创建create事件。 5.4.2 测试delte 第一个窗口输入如下信息: [root@backup ~]# inotifywait -mrq --timefmt \'%y %m %d %H %M\' --format \'%T %w%f\' -e delete /backup 第二个窗口输入如下信息: [root@backup backup]# rm -rf chensiqi 此时第一个窗口会出现如下信息: 17 03 11 07 29 /backup/chensiqi #命令说明: -e delete:指定监听的事件类型。监听删除delete事件 5.4.3 测试close_write 第一个窗口输入如下信息: inotifywait -mrq --timefmt \'%y %m %d %H %M\' --format \'%T %w%f\' -e close_write /backup 第二个窗口输入如下信息: [root@backup backup]# touch close_write.log [root@backup backup]# echo 111 >> close_write.log [root@backup backup]# rm -f close_write.log 此时第一个窗口会出现如下信息: 17 03 11 07 38 /backup/close_write.log 17 03 11 07 39 /backup/close_write.log #命令说明: -e close_write:指定监听类型。监听文件写模式的关闭。 5.4.4 测试move_to 第一个窗口输入如下信息: [root@backup ~]# inotifywait -mrq --timefmt \'%y %m %d %H %M\' --format \'%T %w%f\' -e moved_to /backup 第二个窗口输入如下信息: 此时第一个窗口会出现如下信息: [root@backup backup]# touch chensiqi [root@backup backup]# mv chensiqi chen [root@backup backup]# mkdir ddddd [root@backup backup]# mv chen ddddd/ 5.5 编写inotify实时监控脚本 #!/bin/bash backup_Server=172.16.1.41 /usr/bin/inotifywait -mrq --format \'%w%f\' -e create,close_write,delete /data | while read line do cd /data rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=http://www.likecs.com/etc/rsync.password done提示: