概述
前文说明了如何使用 Rsync 搭配 inotify-tools 完成实时的单向数据同步,在一些特殊场景中,可能还会使用到双向同步。
在软件搭配上:
- inotify-tools - 文件系统监视程序
- Unison - 文件同步工具
环境准备
提示
这里的 Unison 和 ocaml 采用的是从源码编译安装方式,后面会说明具体操作步骤。在版本选择上,ocaml 推荐使用 4.14.2 版本,而 Unison 推荐使用 2.54.0
RL 8.10 环境验证:
# inotify-tools 软件包信息
Shell (192.168.100.20)> dnf info inotify-tools
Last metadata expiration check: 0:26:11 ago on Sun 08 Nov 2026 08:14:20 PM CST.
Installed Packages
Name : inotify-tools
Version : 3.14
Release : 19.el8
Architecture : x86_64
Size : 120 k
Source : inotify-tools-3.14-19.el8.src.rpm
Repository : @System
From repo : epel
Summary : Command line utilities for inotify
URL : http://inotify-tools.sourceforge.net/
License : GPLv2
Description : inotify-tools is a set of command-line programs for Linux providing
: a simple interface to inotify. These programs can be used to monitor
: and act upon filesystem events.
# 免密验证登陆
Shell (192.168.100.20)> ssh -p 22 -i /root/.ssh/id_rsa riu@192.168.100.21
Last login: Sun Nov 8 20:58:13 2026
RL 10.3 环境验证:
# inotify-tools 软件包信息
Shell (192.168.100.21)> dnf5 info inotify-tools
Updating and loading repositories:
Repositories loaded.
Installed packages
Name : inotify-tools
Epoch : 0
Version : 4.23.9.0
Release : 4.el10_1
Architecture : x86_64
Installed size : 209.8 KiB
Source : inotify-tools-4.23.9.0-4.el10_1.src.rpm
From repository : epel
Summary : Command line utilities for inotify
URL : https://github.com/inotify-tools/inotify-tools
License : GPL-2.0-only AND GPL-2.0-only WITH Linux-syscall-note AND LGPL-2.1-or-later
Description : inotify-tools is a set of command-line programs for Linux providing
: a simple interface to inotify. These programs can be used to monitor
: and act upon filesystem events.
Vendor : Fedora Project
# 免密验证登陆
Shell (192.168.100.21)> ssh -p 22 -i /root/.ssh/r8_id_rsa r8riu@192.168.100.20
Last login: Sun Nov 8 21:29:10 2026
友情提示
RL 8.10 和 RL 10.3 的 **/etc/ssh/sshd_config** 文件应该开启
PubkeyAuthentication yes。RL 8 安装 Unison
ocaml 的安装步骤如下:
Shell > wget -c https://github.com/ocaml/ocaml/archive/refs/tags/4.14.2.tar.gz
Shell > tar -vxf 4.14.2.tar.gz -C /usr/local/src/
Shell > cd /usr/local/src/ocaml-4.14.2/
Shell > ./configure --prefix=/usr/local/ocaml && make world opt && make install
...
Shell > ls -l /usr/local/ocaml/
total 16
drwxr-xr-x 2 root root 4096 Nov 9 00:04 bin
drwxr-xr-x 3 root root 4096 Nov 9 00:04 lib
drwxr-xr-x 4 root root 4096 Nov 9 00:04 man
drwxr-xr-x 3 root root 4096 Nov 9 00:04 share
Shell > echo PATH=$PATH:/usr/local/ocaml/bin >> /etc/profile
Shell > . /etc/profile
Unison 的安装步骤如下
Shell > wget -c https://github.com/bcpierce00/unison/archive/refs/tags/v2.54.0.tar.gz
Shell > tar -vxf v2.54.0.tar.gz -C /usr/local/src/
Shell > cd /usr/local/src/unison-2.54.0/
Shell > make
Shell > ls src/unison
src/unison
Shell > cp -p src/unison /usr/local/bin/
RL 10 安装 Unison
ocaml 的安装步骤如下:
Shell > wget -c https://github.com/ocaml/ocaml/archive/refs/tags/4.14.2.tar.gz
Shell > tar -vxf 4.14.2.tar.gz -C /usr/local/src/
Shell > cd /usr/local/src/ocaml-4.14.2/
Shell > ./configure --prefix=/usr/local/ocaml && make world opt && make install
...
Shell > ls -l /usr/local/ocaml/
total 16
drwxr-xr-x 2 root root 4096 Nov 9 00:14 bin
drwxr-xr-x 3 root root 4096 Nov 9 00:14 lib
drwxr-xr-x 4 root root 4096 Nov 9 00:14 man
drwxr-xr-x 3 root root 4096 Nov 9 00:14 share
Shell > echo PATH=$PATH:/usr/local/ocaml/bin >> /etc/profile
Shell > . /etc/profile
Unison 的安装步骤如下
Shell > wget -c https://github.com/bcpierce00/unison/archive/refs/tags/v2.54.0.tar.gz
Shell > tar -vxf v2.54.0.tar.gz -C /usr/local/src/
Shell > cd /usr/local/src/unison-2.54.0/
Shell > make
Shell > ls src/unison
src/unison
Shell > cp -p src/unison /usr/local/bin/
演示
需求:RL 8.10 的 /dir1/ 目录与 RL 10.3 的 /dir2/ 目录实现双向同步。
配置 RL 8.10
Shell > mkdir /dir1/
Shell > setfacl -m u:r8riu:rwx /dir1/
Shell > vim /root/sync1.sh
#!/bin/bash
inotifywait -mrq -e create,delete,modify,move /dir1/ | while read directory event file
do
/usr/local/bin/unison -batch /dir1/ ssh://riu@192.168.100.21:22//dir2 -sshargs "-i /root/.ssh/id_rsa" &>> /tmp/sync1.log
done
Shell > chmod +x /root/sync1.sh
Shell > bash /root/sync1.sh &
Shell > jobs -l
配置 RL 10.3
Shell > mkdir /dir2/
Shell > setfacl -m u:riu:rwx /dir2/
Shell > vim /root/sync2.sh
#!/bin/bash
inotifywait -mrq -e create,delete,modify,move /dir2/ | while read directory event file
do
/usr/local/bin/unison -batch /dir2/ ssh://r8riu@192.168.100.20:22//dir1 -sshargs "-i /root/.ssh/r8_id_rsa" &>> /tmp/sync2.log
done
Shell > chmod +x /root/sync2.sh
Shell > bash /root/sync2.sh &
Shell > jobs -l
注意点:
- 双向同步时,双方服务器的脚本必须都需要启动,否则可能会报错
- 注意双向同步时的用户权限设置
- 使用
unison命令时,注意不要漏掉目标目录路径的前缀 "/",如/usr/local/bin/unison -batch /dir2/ ssh://r8riu@192.168.100.20:22//dir1 -sshargs "-i /root/.ssh/r8_id_rsa"中的/dir1 - 注意观察日志文件的内容,未来排查错误时很有用
- 注意观察 Bash 脚本的状态,处于 Running 是正常状态,处于 Stopped 则需要排查错误
尝试下:
Shell (192.168.100.20)> touch /dir1/{R8-file,R8-f2}
Shell (192.168.100.21)> touch /dir2/R10-file
同步完成:
Shell (192.168.100.20)> ls /dir1/
R10-file R8-f2 R8-file
Shell (192.168.100.21)> ls /dir2/
R10-file R8-f2 R8-file
版权声明:「自由转载-保持署名-非商业性使用-禁止演绎 3.0 国际」(CC BY-NC-ND 3.0)
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论










