安装配置inotify
参考
yum install inotify* -y
[root@n2 shell]# rpm -qa|grep inotify
inotify-tools-3.14-8.el7.x86_64
inotify-tools-devel-3.14-8.el7.x86_64
[root@n2 shell]# rpm -ql inotify-tools-3.14-8.el7.x86_64
/usr/bin/inotifywait # 监控目录变换的
/usr/bin/inotifywatch # 统计次数的
搞清楚inotify的原理
参考
inotifywait -h #查看参数
--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.
--excludei <pattern> #排除不监控哪些
-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
- 同时开两个窗口, 客户端启动inotifywait监控本地的/data目录
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T%w%f' -e create /data
-m|--monitor 保持监控monitor
-r|--recursive 递归监控
-q|--quiet 仅打印简单事件
-e|--event 指定监控事件
- 在本地/data目录新建目录
可以看到日志:
17/03/18 10:56/data/11.md
思路是写shell,每次发生一次变更就执行rsync
生产中使用inotify同步
#!/usr/bin/env bash
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data |\
while read file;do
cd / && rsync -az ./data/ --delete rsync_backup@192.168.14.11::data --password-file=/etc/rsync.password
done
sh inotify.sh & #这样即可
/bin/sh /root/shell/inotify.sh -& #放在rc.local里
生产rsync参数优化
[root@n2 shell]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Mar 17 10:53 max_queued_events # 设置单进程可监控的文件数
-rw-r--r-- 1 root root 0 Mar 17 10:53 max_user_instances # 设置每个用户可运行的iniotifywait或iniotifywatch命令的进程数
-rw-r--r-- 1 root root 0 Mar 17 10:53 max_user_watches # 设置inotify实例时间队列可容纳的事件数量
echo "50000000" > /proc/sys/fs/inotify/max_user_watches
echo "50000000" > /proc/sys/fs/inotify/max_queued_events
inotify性能测试:
10-300K 每秒200-300个并发,基本不会延迟.如果每s同步1000个,则nfsbackup会有延迟
1,如果能接受延迟,可以用
2,可以用多个目录多个脚本同时同步.
3,可以针对文件同步,每一次只同步变化的文件,而已.但是想归想,但效果不好
4,可以用别的方案,如drbd