rsync,inotify,sersync(rsync+sersync实时双向同步)

一、简介

rsync:  

        一款linux系统下的数据镜像备份工具,采用C/S模式,可以在不同主机之间进行同步,类似于cp功能,数据的传输借助于ssh服务,以守护进程的方式进型传输。

inotify:

  inotify是Linux内核提供的一个接口,用于监控文件系统事件,如文件的创建、删除、修改等。当监控的目录发生变化时,inotify会触发相应的事件。结合rsync,可以在检测到文件系统变化后立即执行同步操作。

sersync:

        sersync是基于inotify开发的工具,旨在改进inotify的不足。与直接使用rsync+inotify的简单组合不同,sersync优化了同步过程,能够记住哪些具体文件或目录发生了变化,从而让rsync只处理这些变化的部分,而不是每次都全量扫描目录。

二、对比

rsync+inotifyrsync+sersync
优点:组合简单易用,能够实时响应文件系统的变动相比rsync+inotifyrsync+sersync提供了更细粒度的监控和同步,特别是在处理大数据量或频繁变更的场景时,效率更高
缺点:处理大量文件变化时,需要扫描整个目录来找出差异。需要更多的系统资源,并且配置相对复杂
适用环境:适用于个人项目、小规模文件系统的监控适合于数据量较大、文件变化频繁且对同步效率要求高的环境

三、配置rsync+sersync双向实时同步:

1.系统环境:

操作系统:openEuler 22.03 LTS _x86  

服务器IP:host1:192.172.0.253

                    host2:192.172.0.254

2.安装准备:

 配置ssh免密服务:

host1(192.172.0.253):

ssh-keygen -t rsa  
#出现交互式一直回车就好

ssh-copy-id 192.172.0.254
#如果修改过ssh端口,通过-p选项指定端口,交互式过程中第一次输入yes,第二次输入host2主机的密码
#可以通过user@X.X.X.X的形式指定登录用户,不指定默认为本机服务器当前登录用户

host2(192.172.0.254):

ssh-keygen -t rsa  
ssh-copy-id 192.172.0.253
3.安装rsync并配置:

host1(192.172.0.253):

安装rsyncd

yum -y install rsyncd

修改配置文件(/etc/rsyncd.conf)内容:

uid = root
gid = root
user chroot = no
address = 192.172.0.253
port = 873
host allow = 192.172.0.0/24

max connections = 36000
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[backup]
path = /opt/test
ignore errors = yes
read only = no
auth users = backup
secerts file = /etc/rsync1.passwd

创建密码文件并赋权:

echo "backup:123456" > /etc/rsync1.passwd
chmod 600 /etc/rsync1.passwd

host2(192.172.0.254):

安装rsyncd

yum -y install rsyncd

修改配置文件(/etc/rsyncd.conf)内容:

uid = root
gid = root
user chroot = no
address = 192.172.0.254
port = 873
host allow = 192.172.0.0/24

max connections = 36000
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[backup]
path = /opt/test
ignore errors = yes
read only = no
auth users = backup
secerts file = /etc/rsync2.passwd

创建密码文件并赋权:

echo "backup:123123" > /etc/rsync2.passwd
chmod 600 /etc/rsync1.passwd
4.启动rsync并验证:

首先启动两台主机的rsync并加入开机自启

systemctl start rsyncd && systemctl enable rsyncd

开始验证:

host1(192.172.0.253)上执行:

cd /opt/test
touch {1..5}.file
echo "123123" > /etc/rsync2.passwd
chmod 600 /etc/rsync2.passwd 
cd /opt/test/
rsync -artuz -R --delete ./ backup@192.172.0.254::backup --password-file=/etc/rsync2.passwd

此时查看host2(192.172.0.254)/opt/test/目录,应该有5个file文件

同样在host2(192.172.0.254)上执行:

cd /opt/test
touch {1..5}.txt
echo "123456" > /etc/rsync1.passwd
chmod 600 /etc/rsync1.passwd 
cd /opt/test/
rsync -artuz -R --delete ./ backup@192.172.0.253::backup --password-file=/etc/rsync1.passwd

此时查看host2(192.172.0.254)/opt/test/目录,应该有5个test文件

5.安装sersync并配置:

官网: https://code.google.com/archive/p/sersync/https://code.google.com/archive/p/sersync/

下载链接: 

 https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

在两台服务器上执行:

wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz 
mv GNU-Linux-x86/ /usr/local/sersync 
ln -s /usr/local/sersync/sersync2 /usr/bin/
cd /usr/local/sersync
mv confxml.xml confxml.xmlbak
cp confxml.xmlbak confxml.xml

host1(192.172.0.253)上编辑confxml.xml,修改下部分内容:

 <sersync>
	<localpath watch="/opt/test">
	    <remote ip="192.172.0.254" name="backup"/>  
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>
	    <auth start="start" users="backup" passwordfile="/etc/rsync2.passwd"/> 
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>  
	</rsync>       

 在host2(192.172.0.254)上编辑confxml.xml,修改下部分内容:

 <sersync>
	<localpath watch="/opt/test">
	    <remote ip="192.172.0.253" name="backup"/>  
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>
	    <auth start="start" users="backup" passwordfile="/etc/rsync1.passwd"/> 
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>  
	</rsync>       
6.启动sersync并验证:

在两台主机上执行:

sersync2 -r -d -o /usr/local/sersync/confxml.xml

host1(192.172.0.253)上执行:

rm -f /opt/test/*.txt

查看host2(192.172.0.254)/opt/test/目录下此时应该没有之前创建的1-5.txt了

host2(192.172.0.254)上执行:

rm -f /opt/test/*.file

查看host1(192.172.0.253)/opt/test/目录下此时应该没有之前创建的1-5.file了

四、创建检测脚本:

        因为sersync服务一直处于后台启动状态,为保证服务能一直运行,故制作检测脚本并加入定时计划以保证sersync服务能一直运行。

        在两台服务器上:

vim /opt/shell/check_sersync.sh

        内容如下:

#!/bin/bash
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=`ps -ef |grep 'sersync2'|grep -v 'grep'|wc -l`
if [ $status -eq 0 ];then
    $sersync -r -d -o $confxml
else
    exit 0;
fi

添加定时任务:

执行crontab -e添加一行

*/5 * * * * /opt/shell/check_sersync.sh > /dev/null 2>&1

添加开机自启

systemctl start crond && systemctl enable crond

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值