Inotify安装与配置

uname -a  #查看linux系统版本号,inotify需要linux内核版本大约2.6.13

cd /proc/sys/fs/
ls   #可以查看到在目录下有inotify的目录
cd inotify
ls   #可看到有以下三个文件,说明inotify已经成功安装
max_queued_events  max_user_instances  max_user_watches

#安装inotify-tools-3.14.tar.gz事件捕捉工具
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make && make install

inotifywatch -h    #查看是否有这个命令。如有,安装已成功

#以下是inotify所能监控到的目录或文件的操作
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
运行 inotify 设置监控目录
/usr/local/bin/inotifywait -mrq –timefmt ‘%Y%m%d%H%M’ –format ‘%T %w%f %e’ /home/test/ceshi
–timefmt 时间的格式
–format 输出的格式
-m 保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e 监听的事件

官方已个利用inotify 触发RSYNC同步的例子
#!/bin/sh
# get the current path
CURPATH=`pwd`
inotifywait -mr –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w %f’ \
-e close_write /tmp/test | while read date time dir file; do

FILECHANGE=${dir}${file}
# convert absolute path to relative
FILECHANGEREL=`echo “$FILECHANGE” | sed ‘s_’$CURPATH’/__’`

rsync –progress –relative -vrae ‘ssh -p 22′  $FILECHANGEREL usernam@example.com:/backup/root/dir && \
echo “At ${time} on ${date}, file $FILECHANGE was backed up via rsync”
done

一个的rsync触发同步例子
#!/bin/sh
src=/data/www/wwwroot/
des=/data/www/wwwroot
ip=192.168.1.101

/usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format  ‘%T %w%f’ \
-e modify,delete,create,attrib \
${src} \
| while read  file
do
rsync -avz –delete –progress ${src} root@${ip}:${des} &&
#echo “${src} was rsynced”
#echo “—————————————————–”
done