Rsync + Sersync 服务
1. 方案目标
提供一种近实时的文件同步机制,将源服务器(通常是应用服务器)上的文件变化快速同步到备份服务器或多个目标服务器。
2. 核心组件功能
- Rsync (Remote Sync):
- 功能: 高效的增量文件传输工具,支持本地/远程同步,通过校验算法仅传输差异部分。
- 角色: 在本方案中作为同步执行引擎(通常运行在目标服务器或源服务器上)。
- Sersync (基于 Inotify):
- 功能: 利用 Linux 内核的
inotify机制,监控指定目录的文件系统事件(如创建、修改、删除)。 - 角色: 在本方案中作为事件监听器(运行在源服务器上),检测到变化后触发 Rsync 进行同步。
- 功能: 利用 Linux 内核的
- 组合优势:
Sersync实时监听变化 +Rsync高效传输 = 低延迟、高性能的文件同步方案。
3. 工作流程
- 监听事件: Sersync 监控源目录的文件变化。
- 触发同步: 一旦检测到变化(如文件修改),Sersync 立即调用预配置的 Rsync 命令。
- 增量传输: Rsync 连接到目标服务器,仅传输变化的文件或数据块。
- 同步完成: 目标服务器文件与源服务器保持一致。
安装步骤 (OpenEuler)
1. 安装 Rsync
Rsync 通常系统自带或包含在基础仓库中。确认并安装:
bash
sudo yum install rsync -y
2. 安装 Sersync
Sersync 可能需要从第三方仓库或手动下载安装。这里以手动安装为例(假设下载到 /opt):
bash
# 下载 Sersync (版本可能更新,请替换最新链接)
wget https://example.com/path/to/sersync2.tar.gz -P /opt # 请替换为实际有效的下载链接
# 解压
cd /opt
tar -zxvf sersync2.tar.gz
# 重命名目录(可选)
mv sersync2 /opt/sersync
配置详解
1. Rsync 服务端配置 (目标服务器)
- 配置文件:
/etc/rsyncd.conf - 功能模块定义:
bash
# /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[webdata] # 模块名,客户端同步时指定
path = /data/webapps/ # 同步的目标路径
comment = Web Application Data
read only = no # 允许写入
auth users = rsync_user # 认证用户
secrets file = /etc/rsync.passwd # 密码文件路径
hosts allow = 192.168.1.100 # 允许连接的源服务器IP
- 创建密码文件:
bash
echo "rsync_user:your_strong_password" | sudo tee /etc/rsync.passwd
sudo chmod 600 /etc/rsync.passwd # 关键!设置严格权限
- 启动 Rsync 守护进程:
bash
sudo systemctl start rsyncd
sudo systemctl enable rsyncd
- 防火墙 (如果启用):
bash
sudo firewall-cmd --add-service=rsyncd --permanent
sudo firewall-cmd --reload
2. Sersync 配置 (源服务器)
- 核心配置文件:
/opt/sersync/conf/confxml.xml - 关键配置项:
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="1.0">
<host hostip="localhost" port="8008"></host> <!-- 管理端口 -->
<debug start="false"/> <!-- 调试模式 -->
<fileSystem xfs="false"/> <!-- 是否XFS文件系统 -->
<filter start="true"> <!-- 文件过滤 -->
<exclude expression="(.*)\.tmp"></exclude> <!-- 排除.tmp文件 -->
<exclude expression="(.*)\.log"></exclude> <!-- 排除.log文件 -->
</filter>
<inotify> <!-- 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"/> <!-- 监控修改 (与closeWrite重叠,通常关) -->
</inotify>
<sersync> <!-- Sersync 主配置 -->
<localpath watch="/data/webapps"> <!-- 要监控的本地源路径 -->
<remote ip="192.168.1.200" name="webdata"/> <!-- 目标服务器IP和Rsync模块名 -->
<!-- 可配置多个<remote>实现多目标同步 -->
</localpath>
<rsync> <!-- Rsync 命令参数 -->
<commonParams params="-az"/> <!-- -a归档模式 -z压缩 -->
<auth start="true" users="rsync_user" passwordfile="/etc/rsync.passwd"/> <!-- 认证 -->
<timeout start="true" time="100"/> <!-- 超时设置(秒) -->
<ssh start="false"/> <!-- 是否使用SSH通道 (本方案通常为false,走Rsync协议) -->
</rsync>
<failLog path="/opt/sersync/log/rsync_fail_log.sh"/> <!-- 同步失败日志脚本 -->
<crontab start="false"/> <!-- 是否启用定时全量同步 (本方案实时为主,可关闭) -->
</sersync>
</head>
- 创建 Rsync 密码文件 (源服务器):
bash
echo "your_strong_password" | sudo tee /etc/rsync.passwd # 只包含密码
sudo chmod 600 /etc/rsync.passwd
3. 启动 Sersync (源服务器)
bash
cd /opt/sersync
./sersync2 -d -r -o conf/confxml.xml # -d: 守护进程 -r: 先全量同步一次 -o: 指定配置文件
- 验证进程:
bash
ps -ef | grep sersync
- 查看日志 (可选):
bash
tail -f /opt/sersync/log/rsync_fail_log.sh # 同步失败日志
测试同步
- 在源服务器创建测试文件:
bash
touch /data/webapps/test_sync.txt
- 检查目标服务器:
bash
ls /data/webapps/ # 应能看到 test_sync.txt
关键注意事项
- 目录权限: 确保 Rsync 服务端的目标目录 (
path) 和 Sersync 监控的源目录 (localpath watch) 存在且进程有读写权限。 - 密码安全:
/etc/rsync.passwd文件权限必须为600。 - 防火墙: 确保目标服务器的
873/tcp(Rsync 默认端口) 对源服务器开放。 - SELinux: 如果 OpenEuler 启用 SELinux,可能需要调整策略或暂时设置为
permissive模式测试。 - 日志监控: 定期检查
/opt/sersync/log/rsync_fail_log.sh和/var/log/rsyncd.log以排查问题。