Raja's Exocortex

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

  1. ssh between both servers as root without password, setup ssh keys correctly.
  2. Increase sysctl file watch limit so large number of files can be watched and replicated.
  3. rsync needs to be installed on both source and destination servers.
  4. lsync needs to be installed only on the source server.
  5. Run mkdir /var/log/lsyncd before 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 = 16777216

Filename: /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
}