利用rsync跟inotify实现两台服务器的文件实时同步

数据服务器:192.168.1.239
备份服务器:192.168.1.238

备份服务器安装rsync
tar -zxvf rsync-3.0.9.tar.gz 
cd rsync-3.0.9
./configure
make && make install
备份服务器rsync配置
mkdir /etc/rsyncd
touch /etc/rsyncd/rsyncd.conf
##创建软链接
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf 

vi /etc/rsyncd.conf

uid = root 
gid = root 
use chroot = no 
max connections = 10 
strict modes = yes 

#可以空格,允许多个 
hosts allow = 192.168.1.239 

#默认开启端口 
port = 873  

pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log

#认证模块名 
[inotify] 

#监控目录 
path = /opt/efile/

#此为注释信息,便于识别,可自定义 
comment = Hello My Dear! 
ignore errors 
read only = no 
write only = no 

#此为数据服务器(client)IP地址 
hosts allow = 192.168.1.239

hosts deny = * 
list = false 
uid = root 
gid = root 

#赋予权限的用户,此处我用的是root用户 
auth users = root 

#密码文件
secrets file = /etc/rsyncd/rsync.password 

生成密钥文件

echo "root:root123"> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password 

启动rsync

rsync --daemon
数据服务器

重复备份服务器配置rsyncd

注意秘钥生成,数据服务器只需要设置跟备份服务器的一样的密码即可,不需要设置用户名

echo "root123"> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password 
安装inotify-tools
tar zxvf inotify-tools-3.14.tar.gz  
cd inotify-tools-3.14 
./configure --prefix=/usr/local/inotify
make  
make install
编写inotify同步脚本
#!/bin/bash 


step=10 #间隔的秒数,不能大于60  
  
for (( i = 0; i < 60; i = 1 )); do
   rm -rf /opt/efile/nohup.out
   rm -rf /tmp/rsync.log
   host=192.168.1.238
   #server的ip(备份服务器)
   src=/opt/efile/
   #所要监控的备份目录(此处可以自定义,但是要保证存在)
   des=inotify
   #自定义的模块名,需要与client端定义的一致
   password=/etc/rsyncd/rsync.password
   #密码文件
   user=root
   #用户名(一个实际存在的账号)
   inotify=/usr/local/inotify
   #inotify的安装目录
   ${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src  while read files do
   rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
   echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
done  
  
exit 0 

在后台运行inotify脚本

nohup ./inotify.sh  -log=stdout &
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值