rsync-daemon + lsyncd实现文件近实时备份

实验拓扑结构

可实时监测文件变化,在文件发生变化后及时同步到rsyncd服务器

环境为rocky linux 10.1

cat /etc/redhat-release
Rocky Linux release 10.1 (Red Quartz)

首先搭建rsync服务器

dnf -y install rsync rsync-daemon

groupadd -r rsync

useradd -r -s /sbin/nologin -M -g rsync rsync

mkdir -p /backup/web{1,2}

chown -R rsync:rsync /backup

vim /etc/rsyncd.passwd

web1:web1
web2:web2

chmod 600 /etc/rsyncd.passwd

systemctl enable --now rsyncd

测试rsync服务器连接

在web1上执行测试

dnf -y install rsync

echo web1 > /etc/rsyncd.client.passwd

chmod 600 /etc/rsyncd.client.passwd

mkdir /web1

cd /web1

for i in {1..10}; do echo i \> i.txt; done

rsync -avz /web1/ web1@192.168.230.142::web1 --password-file=/etc/rsyncd.client.passwd

在web2上执行测试

dnf -y install rsync

echo web2 > /etc/rsyncd.client.passwd

chmod 600 /etc/rsyncd.client.passwd

mkdir /web2

cd /web2

for i in {a..k};do echo i \> i.txt; done

rsync -avz /web2/ web2@192.168.230.142::web2 --password-file=/etc/rsyncd.client.passwd

在web1和web2上编译安装lsyncd

git clone https://github.com/lsyncd/lsyncd.git

dnf config-manager --enable crb

dnf -y install "@Development Tools"

dnf -y install make cmake gcc lua lua-devel

cd lsyncd/

mkdir build

cd build

cmake ..

make -j4

make install

web1上准备lsyncd配置文件

默认有delete选项,两边文件保持一持

通过delay值调整同步延迟,设置为0时近乎实时

mkdir -p /var/log/lsyncd

vim /etc/lsyncd.conf

settings {
statusFile = "/tmp/lsyncd.stat",
logfile = "/var/log/lsyncd/lsyncd.log",
statusInterval = 1,
insist = true,
nodaemon = true,
}

sync{
default.rsync,
source="/web1/",
target="web1@192.168.230.142::web1",
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = yes,
verbose = yes,
compress = yes,
password_file = "/etc/rsyncd.client.passwd",
}
}

web2上准备lsyncd配置文件

mkdir -p /var/log/lsyncd

vim /etc/lsyncd.conf

settings {
statusFile = "/tmp/lsyncd.stat",
logfile = "/var/log/lsyncd/lsyncd.log",
statusInterval = 1,
insist = true,
nodaemon = true,
}

sync{
default.rsync,
source="/web2/",
target="web2@192.168.230.142::web2",
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = yes,
verbose = yes,
compress = yes,
password_file = "/etc/rsyncd.client.passwd",
}
}

web1和web2上准备服务启动文件

vim lsyncd.service

Unit

Description=Live sync daemon
After=network.target

Service

Type=simple
ExecStart=/usr/local/bin/lsyncd --nodaemon /etc/lsyncd.conf
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=2s

Install

WantedBy=multi-user.target

install -Dm600 lsyncd.service /usr/lib/systemd/system/lsyncd.service

启动服务并设置开机自启

systemctl enable --now lsyncd

效果测试方法

在rsync服务器上,使用命令watch -n 0.5 'tree'观测目录结构变化,在web1或web2上添加/删除文件或目录,观察同步情况

在rsync服务器上,使用命令watch -n 0.5 'cat /web2/test/t.txt'观测文件内容变化,在web2上修改文件内容,观察同步情况

相关推荐
zhengqweasd34 分钟前
流量没跑满、带宽空闲,业务依然卡顿
运维·服务器·网络
闖641 小时前
入门Linux操作系统|零基础吃透原理、目录、权限、常用命令
linux
bruk_spp2 小时前
linux pinctrl
linux
M哥支付2 小时前
高频交易优选方案:银联快捷支付通道
服务器·网络·其他·微信·金融
Y3815326622 小时前
Docker Compose 服务依赖与健康检查:healthcheck 让容器按正确顺序启动
运维·docker·容器
稳联技术老娜2 小时前
伺服内嵌EtherNet IP方案:嵌入式小板SPI通讯固件Demo程序适配改造
运维·服务器·网络
en.en..3 小时前
Linux系统编程:fcntl 与 ioctl
linux·运维·服务器
自动化监测Learner3 小时前
Navicat Premium 17 中文版安装教程(2026最新版)
java·linux·数据库
荣合技术服务4 小时前
Codex 实战:用 AI 写运维脚本
运维·codex
会周易的程序员4 小时前
5Draft(五帝)测试报告
服务器·c++·分布式·raft·共识算法·共识·算力服务器