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

相关推荐
JP-Destiny13 小时前
linux-安装Ubuntu的docker
linux·ubuntu·docker
EasyCVR13 小时前
从连锁门店到城市级项目,国标GB28181视频监控平台EasyCVR的全场景适配能力有多绝?
运维·网络·音视频
IMPYLH13 小时前
Linux 常用命令列表
linux·运维·服务器·bash
施努卡机器视觉13 小时前
SNK施努卡驱动机构总成半自动装配线:人工与自动化协同解决方案
运维·自动化
空荡forevere13 小时前
Linux文件系统(三)
linux·运维·系统架构·操作系统
lcreek13 小时前
Kali Linux WSL 中文输入法完整安装指南
linux·wsl·中文输入法
zh路西法13 小时前
【RDKX5-sunrise桌面端-图形化无法显示问题】
linux·机器人
Kingairy13 小时前
Docker环境安装
运维·docker·容器
AlfredZhao14 小时前
Linux时区修改为CST
linux·timedatectl