rsync+inotify实现文件实时同步

服务器:192.168.5.98
客户端:192.168.5.99
目的是将服务器上的文件一旦有更新就自动同步到客户端上

1:配置客户端:
yum install rsync -y

vi /etc/rsyncd.conf

uid=root 
gid=root   
use chroot=no 
max connections=100   
timeout=600   
pid file=/root/rsyncd.pid    
lock file=/root/rsyncd.lock    
log file=/root/rsyncd.log    
[test]   
path=/test/ 
ignore errors    
read only=no   
list=no   
hosts allow=192.168.5.0/255.255.255.0    
auth users=testuser   
secrets file=/root/www1.pwd

echo “ testuser:password” >/root/www1.pwd
chmod 600 /root/www1.pwd
然后在客户端上运行rsync

/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

mkdir /test


2:配置服务器rsync


yum install rsync —y

服务器的rsync不需要配置,只需要创建一个客户端的密码文件 里面只填写客户端的密码

echo “ password” >/root/www1.pwd
chmod 600 /root/www1.pwd
mkdir /test

3.配置服务器的inotify

看系统是否支持inotify 
ls /proc/sys/fs/inotify

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

然后写脚本
vi /root/tongbu.sh

#!/bin/bash

host1=192.168.5.99

src=/test/     

des1=test 

user1=testuser 

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' -e close_write,delete,create,attrib ${src} | while read file  

do  

rsync -vzrtopg --delete --progress ${src} ${user1}@${host1}::${des1} --password-file=/root/www1.pwd 

#echo "${files} was rsynced" >> /tmp/rsync.log 2>&1  

#echo "---------------------------------------------------------------------------"  

done



4:测试同步 在服务器上执行


nohup /root/rsync.sh &

 

5:

如果报如下错误:
/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory

解决方法:
ln -sv /usr/local/lib/libinotify* /usr/lib/
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

 

6:rsync排除不需要同步的目录

目录是位于${src}下面的,并且只能用相对路劲
排除单个目录data
rsync -vzrtopg --delete --progress  --exclude 'data' ${src} ${user1}@${host1}::${des1} --password-file=/root/www1.pwd

要排除多个目录
rsync -vzrtopg --delete --progress  --exclude-from '/root/rsyncexclude.txt' ${src} ${user1}@${host1}::${des1} --password-file=/root/www1.pwd

在/root/rsyncexclude.txt里定义要排除的文件夹和文件
data/compile/*
data/dbbackup/*
data/pay_cache/*
data/tmp/index.php 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值