一、介绍
Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。
二、详细部署步骤
目标功能:
源主机H1: 10.10.1.1
目标主机H2: 10.10.1.123、10.10.1.124、10.10.1.126、10.10.1.125
? 将10.10.1.1 /usr/mpsp/yekeqiang/yekeqiang-manage.war/html目录同步10.10.1.123 /usr/mpsp/yekeqiang/yekeqiang-web.war/html目录
? 将10.10.1.1 /usr/mpsp/yekeqiang/yekeqiang-manage.war/html目录同步10.10.1.124 /usr/mpsp/yekeqiang/yekeqiang-web.war/html目录
? 将10.10.1.1 /usr/mpsp/yekeqiang/yekeqiang-manage.war/yekeqianguserfiles 目录同步10.10.1.126/usr/mpsp/yekeqianguserfiles
? 将10.10.1.1 /usr/mpsp/yekeqiang/yekeqiang-manage.war/yekeqianguserfiles 目录同步10.10.1.125/usr/mpsp/yekeqianguserfiles
当源数据有文件或目录更新时,即时启动rsync同步进程。
################################################################
除inotify-tools(需要2.6.13以上内核的inotify功能支持)以外,其他软件均使用RHEL5系统自带的rpm包安装。
一、配置目标主机
一、配置目标主机10.10.1.123(前台web)
shell> vi /etc/exports
/usr/mpsp/yekeqiang/yekeqiang-web.war/html 10.10.1.1(rw,no_root_squash)
shell> /etc/rc.d/init.d/portmap start
shell> chkconfig portmap
shell>/etc/rc.d/init.d/nfs start
shell> chkconfig nfs
二、配置目标主机10.10.1.124(前台web)
shell> vi /etc/exports
/usr/mpsp/yekeqiang/yekeqiang-web.war/html 10.10.1.1(rw,no_root_squash)
shell> /etc/rc.d/init.d/portmap start
shell> chkconfig portmap
shell>/etc/rc.d/init.d/nfs start
shell> chkconfig nfs
三、配置目标主机10.10.1.126(图片服务器)
shell> vi /etc/exports
/usr/mpsp/yekeqianguserfiles 10.10.1.1(rw,no_root_squash)
shell> /etc/rc.d/init.d/portmap start
shell> chkconfig portmap
shell>/etc/rc.d/init.d/nfs start
shell> chkconfig nfs
四、配置目标主机10.10.1.125(图片服务器)
shell> vi /etc/exports
/usr/mpsp/yekeqianguserfiles 10.10.1.1(rw,no_root_squash)
shell> /etc/rc.d/init.d/portmap start
shell> chkconfig portmap
shell>/etc/rc.d/init.d/nfs start
shell> chkconfig nfs
二、配置源主机10.10.1.1 (上传备份发起端)
1、安装inotify-tools工具包
将inotify-tools-3.13.tar.gz 上传到 /usr/mpsp/soft 目录下
Shell>mkdir -p /usr/mpsp/ inotify-tools
shell> tar -zxvf inotify-tools-3.13.tar.gz
shell> cd inotify-tools-3.13
shell> ./configure --prefix=/usr/mpsp/inotify-tools
shell> make
shell> make install
三、挂载(10.10.1.1)
1、挂载10.10.1.123/152,10.10.1.126/52发布的同步目录
shell> /etc/rc.d/init.d/portmap start
shell> chkconfig portmap
shell>cd /usr/mpsp/yekeqiang
shell> mkdir -p 151-html
shell> mkdir -p 152-html
shell> mkdir -p 51-yekeqianguserfiles
shell> mkdir -p 52-yekeqianguserfiles
shell> vi /etc/fstab
10.10.1.123:/usr/mpsp/yekeqiang/yekeqiang-web.war/html /usr/mpsp/yekeqiang/151-html nfs defaults,noexec 0 0
10.10.1.124:/usr/mpsp/yekeqiang/yekeqiang-web.war/html /usr/mpsp/yekeqiang/152-html nfs defaults,noexec 0 0
10.10.1.126:/usr/mpsp/yekeqianguserfiles /usr/mpsp/yekeqiang/51-yekeqianguserfiles nfs defaults,noexec 0 0
10.10.1.125:/usr/mpsp/yekeqianguserfiles /usr/mpsp/yekeqiang/52-yekeqianguserfiles nfs defaults,noexec 0 0
shell> mount /usr/mpsp/yekeqiang/151-html
shell> mount /usr/mpsp/yekeqiang/152-html
shell> mount /usr/mpsp/yekeqiang/51-yekeqianguserfiles
shell> mount /usr/mpsp/yekeqiang/52-yekeqianguserfiles
四、编写触发同步脚本(10.10.1.1)
为10.10.1.123编写同步脚本
shell> vi /usr/mpsp/inotify-tools/151_inosync.sh
#!/bin/sh
SRC=/usr/mpsp/yekeqiang/yekeqiang-manage.war/html/
DST=/usr/mpsp/yekeqiang/151-html/
INWT=/usr/mpsp/inotify-tools/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
$RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /usr/mpsp/inotify-tools/151_inosync.sh
为10.10.1.124编写同步脚本
shell> vi /usr/mpsp/inotify-tools/152_inosync.sh
#!/bin/sh
SRC=/usr/mpsp/yekeqiang/yekeqiang-manage.war/html
DST=/usr/mpsp/yekeqiang/152-html/
INWT=/usr/mpsp/inotify-tools/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
$RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /usr/mpsp/inotify-tools/152_inosync.sh
为10.10.1.126编写同步脚本
shell> vi /usr/mpsp/inotify-tools/51_inosync.sh
#!/bin/sh
SRC=/usr/mpsp/yekeqiang/yekeqiang-manage.war/yekeqianguserfiles/
DST=/usr/mpsp/yekeqiang/51-yekeqianguserfiles/
INWT=/usr/mpsp/inotify-tools/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
$RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /usr/mpsp/inotify-tools/51_inosync.sh
为10.10.1.125编写同步脚本
shell> vi /usr/mpsp/inotify-tools/52_inosync.sh
#!/bin/sh
SRC=/usr/mpsp/yekeqiang/yekeqiang-manage.war/yekeqianguserfiles/
DST=/usr/mpsp/yekeqiang/52-yekeqianguserfiles/
INWT=/usr/mpsp/inotify-tools/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
$RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /usr/mpsp/inotify-tools/52_inosync.sh
五、启动
Shell>cd /usr/mpsp/inotify-tools/
Shell>./151_inosync.sh & >> /usr/mpsp/inotify-tools/151rc.log
Shell>./152_inosync.sh & >> /usr/mpsp/inotify-tools/152rc.log
Shell>./51_inosync.sh & >> /usr/mpsp/inotify-tools/51rc.log
Shell>./52_inosync.sh & >> /usr/mpsp/inotify-tools/52rc.log