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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay