What is Unison?
The Unison technology developed by Benjamin C Pierce under the GPL License allows users to synchronize two single file instances on the same system but different disks or two other systems.
Unison allows you to keep an updated and identical version of the same file or directory. It’s utilizable in a wide variety of contexts, such as SSH, allowing secure transfer of data between devices or directories.
The Unison tool is cross-platform, allowing users to sync files and directories between systems running the same or different operating systems. For example, you can sync files from a Linux system to a BSD system. Because of its security features, Unison syncs files and directories and applies ownership and permissions rights on both systems.
The Unison tool is developed with security in mind and can survive failures where interruptions happen between syncing points. If Unison detects file and directory conflicts, it reports the existing conflicts and allows the users to select which version of the files should apply.
Unison can work with—or supports—slow networks, making it a very efficient tool for bandwidth usage. It only synchronizes parts and directories that have changed, thus preventing heavy load and updating the sync process very easily and quickly.
Despite its primary usage context, Unison is a powerful tool that can be modified to create a wide range of use cases.
This tutorial will discuss how to install and setup the Unison File Synchronization System on Debian 10.
How to Install Unison on Debian 10
Unison is a popular tool; because of that, it’s available in official repositories of major Linux distributions.
To install the command-line version of Unison on Debian, start updating the system:
Next, use the simple apt to command to install the tool.
If you have a desktop environment installed on your system and prefer a GUI version of the tool, install unison-gtk using the command:
NOTE: Ensure you install Unison on both versions of the systems you wish to sync files between them. For compatibility and to minimize the chances of errors, it is better to have the same version of Unison in both systems.
How to Sync Files with Unison
Unison provides us with ways to sync files and directories between local machines and remote machines using direct socket connections and SSH. Since we are syncing between two Debian servers, we will skip local machine synchronization.
Unison Remote and Remote Sync via SSH
To synchronize files and directories between remote machines, you will need to have Unison installed in both systems, have SSH access to both the systems and their IP addresses.
You can also use your local machine as one server and the remote device as the other server.
- 192.168.0.200 – server1
- 192.168.0.201 – server2
The first step is to create a private and public key on the remote server1.
Generating public/private rsa key pair.
Enter file in which to save the key (/home/csalem/.ssh/id_rsa):
Created directory '/home/csalem/.ssh'.
Enter passphrase (empty for no passphrase):
Enter the same passphrase again:
Your identification has been saved in /home/csalem/.ssh/id_rsa.
Your public key has been saved in /home/csalem/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:4p5YuEBEa5lrINzqPgwHRVhSS0TNlIIl70A0q2MFcS8 csalem@Debian-PC
The key’s random art image is:
+---[RSA 2048]----+
|+/@=.. |
|=*O== |
|++OE . |
|+*o.. |
|o+= . S |
|++. o . |
|oo. . o |
|.o . = . |
| .. o o |
+----[SHA256]-----+
Quickly skip over the key generating process and save the file. Please do not add a passphrase because it can hinder unmonitored sync between the servers.
Next, copy the key from server1 to server2 using the command:
Now that you have SSH setup on both systems, you can start Unison to sync files and directories.
Let’s test the use of Unison on both servers by creating the directory we will sync on server1:
On server2, create a directory as above
As mentioned, Unison respects user permissions. Therefore, it is best to change permissions and ownership in both directories to ensure both directories have read and write privileges.
Next, add all the data you wish to synchronize between the servers in dir1. For example, add a few zip files.
Having the files, we want to sync in the right directory, run unison command to sync from server one as:
On the first Unison launch, you will receive a warning that the syncing process can take a while. If you have large files in the directories, this can take a while, but since we are only using it for testing purposes, it should be relatively quick.
Press Enter to start the sync process. Once the sync process completes, go to server2, navigate to dir2, and list the contents of the directory as:
-rw-r--r-- 1 root root 436 Jan 5 12:57 Documents.zip
Modifying Unison Configuration
In most cases, we do not want to interactively sync files between these servers as the process is repetitive and inefficient. We can edit the Unison configuration file to allow automatic sync files.
Use the command below to edit the default configuration file:
In the file, add the following lines:
batch=true
The above lines disable prompts when syncing files, automating the process.
Setting up Unison Cronjob
One sure thing is that programmers are very averse to doing things manually, especially when it’s possible to automate, which is the case here. We can set up a cronjob for Unison to be performing sync automatically.
In server1, create a simple bash script to run unison command as:
In the file, start with a shebang and add the unison sync command:
unison /dir1 ssh://192.168.0.201//dir2
As usual, make the script execute permissions:
Now edit your cronjob file with crontab:
If this is your first time editing crontab—I hope it’s not—choose your editor and enter the following line to sync with Unison every 1 hour:
You can also set up the crontab to run at specific times of the day. You also do not need to use a script and can use a raw command in the crontab.
Conclusion
This tutorial showed you how to install and setup Unison on a Debian system for files and directories synchronization.
Although this quick guide can help you get started, you can learn more by using Unison man pages (manual pages) or the official documentation using the resource provided below:
from Linux Hint https://ift.tt/3sS4IlW
0 Comments