Rsync+inotify做文件同步

Rsync+inotify做文件同步

 

 

服务端配置

1.到http://rsync.samba.org下载最新版本3.0.7

2.解压缩

3.编译

sh#  ./configure –prefix=/usr/local/rsync && make && make install


4.配置/etc/rsyncd.conf

uid=root
gid=root
hosts allow=192.168.61.113,192.168.61.195 #允许访问的主机
#hosts deny=0.0.0.0/32
use chroot=no
max connections=10
secrets file=/etc/rsyncd.pas #用户名密码文件
motd file=/etc/rsyncd.motd
pid file =/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log

[test] #模块1
path=/tongbu1
comment=rsync files
ignore errors
read only=no
list=no
auth users=root
#secrets file=/etc/rsyncd.pas
pid file=/var/run/rsyncd.pid
log file=/var/log/rsyncd.log
lock file=/var/run/rsyncd.lock


[test1] #模块2
path=/tongbu2
comment=rsync files
ignore errors
read only=no
list=no
auth users=root
#secrets file=/etc/rsyncd.pas
pid file=/var/run/rsyncd.pid
log file=/var/log/rsyncd.log
lock file=/var/run/rsyncd.lock

...#可以配置更多的模块

5.配置/etc/rsyncd.pas
root:000000
格式为:用户名:密码,可以设置多个用户密码

6.修改rsyncd.pas的文件属性

sh#  chmod 0600 /etc/rsyncd.pas 

7.启动rsync,默认端口为873

sh# /usr/local/rsync/bin/rsync --daemon --port=873

客户端配置

1.配置/etc/rsyncd_clinet.pas
000000
格式为:密码

2.修改rsyncd_client.pas的文件属性

sh#  chmod 0600 /etc/rsyncd.pas 

配置自动同步inotify

为能在shell下使用inotify特性,需要安装inotify-tools
下载地址:http://inotify-tools.sourceforge.net/
编译安装
./configure
make
make install
完成后,注意查看manpage,man inotify 、 man inotifywait
inotifywait 仅执行阻塞,等待 inotify 事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在 shell 脚本中使用 inotifywait。
inotifywatch 收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。

gedit /opt/inotify-rsync.sh

#!/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]='/tongbu,192.168.61.195,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 [mailto:root@$host::$module root@$host::$module]"
     cmd="rsync -avz --progress --delete --password-file=/etc/rsyncd-clinet.pass $dir [mailto:root@$host::$module root@$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 [mailto:root@$host::$module root@$host::$module]"
     cmd="rsync -avz --progress --delete --password-file=/etc/rsyncd-clinet.pass $dir [mailto:root@$host::$module root@$host::$module]"
      echo $cmd
     $cmd
    fi
    ;;
  esac
 done &

done 

测试

1.启动客户端服务:

sh#  /usr/local/rsync/bin/rsync --daemon --port=873  

2.启动监控脚本,此脚本支持多目录多服务器监控

sh#  ./inotify-rsync.sh 

    3.增加文件

sh#  touch happy.txt 

4.查看/tongbu1、/tongbu2中文件变化

<!-- end content -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值