Synchronize Files between your servers

amjadmh73

Amjad Abujamous

Posted on September 8, 2024

Synchronize Files between your servers

Introduction

This tutorial teaches you how to automate the synchronization of two or more folders on different PCs or servers on the same network.

Buckle up!

1. Static IP Address

The first step is to set a static IP address on each of the devices in the network.

2. Setup ssh keys on both devices

Open up a terminal or WSL on windows and type in the following:

ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode
ssh-copy-id 192.168.1.15
Enter fullscreen mode Exit fullscreen mode

where 192.168.1.15 is the IP address of the secondary (or replica) server.

3. Install Unison

The file synchronization tool

Unison is a reliable file synchronization tool designed to be cross platform, fast, and efficient. One can install it via the releases page in their github repository.

Note that the tool must exist on all PCs that need to synchronize their folders.

4. Configure Unison

Folders and parameters

Configuring Unison is quite easy since it has a built-in GUI. Also, the configuration only needs to be done on the "primary" machine, and the others will periodically (or almost instantly) receive replicas of the files in a specific folder.

4.1 Extract Unison Archive

Extract the archive which has unison in it to your desired folder. In my case, it is ~/Tools/Unison.

Unison Folder

4.2 Launch Unison GUI

Navigate to the bin folder and execute the file unison-gui.

Unison GUI file

Unison GUI

4.3 Create a profile

A Unison profile specifies how the synchronization process will take place and which folders on which machines will be synchronized. To create one, click on "Add" and follow the Profile creation wizard.

Profile creation wizard

Profile Description:

  • Profile name: default
  • Description: Default profile

Connection Setup:

  • Synchronization kind: using ssh
  • Host: 192.168.1.15
  • User: remote_user (replaced by your username on the secondary device)
  • Enable compression: true

Directory selection:

  • Local Directory: Documents
  • Remote Directory: /home/remote_user/Documents

Specific Options:

  • [Unchecked] Synchronization invloving a FAT partition.

Done.

4.4 Configure the profile

Select default and click on "Edit" in the GUI.

Edit default profile

Add the following options by click on "Add", selecting the option, and then applying the value.

  • auto: true
  • batch: true
  • sshcmd: /home/remote_user/Tools/Unison/bin/unison (replace with the executable location on the remote/secondary machine)

4.5 Run unison

You can either run unison using the UI by selecting the profile and click on Open which will run the tool or by navigating to the executable location and typing

./unison default.prf
Enter fullscreen mode Exit fullscreen mode

That's it! Now the contents of the folder on the first machine should be mirrored on the second one. Note that the profile name would be different in case you'd want to run another.

5. Automate Unison

On the Linux bash, Mac Terminal, and WSL, one can automate the command and run it at a certain interval. On Ubuntu, for instance, the command below after navigting to the unison executables folder will do the trick.

while true; do ./unison default.prf; sleep 10; done 
Enter fullscreen mode Exit fullscreen mode

Where 10 is the number of seconds.

More on bash automation and sh files here.

Conclusion

Congratulations! You made it to the end of this tutorial. Hope this tutorial enabled you to do file synchronization seamlessly.

Cover image credit.

💖 💪 🙅 🚩
amjadmh73
Amjad Abujamous

Posted on September 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Synchronize Files between your servers
synchronization Synchronize Files between your servers

September 8, 2024