1 介绍和说明
1.1 软件简介
inofify是一种强大的、细粒度的、异步的文件系统时间监控机制,内置在linux内核2.6.13以上.
inotify-tools工具实现的具体原理是,开启inotify进程,进程会实时监控目标目录的变化情况,然后按要求将变化情况通过rsync命令立即同步至服务器
inotify实时的前提,是成功配置并验证rsync服务的推送功能,然后才能配置inotify服务
1.2 安装软件
系统信息
1 2 3 4 5 6
| cat /etc/redhat-release CentOS release 6.9 (Final) ip addr show eth1|awk 'NR==3{print $2}' 172.16.1.31/24 hostname nfs01
|
查看当前是否支持inotify
1 2 3 4
| uname -r 2.6.32-696.el6.x86_64 ls /proc/sys/fs/inotify/ max_queued_events max_user_instances max_user_watches
|
内核版本高于2.6.13并且/proc/sys/fs/inotify/目录中存在上述三个文件才行
2 部署程序
2.1 安装inotify程序
1 2 3 4 5
| yum install inotify-tools -y rpm -qa |grep inotify-tools inotify-tools-3.14-1.el6.x86_64 inotify-tools工具包不再默认yum源中,如果电脑没有安装epel源,用下面命令安装 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
|
重要命令
1 2 3
| rpm -ql inotify-tools|head -2 /usr/bin/inotifywait <--实现对数据目录信息变化监控 /usr/bin/inotifywatch <--监控数据信息变化,对变化的数据进行统计
|
2.2 部署rsync服务
略:参见rsync部署文档<<rsync操作和部署文档>>
2.3 编写脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| cat >/server/scripts/inotify_rsync.sh <<"EOF"
Path=/data Ip=172.16.1.41 Pass_File=/etc/rsync.password inotifywait -mqr --format '%w%f' -e create,delete,moved_to,close_write $Path |\ while read file do if [ -f $file ];then rsync -az $file --delete rsync_backup@$Ip::backup $Pass_File else cd $Path &&\ rsync -az ./ --delete rsync_backup@$Ip::backup $Pass_File done EOF
|
2.4 编写自启动脚本
脚本编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| cat >/etc/init.d/syncd <<"EOF"
. /etc/init.d/functions
if [ $# -ne 1 ];then usage: $0 {start|stop} exit 1 fi
case "$1" in start) /bin/bash /server/scripts/inotify_rsync.sh & echo $$ >/var/run/inotify.pid if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then action "inotify service is started" /bin/true else action "inotify service is started" /bin/false fi ;; stop) kill -9 `cat /var/run/inotify.pid` >/dev/null 2>&1 pkill inotifywait sleep 2 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then action "inotify service is stopped" /bin/true else action "inotify service is stopped" /bin/false fi ;; *) usage: $0 {start|stop} exit 1 esac EOF
|
设置开机启动
1 2 3 4
| chkconfig --list syncd on chmod +x /etc/init.d/syncd /etc/init.d/syncd start ps -ef|grep inotify
|
通过编写启动脚本,将实时同步做成随机启动
3 inofity命令介绍
3.1 inotifywait重要参数说明[红色]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| -m 始终保持事件监听状态 -r 进行递归监控 -q 简化打印信息,只打印监控时间的信息 -d 后台运行 -e 监控的事件,例如增加、修改、删除等 access 访问 modify 修改 attrib 属性变化 close_write 关闭并有写入 close_nowrite 关闭并没有写入 close 关闭(不管是否写入) open 打开 moved_to 移动(到哪里) moved_from 移动(从哪来) move 移动(所有) create 创建 delete 删除 delete_self 自身目录被删除 unmount 卸载 --format 输出格式,常用'%w%f' --timefmt 输出时间格式,常用'%d%m%y %H:%M'
|
3.2 常用命令组合
1
| inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /data
|
4 inotify调优和总结
4.1 参数文件说明
1 2 3 4 5 6
| ls /proc/sys/fs/inotify/ max_queued_events max_user_instances max_user_watches
max_user_watches: 单用户最大可监视的文件数量(默认8192) max_user_instances: 单用户最大进程数(默认128) max_queued_events: 最大的时间队列(默认16384)
|
4.2 调优方法
调优就是在同步数据量很大时(大并发),调大后两个参数的值
1 2
| echo 655350 >/proc/sys/fs/inotify/max_user_watches echo 655350 >/proc/sys/fs/inotify/max_queued_events
|
并将以上数据写入rc.local中,实现重启不失效(注意规范,要写注释)
4.3 inotify优缺点
经过压力测试,每秒200个(10-100K)文件并发,数据同步几乎无延迟(小于1秒)
- 优点:
监控文件系统时间变化,通过同步工具实现实时数据同步
- 缺点:
并发量大于200个文件(10-100k),同步会有延迟
单线程模式,并发效率低