读书人

linux 利用inotify-tools配合rsync实时

发布时间: 2013-01-28 11:49:56 作者: rapoo

linux 利用inotify-tools配合rsync实时同步文件

1、安装inotify-tools 工具

? ? ??为能在shell下使用inotify特性,需要安装inotify-tools

? ? ? 下载地址:http://inotify-tools.sourceforge.net/
? ? ? 编译安装
? ? ? ? ? ./configure
? ? ? ? ? make
? ? ? ? ? make install

?

2、编写自动监听脚本?

? ?

host=`echo $item | awk -F"," '{print $2}'`
module=`echo $item | awk -F"," '{print $3}'`

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' --event CLOSE_WRITE,create,move,delete $dir | while read date time file event
do
#echo $event'-'$file
case $event in
MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
#cmd="rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd-clinet.pass $dir root@$host::$module"
cmd="rsync -vzrtopg --progress --delete $dir @$host::$module"
#echo $cmd
$cmd
fi
;;

MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
#cmd="rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd-clinet.pass $dir root@$host::$module"
cmd="rsync -vzrtopg --progress --delete $dir @$host::$module"
#cho $cmd
$cmd
fi
;;
esac
done &
done

?3、将脚本加入启动项:

? ? ?添加到rc.local中,可在系统启动时,启动监听。

?

读书人网 >UNIXLINUX

热点推荐