首先在客户端安装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