Logo

dev-resources.site

for different kinds of informations.

Synchronize Files between your servers

Published at
9/8/2024
Categories
synchronization
production
sre
automation
Author
amjadmh73
Author
9 person written this
amjadmh73
open
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.

production Article's
30 articles in total
Favicon
The Making of the Zip Ship Hi-Tech Ultimate Go-Cart Indiegogo Campaign Video
Favicon
Synchronize Files between your servers
Favicon
Dulces Suenos Spanish Pop (Sample Packs)Download
Favicon
PostgreSQL fΓΌr django aufsetzen - django in Produktion (Teil 2)
Favicon
Industrial Juicers: Enhancing Juice Production Capabilities
Favicon
Cloudflare Tunnels VS ngrok
Favicon
In Laravel, always use the env() within config files and nowhere else.
Favicon
How to Set Up Multiple PostgreSQL Instances on a Single Server
Favicon
Use same Dockerfile for Dev & Production
Favicon
Integrating Vite with Flask for Production
Favicon
Everybody Dumps Production At Least Once
Favicon
The Dangers of Using the Same Database for Development and Production
Favicon
Dev Deletes Entire Production Database
Favicon
Mastering Chrome DevTools: Edit production code on-the-fly in your browser ✏️
Favicon
Best way to run Migrations in Production
Favicon
Why should you use a hidden replica set member
Favicon
Software upgrade checklist in production
Favicon
Running CockroachDB on k8s - with tweaks for Production
Favicon
Where engineering and creative production worlds clash!
Favicon
Increasing Product Release Velocity by Debugging and Testing In Production
Favicon
Next.js in Production: Best Practices and Common Pitfalls
Favicon
Deploy a containerised Fast API application in Digital Ocean
Favicon
Production incidents - 7 practical tips to help you through your next incident
Favicon
Fix Page not found error when visiting a route directly in react
Favicon
AWS Amplify - Deploy your application in minutes.
Favicon
Trying Streamyard for various things
Favicon
How to deal with data changing and machine learning models doing worse after training
Favicon
[BTY] Day 10: Real-time machine learning: challenges and solutions - Huyen Chip
Favicon
Installing Gem in Production Rails console
Favicon
Production-Ready Docker Configuration With DigitalOcean Container Registry Part I

Featured ones: