基于rsync daemon 实现 sersync——sersync实现实时数据同步

在这里插入图片描述

1 sersync 介绍

sersync类似于inotify,同样用于监控,但它克服了inotify的缺点.
inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。另外比如:vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的
sersync 优点:

  • sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
  • sersync配置很简单,其中提供了静态编译好的二进制文件和xml配置文件,直接使用即可
  • sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态
  • sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对
    同步失败的文件重新同步
  • sersync不仅可以实现实时同步,另外还自带crontab功能,只需在xml配置文件中开启,即也可以按要求隔一段时间整体同步一次,而无需再额外配置crontab功能
  • sersync 可以二次开发
    sersync项目地址:

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

sersync下载地址:

https://code.google.com/archive/p/sersync/downloads

2 环境配置

2.1 备份主机配置

  • 安装 rsync-daemon rsync
[root@backup-server ~]#dnf -y install rsync-daemon rsync
  • 创建rsync服务器的配置文件
[root@backup-server ~]#vi /etc/rsyncd.conf
uid = root   #提定以哪个用户来访问共享目录,将之指定为生成的文件所有者,默认为nobody
gid = root   #默认为nobody
#port = 874 可指定非标准端口,默认873/tcp
#use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
[backup]  #每个模块名对应一个不同的path目录,如果同名后面模块生效
path = /data/backup/  
comment = backup dir
read only = no     #默认是yes,即只读
auth users = rsyncuser  #默认anonymous可以访问rsync服务器
secrets file = /etc/rsync.pas
  • 服务器端准备目录
[root@backup-server ~]#mkdir -pv /data/backup
  • 服务器端生成验证文件
[root@backup-server ~]#echo "rsyncuser:123456" > /etc/rsync.pas
[root@backup-server ~]#chmod 600 /etc/rsync.pas
  • 服务器端启动rsync服务
[root@backup-server ~]#rsync --daemon
[root@backup-server ~]#systemctl start rsyncd 

2.2 数据主机配置

  • 在数据服务器上下载sersync,并拷贝至相应的目录,设置PATH变量
[root@data-centos8 ~]#wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.
gz
[root@data-centos8 ~]#tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@data-centos8 ~]#cp -a GNU-Linux-x86 /usr/local/sersync
[root@data-centos8 ~]#echo 'PATH=/usr/local/sersync:$PATH' > 
/etc/profile.d/sersync.sh
[root@data-centos8 ~]#source /etc/profile.d/sersync.sh
  • 确认安装rsync客户端工具
[root@data-centos8 ~]#rpm -q rsync &> /dev/null || dnf -y install rsync 
  • 备份sersync配置文件
[root@data-centos8 ~]#cp /usr/local/sersync/confxml.xml{,.bak}
  • 修改sersync配置文件
[root@data-centos8 ~]#vim /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   <host hostip="localhost" port="8008"></host>
   <debug start="false"/> # 是否开启调试模式
   <fileSystem xfs="false"/> 
   <filter start="false"> #不开启文件过滤功能,当为true时,以下类型的文件将不同
步
 <exclude expression="(.*)\.svn"></exclude>
 <exclude expression="(.*)\.gz"></exclude>
 <exclude expression="^info/*"></exclude>
 <exclude expression="^static/*"></exclude>
   </filter>
   <inotify> # 监控事件,默认监控
delete/close_write/moved_from/moved_to/create folder
 <delete start="true"/>
 <createFolder start="true"/>
 <createFile start="false"/>
 <closeWrite start="true"/>
 <moveFrom start="true"/>
 <moveTo start="true"/>
 <attrib start="true"/>  #修改此行为true,文件属性变化后也会同步
 <modify start="false"/>
   </inotify>
   <sersync>  # rsync命令的配置段
 <localpath watch="/data/www"> #修改此行,需要同步的源目录或文件,建议同步目
录
   <remote ip="192.168.100.18" name="backup"/>  #修改此行,指定备份服务器地址和rsync 
daemon的模块名,如果下面开启了ssh start,此时name为远程shell方式运行时的目标目录
   <!--<remote ip="192.168.8.39" name="tongbu"/>--> 
   <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync> 
   <commonParams params="-artuz"/>  # 指定rsync选项
   <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/> #修
改此行为true,指定备份服务器的rsync配置的用户和密码文件
   <userDefinedPort start="false" port="874"/><!-- port=874 -->#指定rsync的非
标准端口号
   <timeout start="false" time="100"/><!-- timeout=100 -->
   <ssh start="false"/> #默认使用rsync daemon运行rsync命令,true为使用远程shell模
式
 </rsync>
 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 
60mins execute once-->                  #错误重传及日志文件路径
 <crontab start="false" schedule="600"><!--600mins--> #不开启crontab功能
   <crontabfilter start="false">  #不开启crontab定时传输的筛选功能
 <exclude expression="*.php"></exclude>
 <exclude expression="info/*"></exclude>
   </crontabfilter>
 </crontab>
 <plugin start="false" name="command"/>
   </sersync>
   #####################################以下行不需要修改
####################################
   <plugin name="command">
 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix 
/opt/tongbu/mmm.sh suffix-->
 <filter start="false">
   <include expression="(.*)\.php"/>
   <include expression="(.*)\.sh"/>
 </filter>
   </plugin>
   <plugin name="socket">
 <localpath watch="/opt/tongbu">
   <deshost ip="192.168.138.20" port="8009"/>
 </localpath>
   </plugin>
   <plugin name="refreshCDN">
 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
   <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx"
passwd="xxxx"/>
   <sendurl base="http://pic.xoyo.com/cms"/>
   <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-
9]*).xoyo.com/images"/>
 </localpath>
   </plugin>
</head>
  • 创建连接rsynd服务器的用户密码文件,并必须修改权限
[root@data-centos8 ~]#echo 123456 > /etc/rsync.pas
 [root@data-centos8 ~]#chmod 600 /etc/rsync.pas
  • 以后台方式执行同步
 [root@data-centos8 ~]#sersync2 -dro /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsyncuser
passwordfile is 	/etc/rsync.pas
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www && rsync -artuz -R --delete ./ rsyncuser@192.168.100.38::backup --password-file=/etc/rsync.pas >/dev/null 2>&1 
run the sersync: 
watch path is: /data/www

3 测试

  • 生成1G的测试文件进行测试
    在这里插入图片描述
  • 删除文件进行测试
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_43555873

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

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

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

打赏作者

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

抵扣说明:

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

余额充值