1、根據之前一篇關於rsync的隨筆部署好rsync服務後,可以開始inotify的部署 2、inotify的部署使用 ①、檢查系統是否支持inotify 如果出現上面結果說明系統支持inotify。 ②、安裝 ③、inotify參數 * 說明: max_user_instances:每個用戶創建i ...
1、根據之前一篇關於rsync的隨筆部署好rsync服務後,可以開始inotify的部署
2、inotify的部署使用
①、檢查系統是否支持inotify
[root@iZ25w1kdi5zZ ~]# ls -lsart /proc/sys/fs/inotify total 0 0 dr-xr-xr-x 0 root root 0 Sep 19 09:38 .. 0 -rw-r--r-- 1 root root 0 Jan 1 13:51 max_user_watches 0 -rw-r--r-- 1 root root 0 Jan 1 13:51 max_user_instances 0 -rw-r--r-- 1 root root 0 Jan 1 13:51 max_queued_events 0 dr-xr-xr-x 0 root root 0 Jan 1 13:51 .
如果出現上面結果說明系統支持inotify。
②、安裝
tar -zxvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14/ ./configure --prefix=/usr/local/inotify-tools-3.14 make && make install ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
③、inotify參數
* 說明:
max_user_instances:每個用戶創建inotify實例最大值
max_queued_events:inotify隊列最大長度,如果值太小,會出現錯誤,導致監控文件不准確
max_user_watches:要知道同步的文件包含的目錄數
* 查看預設參數
[root@11 scripts]# sysctl -a|egrep "inotify.max_queued_events|inotify.max_user_watches|epoll.max_user_watches|inotify.max_user_instances" fs.epoll.max_user_watches = 201011 fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 128 fs.inotify.max_user_watches = 8192 sysctl: reading key "net.ipv6.conf.all.stable_secret" sysctl: reading key "net.ipv6.conf.default.stable_secret" sysctl: reading key "net.ipv6.conf.enp0s3.stable_secret" sysctl: reading key "net.ipv6.conf.enp0s8.stable_secret" sysctl: reading key "net.ipv6.conf.lo.stable_secret" sysctl: reading key "net.ipv6.conf.virbr0.stable_secret" sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"
* 修改參數
-命令修改
sysctl -w fs.inotify.max_user_instances=130
[root@11 scripts]# sysctl -w fs.inotify.max_user_instances=65535 fs.inotify.max_user_instances = 65535 [root@11 scripts]# sysctl -w fs.inotify.max_user_watches=99999999 fs.inotify.max_user_watches = 99999999 [root@11 scripts]# sysctl -w fs.inotify.max_queued_events=99999999 fs.inotify.max_queued_events = 99999999
-配置文件修改
[root@tmp]# vi /etc/sysctl.conf #添加如下代碼 fs.inotify.max_user_instances=130
④、創建實時監控腳本

##例一 [root@11 scripts]# cat inotify.sh #!/bin/sh cmd="/usr/local/inotify-tools/bin/inotifywait" $cmd -mrq --format '%w%f' -e create,close_write,delete /backup|\ while read line do cd /backup &&\ rsync -az --delete --timeout=100 ./ rsync_user@10.0.0.10::www --password-file=/etc/rsync.password doneView Code

##例子二 [root@iZ25w1kdi5zZ ~]# mkdir -p /opt/scripts [root@iZ25w1kdi5zZ ~]# cd /opt/scripts [root@iZ25w1kdi5zZ scripts]# vi inotify_start.sh /usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete \ --fromfile '/opt/scripts/ffile' \ --timefmt '%y-%m-%d %H:%M' --format '%T %f %e' \ --outfile '/tmp/rsync.log' inotifywait常用參數: --timefmt 時間格式 %y年 %m月 %d日 %H小時 %M分鐘 --format 輸出格式 %T時間 %w路徑 %f文件名 %e狀態 -m 始終保持監聽狀態,預設觸發事件即退出。 -r 遞歸查詢目錄 -q 列印出監控事件 -e 定義監控的事件,可用參數: open 打開文件 access 訪問文件 modify 修改文件 delete 刪除文件 create 新建文件 attrb 屬性變更View Code