inotify+rsync做实时同步
inotify的下载地址:https://github.com/rvoicilas/inotify-tools
下载的版本为inotify-tools.3.14 将下载的工具规范地放在一个地方,比如放到/home/goser/tools/下
查看linux系统内核是否支持inotify,只要包含下面三个文件代表系统是支持的
[root@nfs-server inotify-tools-3.14]# ll /proc/sys/fs/inotify/ 总用量 0 -rw-r--r-- 1 root root 0 9月 15 11:05 max_queued_events -rw-r--r-- 1 root root 0 9月 15 11:05 max_user_instances -rw-r--r-- 1 root root 0 9月 15 11:05 max_user_watches
切到/home/goser/tools/下,解压 tar zxf inotify-tools-3.14.tar.gz
因为这是c语言编译的工具,要用make install 安装,安装顺序为
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主要的就是其下面的bin目录中的inotifywait和inotifywatch,这个inotifywait只要监控的目录有变化,如文件的删除、创建、修改等操作的时候,就会触发实时备份。。。
[root@nfs-server inotify-tools-3.14]# ll /usr/local/inotify-tools/bin/ 总用量 88 -rwxr-xr-x 1 root root 44287 9月 15 11:13 inotifywait -rwxr-xr-x 1 root root 41409 9月 15 11:13 inotifywatch
查看inotifywait的帮助,可以查看其可跟的参数选项:
[root@nfs-server ~]# /usr/local/inotify-tools/bin/inotifywait --help inotifywait 3.14 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. -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -r|--recursive Watch directories recursively. --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -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. 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
监控测试
[root@nfs-server ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create,delete,close_write /data/ 在监控中的话,会话窗口是堵塞的,一直在监控着事件的发生。
inotify+rsync企业应用
这个应用支持的并发数为每秒200-300个文件并发,如果超过这个范围,要用到架构drbd了,不过一般的中小网站基本上没问题。
1、在nfs服务器上部署inotify,监控共享目录的变化,向rsync daemon服务上做实时同步
部署过程如上面的过程,这里不再赘述
2、创建inotify脚本,做实时同步
[root@nfs-server ~]# vim /server/scripts/inotify.sh #!/bin/bash inotify=/usr/local/inotify-tools/bin/inotifywait $inotify -mqr --format '%w%f' -e create,delete,close_write /data/ \ |while read file do cd / && rsync -az ./data --delete rsync_backup@192.168.1.104::backup/ \ --password-file=/etc/rsync.password done
3、位nfs服务创建为rsync同步的密码文件
[root@nfs-server data]# echo 'oldboy'>/etc/rsync.password [root@nfs-server data]# chmod 600 /etc/rsync.password
4、脚本测试
sh -x /server/scripts/inotify.sh
5、测试没问题了,让inotify.sh脚本做后台运行
[root@nfs-server ~]# /bin/sh /server/scripts/inotify.sh &
6、将此后台运行的命令加入到/etc/rc.local中,方便服务器重启后也能进行后台运行inotify实时同步。
[root@nfs-server ~]# vi /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local >/etc/udev/rules.d/70-persistent-net.rules /etc/init.d/rpcbind start /etc/init.d/nfs start /bin/sh /server/scripts/inotify.sh &
7、对inotify的优化
在/proc/sys/fs/inotify/下有三个文件,对inotify的机制有限制,可对这三个文件做下优化
max_user_watches 设置可以监控的文件数量 默认值是8192 这个值应该很小了,需要优化
max_queued_events 设置队列可容纳的事件数量 默认值是16384 这个值也很小需要优化
max_user_instances 设置每个用户可运行的inotifywait或inotifywatch命令的进程数,默认值是128 这个值应该不小了 就不需要优化了
优化如下:
[root@nfs-server ~]# echo '50000000'>/proc/sys/fs/inotify/max_user_watches [root@nfs-server ~]# echo '50000000'>/proc/sys/fs/inotify/max_queued_events
浙公网安备 33010602011771号