Lsyncd is a simple and lightweight tool that allows you to mirror your local directory to another directory on the remote server. It works by continuously monitoring the specified directory for changes every few seconds and if any changes are made then they synchronized to the remote server. This tool is very useful to sync the data from a secure area to a non-secure area.

In this tutorial, we will show you how to install and use Lsyncd to synchronize local directories and remote directories.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is setup on your server.

Getting Started

Before starting, it is a good idea to update your system’s package to the latest version. You can update them using the following command:

apt-get update -y

apt-get upgrade -y

Once all the packages are updated, restart your system to apply the changes.

Install Lsyncd

By default, Lsyncd package is available in most Linux operating systems. You can install it by just running the following command:

apt-get install lsyncd -y

Once the Lsyncd is installed, you can verify the installed version of Lsyncd with the following command:

lsyncd --version

You should get the following output:

Version: 2.2.3

Configure Lsyncd to Synchronize Local Directories

In this section, we will configure Lsyncd to synchronize /etc/ directory to /mnt/ directory on local system.

First, create a directory for Lsyncd with the following command:

mkdir /etc/lsyncd

Next, create a new Lsyncd configuration file and define the source and destination directory that you want to sync.

nano /etc/lsyncd/lsyncd.conf.lua

Add the following lines:

settings {
        logfile = "https://www.howtoforge.com/var/log/lsyncd/lsyncd.log",
        statusFile = "https://www.howtoforge.com/var/log/lsyncd/lsyncd.status",
   statusInterval = 20,
   nodaemon   = false
}

sync {
        default.rsync,
        source = "https://www.howtoforge.com/etc/",
        target = "https://www.howtoforge.com/mnt"
}

Save and close the file when you are finished.

systemctl start lsyncd

systemctl enable lsyncd

You can also check the status of the Lsyncd service with the following command:

systemctl status lsyncd

You should see the following output:

? lsyncd.service - LSB: lsyncd daemon init script
     Loaded: loaded (/etc/init.d/lsyncd; generated)
     Active: active (running) since Fri 2020-05-01 03:31:20 UTC; 9s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 36946 ExecStart=/etc/init.d/lsyncd start (code=exited, status=0/SUCCESS)
      Tasks: 2 (limit: 4620)
     Memory: 12.5M
     CGroup: /system.slice/lsyncd.service
             ??36921 /usr/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf.lua
             ??36952 /usr/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf.lua

May 01 03:31:20 ubuntu20 systemd[1]: lsyncd.service: Succeeded.
May 01 03:31:20 ubuntu20 systemd[1]: Stopped LSB: lsyncd daemon init script.
May 01 03:31:20 ubuntu20 systemd[1]: Starting LSB: lsyncd daemon init script...
May 01 03:31:20 ubuntu20 lsyncd[36946]:  * Starting synchronization daemon lsyncd
May 01 03:31:20 ubuntu20 lsyncd[36951]: 03:31:20 Normal: --- Startup, daemonizing ---
May 01 03:31:20 ubuntu20 lsyncd[36946]:    ...done.
May 01 03:31:20 ubuntu20 systemd[1]: Started LSB: lsyncd daemon init script.

You can check the Lsyncd log file for more details as shown below:

tail -f /var/log/lsyncd/lsyncd.log

You should see the following output:

/lsyncd/lsyncd.conf.lua
Fri May  1 03:30:57 2020 Normal: Finished a list after exitcode: 0
Fri May  1 03:31:20 2020 Normal: --- Startup, daemonizing ---
Fri May  1 03:31:20 2020 Normal: recursive startup rsync: /etc/ -> /mnt/
Fri May  1 03:31:20 2020 Normal: Startup of /etc/ -> /mnt/ finished.

You can also check the syncing status with the following command:

tail -f /var/log/lsyncd/lsyncd.status

You should be able to see the changes in the /mnt directory with the following command:

ls /mnt/

You should see that all the files and directories from the /etc directory are added to the /mnt directory:

acpi                    dconf           hosts            logrotate.conf       newt                     rc2.d          subuid-
adduser.conf            debconf.conf    hosts.allow      logrotate.d          nginx                    rc3.d          sudoers
alternatives            debian_version  hosts.deny       lsb-release          nsswitch.conf            rc4.d          sudoers.d
apache2                 default         init             lsyncd               ntp.conf                 rc5.d          sysctl.conf
apparmor                deluser.conf    init.d           ltrace.conf          openal                   rc6.d          sysctl.d
apparmor.d              depmod.d        initramfs-tools  lvm                  opt                      rcS.d          systemd
apport                  dhcp            inputrc          machine-id           os-release               resolv.conf    terminfo
apt                     dnsmasq.d       insserv.conf.d   magic                overlayroot.conf         rmt            timezone
at.deny                 docker          iproute2         magic.mime           PackageKit               rpc            tmpfiles.d
bash.bashrc             dpkg            iscsi            mailcap              pam.conf                 rsyslog.conf   ubuntu-advantage
bash_completion         e2scrub.conf    issue            mailcap.order        pam.d                    rsyslog.d      ucf.conf
bash_completion.d       environment     issue.net        manpath.config       passwd                   screenrc       udev
bindresvport.blacklist  ethertypes      kernel           mdadm                passwd-                  securetty      ufw
binfmt.d                fonts           kernel-img.conf  mime.types           perl                     security       update-manager
byobu                   fstab           landscape        mke2fs.conf          php                      selinux        update-motd.d
ca-certificates         fuse.conf       ldap             modprobe.d           pki                      sensors3.conf  update-notifier
ca-certificates.conf    fwupd           ld.so.cache      modules              pm                       sensors.d      vdpau_wrapper.cfg
calendar                gai.conf        ld.so.conf       modules-load.d       polkit-1                 services       vim
console-setup           groff           ld.so.conf.d     mtab                 pollinate                shadow         vmware-tools
cron.d                  group           legal            multipath            popularity-contest.conf  shadow-        vtrgb
cron.daily              group-          letsencrypt      multipath.conf       profile                  shells         vulkan
cron.hourly             grub.d          libaudit.conf    mysql                profile.d                skel           wgetrc
cron.monthly            gshadow         libnl-3          nanorc               protocols                sos.conf       X11
crontab                 gshadow-        locale.alias     netplan              pulse                    ssh            xattr.conf
cron.weekly             gss             locale.gen       network              python3                  ssl            xdg
cryptsetup-initramfs    hdparm.conf     localtime        networkd-dispatcher  python3.8                subgid         zsh_command_not_found
crypttab                host.conf       logcheck         NetworkManager       rc0.d                    subgid-
dbus-1                  hostname        login.defs       networks             rc1.d                    subuid

Configure Lsyncd to Synchronize Remote Directories

In this section, we will configure Lsyncd to synchronize /etc/ directory on the local system to the /opt/ directory on the remote system.

Before starting, you will need to setup SSH key-based authentication between the local system and remote server so that the local system can connect to the remote server without password.

On the local system, run the following command to generate a public and private key:

ssh-keygen -t rsa

You should see the following output:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:c7fhjjhAamFjlk6OkKPhsphMnTZQFutWbr5FnQKSJjE [email protected]
The key's randomart image is:
 ---[RSA 3072]---- 
| E ..            |
|  ooo            |
| oo=             |
|=.  % o . .      |
|[email protected] oSo. o    |
|ooo=B o .o o o   |
|=o.... o    o    |
| .    o .. o     |
|     .  ... .    |
 ----[SHA256]----- 

The above command will generate a private and public key inside ~/.ssh directory.

Next, you will need to copy the public key to the remote server. You can copy it with the following command:Advertisements

ssh-copy-id [email protected]

You will be asked to provide the password of the remote root user as shown below:

[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

Once the user is authenticated, the public key will be appended to the remote user authorized_keys file and connection will be closed.

Now, you should be able log in to the remote server without entering password.

To test it just try to login to your remote server via SSH:

ssh [email protected]

If everything went well, you will be logged in immediately.

Next, you will need to edit the Lsyncd configuration file and define the rsyncssh and target host variables:

nano /etc/lsyncd/lsyncd.conf.lua

Change the file as shown below:

settings {
        logfile = "https://www.howtoforge.com/var/log/lsyncd/lsyncd.log",
        statusFile = "https://www.howtoforge.com/var/log/lsyncd/lsyncd.status",
   statusInterval = 20,
   nodaemon   = false
}

sync {
        default.rsyncssh,
        source = "https://www.howtoforge.com/etc/",
	host = "remote-server-ip",
        targetdir = "https://www.howtoforge.com/opt"
}

Save and close the file when you are finished. Then, restart the Lsyncd service to start the sync.

systemctl restart lsyncd

You can check the status of synchronization with the following command:

tail -f /var/log/lsyncd/lsyncd.log

You should see the following output:Advertisements

Fri May  1 04:32:05 2020 Normal: --- Startup, daemonizing ---
Fri May  1 04:32:05 2020 Normal: recursive startup rsync: /etc/ -> 45.58.38.21:/opt/
Fri May  1 04:32:06 2020 Normal: Startup of "https://www.howtoforge.com/etc/" finished: 0

You should be able to see the changes in the /opt directory on the remote server with the following command:

ls /opt

You should see that all the files and directories from the /etc directory are added to the remote server’s /opt directory:

acpi                    dconf           hosts            logrotate.conf       newt                     rc2.d          subuid-
adduser.conf            debconf.conf    hosts.allow      logrotate.d          nginx                    rc3.d          sudoers
alternatives            debian_version  hosts.deny       lsb-release          nsswitch.conf            rc4.d          sudoers.d
apache2                 default         init             lsyncd               ntp.conf                 rc5.d          sysctl.conf
apparmor                deluser.conf    init.d           ltrace.conf          openal                   rc6.d          sysctl.d
apparmor.d              depmod.d        initramfs-tools  lvm                  opt                      rcS.d          systemd
apport                  dhcp            inputrc          machine-id           os-release               resolv.conf    terminfo
apt                     dnsmasq.d       insserv.conf.d   magic                overlayroot.conf         rmt            timezone
at.deny                 docker          iproute2         magic.mime           PackageKit               rpc            tmpfiles.d
bash.bashrc             dpkg            iscsi            mailcap              pam.conf                 rsyslog.conf   ubuntu-advantage
bash_completion         e2scrub.conf    issue            mailcap.order        pam.d                    rsyslog.d      ucf.conf
bash_completion.d       environment     issue.net        manpath.config       passwd                   screenrc       udev
bindresvport.blacklist  ethertypes      kernel           mdadm                passwd-                  securetty      ufw
binfmt.d                fonts           kernel-img.conf  mime.types           perl                     security       update-manager
byobu                   fstab           landscape        mke2fs.conf          php                      selinux        update-motd.d
ca-certificates         fuse.conf       ldap             modprobe.d           pki                      sensors3.conf  update-notifier
ca-certificates.conf    fwupd           ld.so.cache      modules              pm                       sensors.d      vdpau_wrapper.cfg
calendar                gai.conf        ld.so.conf       modules-load.d       polkit-1                 services       vim
console-setup           groff           ld.so.conf.d     mtab                 pollinate                shadow         vmware-tools
cron.d                  group           legal            multipath            popularity-contest.conf  shadow-        vtrgb
cron.daily              group-          letsencrypt      multipath.conf       profile                  shells         vulkan
cron.hourly             grub.d          libaudit.conf    mysql                profile.d                skel           wgetrc
cron.monthly            gshadow         libnl-3          nanorc               protocols                sos.conf       X11
crontab                 gshadow-        locale.alias     netplan              pulse                    ssh            xattr.conf
cron.weekly             gss             locale.gen       network              python3                  ssl            xdg
cryptsetup-initramfs    hdparm.conf     localtime        networkd-dispatcher  python3.8                subgid         zsh_command_not_found
crypttab                host.conf       logcheck         NetworkManager       rc0.d                    subgid-
dbus-1                  hostname        login.defs       networks             rc1.d                    subuid

Conclusion

In the above guide, we learned how to install and configure Lsyncd for local synchronization and remote synchronization. You can now use Lsyncd in the production environment for backup purposes. Feel free to ask me if you have any questions.