wei/个人文档/rsync实时同步.md
2026-06-04 13:57:41 +08:00

212 lines
4.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Rsync 实时同步服务部署文档
## 📋 项目概述
实现从源服务器 `10.101.2.197` 到目标服务器 `10.99.11.57``10.101.3.43` 的文件实时同步,主要同步以下内容:
- `dibiao_template_*` 文件
- `扫板底表` 目录及其内容
## 🛠 环境准备
### 系统要求
- 源服务器CentOS 7 (10.101.2.197)
- 目标服务器CentOS 7 (10.99.11.57, 10.101.3.43)
- 用户admin
### 软件安装
在源服务器上安装必要软件:
```bash
# 安装 rsync 和 inotify-tools
sudo yum install -y rsync inotify-tools
```
## 🔑 SSH 免密登录配置
### 生成 SSH 密钥对
```bash
# 在源服务器上执行
ssh-keygen -t rsa -b 4096
# 直接回车使用默认路径和空密码
```
### 分发公钥到目标服务器
```bash
# 分发到第一个目标服务器
ssh-copy-id admin@10.99.11.57
# 分发到第二个目标服务器
ssh-copy-id admin@10.101.3.43
```
### 验证 SSH 连接
```bash
ssh admin@10.99.11.57 "echo '连接10.99.11.57成功'"
ssh admin@10.101.3.43 "echo '连接10.101.3.43成功'"
```
## 📁 同步脚本配置
### 脚本位置
`/home/admin/sync_script.sh`
### 脚本内容
```bash
#!/bin/bash
# 同步源目录
SRC_BASE="/home/admin/sftp/deploy"
DEST_HOSTS=("admin@10.99.11.57" "admin@10.101.3.43")
DEST_BASE="/home/admin/sftp/deploy"
LOG_FILE="/home/admin/rsync_sync.log"
# SSH 选项
SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"
# 同步函数
sync_files() {
local success=true
for host in "${DEST_HOSTS[@]}"; do
echo "$(date): 同步到 $host..." >> $LOG_FILE
# 使用单个 rsync 命令同步所有模式
rsync -avz --delete \
--include='dibiao_template_*' \
--include='扫板底表/' \
--include='扫板底表/扫板底表*' \
--exclude='*' \
-e "ssh $SSH_OPTS" \
$SRC_BASE/ $host:$DEST_BASE/ >> $LOG_FILE 2>&1
if [ $? -ne 0 ]; then
echo "$(date): 同步到 $host 失败" >> $LOG_FILE
success=false
else
echo "$(date): 同步到 $host 成功" >> $LOG_FILE
fi
done
if $success; then
echo "$(date): 所有同步完成" >> $LOG_FILE
else
echo "$(date): 部分同步失败" >> $LOG_FILE
fi
}
# 记录开始时间
echo "=== 同步脚本启动于: $(date) ===" >> $LOG_FILE
# 初始同步
sync_files
# 实时监控
inotifywait -m -r -e create,close_write,delete,moved_to,moved_from \
--format "%w%f %e" \
$SRC_BASE | while read full_path event
do
filename=$(basename "$full_path")
dir_path=$(dirname "$full_path")
if [[ "$filename" =~ ^dibiao_template_ ]] || \
{ [[ "$dir_path" =~ .*扫板底表.* ]] && [[ "$filename" =~ ^扫板底表 ]]; }; then
echo "$(date): 检测到变化: $full_path" >> $LOG_FILE
sync_files
fi
done
```
### 设置脚本权限
```bash
chmod +x /home/admin/sync_script.sh
```
## 🔧 Systemd 服务配置
### 服务文件位置
`/etc/systemd/system/rsync-realtime.service`
### 服务文件内容
```ini
[Unit]
Description=Real-time Rsync Service
After=network.target
[Service]
Type=simple
User=admin
WorkingDirectory=/home/admin
ExecStart=/home/admin/sync_script.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
```
### 服务管理命令
```bash
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 启用开机自启
sudo systemctl enable rsync-realtime.service
# 启动服务
sudo systemctl start rsync-realtime.service
# 检查服务状态
sudo systemctl status rsync-realtime.service
# 停止服务
sudo systemctl stop rsync-realtime.service
# 重启服务
sudo systemctl restart rsync-realtime.service
# 查看服务日志
sudo journalctl -u rsync-realtime.service -f
```
## ✅ 功能验证
### 测试文件同步
```bash
# 在源服务器创建测试文件
cd /home/admin/sftp/deploy/
touch dibiao_template_test_$(date +%s).txt
echo "测试内容" > dibiao_template_testfile.txt
# 在目标服务器验证文件同步
ssh admin@10.99.11.57 "ls -la /home/admin/sftp/deploy/dibiao_template_*"
ssh admin@10.101.3.43 "ls -la /home/admin/sftp/deploy/dibiao_template_*"
```
### 监控同步日志
```bash
# 查看实时同步日志
tail -f /home/admin/rsync_sync.log
# 查看 systemd 服务日志
sudo journalctl -u rsync-realtime.service -f
```
## 🐛 故障排除
### 常见问题及解决方案
#### 1. SSH 权限被拒绝
```bash
# 检查 SSH 密钥权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
# 重新分发公钥
ssh-copy-id admin@目标服务器IP
```
### 日志文件说明
- **服务日志**: `/home/admin/rsync_sync.log` - 记录所有同步操作