sersync 实现实时数据同步

sersync 是一种基于 Linux 的开源工具,主要用于实时同步文件和文件夹到远程服务器。它使用了 inotify 机制来监控文件系统的变化,并可以通过 rsync 协议或 FTP 来传输文件。sersync 非常适合需要高实时性文件同步的场景,比如网站的镜像备份或者多服务器负载均衡环境下的文件共享。

原理

sersync 结合了两个核心技术:inotifyrsync

  • Inotify 是 Linux 内核的一个特性,能够监控文件系统事件,如文件的创建、修改、删除等。
  • Rsync 是一个文件同步和文件传输程序,可以优化文件数据的传输,只同步变化的部分,减少数据传输量。

sersync 利用 inotify 实时监控指定目录下的文件变化事件,并触发 rsync 命令来同步这些变化到远程系统。这种结合方式使得文件同步几乎实时进行,大大减少了数据的延迟。

基于rsync daemon 实现 sersync

sersync项目路径:https://code.google.com/archive/p/sersync/

image-20240417134427171

备份服务器配置

[root@backup-server ~]# yum -y install rsync-daemon
[root@backup-server ~]# systemctl enable --now rsyncd
[root@backup-server ~]# vim /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
uid = root
gid = root
port = 873
use chroot = yes
max connections = 0
ignore errors
exclude = lost+found/
reverse lookup = no
[backup]
    path = /data/backup
    comment = Backup folder
    read only = no
    list = yes
    auth users = rsyncuser
	secrets file = /etc/rsyncd.secrets
[root@backup-server ~]#
[root@data-server ~]# echo "123456" > /etc/rsyncd.secrets
# 修改权限
[root@data-server ~]# chmod 600 /etc/rsyncd.secrets

数据服务器配置

# 在数据服务器上下载sersync,并拷贝到/usr/local/下,设置环境变量
[09:29:27 root@data-server ~]#tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[09:29:27 root@data-server ~]#mv GNU-Linux-x86/ /usr/local/sersync
[09:33:59 root@data-server ~]#ll /usr/local/sersync
total 1772
-rwxr-xr-x 1 root root    2214 Oct 26  2011 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2
[09:35:21 root@data-server ~]#ln -s /usr/local/sersync/sersync2 /usr/bin/
# sersync底层也是使用inotify和rsync,所以要确保安装rsync客户端工具
[09:35:21 root@data-server ~]#rpm -q rsync &> /dev/null || dnf -y install rsync
#修改sersync配置文件
[15:14:23 root@data-server www]#vi /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="true"/> <!-- 是否开启调试模式 -->
    <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 start="true"/> <!-- 监控删除事件 -->
 <createFolder start="true"/>  <!-- 监控创建文件夹事件 -->
 <createFile start="true"/> <!-- 监控创建文件事件 -->
 <closeWrite start="true"/>
 <moveFrom start="true"/> <!-- 监控文件移动事件 -->
 <moveTo start="true"/> 
 <attrib start="true"/> <!-- 监控文件权限修改事件 -->
 <modify start="true"/> <!-- 监控文件修改事件 -->
    </inotify>

    <sersync>
 <localpath watch="/data/www"> <!-- 监控的本地目录 -->
     <remote ip="10.0.0.107" name="backup"/> <!-- 同步目标的 IP 和名称 -->
     <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync>
     <commonParams params="-az"/> <!-- rsync 通用参数-artuz -->
     <auth start="true" users="rsyncuser" passwordfile="/etc/rsyncd.secrets"/> <!-- rsync 验证用户,以及验证用户密码的存放路径 -->
     <userDefinedPort start="false" port="874"/><!-- port=874 -->
     <timeout start="false" time="100"/><!-- timeout=100 -->
     <ssh start="false"/>
 </rsync>
 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
 <crontab start="false" schedule="600"><!--600mins-->
     <crontabfilter start="false">
   <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>
[15:14:23 root@data-server www]#
# 创建连接rsynd服务器的用户密码文件,并必须修改权限
[15:14:23 root@data-server www]# echo 123456 > /etc/rsyncd.secrets
[15:14:23 root@data-server www]# chmod 600 /etc/rsyncd.secrets

# 查看帮助
[15:14:23 root@data-server www]# sersync2 -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________


连接测试

# 以后台方式启动
[15:14:23 root@data-server www]# sersync2 -dro /usr/local/sersync/confxml.xml
# 以前台方式执行同步,开启debug模式,方便看到结果
[10:50:25 root@data-server www]# sersync2 -ro /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: -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
will ignore the inotify createFile event
Open debug, you will see debug infomation
use rsync password-file :
user is rsyncuser
passwordfile is  /etc/rsyncd.secrets
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 -az -R --delete ./ rsyncuser@10.0.0.107::backup --password-file=/etc/rsyncd.secrets
crontab command:cd /data/www && rsync -az -R --delete ./ rsyncuser@10.0.0.107::backup --password-file=/etc/rsyncd.secrets
run the sersync:
watch path is: /data/www
add watch: /data/www return wd is: 1

# 在测试过程发现当rsync的参数是 -artuz的时候,我创建,删除文件是没有问题的,但是我修改文件权限的时候(chmod/chown)就同步不过去,这是因为u选项是源文件比目标文件新才会拷贝,但是当我们使用chmod修改以后,文件的mtime是不会变的,而是ctime在变,但是rsync只监控mtime,所以不得已把u去掉了,就可以了。
[15:50:13 root@data-server www]#stat b.txt
  File: b.txt
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d  Inode: 33554561    Links: 1
Access: (0000/----------)  Uid: (  100/    xing)   Gid: ( 1000/    xing)
Access: 2024-04-17 10:50:40.288807321 +0800
Modify: 2024-04-17 10:50:40.288807321 +0800
Change: 2024-04-17 10:51:15.206601870 +0800
 Birth: 2024-04-17 10:50:40.288807321 +0800
[15:50:17 root@data-server www]#

#sersync支持多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置文件运行
[root@data-centos8 ~]#sersync2 -rd -o /etc/sersync.d/nginx.xml

# 备份服务器上监控文件变化
[root@backup-server backup]# watch -n0.5 ls -l /data/backup/

基于远程shell 实现 sersync

# 不需要rsync-daemon,只需要配置基于key验证的ssh即可
[15:19:05 root@data-server www]# ssh-keygen
[15:19:05 root@data-server www]# ssh-copy-id 10.0.0.107

# 下载sersync,并拷贝相应目录,配置环境变量,参考上面

# 修改sersync配置文件
[15:19:05 root@data-server www]#cat /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="true"/>
    <fileSystem xfs="false"/>
    <filter start="false">
 <exclude expression="(.*)\.svn"></exclude>
 <exclude expression="(.*)\.gz"></exclude>
 <exclude expression="^info/*"></exclude>
 <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
 <delete start="true"/>
 <createFolder start="true"/>
 <createFile start="true"/>
 <closeWrite start="true"/>
 <moveFrom start="true"/>
 <moveTo start="true"/>
 <attrib start="true"/>
 <modify start="true"/>
    </inotify>

    <sersync>
 <localpath watch="/data/www">
     <remote ip="10.0.0.107" name="/data/backup"/> <!--由于是基于ssh传输,所以这里不写模块名称了,要写真实的目录-->
     <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync>
     <commonParams params="-az"/>
     <auth start="false" users="rsyncuser" passwordfile="/etc/rsyncd.secrets"/> <!--必须修改,不启用认证 start="false" -->
     <userDefinedPort start="false" port="874"/><!-- port=874 -->
     <timeout start="false" time="100"/><!-- timeout=100 -->
     <ssh start="false"/> <!--修改为true,使用远程shell方式的rsync连接方式,无需在目标主机上配置启动rsync-daemon服务-->
 </rsync>
 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
 <crontab start="false" schedule="600"><!--600mins-->
     <crontabfilter start="false">
   <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>

# 测试
[15:19:05 root@data-server www]#sersync2 -dro /usr/local/sersync/confxml.xml

# 备份服务器上监控文件变化
[root@backup-server backup]# watch -n0.5 ls -l /data/backup/
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XingYuyu_Coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值