【inotify+unison实时——双向同步】

简述

rsync在单向同步上效率非常高,但在双向同步上较差;unison则是双向同步的优秀工具,但其缺点是同步效率较低。

环境要求:

  1. 准备好同步所需的两个目录,src服务器:/filesrc;dst服务器:/filedst
  2. 若用root登录,生成密钥对,用于免密验证
  3. 准备好inotify和unsion

安装步骤:

1)yum -y install inotify-tools
2)yum -y install unison
3)yum -y install  ocaml  #编译工具

配置脚本

在src服务器上:-batch 批处理
unison具体参数查看

#!/bin/bash
a="inotifywait -mrq -e create,delete /filesrc"
b="/usr/bin/unison -batch /filesrc/ ssh://192.168.88.20//filedst/"
$a |  while read directory event file
do
	$b
done

在dst服务器上:

#!/bin/bash
a="inotifywait -mrq -e create,delete /filedst" 
b="/usr/bin/unison -batch  /filedst/  ssh://192.168.28.148//filesrc/"
$a | while read directory event file
do 
	$b
done

实验测试

1)首先分别在src、dst服务器上将所需的软件下载好,并创建对应目录/filesrc /filedst
yum -y install inotify-tools
yum -y install unison
yum -y install ocaml #编译工具
在这里插入图片描述

2)再将脚本写入,并放置后台运行

[root@localhost filesrc]# vim /root/src.sh
#!/bin/bash
a="inotifywait -mrq -e create,delete /filesrc"
b="/usr/bin/unison -batch /filesrc/ ssh://192.168.88.20//filedst/"
$a |  while read directory event file
do
        $b
done
[root@localhost filesrc]# bash /root/src.sh &
[1] 15398


[root@localhost filedst]# vim /root/dst.sh
#!/bin/bash
a="inotifywait -mrq -e create,delete /filedst"
b="/usr/bin/unison -batch  /filedst/  ssh://192.168.28.148//filesrc/"
$a | while read directory event file
do
        $b
done
[root@localhost filedst]# bash /root/dst.sh &
[1] 12977


3)接着开始测试,在src上创建叫zlf.txt的文件,观察是否在dst上同步生成

[root@localhost filesrc]# touch zlf.txt
[root@localhost filesrc]# Contacting server...
Connected [//localhost.localdomain//filedst -> //localhost.localdomain//filesrc]
Looking for changes
  Waiting for changes from server
Reconciling changes
props    <-?-> props      /  
local        : dir props changed  modified on 2022-10-31 at 14:13:39  size 0         rwxrwxrwx
localhost... : dir props changed  modified on 2022-10-31 at 14:02:11  size 0         rwxr-xr-x
new file ---->            zlf.txt  
local        : new file           modified on 2022-10-31 at 14:13:39  size 0         rw-r--r--
localhost... : absent
Propagating updates
UNISON 2.40.128 started propagating changes at 14:13:40.32 on 31 Oct 2022
[CONFLICT] Skipping 
[BGN] Copying zlf.txt from /filesrc to //localhost.localdomain//filedst
[END] Copying zlf.txt
UNISON 2.40.128 finished propagating changes at 14:13:40.34 on 31 Oct 2022
Saving synchronizer state
Synchronization complete at 14:13:40  (1 item transferred, 1 skipped, 0 failed)
  skipped: 



[root@localhost filedst]# Contacting server...
ls
zlf.txt
[root@localhost filedst]# Connected [//localhost.localdomain//filedst -> //localhost.localdomain//filesrc]
Looking for changes
  Waiting for changes from server
Reconciling changes
props    <-?-> props      /  
local        : dir props changed  modified on 2022-10-31 at 14:13:40  size 0         rwxr-xr-x
localhost... : dir props changed  modified on 2022-10-31 at 14:13:39  size 0         rwxrwxrwx
No updates to propagate

[root@localhost filedst]# ls
zlf.txt

结果dst上成功显示。
4)最后我们可以在dst上创建zlf.html的文件,观察是否能在src上产生对应文件。

[root@localhost filedst]# touch zlf.html
[root@localhost filedst]# Contacting server...
Connected [//localhost.localdomain//filedst -> //localhost.localdomain//filesrc]
Looking for changes
  Waiting for changes from server
Reconciling changes
props    <-?-> props      /  
local        : dir props changed  modified on 2022-10-31 at 14:18:33  size 0         rwxr-xr-x
localhost... : dir props changed  modified on 2022-10-31 at 14:13:39  size 0         rwxrwxrwx
new file ---->            zlf.html  
local        : new file           modified on 2022-10-31 at 14:18:33  size 0         rw-r--r--
localhost... : absent
Propagating updates
UNISON 2.40.128 started propagating changes at 14:18:34.48 on 31 Oct 2022
[CONFLICT] Skipping 
[BGN] Copying zlf.html from /filedst to //localhost.localdomain//filesrc
Shortcut: copied /filesrc/zlf.html from local file /filesrc/zlf.txt
[END] Copying zlf.html
UNISON 2.40.128 finished propagating changes at 14:18:34.50 on 31 Oct 2022
Saving synchronizer state
Synchronization complete at 14:18:34  (1 item transferred, 1 skipped, 0 failed)
  skipped: 


Contacting server...
Connected [//localhost.localdomain//filedst -> //localhost.localdomain//filesrc]
Looking for changes
  Waiting for changes from server
Reconciling changes
props    <-?-> props      /  
local        : dir props changed  modified on 2022-10-31 at 14:18:34  size 0         rwxrwxrwx
localhost... : dir props changed  modified on 2022-10-31 at 14:18:33  size 0         rwxr-xr-x
No updates to propagate

[root@localhost filesrc]# ls
zlf.html  zlf.txt

ok,结果显示也是正确的!!如果我们想监控文件内容是否发生改变,系统进行同步,我们可以考虑在脚本内变量a中增加modify

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值