Rsync 实时文件同步部署方案

1.环境设置

服务端 IP: 172.16.103.193
客户端 IP: 172.16.103.12

1. 配置 rsync 服务端

1. 安装 rsync

在服务端上,首先需要安装 rsync。可以使用以下命令安装:

sudo yum install rsync    # CentOS/RedHat 系列
sudo apt-get install rsync # Ubuntu/Debian 系列

2.配置 rsync 服务端

编辑配置文件 /etc/rsyncd.conf:

uid = root
gid = root
use chroot = true
address = 172.16.103.193
port = 873
max connections = 4
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 172.16.103.0/24
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[backup]
   path = /backup129/
   comment = backup file
   ignore errors = yes
   read only = no
   write only = no
   hosts allow = 172.16.103.12
   hosts deny = *
   list = false
   uid = root
   gid = root
   auth users = root
   secrets file = /etc/rsync.password

3.设置密码文件 /etc/rsync.password:

该文件包含认证用户的用户名和密码。确保文件权限设置正确:

echo "root:your_password" > /etc/rsync.password
chmod 600 /etc/rsync.password

4.启动 rsync 服务:

sudo systemctl start rsyncd.service
sudo systemctl enable rsyncd.service

5.检查服务状态:

sudo systemctl status rsyncd.service

6.检查防火墙配置

确保防火墙允许端口 873 通信:

firewall-cmd --zone=public --add-port=873/tcp --permanent
firewall-cmd --reload

2. 配置 rsync 客户端

在客户端上,你可以使用 rsync 命令来同步数据。

1.安装 rsync

同样,确保客户端上也安装了 rsync:

sudo yum install rsync  inotify-tools   # CentOS/RedHat 系列
sudo apt-get install rsync inotify-tools # Ubuntu/Debian 系列

2.检查防火墙配置

确保防火墙允许端口 873 通信:

firewall-cmd --zone=public --add-port=873/tcp --permanent
firewall-cmd --reload

3.使用 rsync 命令进行同步

使用以下命令从服务端同步数据到客户端:

rsync -avH --port 873 --delete /root/data/backuptest/ root@172.16.103.193::backup --password-file=/etc/rsync.password

4.创建同步脚本

创建 /root/rsync_inotify.sh 脚本,用于监控根目录的文件变更并同步到服务端:

vim /root/rsync_inotify.sh
将以下内容复制到脚本文件中:
#!/bin/bash
  
# 监控的目录(/home)
WATCH_DIR="/home"

# 远程服务器地址
REMOTE_USER="root"
REMOTE_HOST="172.16.103.193"
REMOTE_MODULE="backup"
PASSWORD_FILE="/etc/rsync.password"

# 使用inotifywait监听/home目录中的文件变化
inotifywait -m -r -e modify,create,delete,move "${WATCH_DIR}" | while read path action file; do
    echo "Detected ${action} on ${file} in ${path}. Starting rsync..."

    # 执行rsync同步操作
    rsync -avH --port 873 --delete "${WATCH_DIR}" "${REMOTE_USER}@${REMOTE_HOST}::${REMOTE_MODULE}" --password-file=${PASSWORD_FILE}

    echo "Sync completed for ${file} in ${path}."
done

5.设置脚本可执行权限

赋予脚本执行权限:

chmod +x /root/rsync_inotify.sh

6. 添加开机自启

为了确保每次系统启动后脚本都自动运行,可以将使用 systemd 创建一个服务。

1.使用 systemd 创建服务

创建 systemd 服务文件 /etc/systemd/system/inotify-rsync.service:

[Unit]
Description=Inotify Rsync Service
After=network.target

[Service]
ExecStart=/root/rsync_inotify.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
2.启动服务并设置为开机自启:
sudo systemctl daemon-reload
sudo systemctl start inotify-rsync.service
sudo systemctl enable inotify-rsync.service
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑蛋同志

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值