UNIX/Linux:如何使用统一文件同步器

时间:2020-01-09 10:43:56  来源:igfitidea点击:

我知道如何将rsync用于Unix系统,该系统将文件和目录从一个位置同步到另一个位置,同时最大程度地减少了数据传输。
但是,如何在一台计算机上或者一台计算机与另一台服务器之间的两个目录之间同步文件。
如何在多个服务器上维护相同版本的文件?

Unison是用于Unix和Windows的文件同步工具。

它允许将文件和目录集合的两个副本存储在不同的主机(或者同一主机上的不同磁盘)上,分别进行修改,然后通过将每个副本中的更改传播到另一个副本来使其更新。
这对于以下用途很有用:

  • 备份。
  • Web服务器群集。
  • 同步家庭和办公室文件。

安装unison

在RHEL/CentOS Linux下执行以下命令(确保已打开EPEL存储库):

# yum install unison

在Debian/Ubuntu Linux下执行以下命令:

# apt-get update && apt-get install unison

在FreeBSD下执行以下命令:

# cd /usr/ports/net/unison/ && make install clean

如何使用unison?

在此示例中,将/tmp/test1同步到/tmp/test2,如下所示:

# mkdir /tmp/test{1,2}
# cd /tmp/test1
# touch file{1,2,3}
# ls -l

输出示例:

total 12
-rw-r--r-- 1 root root 0 Aug 16 12:09 file1
-rw-r--r-- 1 root root 0 Aug 16 12:09 file2
-rw-r--r-- 1 root root 0 Aug 16 12:09 file3

现在,尝试同步它:

# unison /tmp/test1 /tmp/test2

输出示例:

Contacting server...
Connected [//Hyman-desktop//tmp/test1 -> //Hyman-desktop//tmp/test2]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
	/tmp/test1
	/tmp/test2
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 Jan 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 Jan 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

Press return to continue.[] Reconciling changes

test1          test2              
file     ---->            file1  [f] 
file     ---->            file2  [f] 
file     ---->            file3  [f] 

Proceed with propagating updates? [] y
Propagating updates

UNISON 2.27.57 started propagating changes at 12:11:18 on 16 Aug 2010
[BGN] Copying file1 from /tmp/test1 to /tmp/test2
[END] Copying file1
[BGN] Copying file2 from /tmp/test1 to /tmp/test2
[END] Copying file2
[BGN] Copying file3 from /tmp/test1 to /tmp/test2
[END] Copying file3
UNISON 2.27.57 finished propagating changes at 12:11:18 on 16 Aug 2010

Saving synchronizer state
Synchronization complete  (3 items transferred, 0 skipped, 0 failures)

-batch(批处理模式)选项完全不问任何问题,请执行:

# rm /tmp/test1/file3
# echo 'foo' >> /tmp/test2/file2
# unison -batch /tmp/test1 /tmp/test2

输出示例:

Contacting server...
Connected [//Hyman-desktop//tmp/test1 -> //Hyman-desktop//tmp/test2]
Looking for changes
Reconciling changes
                     file3  
test1        : deleted
test2        : unchanged file     modified on 2010-08-16 at 12:09:31  size 0         rw-r--r-
Propagating updates
UNISON 2.27.57 started propagating changes at 12:15:11 on 16 Aug 2010
[BGN] Updating file file2 from /tmp/test2 to /tmp/test1
[END] Updating file file2
[BGN] Deleting file3 from /tmp/test2
[END] Deleting file3
UNISON 2.27.57 finished propagating changes at 12:15:11 on 16 Aug 2010
Saving synchronizer state
Synchronization complete  (2 items transferred, 0 skipped, 0 failures)

远程服务器同步器

首先,请确保在本地和远程服务器上使用相同的版本。
使用以下命令测试本地统一客户端可以启动并连接到远程服务器:

# unison -testServer /tmp/test1 ssh://server1.theitroad.com//tmp/test1

输出示例:

Contacting server...
Connected [//Hyman-desktop//tmp/test1 -> //server1.theitroad.com//tmp/test1]

要同步,请执行:

# unison -batch /tmp/test1 ssh://server1.theitroad.com//tmp/test1

从server1.theitroad.com//tmp/test1目录中删除或者添加新文件,然后重试:

# unison -batch /tmp/test1 ssh://server1.theitroad.com//tmp/test1

样例Shell脚本

创建一个示例shell脚本,如下所示(sync.dirs.sh):

#!/bin/bash
# set paths / dirs
_paths="/var/www/html/ \
/etc/ \
/home/Hyman/ \
/projects/scripts/*.pl"
 
# binary file name
_unison=/usr/bin/unison
 
# server names 
# sync server1.theitroad.com with rest of the server in cluster
_rserver="server2.theitroad.com server3.theitroad.com"
 
# sync it
for r in ${_rserver}
do
	for p in ${_paths}
	do
        	${_unison} -batch "${p}"  "ssh://${r}/${p}"
	done
done

保存并关闭文件。
设置cronjob如下:

*/30 * * * * /path/to/sync.dirs.sh &>/tmp/sync.dirs.sh.log

确保设置ssh密钥或者使用钥匙串来避免出现密码提示。

如何按需调用unison?

您需要使用incrond(inotify cron守护程序)是一个守护程序,它监视文件系统事件(例如添加新文件,删除文件等)并执行命令或者Shell脚本。
它的用法通常类似于cron。
在此示例中,每当从/var/www/html上传或者删除文件时,请调用sync.dirs.sh(有关更多信息,请参见inotify FAQ):

/var/www/html IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /path/to/sync.dirs.sh