2 Way Syncing between 2 Servers using lsyncd

·

2 min read

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
  1. Create lsyncd log folder
  • sudo mkdir /var/log/lsyncd
  1. Create log and status files
  • sudo touch /var/log/lsyncd/lsyncd.{log,status}
  1. 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
  1. Start the lsyncd service
  • sudo service lsyncd start
  1. 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