时间:2025-08-10 14:52
人气:
作者:admin
方案架构
详细实施步骤
一、目标服务器配置(数据接收端)
bash
yum install rsync -y # CentOS
apt install rsync -y # Ubuntu
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
[data_backup] # 模块名称(客户端同步时指定)
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
bash
systemctl start rsyncd
systemctl enable rsyncd
bash
firewall-cmd --add-port=873/tcp --permanent
firewall-cmd --reload
二、源服务器配置(数据发送端)
bash
yum install rsync -y # CentOS
apt install rsync -y # Ubuntu
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
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
bash
vim /opt/sersync/confxml.xml
xml
<!-- 监控目录设置 -->
<localpath watch="/source/data"> <!-- 源服务器需同步的目录 -->
<remote ip="目标服务器IP" name="data_backup"/> <!-- 目标服务器模块名 -->
</localpath>
<!-- Rsync 参数 -->
<rsync>
<commonParams params="-artuz"/> # 归档模式(保留属性)
<auth start="true" users="rsync_user" passwordfile="/etc/rsync.password"/>
<timeout start="false" time="100"/> <!-- 超时设置 -->
</rsync>
bash
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
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
bash
tail -f /opt/sersync/rsync_fail_log.sh # 同步失败日志
四、多维优化与监控
1. 性能优化
bash
echo "fs.inotify.max_user_watches=1000000" >> /etc/sysctl.conf
sysctl -p
xml
<!-- 在 confxml.xml 中增加 -->
<commonParams params="--bwlimit=10240 -artuz"/> <!-- 限速 10MB/s -->
2. 高可用方案
bash
*/5 * * * * pgrep sersync2 || /opt/sersync/sersync2 -d -o /opt/sersync/confxml.xml
3. 安全加固
xml
<!-- 修改 confxml.xml -->
<rsync>
<ssh start="true" port="22" user="rsync_user"/>
</rsync>
4. 故障排查工具
bash
/opt/sersync/sersync2 -r -o /opt/sersync/confxml.xml
bash
tail -f /proc/sys/fs/inotify/* # 监控事件队列
方案优势
注意:大规模集群建议使用 Lsyncd 或 DRBD,单服务器场景本方案为最优解。