Setting up replication with lsync

For replicating data between servers, NFS isn’t always the right tool. If you need the same content in two directories across several servers, lsync works well.

I’ll cover file replication using the folders of a Bind DNS server as an example.

The first/primary server needs to connect to the second/dependent server without a password. Set up ssh keys for that:

ssh-keygen
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Copy the authorized_keys2 file itself to the remote server and put it in /root/.ssh.

Install everything you need:

yum -y install lua lua-devel pkgconfig gcc asciidoc lsyncd

Add lsync to startup:

chkconfig lsyncd on

Sync logs grow fast, so you need to rotate them periodically. Create /etc/logrotate.d/lsyncd with this content:

/var/log/lsyncd/*log {
  rotate daily
  missingok
  notifempty
  compress
  sharedscripts
  postrotate
  if [ -f /var/lock/lsyncd ]; then
    /sbin/service lsyncd restart > /dev/null 2>/dev/null || true
  fi
  endscript
}

Now edit the config file and set up the sync:

sync {
  default.rsyncssh,
  host="second_server_ip",
  source="/var/named/",
  targetdir="/var/named/",
  rsync = {
  compress = true,
    group = true,
    owner = true,
    rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
  }
}

If your directory updates dynamically, it makes sense to sync with a delay so it doesn’t trigger a sync every second.

Add this line to the sync section for that:

statusInterval = 20

You can also enable additional rsync options.

After restarting the lsyncd daemon, check the contents of /var/named/ on the second server.