linux 实时同步inotify

80652319 2017-04-05

#实时同步inotify

1、inotify简介inotify是一种强大的,细腻度的,异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过INOTIFY可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tloos就是实施这样监控的软件。

2、inotify实施检查rsync daemon服务是否服务正常,可以推送数据实施同步ps -ef |grep rsync|grep -v grep root 5959 1 0 18:52 ? 00:00:00 rsync --daemon1)坚持当前系统是否支持inotifyuname -r 版本在2.6.13以上才支持2.6.32-504.el6.x86_64# ls -l /proc/sys/fs/inotify-rw-r--r-- 1 root root 0 Apr 4 20:23 max_queued_events-rw-r--r-- 1 root root 0 Apr 4 20:23 max_user_instances-rw-r--r-- 1 root root 0 Apr 4 20:23 max_user_watches#显示这三个文件则证明支持INOTIFYproc/sys/fs/inotify/max_queued_evnets 表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

/proc/sys/fs/inotify/max_user_instances 表示每一个real user ID可创建的inotify instatnces的数量上限。

/proc/sys/fs/inotify/max_user_watches 表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches

2)下载inotify源码包,编译安装wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gztar zxvf inotify-tools-3.13.tar.gzcd inotify-tools-3.13./configure --prefix=/usr/local/inotify-tools-3.13make && make installln -s /usr/local/inotify-tools-3.13/ /usr/local/inotifycd /usr/local/inotify

./inotifywait -helpinotifywait 3.13Wait 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. #始终保持事件监听状态 -r|--recursive Watch directories recursively. #递归查询目录 --fromfile <file> Read files to watch from <file> or - for stdin. -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 #文件或目录被卸载实时监控命令:/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create,delete,close_write,attrib /data

inotify缺点:1)并发不能大于200个文件

实时监控inotify脚本

#!/bin/bash
#inotify jiankong
cmd="/usr/local/inotify/bin/inotifywait"
src="/data"
mod="test"
user="rsyncback"
ip=192.168.233.129
passfile="/etc/rsync.password"
rsyc="/usr/bin/rsync"
#judge
if [ -f "$cmd" ] && [ -e "$src" ] && [ -n "$user" ] && [ -n "$mod" ] && [ -f "$passfile" ] && [ -f "$rsyc" ];then
 echo "file path is ok"
else
  exit 9
fi
while true
do
$cmd -mrq --format '%w%f' -e create,delete,modify,attrib,close_write,move $src|\
while read line
do
   [ ! -e "$line" ] && break || \
   echo $line >> /root/inotiity.log
   $rsyc -az --delete $line ${user}@${ip}::$mod --password-file=$passfile
done
cd $src && $rsyc -az --delete ./ ${user}@${ip}::$mod --password-file=$passfile
done

相关推荐