在Debian 10(Buster)上的两台服务器之间设置Unison文件同步

在Debian 10(Buster)上的两台服务器之间设置Unison文件同步

本教程将向您展示如何使用Unison在两个Debian 10服务器之间设置文件同步。 Unison是类似于rsync的文件同步工具。最大的区别是跟踪/同步两个方向的更改。即,在server1上修改的文件在server2上重复,反之亦然。

1引言

在本教程中,我们使用两个Debian服务器:

  • server1.example.com,IP地址为192.168.0.100
  • server2.example.com和IP地址192.168.0.101

在两个服务器之间同步目录/ var / www。对于本教程,请以root用户身份运行Unison,以便Unison具有足够的权限来同步用户和组权限。

本教程中的所有命令均以root用户身份运行。以root用户身份登录Shell上的两台服务器,然后从步骤2开始。Unison安装

2 Unison安装

服务器1 /服务器2:

必须在server1和server2上安装Unison。由于我们使用SSH从server1连接到server2,因此我们还需要SSH软件包并安装nano编辑器以在shell中编辑文件。这可以如下实现。

apt-get -y install unison openssh-server ssh nano

3在server1上创建私钥/公钥对

服务器1:

接下来,在server1.example.com上创建一个私钥/公钥对。

ssh-keygen -t dsa

[email protected]:〜#Ssh-keygen -t dsa生成公共/私有dsa密钥对。输入es es s! (/Root/.ssh/id_dsa):<-- ENTER创建目录//root/.ssh'。如果没有则输入无密码):<-Enter重新输入相同的密码:<-ENTER ID为/它保存在root / .ssh / id_dsa中。您的公共密钥保存在/root/.ssh/id_dsa.pub.key。指纹为:ba:82:e1:a1:42:9b:d4:c8:99:c8:bd:8b:7d:4d:d4:66 [email protected]密钥的随机图是: +-[DSA 1024]---- + | || ||。 || E || + *。 S || .Ooo o || ooo +。 + || oo = ... o || .. oo .. | + ----------------- +[email protected]:〜#

请勿输入密码,这一点很重要。否则,如果没有人工干预,镜像将无法工作。只需按Enter键!

然后将公钥复制到server2.example.com。

ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
# ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
The authenticity of host '192.168.1.102 (192.168.0.101)' can't be established.ECDSA key fingerprint is 2b:3c:35:ad:3d:e2:fc:16:2f:55:5c:e1:2c:d7:3d:a9.Are you sure you want to continue connecting (yes/no)? <-- yes (you will see this only if this is the first time you connect to server2)/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys[email protected]'s password: <-- server2 root password
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"and check to make sure that only the key(s) you wanted were added.

现在检查server2以查看server1的公钥是否正确传输。

服务器2:

cat $HOME/.ssh/authorized_keys
[email protected]:/home/administrator# cat $HOME/.ssh/authorized_keysssh-dss AAAAB3NzaC1kc3MAAACBAKHLdAztIr8muZIlQYuE/4f75kmgTwWqJRZJ1dTqHDnHWsy48emDU8v85hxAPg43k9aF7/zAwpA0MNNNk5T9Tx/DyUkK/KcyVP2f4p8tvovrkUvoxsZACkTUmFqKdq2x6/AGfjsCRmkpLhZuad7r5rKEXHRh8KYGHqD1Id8wcpy5AAAAFQCww3OekKcKMshMAwBK3XQmmYEGUwAAAIEAgjztlwh8OFYxwQve/RrhI2sceCXwS/yjQyH7q0zdWB9Fr4s/16T2PLBT+7M3vb+JlPDO3JRqgaYbel1kS2F2iKrY0EX0FI3/9fVDfWoz3mhCscPLriqy5AcsHitxQNfiZgA5wDiSjWpk1v+FbIC+VuqbKdQuE4MBKj19N9YALIUAAACABQ4NDsa2UBc8jsxvghjoLhUWF7HChaCksXQcL6i98VNRcemtPC6wpIri75iR4Uhv1666bDOBAdmIBX9Qf7A/+czPKPaj4CGI1hVy1pgYMa3btnEvoSnH/ONtjpOz9q+3up1OOOn+5fud7xjJn+Fq8WoGROgarBpCbQU3w2GUUnM= [email protected]

4 Run Unison

服务器1:

现在,您可以首次运行Unison并同步两台服务器上的/ var / www目录。在server1上运行:

unison /var/www ssh://192.168.0.101//var/www

输出与此类似,这是Unison第一次运行,因此您可能需要回答一些问题:

[email protected]:/var/www# unison /var/www ssh://192.168.0.101//var/wwwContacting server...Connected [//server1//var/www -> //server2//var/www]Looking for changesWarning: No archive files were found for these roots, whose canonical names are:/var/www//server2//var/wwwThis can happen eitherbecause this is the first time you have synchronized these roots,or because you have upgraded Unison to a new version with a differentarchive format.
Update detection may take a while on this run if the replicas arelarge.
Unison will assume that the 'last synchronized state' of both replicaswas completely empty. This means that any files that are differentwill be reported as conflicts, and any files that exist only on onereplica 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 machinesis getting its address from DHCP, which is causing its host name to changebetween synchronizations. See the documentation for the UNISONLOCALHOSTNAMEenvironment 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.[] <-- Press Enter
Waiting for changes from serverReconciling changes
local server2dir ----> example.com [f] <-- Press Enterdir ----> example.de [f] <-- Press Enter
Proceed with propagating updates? [] <-- Enter "y"Propagating updates
UNISON 2.48.4 started propagating changes at 13:24:01.10 on 05 May 2020[BGN] Copying example.com from /var/www to //server2//var/www[BGN] Copying example.de from /var/www to //server2//var/wwwShortcut: copied /var/www/example.de/web/index.html from local file /var/www/.unison.example.com.d3783bddaaf59b9ba4d2ed0433f9db63.unison.tmp/web/index.html[END] Copying example.de[END] Copying example.comUNISON 2.48.4 finished propagating changes at 13:24:01.98 on 05 May 2020
Saving synchronizer stateSynchronization complete at 13:24:01 (2 items transferred, 0 skipped, 0 failed)

如果检查server1和server2上的/ var / www目录,则可以看到它们是同步的。

当然,您不想以交互方式运行Unison,因此可以创建一个配置文件(/root/.unison/default.prf),其中包含您需要在命令行上指定的所有设置。

nano /root/.unison/default.prf
# Roots of the synchronization
root = /var/www
root = ssh://192.168.0.101//var/www

# Paths to synchronize
#path = current
#path = common
#path = .netscape/bookmarks.html

# Some regexps specifying names and paths to ignore
#ignore = Path stats    ## ignores /var/www/stats
#ignore = Path stats/*  ## ignores /var/www/stats/*
#ignore = Path */stats  ## ignores /var/www/somedir/stats, but not /var/www/a/b/c/stats
#ignore = Name *stats   ## ignores all files/directories that end with "stats"
#ignore = Name stats*   ## ignores all files/directories that begin with "stats"
#ignore = Name *.tmp    ## ignores all files with the extension .tmp

#          When set to true, this flag causes the user interface to skip
#          asking for confirmations on non-conflicting changes. (More
#          precisely, when the user interface is done setting the
#          propagation direction for one entry and is about to move to the
#          next, it will skip over all non-conflicting entries and go
#          directly to the next conflict.)
auto=true

#          When this is set to true, the user interface will ask no
#          questions at all. Non-conflicting changes will be propagated;
#          conflicts will be skipped.
batch=true

#          !When this is set to true, Unison will request an extra
#          confirmation if it appears that the entire replica has been
#          deleted, before propagating the change. If the batch flag is
#          also set, synchronization will be aborted. When the path
#          preference is used, the same confirmation will be requested for
#          top-level paths. (At the moment, this flag only affects the
#          text user interface.) See also the mountpoint preference.
confirmbigdel=true

#          When this preference is set to true, Unison will use the
#          modification time and length of a file as a `pseudo inode
#          number' when scanning replicas for updates, instead of reading
#          the full contents of every file. Under Windows, this may cause
#          Unison to miss propagating an update if the modification time
#          and length of the file are both unchanged by the update.
#          However, Unison will never overwrite such an update with a
#          change from the other replica, since it always does a safe
#          check for updates just before propagating a change. Thus, it is
#          reasonable to use this switch under Windows most of the time
#          and occasionally run Unison once with fastcheck set to false,
#          if you are worried that Unison may have overlooked an update.
#          The default value of the preference is auto, which causes
#          Unison to use fast checking on Unix replicas (where it is safe)
#          and slow checking on Windows replicas. For backward
#          compatibility, yes, no, and default can be used in place of
#          true, false, and auto. See the section "Fast Checking" for more
#          information.
fastcheck=true

#          When this flag is set to true, the group attributes of the
#          files are synchronized. Whether the group names or the group
#          identifiers are synchronizeddepends on the preference numerids.
group=true

#          When this flag is set to true, the owner attributes of the
#          files are synchronized. Whether the owner names or the owner
#          identifiers are synchronizeddepends on the preference
#          extttnumerids.
owner=true

#          Including the preference -prefer root causes Unison always to
#          resolve conflicts in favor of root, rather than asking for
#          guidance from the user. (The syntax of root is the same as for
#          the root preference, plus the special values newer and older.)
#          This preference is overridden by the preferpartial preference.
#          This preference should be used only if you are sure you know
#          what you are doing!
prefer=newer

#          When this preference is set to true, the textual user interface
#          will print nothing at all, except in the case of errors.
#          Setting silent to true automatically sets the batch preference
#          to true.
silent=true

#          When this flag is set to true, file modification times (but not
#          directory modtimes) are propagated.
times=true

注释应描述文件,但路径指令除外。如果不指定路径指令,则根指令目录将同步。如果您指定路径指令,则该路径是相对于根路径的(例如,root = / var / www,而path = current会转换为/ var / www / current),而这些都不是整个指定目录,在根指令中将仅同步的子目录。

有关可用选项的更多详细信息,请参见Unison手册页。

man unison

我已将所有设置都放在配置文件中(尤其是root(以及可选的path)指令),因此我可以不带任何参数地运行Unison。

unison

5创建Unison Cron工作

服务器1:

我想自动执行同步,因此在server1.example.com上创建cron作业。

crontab -e
*/5 * * * * /usr/bin/unison &> /dev/null

这将导致Unison每5分钟运行一次。必要时进行调整(请参阅

man 5 crontab

)。我正在使用Unison的完整路径(/ usr / bin / unison),以确保cron知道Unison在哪里。 Unison的位置可能会有所不同。投放广告

which unison

找出你在哪里。

6测试一致

接下来,测试Unison的双向同步,以查看您的设置是否功能齐全。

在server1上运行以下命令以创建内容为“ Test 1”的测试文件。

服务器1

echo "Test 1" > /var/www/test.txt

现在至少等待5分钟(因为我创建了一个每5分钟运行一次的cronjob)。然后在server2上运行它。

cat /var/www/test.txt

在屏幕上显示文件test.txt的内容。输出将类似于此屏幕截图。

然后在server2上运行以下命令以将测试文件的内容更新为“ Test 2”。

服务器2

echo "Test 2" > /var/www/test.txt

然后至少等待5分钟。然后在server1:Advertisements上运行cat命令。

服务器1

cat /var/www/test.txt

输出看起来像这样:

档案已同步

Source

Sidebar