DEV Community

Bryan
Bryan

Posted on • Edited on • Originally published at devlogbook.com

2

2 Way Syncing between 2 Servers using lsyncd

Requirements

  1. At least 2 servers
  2. lsyncd installed on both servers
  3. ssh access between both servers

Assumptions

  1. username for both servers is: forge
  2. IP for server #1: 111.111.111
  3. IP for server #2: 222.222.222
  4. Folder to sync between the 2 servers (can be different, this example both same path and directory): syncro
  5. SSH key (can be different, this example same key name): syncro_key

Setup

This setup is for server #1, to setup server #2 it's the same. Just see the comments in the code in step #4

  1. Install lsyncd
    • sudo apt install lsyncd
  2. Create lsyncd log folder
    • sudo mkdir /var/log/lsyncd
  3. Create log and status files
    • sudo touch /var/log/lsyncd/lsyncd.{log,status}
  4. Create config file

    • sudo nano /etc/lsyncd.conf.lua
    • Note: Anything after -- are comments in the code
    -- server #1 setup
    settings {
          logfile = "/var/log/lsyncd/lsyncd.log", -- location of log file created in step 3
          statusFile = "/var/log/lsyncd/lsyncd.status", -- location of status file created in step 3
    }
    
    targetlist = {
        "forge@222.222.222:/home/forge/syncro", -- server #2 will have target forge@111.111.111
    }
    
    for _, server in ipairs( targetlist ) do
        sync {
            default.rsync,
            source = "/home/forge/syncro", -- folder which to sync from
            target = server,
            delete = 'running',
            rsync = {
                rsh = "ssh -i /home/forge/.ssh/syncro_key", -- ssh key
                update = true,
                archive = true,
                compress = true,
            }
        }
    end
    
  5. Start the lsyncd service

    • sudo service lsyncd start
  6. Set the config lsyncd service will use

    • sudo lsyncd /etc/lsyncd.conf.lua
    • IMPORTANT When the server restarts run this again

Notes

There is a limit to how many files/folders that can be watched

  • To check the current limit:
    • cat /proc/sys/fs/inotify/max_user_watches
  • To permanently alter the limit : (500000 is an example number)
    • sudo sysctl fs.inotify.max_user_watches=500000

Image of Stellar post

From Hackathon to Funded - Stellar Dev Diaries Ep. 1 🎥

Ever wondered what it takes to go from idea to funding? In episode 1 of the Stellar Dev Diaries, we hear how the Freelii team did just that. Check it out and follow along to see the rest of their dev journey!

Watch the video

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay