LSyncd Realtime File Replication
Near realtime replication of files from source app1 to destination app2 server. Files will be replication near realtime, and deleted files on source will be deleted in the destination. This is useful to mirror application files (eg. .php) and uploaded files to standby servers. Database replication needs to be configured separately.
Prerequisites
sshbetween both servers asrootwithout password, setup ssh keys correctly.- Increase
sysctlfile watch limit so large number of files can be watched and replicated. rsyncneeds to be installed on both source and destination servers.lsyncneeds to be installed only on the source server.- Run
mkdir /var/log/lsyncdbefore invoking lsyncd from the first time.
Note: If lsyncd does not start automatically via systemd, run systemctl stop lsyncd.service and then systemctl start lsyncd.service.
Filename: /etc/lsyncd/lsyncd.conf.lua
-- lsync config file to mirror app data from app1 to app2
-- Filename: /etc/lsyncd/lsyncd.conf.lua
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
insist = 1,
}
sync {
default.rsync,
delete = true,
source = "/srv/app.example.com/www/",
target = "app2:/srv/app.example.com/www/",
rsync = {
archive = true,
},
}
sync {
default.rsync,
delete = true,
source = "/srv/app.example.com/appdata/",
target = "app2:/srv/app.example.com/appdata/",
rsync = {
archive = true,
},
}
-- Also sync certbot/letsencrypt ssl certificates for apache2
sync {
default.rsync,
delete = true,
source = "/etc/letsencrypt/",
target = "app2:/etc/letsencrypt/",
rsync = {
archive = true,
},
}Filename: /etc/sysctl.d/99-lsyncd.conf
Increase the file open limits so lsyncd can watch folders like /srv. Else lsyncd will exit due to large number of open files for watching and file replication will fail.
# Filename: /etc/sysctl.d/99-lsyncd.conf
# lsyncd needs high value for watching app data
# Set to 16 million files
fs.inotify.max_user_watches = 16777216Filename: /etc/logrotate.d/lsyncd
# Filename: /etc/logrotate.d/lsyncd
# Logrotate script for lsyncd
/var/log/lsyncd/lsyncd.log {
daily
rotate 2
compress
missingok
notifempty
sharedscripts
postrotate
if [ -f /run/lsyncd.pid ]; then
systemctl restart lsyncd > /dev/null 2>/dev/null || true
fi
endscript
}