您的位置:首页 > 其它

unison_inotify实现文件双向同步

2017-02-10 00:00 435 查看
主机:
192.168.40.105 test5
192.168.40.106 test6

系统
centos 6.8

必备的软件包
inotify-tools-3.14.tar.gz
http://pan.baidu.com/s/1nvSAs1F
unison-2.40.128.tar.gz
http://pan.baidu.com/s/1slyFWi5
ocaml-3.12.1.tar.gz
http://pan.baidu.com/s/1mipwIoC

首先实现双机互信
host5
生成密钥对
[root@test5 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
46:e0:39:fc:e6:d6:d0:6c:72:f6:3b:fc:0f:1e:34:5b root@test5
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| o o |
| = . |
| + o |
| S * o E|
| + B . . + |
| o ... + |
| . oo o |
| .oo..|
+-----------------+
把公钥复制到远程主机
[root@test5 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.106
The authenticity of host '192.168.40.106 (192.168.40.106)' can't be established.
RSA key fingerprint is 52:69:48:c4:70:3c:9c:91:c1:c2:c5:2b:c2:29:30:49.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.106' (RSA) to the list of known hosts.
root@192.168.40.106's password:
Now try logging into the machine, with "ssh '192.168.40.106'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
测试,看是否不用输入密码直接执行远程命令
[root@test5 ~]# ssh 192.168.40.106 'hostname'
test6

host6
同样执行实现密钥进行认证
[root@test6 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
54:b3:69:7d:94:33:6c:73:9a:e8:6f:b4:cf:e6:87:b3 root@test6
The key's randomart image is:
+--[ RSA 2048]----+
| o ... |
| . = .B .|
| . + .o.B |
| . . ..o |
| S . |
| . . |
| o o |
| *.o|
| .E*=|
+-----------------+
[root@test6 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.105
The authenticity of host '192.168.40.105 (192.168.40.105)' can't be established.
RSA key fingerprint is c4:72:2a:50:9e:b6:9d:4b:86:0e:4d:e6:c5:e8:50:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.105' (RSA) to the list of known hosts.
root@192.168.40.105's password:
Now try logging into the machine, with "ssh '192.168.40.105'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@test6 ~]# ssh 192.168.40.105 'hostname'
test5

保持两主机时间一致,通过ntpdate命令同步一台时间服务器
两主机都执行
ntpdate NTP_SERVER

备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

test5上
环境准备
#!/bin/bash

SRC="/data/oops"
DES=192.168.40.106

yum install -y automake gcc gcc-c++ libtool 1> /dev/null
tar xf ocaml-3.12.1.tar.gz
cd ocaml-3.12.1
./configure 1> /dev/null
make world.opt 1> /dev/null
make install 1> /dev/null

yum install -y ctags-etags 1> /dev/null
cd ..
tar xf unison-2.40.128.tar.gz
cd unison-2.40.128
#表示:使用命令方式,加入线程支持
make UISTYLE=text THREADS=true 1> /dev/null
make install 1> /dev/null
cp unison /usr/local/bin/
scp unison ${DES}:/usr/local/bin/
mkdir ~/.unison
cat > ~/.unison/default.prf << EOF
root = ${SRC}
root = ssh://root@${DES}/${SRC}
batch = true
maxthreads = 300
owner = true
group = true
perms = -1
silent = true
fastcheck = false
sshargs = -C
xferbycopying = true
log = true
logfile = /tmp/unison.log
EOF

cd ..
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify 1> /dev/null
make 1> /dev/null
make install 1> /dev/null

cd ~
cat > unison.sh << EOF
#!/bin/bash

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
unison
echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash unison.sh &

test6上
#!/bin/bash

SRC="/data/oops"
DES=192.168.40.105

yum install -y automake gcc gcc-c++ libtool 1> /dev/null
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify 1> /dev/null
make 1> /dev/null
make install 1> /dev/null

mkdir ~/.unison
cat > ~/.unison/default.prf << EOF
root = ${SRC}
root = ssh://root@${DES}/${SRC}
batch = true
maxthreads = 300
owner = true
group = true
perms = -1
silent = true
fastcheck = false
sshargs = -C
xferbycopying = true
log = true
logfile = /tmp/unison.log
EOF

cd ~
cat > unison.sh << EOF
#!/bin/bash

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
unison
echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash unison.sh &
参考链接:
http://www.cnblogs.com/MYSQLZOUQI/p/5184642.html
http://openskill.cn/article/161

测试:
新增文件
[root@test6 ~]# cp install.log /data/oops/
Contacting server...
[root@test6 ~]# Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
/data/oops
//test5//data/oops
This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.

Update detection may take a while on this run if the replicas are
large.

Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.

If you see this message repeatedly, it may be because one of your machines
is getting its address from DHCP, which is causing its host name to change
between synchronizations. See the documentation for the UNISONLOCALHOSTNAME
environment variable for advice on how to correct this.

Donations to the Unison project are gratefully accepted:
http://www.cis.upenn.edu/~bcpierce/unison

Waiting for changes from server
Reconciling changes
file ----> install.log
local : file modified on 2017-02-10 at 20:19:30 size 16949 --rw-r--r-- user=0 group=0
test5 : absent
Propagating updates
UNISON 2.40.128 started propagating changes at 20:19:31.99 on 10 Feb 2017
[BGN] Copying install.log from /data/oops to //test5//data/oops
[END] Copying install.log
UNISON 2.40.128 finished propagating changes at 20:19:32.01 on 10 Feb 2017
Saving synchronizer state
Synchronization complete at 20:19:32 (1 item transferred, 0 skipped, 0 failed)
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.

[root@test6 ~]#
[root@test6 ~]#
[root@test6 ~]# ls /data/oops/
install.log

[root@test5 ~]# Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.

[root@test5 ~]# ls /data/oops/
install.log
两主机上都有了

取消显示这些信息
在配置文件中添加
silent = true
[root@test5 ~]# vim .unison/default.prf
[root@test5 ~]# ps aux | grep unison
root 40104 0.0 0.0 106108 1148 pts/1 S 20:17 0:00 bash unison.sh
root 40106 0.0 0.0 106112 724 pts/1 S 20:17 0:00 bash unison.sh
root 40143 0.0 0.0 103320 844 pts/1 S+ 20:22 0:00 grep unison
[root@test5 ~]# kill 40104 40106
[root@test5 ~]# ps aux | grep unison
root 40146 0.0 0.0 103316 832 pts/1 S+ 20:23 0:00 grep unison

[root@test6 ~]# vim .unison/default.prf
[root@test6 ~]# ps aux | grep unison
root 32015 0.0 0.0 106108 1148 pts/1 S 20:17 0:00 bash unison.sh
root 32017 0.0 0.0 106112 720 pts/1 S 20:17 0:00 bash unison.sh
root 32071 0.0 0.0 103316 836 pts/1 S+ 20:22 0:00 grep unison
[root@test6 ~]# kill 32015 32017
[root@test6 ~]# ps aux | grep unison
root 32074 0.0 0.0 103316 836 pts/1 S+ 20:23 0:00 grep unison

再次启动
两主机都执行
bash unison.sh &
[root@test5 ~]# cp inotify-tools-3.14.tar.gz /data/oops/
[root@test5 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz install.log

[root@test6 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz install.log

删除文件
[root@test6 ~]# rm -rf /data/oops/install.log
[root@test6 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz

[root@test5 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz

OK两边文件都保持了一致了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: