今天文件同步有问题,所以决定加上inotify好久没有使用了,整理出来,以便以后使用


1.inotify介绍

inotify是一种强大细粒度、异步文件系统事件监控机制,Linux内核从2.6.13开始支持inotify,通过inotify可以监控文件系统中添加、删除、修改、移动等细微事件。

第三方软件inotify-tools 是基于此的第三方工具


2.下载

地址:https://github.com/rvoicilas/inotify-tools/wiki


3.安装

tar -xf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14
./configure
make
make install


安装完成生产的命令:

# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44279 6月   2 15:03 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41369 6月   2 15:03 /usr/local/bin/inotifywatch

4.inotify 参数说明

inotify定义了一些接口参数,用来限制inotify消耗kernel memory的大小。都是内存参数,可以根据需要调节

# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 6月   2 16:06 max_queued_events
-rw-r--r-- 1 root root 0 6月   2 16:06 max_user_instances
-rw-r--r-- 1 root root 0 6月   2 16:06 max_user_watches


max_queued_events:表示调用inotify_init时分配到inotify instace中可以排队的event数的最大值,超出被丢弃。

max_user_instances:表示每一个real user ID可创建的inotify instance的上限

max_user_watches:表示每个inotify实例相关联的watches的上限,就是每个inotify实例可监控的最大目录数量。


5.事件参数说明

可以使用man inotify 、 man inotifywait、man inotifywatch即可得到帮助信息


inotify 可以监视的文件系统事件包括:

IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

inotifywait 参数:

-m "--mointor" 始终保持事件监听
-r "--recursive" 表示递归查询目录
-q "--quiet" 打印监控事件
-e "--event" 指定要监控的事件,如modify、delete等

6.测试同步

192.168.1.87(inotify)

192.168.1.88(client)


6.1.脚本

#!/bin/bash
SRC=/tmp/ckl
DIR=/tmp
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib ${SRC} \
| while read line
do
        /usr/bin/rsync -auv ${SRC} 192.168.1.88:${DIR}
        echo "$line is rsyncd" >> /tmp/sync.log 2>&1
done

6.2.在1.87上执行脚本

sh sync_file.sh

6.3.在1.87上创建文件

touch /tmp/ckl/nimei.txt
touch /tmp/ckl/haha.txt


6.4.查看脚本输出:

....
ckl/
ckl/haha.txt
ckl/nimei.txt
sent 155 bytes  received 54 bytes  418.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list
sent 74 bytes  received 13 bytes  174.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list
ckl/
ckl/.haha.txt.swp

6.3.在1.88上查看

# ll /tmp/ckl/
total 4
-rw-r--r-- 1 root root 22 Jun  2 15:54 haha.txt
-rw-r--r-- 1 root root  0 Jun  2 15:48 nimei.txt


6.4.修改文件,测试同步,略过

查看日志:

02/06/16 15:54 /tmp/ckl/haha.txtCREATE is rsyncd
02/06/16 15:54 /tmp/ckl/haha.txtATTRIB is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpCREATE is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swxCREATE is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swxDELETE is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpDELETE is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpCREATE is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpATTRIB is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/haha.txtMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/haha.txtMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/haha.txtATTRIB is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpMODIFY is rsyncd
02/06/16 15:54 /tmp/ckl/.haha.txt.swpDELETE is rsyncd