2 Way Syncing between 2 Servers using lsyncd
Requirements
At least 2 servers
lsyncd installed on both servers
ssh access between both servers
Assumptions
username for both servers is: forge
IP for server #1: 111.111.111
IP for server #2: 222.222.222
Folder to sync between the 2 servers (can be different, this example both same path and directory): syncro
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
- Install lsyncd
sudo apt install lsyncd
- Create lsyncd log folder
sudo mkdir /var/log/lsyncd
- Create log and status files
sudo touch /var/log/lsyncd/lsyncd.{log,status}
- 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
- Start the lsyncd service
sudo service lsyncd start
- 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