千家信息网

Linux中sersync数据实时同步的方法

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍了Linux中sersync数据实时同步的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。sersync其实是利用i
千家信息网最后更新 2026年11月04日Linux中sersync数据实时同步的方法

这篇文章主要介绍了Linux中sersync数据实时同步的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

sersync其实是利用inotify和rsync两种软件技术来实现数据实时同步功能的,inotify是用于监听sersync所在服务器上的文件变化,结合rsync软件来进行数据同步,将数据实时同步给客户端服务器。

工作过程:在同步主服务器上开启sersync,负责监听文件系统的变化,然后调用rsync命令把更新的文件同步到目标服务器上,主服务器上安装sersync软件,目标服务器上安装rsync服务。

1、客户端配置

[root@localhost2 ~]# cat /etc/rsyncd.conf ##created by cai at 2018-2-24uid=rsyncgid=rsyncuse chroot = nomax connections = 200timeout = 300pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[data]path = /data/ignore errorsread only = falselist = falsehosts allow = 192.168.181.128:52000/24hosts deny = 0.0.0.0/32auth users = rsync_bodysecrets file = /etc/rsync.password[root@localhost2 ~]# ls -ld /data/drwxrwxrwx. 3 rsync rsync 4096 Feb 24 16:58 /data/[root@localhost2 ~]# cat /etc/rsync.password rsync_body:admin[root@localhost2 ~]# ls -ld /etc/rsync.password -rw-------. 1 root root 17 Feb 24 16:26 /etc/rsync.password  #600权限[root@localhost2 ~]# netstat -lntup| grep "rsync"tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1497/rsync tcp 0 0 :::873 :::* LISTEN 1497/rsync

2、主服务器配置

[root@localhost1 ~]# cat /etc/rsync.password admin[root@localhost1 ~]# ls -ld /etc/rsync.password -rw------- 1 root root 6 Feb 24 03:54 /etc/rsync.password

3、安装sersync服务

采用inotify来对文件进行监控,当监控到文件有文件发生改变的时候,就会调用rsync实现触发式实时同步!

安装sersync(注意sersync是工作在rsync的源服务器上,也就是客户端上)

[root@salt-client01 ~]# cd /usr/local/src/[root@salt-client01 src]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/GNU-Linux-x86/GNU-Linux-x86/sersync2GNU-Linux-x86/confxml.xml[root@salt-client01 src]# cd /usr/local/[root@salt-client01 local]# mv GNU-Linux-x86 sersync[root@salt-client01 local]# cd sersync/[root@salt-client01 sersync]# mkdir conf bin log[root@salt-client01sersync]# mv confxml.xml conf[root@salt-client01sersync]# mv sersync2 bin/sersync

修改配置文件

***********************************30行开始******************************   #-artuz为rsync同步时的参数            ************************************第36行***********************************        *******************************************************************************    *注:若有多个目录备份可以穿件多个配置文件在启动时的-o参数中添加即可[root@salt-client01 conf]# diff confxml.xml confxml.xml.bak24,25c24,25<     #data就是本地需要同步的文件夹到服务器端的目录<      #data (server的模块名)是rsync 服务端的文件夹,也就是推送到服务器端的目标文件夹,可以配置多个,--->   >     31c31<       #true 才能生效,rsync_body同步时候虚拟账号,后面是密码文件--->     33c33<               #true 才能生效--->     36c36<    #检测rsync进程判断,没有自动启--->   

启动sersync

[root@salt-client01 src]# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile #声明环境变量[root@salt-client01 src]# source /etc/profile[root@salt-client01 src]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml #启动set the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -r   rsync all the local files to the remote servers before the sersync workoption: -d   run as a daemonoption: -o   config xml name: /usr/local/sersync/conf/confxml.xmldaemon thread num: 10parse xml config filehost ip : localhost  host port: 8008daemon start,sersync run behind the consoleuse rsync password-file :user is  rsync_bodypasswordfile is   /etc/rsync.passwordconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync 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 onceworking please wait...execute command: cd /data && rsync -artuz -R --delete ./ --timeout=100 rsync_body@192.168.91.166::data --password-file=/etc/rsync.password >/dev/null 2>&1run the sersync:watch path is: /data #此时可以看出sersync已经启动成功了#检测脚本[root@salt-client01 log]# pwd/usr/local/sersync/log[root@salt-client01 log]# vim rsync_fail_log.sh[root@salt-client01 log]# chmod +x rsync_fail_log.sh[root@salt-client01 ~]# cat /usr/local/sersync/log/rsync_fail_log.sh#!/bin/bash#Purpose: Check sersync whether it is alive#Author: cai meng zhiSERSYNC="/usr/local/sersync/bin/sersync2"CONF_FILE="/usr/local/sersync/conf/confxml.xml"STATUS=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)if [ $STATUS -eq 0 ];then    $SERSYNC -d -r -o $CONF_FILE &else    exit 0;fi脚本写好以后,添加到计划任务中去*/1 * * * * /bin/bash /usr/local/sersync/log/rsync_fail_log.sh > /dev/null 2>&1测试同步:客户端新增文件[root@salt-client01 data]# cp /etc/passwd 192.168.91.156.passwd[root@salt-client01 data]# lltotal 4-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd-rw-r--r-- 1 root root  0 Nov 10 17:27 3服务端检测[root@salt-master data]# cd /data/[root@salt-master data]# lltotal 8-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd #说明已经同步过来了-rw-r--r-- 1 root root  0 Nov 10 17:27 3drwxr-xr-x 2 root root 4096 Nov 10 17:27 data客户端测试删除[root@salt-client01 data]# rm rf 192.168.91.156.passwd rm: cannot remove `rf': No such file or directoryrm: remove regular file `192.168.91.156.passwd'? y[root@salt-client01 data]# lltotal 0-rw-r--r-- 1 root root 0 Nov 10 17:27 3[root@salt-client01 data]#服务器端:[root@salt-master data]# lltotal 4-rw-r--r-- 1 root root  0 Nov 10 17:27 3  #说明已经删除掉了drwxr-xr-x 2 root root 4096 Nov 10 17:27 data

4、常见错误汇总

错误一:@ERROR: auth failed on module xxxxxrsync: connection unexpectedly closed(90 bytes read so far)rsync error: error in rsync protocoldata stream (code 12) at io.c(150)说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。 错误二:password file must not beother-accessiblecontinuing without password filePassword:说明:这是因为rsyncd.pwdrsyncd.sec的权限不对,应该设置为600。如:chmod600 rsyncd.pwd 错误三:@ERROR: chroot failedrsync: connection unexpectedly closed(75 bytes read so far)rsync error: error in rsync protocoldata stream (code 12) at io.c(150)说明:这是因为你在 rsync.conf中设置的 path 路径不存在,要新建目录才能开启同步 错误四:rsync: failed to connect to218.107.243.2: No route to host (113)rsync error: error in socket IO (code10) at clientserver.c(104) [receiver=2.6.9]说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致 错误五:@ERROR: access denied to www fromunknown (192.168.1.123)rsync: connection unexpectedly closed(0 bytes received so far) [receiver]rsync error: error in rsync protocoldata stream (code 12) at io.c(359)说明:此问题很明显,是配置选项hostallow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致 错误六:rsync error: received SIGINT,SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]rsync error: received SIGUSR1 (code19) at main.c(1182) [receiver=2.6.9]说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器错误七:rsync: read error: Connection resetby peer (104)rsync error: error in rsync protocoldata stream (code 12) at io.c(604) [sender=2.6.9]说明:原数据目录里没有数据存在

感谢你能够认真阅读完这篇文章,希望小编分享的"Linux中sersync数据实时同步的方法"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

服务 同步 文件 服务器 错误 数据 配置 实时 客户 客户端 问题 目录 篇文章 方法 多个 文件夹 目标 端的 脚本 软件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 广州紫鲸互联网科技有限公司 科技互联网产品 华宇互联网科技 狮子座互联网科技 互联网金融 科技创新 互联网科技公司名称 温州喔噻互联网科技有限公司 互联网科技寓意 荆州市昕露互联网络科技有限公司 互联网科技公司有风险吗 崇明区互联网教育科技售后服务 移动互联网年度科技创新 未来五年的互联网科技 深圳市优智互联网科技有限公司 阿里互联网科技企业 国大互联网教育科技有限公司薪资 中天科技中国工业互联网 富国互联网科技002692 山东亿川互联网科技有限责任公司 嘟嘟瓜互联网科技 紫鲸互联网科技 深圳天众互联网科技公司 联恩商钥互联网科技有限公司 顺网科技2021年互联网排名 山东吉时兔互联网科技有限公司 杭州壁岐互联网科技有限公司 互联网保险科技创业 世界互联网科技成果奖 中国新注册互联网科技
0