实验拓扑结构

可实时监测文件变化,在文件发生变化后及时同步到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上修改文件内容,观察同步情况

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐