项目背景:
公司有两台服务器,A和B,保持A和B的数据完全一致,任何一台服务器数据改变,都会更新到另一台!
实验环境:
vmware workstation 11
服务器A:ip:192.168.0.19 iptables关闭 setenforce 0
服务器B:ip:192.168.0.12 iptables关闭 setenforce 0
SecureCRT (ssh远程连接软件)
实验流程:
一、配置双机ssh信任:
下面这个连接是我写的如何配置双机ssh信任的教程
http://9399369.blog.51cto.com/9389369/1750915
二、unison软件安装
直接用yum安装
[root@baculaServer ~]# yum install -y unison #主机A上
[root@Slave-drbd ~]# yum install -y unison #主机B上
安装完以后就直接可以用了。
三、本地使用unison
1:创建测试目录test1 test2
[root@baculaServer ~]# mkdir test1 test2
2:在test1里面创建两个测试文件,test2不做修改
[root@baculaServer ~]# cd test1/
[root@baculaServer test1]# ifconfig >testfile1.txt
[root@baculaServer test1]# ping -c 3 www.baidu.com >testfile2.txt
3:数据同步
[root@baculaServer ~]# unison test1 test2 -batch
Contacting server...
Connected [//baculaServer //root/test1 -> //baculaServer //root/test2]
Looking for changes
Reconciling changes
deleted ----> hgfs
test1 : deleted
test2 : unchanged dir modified on 2016-03-14 at 0:54:37 size 0 rwxr-xr-x
new file ----> test1.txt
test1 : new file modified on 2016-03-14 at 1:15:57 size 889 rw-r--r--
test2 : absent
new file ----> test2
test1 : new file modified on 2016-03-14 at 1:16:19 size 548 rw-r--r--
test2 : absent
Propagating updates
UNISON 2.27.57 started propagating changes at 01:17:41 on 14 Mar 2016
[BGN] Copying test1.txt from /root/test1 to /root/test2
[END] Copying test1.txt
[BGN] Copying test2 from /root/test1 to /root/test2
[END] Copying test2
[BGN] Deleting hgfs from /root/test2
[END] Deleting hgfs
UNISON 2.27.57 finished propagating changes at 01:17:41 on 14 Mar 2016
Saving synchronizer state
Synchronization complete (3 items transferred, 0 skipped, 0 failures)
4:测试
[root@baculaServer ~]# cd test2
[root@baculaServer test2]# ls
test1.txt test2
可以看到我们没有创建这俩文件在test2目录,但是现在出现了!!!yes
四、远程使用unison
[root@baculaServer ~]# unison /test1 ssh://[email protected]//mnt -batch
Contacting server...
Connected [//Slave-drbd//mnt -> //baculaServer //test1]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
/test1
//Slave-drbd//mnt
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
<---- dir /
local : absent
Slave-drbd : dir modified on 2016-03-14 at 1:22:20 size 0 rwxr-xr-x
Propagating updates
UNISON 2.27.57 started propagating changes at 01:23:58 on 14 Mar 2016
[BGN] Copying from //Slave-drbd//mnt to /test1
[END] Copying
UNISON 2.27.57 finished propagating changes at 01:23:58 on 14 Mar 2016
Saving synchronizer state
Synchronization complete (1 item transferred, 0 skipped, 0 failures)
查看服务器B
[root@Slave-drbd ~]# cd /mnt/
[root@Slave-drbd mnt]# ls
hgfs testfile1.txt testfile2.txt
可以看到我们多增的这两个文件。
五、删除B服务器的ifconfig.txt,看一下能不能同步成功
这是在服务器B上执行的:
[root@Web1 tmp]# \rm ifconfig.txt
[root@Web1 tmp]# cat ifconfig.txt
cat: ifconfig.txt: No such file or directory
在服务器A上执行同步命令
[root@Server fuchao1]# unison /fuchao1/ ssh://[email protected]//tmp -batch
查看/fuchao1目录
[root@Server fuchao1]# ls
ks-script-HprTeo virtual-02.bd0Nzs VMwareDnD
ks-script-HprTeo.log virtual-02.Nvhjul vmware-root
ping.txt vmware-02 yum.log
pulse-eft3MC9bdiQw vmware-config0 yum_save_tx-2016-03-14-00-51vq7ClS.yumtx
可以看到我们现在/fuchao1目录和服务器B/tmp目录的一模一样!!!!
总结:双向同步更新,有时候特别有用!希望大家能在实际的生产中懂得它的妙用!谢谢大家!!