unison的安装及基本使用,负载均衡(二)

前言:

Unison是Windows、Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致。Unison拥有与其它一些同步工具或文件系统的相同的特性,但也有自身的特点:
a.跨平台使用;
b.对内核和用户权限没有特别要求;
c.Unison是双向的,它能自动处理两分拷贝中更新没有冲突的部分,有冲突的部分将会显示出来让用户选择更新策略;
d.只要是能连通的两台主机,就可以运行unison,可以直接使用socket连接或安全的ssh连接方式,对带宽的要求不高,使用类似rsync的压缩传输协议。

示例环境:两台centos7的环境
vm1 60.30.30.30
vm2 30.60.60.60

安装:

以60.30.30.30为例,需要双向操作

编译和安装ocaml

1
2
3
4
5
6
[root@ 60.30.30.30 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-4.02/ocaml-4.02.0.tar.gz
[root@ 60.30.30.30 ~]# tar -xzvf ocaml-4.02.0.tar.gz
[root@ 60.30.30.30 ~]# cd ocaml-4.02.0
[root@ 60.30.30.30 ocaml-4.02.0]# ./configure
[root@ 60.30.30.30 ocaml-4.02.0]# make world opt
[root@ 60.30.30.30 ocaml-4.02.0]# make install

编译安装Unison

1
2
3
4
5
6
[root@ 60.30.30.30 ~]# wget https://www.seas.upenn.edu/~bcpierce/unison/download/releases/unison-2.48.3/unison-2.48.3.tar.gz
[root@ 60.30.30.30 ~]# tar -xzvf unison-2.48.3.tar.gz
[root@ 60.30.30.30 ~]# cd unison-2.48.3
[root@ 60.30.30.30 unison-2.48.3]# make UISTYLE=text
[root@ 60.30.30.30 unison-2.48.3]# make install
[root@ 60.30.30.30 unison-2.48.3]# cp unison /usr/bin

配置ssh的无密码登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 检查ssh密钥文件是否存在
[root@ 60.30.30.30 home]# ls ~/.ssh/id_rsa
/root/.ssh/id_rsa
#如果上面命令无内容,则生成一个新的id_rsa
[root@ 60.30.30.30 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
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:
71:81:27:15:4f:35:98:04:ea:89:0d:8e:8b:fa:ea:04 root@iz2zectwfo48lksdf8g1p2z
The key's randomart image is:
+--[ RSA 2048]----+
| o=+o+o |
| o..+o . |
| . oo. . |
| o = + |
|E . o S |
|. . . |
| .. . |
|.. |
|++. |
+-----------------+
# 把公钥的内容追加到ssh服务器的 ~/.ssh/authorized_keys
[root@ 60.30.30.30 ~]# scp ~/.ssh/id_rsa.pub root@30.60.60.60
[root@ 60.30.30.30 ~]# ssh root@30.60.60.60
[root@ 60.30.30.30 ~]# cat id_rsa.pub >> ~/.ssh/authorized_keys
# 设置~/.ssh 相关文件权限
[root@ 60.30.30.30 ~]# chmod 700 ~/.ssh
[root@ 60.30.30.30 ~]# chmod 600 ~/.ssh/id_rsa

编写脚本

以同步/home/test/的文件为例,

服务器一,60.30.30.30

1
[root@ 60.30.30.30 ~]# vim unsion.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
#/bin/sh
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
other_server="30.60.60.60"
src1="/home/test/"
dst2="/home/test/"
/usr/bin/inotifywait -mrq -e create,delete,delete_self,modify,move $src1 | while read line; do
/usr/bin/unison $src1 ssh://$other_server/$dst2
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
fi

服务器二,30.60.60.60

1
[root@ 30.60.60.60 ~]# vim unsion.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
#/bin/sh
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
other_server="60.30.30.30"
src1="/home/test/"
dst2="/home/test/"
/usr/bin/inotifywait -mrq -e create,delete,delete_self,modify,move $src1 | while read line; do
/usr/bin/unison $src1 ssh://$other_server/$dst2
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
fi

执行脚本

1
[root@ 30.60.60.60 ~]# nohup unsion.sh > /dev/null 2>&1 &