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

1、安装inotify-tools 工具

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

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

 

2、编写自动监听脚本 

   

shell 写道
#!/bin/bash
###########################
# 在这里配置本地文件夹,目标host,目标的rsync_module。rsync_module在同步机器的/etc/rsyncd.conf文件中配置
# 逗号前后不要有空格
#sync[0]='/tongbu,192.168.61.113,test' # localdir,host,rsync_module
#sync[0]='/tongbu,192.168.61.195,test' # localdir,host,rsync_module
sync[0]='/root,192.168.1.3,test'
#sync[1]='/tongbu,192.168.61.195,test1'
###########################

for item in ${sync[@]}; do

dir=`echo $item | awk -F"," '{print $1}'`
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中,可在系统启动时,启动监听。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值