数据摆渡-Centos7-rsync-双向文件同步

数据摆渡系列文章目录

第一篇:CentOs7中基于rsync的双向文件同步
第二篇:Windows中基于GoodSync双向文件同步


一、前言

本篇文章关于,两台服务器需要对不同的文件进行自动同步的解决方案。

二、使用rsync 实现两台服务器文件双向同步

关闸、网关下的,内外网数据摆渡解决方案。

1.服务器说明

主机说明同步文件夹
192.168.0.35外网,需要同步内网的/adapter/data/recv/response//adapter/data/send/request/
192.168.0.37内网,需要同步外网的/adapter/data/send/request//adapter/data/recv/response/

2. rsync 安装(两台服务器)

yum install -y rsync

3.建立 rsync 密码认证文件(两台服务器)

两台主机的rsync用户名和密码建议配置一样,方便配置且不容易搞错

vi /etc/rsyncd.passwd     #内容格式 用户名:密码            rsync:123456
vi /root/rsyncd.passwd    #内容格式 密码                  123456

4.rsync 配置

外网 network

uid = root
gid = root
use chroot = 0
#post rsync使用的端口号
port = 873 
#允许那些IP访问
# hosts allow = 172.16.25.127
hosts allow = * 
max connections = 0 
timeout = 300 
pid file = /var/run/rsyncd.pid
ock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
transfer logging = yes 
syslog facility = local3
#方括号中为模块名称
[network-adapter]
#指定当前模块在rsync服务器上的同步路径
#当前为外网模块,需要同步内网的响应数据
path = /adapter/data/recv/response/
#注释,可以同模块名一样
comment = network-adapter
ignore errors
#是否允许客户端上传文件
read only = no
list = no
#指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
auth users = rsync
#保存密码和用户名文件,需要自己生成
secrets file = /etc/rsyncd.passwd

内网 intranet

uid = root
gid = root
use chroot = 0 
#post rsync使用的端口号
port = 873 
#允许访问的IP
# hosts allow = 172.16.25.127
hosts allow = * 
max connections = 0 
timeout = 300 
pid file = /var/run/rsyncd.pid
ock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
transfer logging = yes 
syslog facility = local3
#方括号中为模块声明,对应命名
#此处为内网模块,需要同步外网的请求数据
[intranet-adapter]
#指定当前模块在rsync服务器上的同步路径
path = /adapter/data/send/request/
#注释,可以同模块名一样
comment = intranet-adapter
ignore errors
#是否允许客户端上传文件
read only = no
list = no
#指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
auth users = rsync
#保存密码和用户名文件,需要自己生成
secrets file = /etc/rsyncd.passwd

5.rsync密码文件赋600权限(两台服务器)

chmod 600 /etc/rsyncd.passwd

chmod 600 /root/rsyncd.passwd

6.rsync 启动(两台服务器)

#以守护进程方式启动rsync服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

7.验证同步

外网 network

   rsync -vzrtopg --delete --progress /adapter/data/send/request/ rsync@192.168.0.37::intranet-adapter --password-file=/root/rsyncd.passwd

内网 intranet

    	rsync -vzrtopg --delete --progress /adapter/data/recv/response/ rsync@192.168.0.35::network-adapter --password-file=/root/rsyncd.passwd

8.停止rsync

ps -ef | grep rsync
kill -9 xxx
rm /var/run/rsync.pid -f

9.编写启动脚本,2秒同步文件

外网 network

while true
do 
   rsync -vzrtopg --delete --progress /adapter/data/send/request/ rsync@192.168.0.37::intranet-adapter --password-file=/root/rsyncd.passwd
 
    sleep 2
done

内网 intranet

#!/usr/bin/bash

while true
do  
    	rsync -vzrtopg --delete --progress /adapter/data/recv/response/ rsync@192.168.0.35::network-adapter --password-file=/root/rsyncd.passwd
	sleep 2
done


10.inotify-tools 方式实现文件同步

当监控到文件夹或者文件发生改变的时候利用rsync进行同步。

安装(两台服务器)

yum install -y inotify-tools

启动脚本

#!/bin/bash
    srcdir=/adapter/approval/recv/response/
  while true
      do
        rsync -vzrtopg --delete --progress ${srcdir} rsync@192.168.0.252::network-adapter --password-file=/root/rsyncd.passwd
      done

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dingwen_blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值