rsync+sersync实时同步目录和文件
0、服务器准备
0.1、三台服务器:
rsync:192.168.1.9
sersync:192.168.1.10、192.168.1.11
0.2、部署这套服务的背景
将多个子公司的备份数据实时同步到备份服务器上,rsync作为备份服务器,sersync部署在各个子公司上,只需将各个子公司服务器上需要备份的数据拷贝到/data/sersync/tongbu目录即可实时同步到rsync上对应的/data/rsync/data/zgjt、zggy目录上
0.3、服务器以及对应同步目录
sersync服务器 | 192.168.1.10 | 源目录:/data/sersync/tongbu | rsync服务器 | 192.168.1.9 | 目标目录:/data/rsync/data/zgjt |
sersync服务器 | 192.168.1.11 | 源目录:/data/sersync/tongbu | rsync服务器 | 192.168.1.9 | 目标目录:/data/rsync/data/zggy |
1、rsync部分
# 下载rsync服务
yum install rsync
1.1、配置 rsync 服务端
在源服务器上,配置 rsync 服务端。编辑 /etc/rsyncd.conf 文件,创建并配置一个模块,模块定义了同步的规则。
[root@rl--0001 rsync]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: uid = root gid = root use chroot = no # 安全相关 max connections = 2000 # 并发连接数 timeout = 600 # 超时时间(秒) pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false #hosts allow = 172.16.4.0/23 #hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password #需要创建对应的用户密码文件,并放置到指定目录,我这里是在/etc [zg] comment = zg path = /data/rsync/data/zgjt [zggy] comment = zggy path = /data/rsync/data/zggy
1.2、设置远程同步认证文件(需要和上边rsyncd.conf配置文件中的auth users、secrets file 两项保持一致)
echo "rsync_backup:Hik@123" | sudo tee /etc/rsync.passwordchmod 600 /etc/rsync.password
1.3、启动脚本(也可以添加crontab,做守护脚本)
[root@rl--0001 rsync]# cat rc_daemon.sh #!/bin/bash rsync_port=873 # 修改为你的 rsync 端口号 log_file="/var/log/rsync_check.log" # 修改为你希望保存日志的文件路径 pid_file="/var/run/rsyncd.pid" # 获取当前时间的时间戳 timestamp=$(date +"%Y-%m-%d %H:%M:%S") # 使用 ps 命令检测 rsync 进程,并使用 nc 命令检测 rsync 的端口 if ps aux | grep -q "[r]sync.*--daemon"; then echo "$timestamp: rsync process and port are both running. No action needed." | tee -a $log_file else echo "$timestamp: rsync process or port is not running. Restarting rsync..." | tee -a $log_file # 停止 rsync 进程(如果存在) pkill -f "[r]sync.*--daemon" # 删除旧的 PID 文件 rm -f "$pid_file" # 启动 rsync(在这里修改为你的 rsync 启动命令,包括端口参数) /usr/bin/rsync --daemon --port=$rsync_port echo "$timestamp: rsync has been restarted." | tee -a $log_file fi
2、sersync安装
2.1、下载sersync安装包
wget https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz mv /data/GNU-Linux-x86 /data/sersync
2.2、confxml.xml 配置文件说明
<inotify>部分
这部分定义了同步时的策略,包含目录和文件删除、创建、属性变化等
<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> 这是 inotify 部分的配置,用于指定 sersync 监听的不同文件系统事件。下面是每一行的含义: <delete start="true"/>: 文件或目录被删除。 <createFolder start="true"/>: 目录被创建。 <createFile start="true"/>: 文件被创建。 <closeWrite start="true"/>: 文件被关闭以进行写入。 <moveFrom start="true"/>: 文件或目录从监控的目录中移出。 <moveTo start="true"/>: 文件或目录被移动到监控的目录中。 <attrib start="true"/>: 文件属性发生变化。 <modify start="true"/>: 文件内容被修改。
<localpath> 部分:
这部分定义了本地监控的路径为 /data/sersync/tongbu,并指定了一个远程服务器 18.0.141.70 作为同步目标,使用名称 "zcdl" 标识。这意味着 /data/sersync/tongbu 中的文件更改将实时同步到远程服务器
<localpath watch="/data/sersync/tongbu"> <remote ip="18.0.141.70" name="zcdl"/> </localpath>
<rsync> 部分
这是 rsync 相关的配置,指定了 rsync 的参数(-artuz),启用了身份验证,指定了用户名为 "rsync_backup",并指定了密码文件的路径。此外,定义了用户自定义的端口、超时时间等。
<rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="true" port="873"/> <timeout start="true" time="100"/> <ssh start="false"/> </rsync>
<failLog> 部分
这一部分定义了一个日志文件路径,用于记录同步失败的情况,并且配置了每隔 60 分钟执行一次
<failLog path="/data/sersync/rsync_fail_log.sh" timeToExecute="60"/>
<crontab> 部分:
这一部分定义了一个 cron 任务,但目前是禁用状态。如果启用,将每隔 600 分钟执行一次。其中,还定义了一个过滤器,排除了匹配 *.php 和 info/* 表达式的文件。
<crontab start="false" schedule="600"> <crontabfilter start="false"> <exclude expression="*.php"/> <exclude expression="info/*"/> </crontabfilter> </crontab>
<plugin> 部分:
这一部分定义了一个插件,使用了命令行插件 (name="command"),并指定了一些参数。插件配置中还有一个过滤器,用于包含特定的文件。
<plugin start="false" name="command"> <param prefix="/bin/sh" suffix="" ignoreError="true"/> <filter start="false"> <include expression="(.*)\.php"/> <include expression="(.*)\.sh"/> </filter> </plugin>
2.3、confxml.xml配置文件展示
[root@rl--0002 sersync]# cat 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"> <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="false"/> <modify start="false"/> </inotify> <sersync> <localpath watch="/data/sersync/tongbu"> <remote ip="192.168.1.9" name="zg"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="true" port="873"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/data/sersync/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>
2.4、配置远程同步认证文件(此文件的密码需要和rsync服务器上的/etc/rsync.password文件中的密码保持一致,但是这块是不需要用户的)
echo "Hik@123" > /etc/rsync.passwordchmod 600 /etc/rsync.password
2.5、sersync启动(也可以结合crontab做守护脚本)
[root@rl--0002 sersync]# cat sc_daemon.sh #!/bin/bash log_file="/var/log/sersync2_check.log" # 修改为你希望保存日志的文件路径 sersync2_executable="/data/sersync/sersync2" # sersync2 可执行文件的路径 confxml_file="/data/sersync/confxml.xml" # sersync2 配置文件的路径 # 获取当前时间的时间戳 timestamp=$(date +"%Y-%m-%d %H:%M:%S") # 检查 sersync2 进程是否在运行 if pgrep -x "sersync2" >/dev/null; then sersync2_pid=$(pgrep -x "sersync2") echo "$timestamp: sersync2 process with PID $sersync2_pid is already running." | tee -a $log_file else echo "$timestamp: sersync2 process is not running. Starting sersync2..." | tee -a $log_file # 启动 sersync2(在这里修改为你的 sersync2 启动命令,包括参数) $sersync2_executable -d -r -o $confxml_file echo "$timestamp: sersync2 has been started." | tee -a $log_file fi
3、测试
在sersync服务器上(192.168.1.10、192.168.1.11)上,将需要同步的文件复制到/data/sersync/tongbu目录下,即可远程同步到备份服务器(192.168.1.9)的对应目录上,怎么对应目录?
3.1 需要在rsync服务器的配置文件rsyncd.conf中申明远程名称以及目录,在sersync的config.xml文件中,申明rsync的远程名称即可
rsync:
[zg] #对应sersync的name comment = zg path = /data/rsync/data/zgjt
sersync:
<sersync> <localpath watch="/data/sersync/tongbu"> <remote ip="192.168.1.9" name="zg"/> #这块就对应rsync上的[zg] <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="true" port="873"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync>
至此就部署完成了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探