双向同步镜像软件Unison!!!

本文详细介绍了如何在两台服务器间通过SSH和unison软件实现数据的双向同步更新,包括配置步骤、数据同步过程及验证方法。通过实例演示,展示了如何确保服务器数据一致性,特别适用于需要实时数据同步的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目背景:

公司有两台服务器,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           # -batch             batch mode: ask no questions at all

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://root@192.168.0.12//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://root@192.168.0.12//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目录的一模一样!!!!



总结:双向同步更新,有时候特别有用!希望大家能在实际的生产中懂得它的妙用!谢谢大家!!

  首先,打开Update Now!,在“动作”里面选择增加主机,软件会弹出一个对话框,要求输入用户名和密码,你就将你申请主页得来的名称以及密码输入,然后确定。Update Now!会让您输入本地初始地址,也就是那些网页资料在你 氐挠才躺厦娴哪柯剂耍绻愕耐辰峁估锩婧凶幽柯迹捅匦虢旅娴囊桓鲂】蛞惭∩希缓笤俳邢乱徊健pdate Now!会要求您输入主页提供商的名称,系统也提供了几个缺省的主页提供商,如果你的主页是建设在那几个地址上的,那么几乎就不需要进行什么设置了。Update Now!提供了“深圳热线963.net、衡阳电信、自贡169、保定热线”等四个站点。如果你的站点是其他的,就要选择下面的“手工设置”了。   接着再进行下一步,Update Now!会要求你输入主机的地址,主要不能输入“http://”或者“ftp://”等等前缀,输入地址之后就是FTP端口,一般选择缺省的“21”就足够了。然后就是输入主机的初始路径,譬如“碧海银沙”的主页一般是“/personal/用户名”,网页的一般是“public_html”,输入了之后再“下一步”,Update Now!就会将刚才你所输入的东西重新显示出来,让您对正一下,好了,一切OK,选择完成,主机添加完成。如果你的主页没有更改,那么无论你怎么按“开始”,Update Now!都不会有动作的,如果每次你对主页进行了文件更新之后,想看看Update Now!会对其作什么改动,又或者您想对站点的设置进行详细的修改,可以在已经添加的主机名称上面按鼠标右键,再选择“编辑”,就可以对一些细节进行调整。在里面你可以对新、旧文件进行更新碰上一些你不希望Update Now!帮你更新的文件,你可以在新文件的菜单里面删除它,同样如果你想重新上载一些没有经过改动的文件,同样可以添加到新文件菜单里面。不过我在这里要说Update Now!的一句坏话,因为如果你对主页的某些文件做了删除,Update Now!是不能帮你自动删除了,只能靠你自己登录上去了。   如果您想修改一些关于FTP参数的设置,您可以在选项的按钮上面按一下,进入菜单之后,您就可以去修改譬如:ACSII文件、对话框显示时间、代理服务器地址、文件上载顺序、自动拨号、网络超时等等项目了。总之,Update Now!的功能是非常强大而且详尽的,经常更新主页的您一定不能错过!   可以这么说,Update Now!出现大大方便了我们这些喜欢做主页的网虫!因为是国产的共享软件,我们更要支持!要注册(实在好用,笔者已经决意注册一个了!不贵,30元!)!到作者的主页http://newsoft.126.com里面就可以找到软件和注册方法了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值