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上修改文件内容,观察同步情况

相关推荐
傲世仙尊3 分钟前
从抢票事故到锁的原理-互斥条件变量与生产消费模型
linux·驱动开发
AI+程序员在路上3 分钟前
AP6275S蓝牙双接口解析:HCI UART与PCM的分工与协同
linux·c语言·物联网
筝筝ba17 分钟前
openBMC自动化测试说明
服务器
小此方19 分钟前
Linux网络(十八):TCP连接管理详解:从三次握手、四次挥手到CLOSE_WAIT与TIME_WAIT,深入理解2MSL与端口复用
linux·网络·网络协议·tcp/ip
进击的荆棘20 分钟前
Linux系统——文件(上)
linux·运维·服务器·文件
k4m7v2pz35 分钟前
ThinkPad E490 风扇 “转两秒就停“ 实录:迟滞曲线 + 最短运转时间
linux·脚本·archlinux·thinkpad·风扇·散热
拂拉氏35 分钟前
【知识讲解】 Linux文件系统底层认识
linux·文件系统
承渊政道36 分钟前
星空组网排查指南:从设备在线到服务可用的五步检查
运维·星空组网·排查指南
IT大白鼠40 分钟前
彭大帅的AI运维助手——自然语言管理 Linux 集群与网络设备——第 1 篇 · 骨架:说人话,跑命令:自然语言 SSH 运维的骨架
linux·运维·人工智能
檀越剑指大厂1 小时前
抽屉里的手机还能开博客?用 Typecho 和 cpolar 搭一台可远程访问的小服务器
运维·服务器·智能手机