您的位置:首页 > 其它

安装inotify和对应inotify命令操作,以及inotify和rsync的组合操作

2016-09-04 16:26 393 查看
一、安装inotify
1、1检查系统版本
[root@nfs01 ~]# cat /etc/redhat-release CentOS release 6.7 (Final)1、2检查系统参数[root@nfs01 ~]# uname -aLinux backup 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux1、3检查当前系统是否支持inotify#inotify是从2.6版本开始支持的[root@nfs01 ~]# ls -l /proc/sys/fs/inotify/#下述三个文件对inotify机制有一定的限制total 0-rw-r--r-- 1 root root 0 Jun 10 14:42 max_queued_events#max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。-rw-r--r-- 1 root root 0 Jun 10 10:24 max_user_instances#max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch进程数。-rw-r--r-- 1 root root 0 Jun 10 14:42 max_user_watches#max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)。1、4调整参数
[root@nfs01 inotify]# pwd/proc/sys/fs/inotify[root@nfs01 inotify]# echo "50000000">max_user_watches[root@nfs01 inotify]# echo "50000000">max_queued_events
1、5下载inotify源码包
[root@nfs01 backup]# mkdir/home/oldboy/tools –p[root@nfs01 backup]# cd /home/oldboy/tools[root@nfs01 tools]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz1、6安装inotify[root@nfs01 tools]# cd /home/oldboy/tools[root@nfs01 tools]# pwd/home/oldboy/tools[root@nfs01 tools]# cd inotify-tools-3.14[root@nfs01 inotify-tools-3.14]# lltotal 1532-rw-r--r-- 1 1000 1000 316757 Mar 12 2010 aclocal.m4-rw-r--r-- 1 1000 1000 39 Mar 12 2010 AUTHORSdrwxr-xr-x 2 root root 4096 May 26 06:23 autom4te.cache-rw-r--r-- 1 1000 1000 0 Mar 12 2010 ChangeLog-rwxr-xr-x 1 1000 1000 46260 Mar 12 2010 config.guess-rw-r--r-- 1 root root 2372 May 26 06:23 config.h-rw-r--r-- 1 root root 2127 May 26 06:23 config.h.in-rw-r--r-- 1 1000 1000 2195 Mar 12 2010 config.h.in~-rw-r--r-- 1 root root 21213 May 26 06:23 config.log-rwxr-xr-x 1 root root 59102 May 26 06:21 config.status-rwxr-xr-x 1 1000 1000 33952 Mar 12 2010 config.sub-rwxr-xr-x 1 1000 1000 386339 Mar 12 2010 configure-rw-r--r-- 1 1000 1000 1810 Mar 12 2010 configure.ac-rw-r--r-- 1 1000 1000 17999 Mar 12 2010 COPYING-rwxr-xr-x 1 1000 1000 18615 Mar 12 2010 depcomp-rw-r--r-- 1 1000 1000 9498 Mar 12 2010 INSTALL-rwxr-xr-x 1 1000 1000 13663 Mar 12 2010 install-shdrwxrwxrwx 3 1000 1000 4096 May 26 06:21 libinotifytools-rwxr-xr-x 1 root root 257893 May 26 06:21 libtool-rwxr-xr-x 1 1000 1000 243452 Mar 12 2010 ltmain.sh-rw-r--r-- 1 root root 23025 May 26 06:21 Makefile-rw-r--r-- 1 1000 1000 245 Mar 12 2010 Makefile.am-rw-r--r-- 1 1000 1000 22603 Mar 12 2010 Makefile.indrwxrwxrwx 2 1000 1000 4096 May 26 06:21 man-rwxr-xr-x 1 1000 1000 11419 Mar 12 2010 missing-rw-r--r-- 1 1000 1000 48 Mar 12 2010 NEWS-rw-r--r-- 1 1000 1000 246 Mar 12 2010 READMEdrwxrwxrwx 4 1000 1000 4096 May 26 06:23 src-rw-r--r-- 1 root root 23 May 26 06:23 stamp-h1[root@nfs01 inotify-tools-3.14]#./configure --prefix=/usr/local/inotify-tools-3.14[root@nfs01 inotify-tools-3.14]# echo $?0[root@nfs01 inotify-tools-3.14]# make&& make install[root@nfs01 inotify-tools-3.14]# echo$? 0[root@nfs01 tools]# pwd/home/oldboy/tools[root@nfs01 tools]# ls -l /usr/local/inotify-toolslrwxrwxrwx 1 root root 30 May 26 06:26 /usr/local/inotify-tools -> /usr/local/inotify-tools-3.14/1、7查看编译参数
[root@nfs01 tools]# cd inotify-tools-3.14[root@nfs01 inotify-tools-3.14]# pwd/home/oldboy/tools/inotify-tools-3.14[root@nfs01 inotify-tools-3.14]# ./configure -h#--prefix=PATH可指定编译安装的路径1、8查看编译结果
[root@nfs01 inotify-tools-3.14]# cd /usr/local/inotify-tools[root@nfs01 inotify-tools]# lltotal 16drwxr-xr-x 2 root root 4096 May 26 06:23 bin#inotify执行命令(二进制)drwxr-xr-x 3 root root 4096 May 26 06:23 include#inotify程序所需要的头文件drwxr-xr-x 2 root root 4096 May 26 06:23 lib#动态链接的库文件drwxr-xr-x 4 root root 4096 May 26 06:23 share#帮助文档1、9查看bin目录
[root@nfs01 bin]# pwd/usr/local/inotify-tools/bin[root@nfs01 bin]# lltotal 88-rwxr-xr-x 1 root root 44287 May 26 06:23 inotifywait-rwxr-xr-x 1 root root 41409 May 26 06:23 inotifywatch#inotifywait:在被监控的文件或目录上等待特定文件系统时间(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。#inotifywatch:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。[root@nfs01 bin]# pwd/usr/local/inotify-tools/bin[root@nfs01 bin]# ./inotifywait --help#可看到inotify命令常用参数1、10命令加入环境变量[root@nfs01 bin]# echo "PATH=/usr/local/inotify-tools/bin:[b]$PATH" >>/etc/profile[/b][root@nfs01 bin]# source /etc/profile二、inotify命令操作操作[root@nfs01 bin]# cd ~#进入家目录是测试命令是否可行[root@nfs01 ~]# pwd/root#此处要打开两个窗口,一个窗口执行命令,另一个窗口执行增删改操作。下面的颜色代表对应的操作和结果。[root@nfs01 data]# rm -rf *[root@nfs01 data]# touch test1.txt[root@nfs01 data]# echo "111111111">test2.txt[root@nfs01 data]# rm -f test1.txt[root@nfs01 ~]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,close_write,delete /data/10/06/16 19:12 /data/TEST/data/TEST10/06/16 19:12 /data/TEST/data10/06/16 19:12 /data/TEST10/06/16 19:12 /data/test110/06/16 19:12 /data/test1010/06/16 19:12 /data/test210/06/16 19:12 /data/test310/06/16 19:12 /data/test410/06/16 19:12 /data/test510/06/16 19:12 /data/test610/06/16 19:12 /data/test710/06/16 19:12 /data/test810/06/16 19:12 /data/test910/06/16 19:12 /data/test1.txt10/06/16 19:12 /data/test1.txt10/06/16 19:12 /data/test2.txt10/06/16 19:12 /data/test2.txt10/06/16 19:13 /data/test1.txt#注意这里测试的是如果data目录下连着创建相同的文件,那么inotifywait对应的目录下会接着打印日志的,只关注变化的内容。
三、inotify和rsync的结合使用3、1前提条件这里使用的是以10.0.0.31-nfs01为客户端,10.0.0.41-backup为服务端的测试。现在是以NAT模式搭建的虚拟机,10网段对应外网,以下使用的也是外网IP,建议使用内网网段进行操作。3、2客户端操作[root@nfs01 scripts]# mkdir -p /server/scripts#建立脚本统一存放地址[root@nfs01 scripts]# vim inotify.sh #!/bin/shinotify=/usr/local/inotify-tools/bin/inotifywait$inotify -mrq --format '%w%f' -e create,close_write,delete /data \|while read filedo cd / && rsync -az ./data --delete rsync_backup@10.0.0.41::backup/ --password-file=/etc/rsync.passworddone#关于rsync的基本配置和操作请参考我的另一篇博文“rsync的配置及以rsync的daemon工作模式传输数据http://ylcodes01.blog.51cto.com/5607366/1787772”
[root@nfs01 scripts]# /bin/sh -x /server/scripts/inotify.sh #测试[root@nfs01 data]# ls -l /data/ #客户端创建test2.txt文件total 0-rw-r--r-- 1 root root 0 Jun 10 19:36 test2.txt[root@backup data]# ls -l /backup/data/test2.txt #服务端查看结果-rw-r--r-- 1 rsync rsync 0 Jun 10 19:36 /backup/data/test2.txt[root@nfs01 data]# vim /etc/rc.local /bin/sh /server/scripts/inotify.sh & #加入开机启动项,&表示后台执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  inotify