双向同步镜像工具----unison

2820阅读 0评论2012-11-07 lyblyc121
分类:

unison能够实现客户端和服务器的双向同步,有一端发生数据变化(包括删除文件),都会同步到对方。优点:跨平台,对内核要求低,增量同步等。

unison 使用ocaml语言编写,所以需要先安装ocaml语言包
cd ocaml-3.10.2/
./configure
make world.opt  这一步时间比较长
make install

安装unison包
make UISTYLE=text   这一命令会生成一个unison可执行程序
./unison   能显示unison使用方法,说明unison已经安装成功了
mv unison /usr/sbin/ 

下面介绍下,它的使用方法
本地文件夹得同步:

[root@station1 ~]# mkdir /data1
[root@station1 ~]# mkdir /data2
[root@station1 ~]# echo "d1" > /data1/txt
[root@station1 ~]# echo "d2" > /data2/txt2
[root@station1 ~]# unison /data1 /data2 -batch
Contacting server...
Connected [//station1.test.com//data1 -> //station1.test.com//data2]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
        /data1
        /data2
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:
~bcpierce/unison

Reconciling changes
file     ---->            txt
data1        : file               modified on 2012-11-05 at 14:21:23  size 3         rw-r--r--
data2        : absent
         <---- file       txt2
data1        : absent
data2        : file               modified on 2012-11-05 at 14:21:32  size 3         rw-r--r--
Propagating updates
UNISON 2.32.52 started propagating changes at 14:21:53 on 05 Nov 2012
[BGN] Copying txt from /data1 to /data2
[END] Copying txt
[BGN] Copying txt2 from /data2 to /data1
[END] Copying txt2
UNISON 2.32.52 finished propagating changes at 14:21:53 on 05 Nov 2012
Saving synchronizer state
Synchronization complete at 14:21:53  (2 items transferred, 0 skipped, 0 failed)
[root@station1 ~]# echo "this file chaged" > /data1/txt2
[root@station1 ~]# unison /data1 /data2 -batch
Contacting server...
Connected [//station1.test.com//data1 -> //station1.test.com//data2]
Looking for changes
Reconciling changes
changed  <-?-> changed    txt2
data1        : changed file       modified on 2012-11-05 at 14:22:44  size 17        rw-r--r--
data2        : changed file       modified on 2012-11-05 at 14:22:04  size 0         rw-r--r--
changed  ---->            txt
data1        : changed file       modified on 2012-11-05 at 14:22:04  size 0         rw-r--r--
data2        : unchanged file     modified on 2012-11-05 at 14:21:23  size 3         rw-r--r--
Propagating updates
UNISON 2.32.52 started propagating changes at 14:23:12 on 05 Nov 2012
[CONFLICT] Skipping txt2
[BGN] Updating file txt from /data1 to /data2
[END] Updating file txt
UNISON 2.32.52 finished propagating changes at 14:23:12 on 05 Nov 2012
Saving synchronizer state
Synchronization complete at 14:23:12  (1 item transferred, 1 skipped, 0 failed)
  skipped: txt2
[root@station1 ~]#


远程同步
unison使用ssh进行同步,所以首先配置station1(192.168.1.100)和station2(192.168.1.101)双机ssh信任

在station2上:
[root@station2 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
a0:fc:b0:68:02:dc:61:7c:0f:e3:64:48:59:1d:96:4f root@station2.test.com
The key's randomart image is:
+--[ RSA 2048]----+
|   .o..oo        |
|  o.. .o E       |
|   = *. o        |
|. o.*.+. .       |
|.. .+. .S        |
|.  . +           |
|. o . .          |
| o               |
|                 |
+-----------------+
[root@station2 ~]# ssh-copy-id 192.168.1.100
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
RSA key fingerprint is 94:d7:85:e6:16:06:5f:32:f4:3e:d3:1b:85:31:12:50.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
root@192.168.1.100's password:
Now try logging into the machine, with "ssh '192.168.1.100'", and check in:

  .ssh/authorized_keys

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

[root@station2 ~]# ssh 192.168.1.100
Last login: Mon Nov  5 13:19:32 2012 from 192.168.1.1
[root@station1 ~]#


在station1上
[root@station1 ~]# 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:
11:6d:3f:42:15:32:c0:55:4c:57:cf:e1:7a:b6:46:95 root@station1.test.com
The key's randomart image is:
+--[ RSA 2048]----+
|       .o++==..o.|
|        ..+o....+|
|        .o .   E+|
|         .. o . .|
|        S  . o + |
|              + .|
|               o |
|              .  |
|                 |
+-----------------+
[root@station1 ~]# ssh-copy-id 192.168.1.101
The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.
RSA key fingerprint is 3a:f2:a8:42:34:21:46:c3:cf:fa:13:0e:34:76:35:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.101' (RSA) to the list of known hosts.
root@192.168.1.101's password:
Now try logging into the machine, with "ssh '192.168.1.101'", and check in:

  .ssh/authorized_keys

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

[root@station1 ~]# ssh 192.168.1.101
Last login: Mon Nov  5 14:28:10 2012 from 192.168.1.1
[root@station2 ~]#


测试结果:

[root@station1 tmp]# unison ssh://root@192.168.1.101//root/station1_etc /etc -batch 
Contacting server...
Connected [//station1.test.com//etc -> //station2.test.com//root/station1_etc]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
        //station2.test.com//root/station1_etc
        /etc
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.
。。。。。。。。。。。。。。。。。

[root@station1 tmp]# du -hs /etc
24M     /etc
[root@station1 tmp]# ssh 192.168.1.101
Last login: Mon Nov  5 14:32:14 2012 from 192.168.1.100
[root@station2 ~]# du -hs station1_etc/
24M     station1_etc/
[root@station2 ~]#
测试成功!!


当然也可以写成配置文件去完成,在~/.unison目录下面有个默认的配置文件default.prf
上述操作也可以这样来做
编辑默认的default.prf,添加:
root=/etc
root=ssh://root@192.168.1.101//root/station1_etc
batch=true

然后直接执行unison命令也可达到上述效果。

也可能想定时更新?或者是实时同步?使用crontab,inotify吧。。。。这里就不说了。。

unison的官方man文档 unison-manual.zip   
上一篇:awk操作指令图[转载]
下一篇:基于Linux服务器的性能分析与优化(2)