如何在Linux上使用Unison同步文件
本教程将引导我们逐步了解如何在Linux上安装Unison文件同步工具,以及如何在同一系统上的两个不同目录和/或者磁盘以及局域网上的两个不同Linux机器之间使用Unison同步文件。
Unison简介
Unison是一个免费,开源,多平台和双向文件同步工具。
它用于在两个不同系统或者同一系统上的不同磁盘上存储文件和目录的两个副本。
文件和目录的副本分别进行修改,然后通过将每个副本中的更改传播到另一个副本来进行更新。
简单来说,一个位置的任何更改都将复制到另一位置,反之亦然。
由于它是一个多平台应用程序,因此我们可以在运行不同操作系统的计算机之间同步文件和目录。
例如,我们可以将Linux计算机中存储的文件与Windows或者Unix计算机同步,反之亦然。
不仅文件内容,甚至文件所有权和权限也将同步。
如果两个副本上的内容均不同,则会导致冲突。
Unison将检测并显示此类冲突。
在这种情况下,我们需要指示Unison哪个副本应覆盖另一个副本。
不冲突的更新将自动传播。
Unison可以与连接到局域网或者Internet的任何两台计算机配合使用。
这些文件通过加密的ssh连接同步到远程系统。
Unison的另一个显着特征是,它可以在慢速链接(例如PPP连接)上正常工作。
与rsync相似,Unison将仅传输文件中已更改的部分。
Unison的rsync算法的内置实现可以非常快速地将更新传输到现有文件。
Unison是一个用户级程序,因此无需修改内核或者在任一主机上都具有超级用户特权。
Unison程序由Benjamin C. Pierce创建,并根据GPL许可发布。
它可以在GNU/Linux,Windows和Unix平台下运行。
在Linux上安装Unison
许多Linux操作系统的正式存储库中的默认存储库中都可以使用Unison。
要在Arch Linux及其变体上安装Unison,请运行:
$sudo pacman -S unison
Debian,Ubuntu,Linux Mint:
$sudo apt install unison
Fedora :
$sudo dnf install unison
CentOS 7:
首先,启用[EPEL]存储库:
$sudo yum install epel-release
然后安装统一:
$sudo yum install unison
openSUSE:
$sudo zypper install unison
对于那些喜欢GUI而不是CLI的用户,还有一个名为unison-gtk的简单图形界面。
在一些Linux发行版的默认存储库中可以找到它。
在Arch Linux及其变体上,使用以下命令安装Unison-gtk:
$sudo pacman -S unison-gtk2
如果我们使用的是基于Debian的系统,请使用以下命令进行安装:
$sudo apt install unison-gtk
请注意,如果要在两个不同系统之间同步文件,则必须在要保持同步的所有系统上安装Unison。
另外,建议在两个系统上使用相同的版本。
在Linux上与Unison同步文件
我们可以在以下三种不同的模式下使用Unison:
- 同步存储在同一台计算机上不同目录中的文件,
- 同步存储在同一台计算机上不同磁盘中的文件和目录,
- 在本地和远程计算机之间同步文件和目录:
- 通过直接套接字连接与远程计算机同步文件,
- 使用SSH与远程计算机同步文件。
1.同步存储在同一台计算机上不同目录中的文件
出于本教程的目的,我将创建两个根目录(即副本),即'directory1'和'directory2'。
$mkdir ~/directory1
$mkdir ~/directory2
将一些内容放在上述目录中的任何一个上。
我在'directory1'中存储了一些文件和目录。
让我们看一下“ directory1”的内容:
$ls -ltRF directory1/
输出示例:
directory1/: total 7780 drwxrwxr-x 2 sk sk 4096 Oct 2 15:35 dir1/ -rw-rw-r-- 1 sk sk 7779007 Sep 25 16:51 file2 -rw-rw-r-- 1 sk sk 179765 Aug 17 12:51 file1 directory1/dir1: total 140 -rw-rw-r-- 1 sk sk 141729 Aug 6 22:34 file3
如我们所见,我在'directory1'的根目录中有一个名为'dir1'的文件夹和另外两个文件,分别为'file1'和'file2'。
在“ dir1”内部,还有另一个名为“ file3”的文件。
而且“ directory2”副本没有任何内容。
$ls -ltRF directory2/ directory2/: total 0
让我们使用与命令一致的方式来同步这两个目录:
$unison directory1/directory2/
如果我们要继续将'directory1'的内容与'directory2'进行同步,将提示我们。
只需按“ ENTER”键。
接下来,Unison将要求我们确认要同步的文件和目录。
按“ ENTER”或者按“ f”键确认要同步的文件和文件夹。
最后,按“ y”并按“ ENTER”键继续进行更新。
Contacting server... Looking for changes Warning: No archive files were found for these roots, whose canonical names are: /home/sk/directory1 /home/sk/directory2 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 Reconciling changestinue.[<spc>]/file1 directory1 directory2 dir ----> dir1 [f] f file ----> file1 [f] f file ----> file2 [f] f Proceed with propagating updates? [] y Propagating updates UNISON 2.48.4 started propagating changes at 15:43:29.75 on 02 Oct 2017 [BGN] Copying dir1 from /home/sk/directory1 to /home/sk/directory2 [END] Copying dir1 [BGN] Copying file1 from /home/sk/directory1 to /home/sk/directory2 [END] Copying file1 [BGN] Copying file2 from /home/sk/directory1 to /home/sk/directory2 [END] Copying file2 UNISON 2.48.4 finished propagating changes at 15:43:29.81 on 02 Oct 2017 Saving synchronizer state Synchronization complete at 15:43:29 (3 items transferred, 0 skipped, 0 failed)
现在,“ directory1”的所有内容将被复制到“ directory2”。
我们可以使用“ ls”命令进行验证:
看到了吗?
两个副本的内容相同。
就像我已经提到的,两个位置上的文件许可权以及文件和文件夹的inode数量也同步。
我们可以在上面的输出中看到权限和索引节点也相同。
同步目录时,我们是否注意到输出中的箭头标记?
例如,我们应该已经注意到输出中间的以下几行:
directory1 directory2 dir ----> dir1 [f] f file ----> file1 [f] f file ----> file2 [f] f
箭头标记指示更改需要传播的方向。
在我们的例子中,箭头从左到右指向。
意味着'dir1','file1'和'file2'被传播到第二个副本,例如:'directory2'。
现在在“ directory2”副本中创建一个新文件,看看会发生什么。
$touch directory2/file4
使用以下命令同步两个副本:
$unison directory1/directory2/
现在,我们将看到一个箭头,从右向左指向。
这意味着第二副本中的更改将传播到第一副本。
换句话说,“ directory2”的内容被复制到“ directory1”。
Contacting server... Looking for changes Reconciling changes directory1 directory2 <---- new file file4 [f] f Proceed with propagating updates? [] y Propagating updates UNISON 2.48.4 started propagating changes at 16:04:08.78 on 02 Oct 2017 [BGN] Copying file4 from /home/sk/directory2 to /home/sk/directory1 [END] Copying file4 UNISON 2.48.4 finished propagating changes at 16:04:08.78 on 02 Oct 2017 Saving synchronizer state Synchronization complete at 16:04:08 (1 item transferred, 0 skipped, 0 failed)
Unison将自动识别两个副本中需要更新的文件。
首次运行Unison时,它将记录两个位置中所有文件和文件夹的修改时间戳记,权限,所有权和索引节点号。
基于此信息,当我们再次运行同一命令时,Unison将决定需要更新哪些文件。
如前所述,所有信息都将保存在“~/.unison”目录下。
要自己进行验证,请修改现有文件的内容。
我将修改“ directory1”副本中“ file4”的内容。
$echo "Welcome to theitroad" > directory1/file4
现在再次开始同步:
$unison directory1/directory2/
这次Unison将同步修改后的文件。
Contacting server... Looking for changes Reconciling changes directory1 directory2 changed ----> file4 [f] f Proceed with propagating updates? [] y Propagating updates UNISON 2.48.4 started propagating changes at 16:05:27.13 on 02 Oct 2017 [BGN] Updating file file4 from /home/sk/directory1 to /home/sk/directory2 [END] Updating file file4 UNISON 2.48.4 finished propagating changes at 16:05:27.14 on 02 Oct 2017 Saving synchronizer state Synchronization complete at 16:05:27 (1 item transferred, 0 skipped, 0 failed)
从上面的输出中可以看到,Unison已检测到第一个副本中的“ file4”已更改,并将其与第二个副本同步。
如果两个副本都被修改且它们的内容不同,则更改将发生冲突。
在这种情况下,Unison将询问用户如何在两侧传播更新。
冲突将用左右箭头显示,如下所示。
directory1 directory2 new file <-?-> new file file6 []
如果箭头指示某些默认操作,只需按“ ENTER”键继续下一个更改的文件。
如果要对该文件执行其他操作,则可以按'“ <”'或者'“>”,以强制将更改从右向左或者从左向右传播。
directory1 directory2 new file <==== new file file6 [] <
如果我们不想进行任何更改,只需按'“ /”'跳过此文件,并保留两个副本。
1.1. Unison的个人资料
Unison使用配置文件在同一台或者两台不同计算机上的两个目录之间同步文件和文件夹。
配置文件不过是一个文本文件,我们其中定义了根目录的实际路径以及应该同步的路径首选项。
所有配置文件都将存储在“ $HOME”目录中的“ .unison”目录中。
首次运行Unison时,会在'~/.unison'目录下创建一个名为'default.prf'的默认配置文件。
默认配置文件未定义任何首选项。
因此,存储在一个副本中的所有内容都将同步到另一副本。
我们还可以使用路径首选项列表创建自定义配置文件。
这将避免同步整个两个副本。
使用自定义配置文件,我们可以指示Unison:
- 同步特定的文件和文件夹,
- 从同步中忽略特定文件或者特定类型的文件。
让我们来看一个例子。
创建两个副本,并将一些文件和文件夹保存在一个副本中,将另一个保留为空。
$mkdir ~/directory1
$mkdir ~/directory2
然后,创建一个名为'dir_profile.prf'的新配置文件:
$nano .unison/dir_profile.prf
定义根目录以及要同步的文件和目录的路径:
# Roots of the synchronization root = /home/sk/directory1 root = /home/sk/directory2 # Paths to synchronize path = dir1 path = file2
两个根可以任意顺序给出。
Unison会在执行其他任何操作之前将其分类为规范的顺序。
如果我们未指定任何路径,Unison将从给定的一对根开始简单地同步两个完整副本。
如果给出了一个或者多个路径首选项,则Unison将仅同步这些路径及其子级。
在上面的示例中,我定义了“ dir1”和“ file2”应同步到其他根目录。
保存并关闭文件。
创建概要文件后,使用以下命令启动同步过程:
$unison dir_profile.prf
如果我们要继续将'directory1'的内容与'directory2'进行同步,将提示我们。
只需按“ ENTER”键,然后按“ f”键以确认要同步的路径。
最后按“ y”并按“ ENTER”键继续进行更新。
Contacting server... Looking for changes Warning: No archive files were found for these roots, whose canonical names are: /home/sk/directory1 /home/sk/directory2 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 Reconciling changestinue.[<spc>] | dir1 directory1 directory2 dir ----> dir1 [f] file ----> file2 [f] Proceed with propagating updates? [] y Propagating updates UNISON 2.48.4 started propagating changes at 15:42:32.99 on 01 Oct 2017 [BGN] Copying dir1 from /home/sk/directory1 to /home/sk/directory2 [END] Copying dir1 [BGN] Copying file2 from /home/sk/directory1 to /home/sk/directory2 [END] Copying file2 UNISON 2.48.4 finished propagating changes at 15:42:33.04 on 01 Oct 2017 Saving synchronizer state Synchronization complete at 15:42:33 (2 items transferred, 0 skipped, 0 failed)
现在转到第二个副本“ directory2”,看看内容是否已同步。
1.1. 忽略同步文件
有时,我们可能不想同步特定文件或者特定类型的文件。
如果是这样,我们可以在配置文件中定义要忽略的文件,如下所示。
ignore=Name text.* ignore=Name .*~ ignore=Name *.pdf
有关更多示例配置文件,请参考此处。
1.2. 使用unison-gtk同步本地文件和目录
你们中有些人可能对命令行不满意。
值得庆幸的是,Unison有一个简单的图形界面,使用户可以轻松地创建配置文件并通过图形窗口基于配置文件同步文件。
确保已安装“ unison-gtk”软件包,如上面的安装部分所示。
从菜单或者应用程序启动器启动unison-gtk。
首次启动时,我们将看到配置文件选择窗口。
如果我们已经按照说明创建了配置文件,则将在此处看到它。
在配置文件窗口中,我们可以使用新的根目录和要同步的路径来创建新的配置文件。
或者,我们可以简单地编辑现有配置文件并修改根目录或者添加新文件或者更改要同步的路径。
如果我们不再需要个人资料,只需选择它,然后单击“删除”即可。
我将创建一个新的配置文件。
为此,请在配置文件选择窗口中单击添加按钮,然后单击转发。
为新个人资料输入合适的名称,并附上简短说明:
接下来,选择我们要执行的同步类型。
由于它是两个本地目录之间的同步,因此我选择了“本地”。
选择要同步的两个根目录。
如果根目录之一在FAT分区上,则选中以下复选框。
USB拇指驱动器通常是这种情况。
最后,单击“应用”以创建配置文件。
现在,新创建的配置文件将出现在配置文件选择窗口中。
选择它,然后单击“打开”。
现在,Unison图形将向我们显示要在两侧进行的更改。
就像Unison命令行界面一样,默认操作也由图形界面中的箭头指示。
如下面的屏幕快照所示,文件从左到右传播,例如:第一个副本到第二个副本。
如果副本的内容不相同,Unison会询问我们有关哪个副本应覆盖另一个副本的说明。
如果我们对更改的传播方向感到满意,请单击工具列上的“执行”,或者从菜单列中选择“同步->执行”。
同步成功完成后,我们将在“状态”部分下看到刻度线。
2.同步存储在同一台计算机上不同磁盘中的文件和目录
两个磁盘之间的同步文件与上面的完全相同。
只要确保我们提到了外部驱动器中根目录的正确路径即可。
我们现在知道如何在本地系统中相同硬盘和不同磁盘中的两个目录之间同步文件。
下一节说明了如何在两个不同的系统之间同步文件。
3.在本地和远程计算机之间同步文件和目录
如前所述,Unison可以通过直接套接字连接和加密的SSH连接在本地和远程系统之间同步文件。
如我们所知,SSH比套接字方法更好,更安全。
就本教程而言,我将使用SSH方法来同步文件。
确保在两个系统上都安装了openSSH和Unison。
另外,请确保两个系统都可以通过SSH进行通信。
请注意,本地和远程系统应具有相同版本的Unison。
要检查Unison的版本,请在本地和远程sysem上运行以下命令:
$unison -version unison version 2.48.4
由于文件将通过SSH同步,因此远程系统是否具有unison-gtk都无关紧要。
Unison命令行界面在远程计算机上就足够了!在本地计算机上,我们可以使用文本或者图形统一界面。
3.1. 通过命令行在本地和远程计算机之间同步文件和目录
现在,让我们通过运行以下命令,使用同步将本地系统中的“ directory1”同步到远程系统中的“ directory1”:
$unison directory1/ssh://Hyman@theitroad/directory1/
其中“ Hyman @ theitroad”是我的远程系统的用户名和ip地址。
系统将提示我们输入远程用户的密码。
接下来,按ENTER键以同意将本地计算机的“目录1”的内容与远程计算机的“目录” 1同步。
然后按“ f”键确认要同步的路径。
最后按“ y”并按“ ENTER”键继续进行更新。
Contacting server... Hyman@theitroad's password: Connected [//theitroad//home/sk/directory1 -> //ubuntuserver//home/theitroad/directory1] Looking for changes Warning: No archive files were found for these roots, whose canonical names are: /home/sk/directory1 //ubuntuserver//home/theitroad/directory1 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.[<spc>] Waiting for changes from server Reconciling changes local ubuntuserver file ----> file1 [f] f file ----> file2 [f] f Proceed with propagating updates? [] y Propagating updates UNISON 2.48.4 started propagating changes at 14:49:18.44 on 03 Oct 2017 [BGN] Copying file1 from /home/sk/directory1 to //ubuntuserver//home/theitroad/directory1 [BGN] Copying file2 from /home/sk/directory1 to //ubuntuserver//home/theitroad/directory1 [END] Copying file1 [END] Copying file2 UNISON 2.48.4 finished propagating changes at 14:49:18.45 on 03 Oct 2017 Saving synchronizer state Synchronization complete at 14:49:18 (2 items transferred, 0 skipped, 0 failed)
上面的命令会将“ directory1”的内容从本地系统复制到远程系统上的“ directory1”。
现在添加新文件或者更新现有文件,然后尝试再次同步。
我们应该在两边都看到类似的结果。
如果要将内容同步到远程系统上主目录以外的其他位置,则必须通过在远程主机名和路径开头之间添加一个另外的斜杠来指定远程目录的绝对路径,如下所示:
$unison directory1/ssh://Hyman@theitroad//sync/the/files/here/
如果我们不想同步整个目录,则只需创建一个自定义配置文件并定义要同步的路径即可,如标题1.1中所述。
上面的Unison个人资料。
3.2. 使用unison-gtk在本地和远程计算机之间同步文件和目录
从菜单或者启动器启动Unison-gtk应用程序。
在配置文件选择窗口中单击添加按钮以创建新的配置文件。
单击前进。
输入Unison配置文件名称及其描述,然后单击转发。
这是重要的一步。
从“同步类型”下拉框中选择“ ssh”。
输入远程系统的IP地址或者主机名以及远程用户名。
还要选中“启用压缩”框以提高数据传输性能。
选择要同步的本地和远程目录位置。
如果目录之一位于FAT分区上,则必须选中此选项。
U盘通常是这种情况。
单击“应用”完成配置文件的创建。
现在,新创建的配置文件将出现在配置文件选择窗口中。
选择配置文件,然后单击“打开”。
系统将要求我们输入远程用户的密码。
输入密码,然后单击确定。
输入密码后,Unison将列出要从本地传播到远程的更改。
默认操作用箭头指示。
如果我们不想执行默认操作,请按菜单列中的“从左到右”或者“从右到左”选项,以指示哪个副本应覆盖另一个副本。
最后,单击“ Go”(复制)。
现在,Unison将使用远程目录复制本地目录的全部内容。
同步完成后,我们将在“状态”部分下看到对勾标记。
我们可以在两个位置上验证文件和目录的内容。
上面的方法会将本地副本的全部内容同步到远程副本。
如果要同步特定的文件或者目录,请创建自定义配置文件并定义要同步的路径,如标题1.1中所述。
上面的Unison个人资料。
3.3. 设置基于SSH密钥的身份验证(可选)
每当我们通过SSH在两个不同系统之间同步文件时,都需要输入远程用户的密码。
如果我们不想每次都输入密码,则可以按照以下链接中的说明设置无密码的SSH身份验证。
不要使用密码生成SSH密钥对。
否则,在系统之间同步文件时仍然需要输入密码。
3.4. 设置cron作业(可选)
我们可以使用cron作业在特定的时间间隔自动同步文件。
例如,在“ crontab”文件中添加以下条目以每天运行同步(它将在00:00运行):
0 0 * * * unison directory1/ssh://Hyman@theitroad//home/theitroad/directory1/
有关设置cron作业的更多详细信息,请参阅以下教程:
- Cron Jobs教程
获得帮助
我只介绍了基础知识。
这仅足以在Linux系统上设置有效的同步。
如果我们想了解更多高级用法,请参阅“ Unison帮助”部分。
要显示本地文档页面,请运行:
$unison -doc topics
我们将在本地文档中看到可用主题的列表。
Documentation topics: about About Unison people People lists Mailing Lists and Bug Reporting status Development Status copying Copying ack Acknowledgements install Installation tutorial Tutorial basics Basic Concepts failures Invariants running Running Unison ssh Installing Ssh news Changes in Version 2.48.4 Type "unison -doc <topic>" for detailed information about <topic> or "unison -doc all" for the whole manual
要查看有关主题的详细信息,例如统一教程,请运行:
$unison -doc tutorial
我们还可以使用以下命令显示整个Unison手册:
$unison -doc all
我还在本教程的结尾处添加了Unison的官方链接。
仔细阅读文档并熟悉Unison的工作方式。
学到足够的知识后,请尝试在生产环境中设置与Unison的本地和/或者远程同步。