Rsync + Sersync 实时数据同步方案
方案架构[*]源服务器:部署 Sersync(监控文件变化) + Rsync(推送数据)
[*]目标服务器:部署 Rsync Daemon(接收数据)
[*]同步逻辑:源服务器文件变动 → Sersync 实时触发 → Rsync 增量同步至目标服务器
详细实施步骤
一、目标服务器配置(数据接收端)
[*]安装 Rsync
bash
yum install rsync -y # CentOS
apt install rsync -y # Ubuntu
[*]创建 Rsync 配置文件
bash
vim /etc/rsyncd.conf
ini
uid = root
gid = root
use chroot = no
max connections = 2000
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
# 模块名称(客户端同步时指定)
path = /data/backup # 同步目录
comment = Backup Directory
read only = no # 允许写入
auth users = rsync_user # 认证用户
secrets file = /etc/rsync.password # 密码文件
[*]创建认证文件
bash
echo "rsync_user:your_password" > /etc/rsync.password
chmod 600 /etc/rsync.password
[*]创建同步目录
bash
mkdir -p /data/backup
chown -R rsync_user:rsync_user /data/backup
[*]启动 Rsync 守护进程
bash
systemctl start rsyncd
systemctl enable rsyncd
[*]开放防火墙端口
bash
firewall-cmd --add-port=873/tcp --permanent
firewall-cmd --reload
二、源服务器配置(数据发送端)
[*]安装 Rsync
bash
yum install rsync -y # CentOS
apt install rsync -y # Ubuntu
[*]创建 Rsync 密码文件
bash
echo "your_password" > /etc/rsync.password
chmod 600 /etc/rsync.password
[*]测试手动同步
bash
rsync -avz /source/data/ rsync_user@目标服务器IP::data_backup --password-file=/etc/rsync.password
[*]作用:验证配置正确性,确保网络和权限正常。
[*]安装 Sersync
bash
wget https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /opt/
mv /opt/GNU-Linux-x86/ /opt/sersync
[*]修改 Sersync 配置文件
bash
vim /opt/sersync/confxml.xml
xml
# 归档模式(保留属性)
[*]启动 Sersync
bash
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
[*]参数说明:
[*]-d:守护进程模式
[*]-r:启动时先全量同步
[*]-o:指定配置文件
[*]设置开机自启
bash
echo "/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml" >> /etc/rc.local
chmod +x /etc/rc.local
三、验证实时同步
[*]在源服务器创建测试文件
bash
touch /source/data/testfile.txt
[*]检查目标服务器同步结果
bash
ls /data/backup # 应出现 testfile.txt
[*]查看 Sersync 日志
bash
tail -f /opt/sersync/rsync_fail_log.sh # 同步失败日志
四、多维优化与监控
1. 性能优化
[*]调整 inotify 限制(解决监控文件数不足问题):
bash
echo "fs.inotify.max_user_watches=1000000" >> /etc/sysctl.conf
sysctl -p
[*]Rsync 带宽限制(避免影响业务):
xml
2. 高可用方案
[*]双活监控:部署多个 Sersync 进程监控不同目录。
[*]异常重启:添加 crontab 监控进程:
bash
*/5 * * * * pgrep sersync2 || /opt/sersync/sersync2 -d -o /opt/sersync/confxml.xml
3. 安全加固
[*]Rsync 最小权限:
[*]目标服务器使用非 root 用户运行 Rsync。
[*]配置文件设置 uid = rsync_user, gid = rsync_user。
[*]SSH 隧道加密:
xml
4. 故障排查工具
[*]手动触发同步:
bash
/opt/sersync/sersync2 -r -o /opt/sersync/confxml.xml
[*]查看 inotify 事件:
bash
tail -f /proc/sys/fs/inotify/* # 监控事件队列
方案优势
<ol start="1">秒级延迟:Sersync 基于 inotify 内核事件触发,响应速度
页:
[1]