首先在客戶端安裝inotify程序,在服務端啓動rsync --daemon;當客戶端檢測到的目錄及內容發生變化時而後客戶端會將發生變化的目錄及內容推送
-rw-r--r-- 1 root root 0 5月 18 17:32 max_queued_events 最大隊列事件(大一點比較好)
-rw-r--r-- 1 root root 0 5月 18 17:32 max_user_instances
--exclude <pattern> 排除文件或者目錄時不區分大小寫
--excludei <pattern> 排除文件或者目錄時不區分大小寫
-m|--monitor 始終保持事件監聽狀態
-d|--daemon
-r|--recursive 遞歸查詢目錄
--fromfile <file>
-o|--outfile <file>
-s|--syslog
-q|--quiet 打印不多的信息,打印監控事件
-qq
--format <fmt> 打印使用指定的輸出相似格式的字符串
--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.
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.
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
4.實戰:
在inotify端執行:
/opt/inotify/bin/inotifywait -mrq --timefmt '%d%m%y%H:%M' --format '%T%w%f' -e create,delete,open /backup 在backup目錄下的建立操做進行監控
/opt/inotify/bin/inotifywait -mrq --timefmt '%d%m%y%H:%M' --format '%T%w%f' -e delete /backup
簡化的事件輸出:
#!/bin/bash
/opt/inotify/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /backup
while read file
do
rsync -az "$file" --delete rsync_backup@serverip::backup --password-file=/etc/rsync.password
done
執行上述的腳本,而後再往backup下建立和刪除文件測試,可是不能實現刪除同步操做。
優化版腳本以下:
#!/bin/bash
#parameter
host=172.1.1.4
src="/backup"
dst="oldboy"
user="rsync_backup"
pass="/etc/rsync.password"
cmd="/opt/inotify/bin/inotifywait"
#judge
if [ ! -e $src ] \
|| [ ! -e $pass ] \
|| [ ! -e $cmd ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "please check file and folder !"
exit 9
fi
#exec
$cmd -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -azP --delete --timeout=100 --password-file=$pass $src $user@$host::$dst &> /dev/null
cd $src && rsync -az -R --delete ./ --timeout=100 $user@$host::$dst --password-file=$pass &> /dev/null
done
exit 0
經過start|stop控制腳本事項inotify操做結合上述腳本實現開關操做
#!/bin/bash
. /etc/init.d/functions
if [ $# -ne 1 ];then
usage:$0 {start|stop}
exit 1
fi
case "$1" in
start)
/bin/bash /server/scripts/inotify.sh &
echo $$ > /opt/inotify/i.pid
if [ `ps -ef | grep inotify|wc -l` -gt 2 ];then
action "inotify server is started" /bin/true
else
action "inotify server is error" /bin/false
fi
;;
stop)
kill -9 `cat /opt/inotify/i.pid` > /dev/null 2>&1
pkill inotifywait
sleep 1
if [ `ps -ef | grep inotify |wc -l` -eq 0 ];then
action "inotify server is stopped" /bin/true
else
action "inotify server is stopped error" /bin/false
fi
;;
*)
usage :$0 {start|stop}
exit 1
esac
壓力測試 及優化
查看inotify對應的內核參數
cd /proc/sys/fs/inotify/
cat *
16384
表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。
128
表示每個real user ID可建立的inotify instatnces的數量上限
8192
表示同一用戶同時能夠添加的watch數目(watch通常是針對目錄,決定了同時同一用戶能夠監控的目錄數量)
上述三個參數能夠按照實際狀況儘量地調大一點
重啓機器以後會失效,永久操做的方法
vim /etc/sysctl.conf
fs.inotify.max_queued_events= 99999999
fs.inotify.max_user_watches= 99999999
fs.inotify.max_user_instances= 65535
修改完成以後sysctl -p 刷新生效便可
注意: max_queued_events 是inotify管理的隊列的最大長度,文件系統變化越頻繁,這個值就應該越大。
(小文件的最大併發數也就200左右,不然會有延遲)
echo {a..j} | tr " " "\n" > a.log #豎着 寫文件
split -l 2 a.log #兩行一個分割文件
inotify的缺點
(1)、併發大於200有延遲
(2)、推送效率不高
改進版
#!/bin/bash
cmd="/opt/inotify/bin/inotifywait"
$cmd -mqr --format '%w%f' -e create,close_write,delete /backup|\
while read line
do
[ ! -e "$line" ] && continue
done