inotify是内核的一个功能,众所周知内核的功能我们必须要配合工具才能使用,通常情况下用户要使用内核的功能,都需要用户空间的软件去调用才可以达到使用内核的功能的目的,用户是无法直接操内核的。实现inotify软件有inotify-tools、sersync、lrsyncd。我们这里以inotify-tools这个软件包为例进行实验;inotify-tools包主要有两个文件,一个是inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,常用于实时同步的目录监控;一个是inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。通常情况下我们使用iontifywait就可以了。接下来我们来安装inotify-tools
[root@test ~]# cat /proc/sys/fs/inotify/max_queued_events 16384 [root@test ~]# echo 'fs.inotify.max_queued_events=999999' >> /etc/sysctl.conf [root@test ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). fs.inotify.max_queued_events=999999 [root@test ~]# sysctl -p fs.inotify.max_queued_events = 999999 [root@test ~]# cat /proc/sys/fs/inotify/max_queued_events 999999 [root@test ~]#
[root@test ~]# yum info inotify-tools 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 可安装的软件包 名称 :inotify-tools 架构 :x86_64 版本 :3.14 发布 :9.el7 大小 :51 k 源 :epel/x86_64 简介 : Command line utilities for inotify 网址 :http://inotify-tools.sourceforge.net/ 协议 : GPLv2 描述 : inotify-tools is a set of command-line programs for Linux providing : a simple interface to inotify. These programs can be used to monitor : and act upon filesystem events. [root@test ~]#
[root@test ~]# 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 [root@test ~]#
[root@test ~]# rpm -qa rsync rsync-3.1.2-6.el7_6.1.x86_64 [root@test ~]# yum info rsync 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 已安装的软件包 名称 :rsync 架构 :x86_64 版本 :3.1.2 发布 :6.el7_6.1 大小 :815 k 源 :installed 来自源:updates 简介 : A program for synchronizing files over a network 网址 :http://rsync.samba.org/ 协议 : GPLv3+ 描述 : Rsync uses a reliable algorithm to bring remote and host files into : sync very quickly. Rsync is fast because it just sends the differences : in the files over the network instead of sending the complete : files. Rsync is often used as a very powerful mirroring process or : just as a more capable replacement for the rcp command. A technical : report which describes the rsync algorithm is included in this : package. [root@test ~]#
[root@test-centos7-node1 ~]# tree . └── test 1 directory, 0 files [root@test-centos7-node1 ~]# echo "this is test file" >test/f1 [root@test-centos7-node1 ~]# rsync -avz --password-file=/etc/rsync.pass /root/test/ rsyncuser@192.168.0.99::backup sending incremental file list ./ f1 sent 93 bytes received 30 bytes 246.00 bytes/sec total size is 18 speedup is 0.15 [root@test-centos7-node1 ~]#
[root@test-centos7-node1 ~]# cat inotify_rsync.sh #!/bin/bash SRC='/root/test/' DEST='rsyncuser@192.168.0.99::backup' inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write ${SRC} |whileread DATE TIME DIR FILE;do FILEPATH=${DIR}${FILE} rsync -az --delete --password-file=/etc/rsync.pass $SRC$DEST && echo"At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log done [root@test-centos7-node1 ~]#
提示:解释下上面的脚本,首先我们定义要监控的目录src和rsync的地址dest(这个地址就是客户端rsync连接rsync服务端的地址),然后通过inotifywait来指定监控的资源和事件,以及输出信息的格式,然后每一次触发事件的输出信息就交给while read处理,while read 把事件输出信息分别用DATA TIME DIR FILE来保存;然后把DIR和FILE做合并,得到变化资源的真实路径,然后通过rsync把变化的资源推到rsync服务器上,然后把对应的事件信息写到/var/log/changelist.log中。