#實時同步inotifylinux
一、inotify簡介
inotify是一種強大的,細膩度的,異步的文件系統事件監控機制,linux內核從2.6.13起,加入了inotify支持,經過INOTIFY能夠監控文件系統中添加、刪除、修改、移動等各類事件,利用這個內核接口,第三方軟件就能夠監控文件系統下文件的各類變化狀況,而inotify-tloos就是實施這樣監控的軟件。express
二、inotify實施
檢查rsync daemon服務是否服務正常,能夠推送數據實施同步
ps -ef |grep rsync|grep -v grep
root 5959 1 0 18:52 ? 00:00:00 rsync --daemon
1)堅持當前系統是否支持inotify
uname -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
#顯示這三個文件則證實支持INOTIFY
proc/sys/fs/inotify/max_queued_evnets
表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。bash
/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_watchesless
2)下載inotify源碼包,編譯安裝
wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify-tools-3.13
make && make install
ln -s /usr/local/inotify-tools-3.13/ /usr/local/inotify
cd /usr/local/inotify異步
./inotifywait -help
inotifywait 3.13
Wait 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. #經過此參數能夠指定須要監控的事件,以下:ui
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.this
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 /dataspa
inotify缺點:
1)併發不能大於200個文件.net
實時監控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