How to Install lsyncd on Ubuntu 20.04 LTS

Reading Time: 4 minutes

What is lsyncd?

lsyncd is a rsync-based tool that monitors specified directories (including subdirectories) for updates and modifications, and then syncs those changes to a specified destination. It is a lightweight command application that is easy to install and configure using the popular Lua language.

It’s extremely useful in tracking data modification within a configured directory, and if changes are seen, it syncs the data between the directories which are frequently updated with new contents. By default, it is rsync only.

Prerequisites

  • These instructions are intended specifically for installing the lsyncd on Ubuntu 20.04 LTS.
  • We will be working from a Liquid Web Core Managed Ubuntu 20.04 LTS server.
  • We are logged in as the root user.

Install lsyncd

Step 1: Update and Upgrade

First, we will follow our simple best practice advice by ensuring the list of available packages is up to date on the server before installing anything new.

root@host3:~# apt-get -y update && apt-get upgrade -y
 Hit:1 http://syspackages.sourcedns.com/packages/stable/ubuntu focal InRelease
 Hit:2 http://us.archive.ubuntu.com/ubuntu focal InRelease
 Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
 Get:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
 Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
 Get:6 http://us.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [378 kB]
 Get:7 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [670 kB]
 Get:8 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [695 kB]
 Get:9 http://us.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [517 kB]
 Fetched 2,576 kB in 1s (3,783 kB/s)
 Reading package lists… Done
 Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 Calculating upgrade… Done
 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
 root@host3:~# 

Step 2: Install

As a precaution, we will verify lsyncd is not installed on our system.

root@host3:~# which lsyncd
 root@host3:~#

Next, we will install lsyncd and any required packages.

root@host3:~# apt-get -y update && apt-get upgrade -y
 Hit:1 http://syspackages.sourcedns.com/packages/stable/ubuntu focal InRelease
 Hit:2 http://us.archive.ubuntu.com/ubuntu focal InRelease
 Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
 Get:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
 Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
 Get:6 http://us.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [378 kB]
 Get:7 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [670 kB]
 Get:8 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [695 kB]
 Get:9 http://us.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [517 kB]
 Fetched 2,576 kB in 1s (3,783 kB/s)
 Reading package lists… Done
 Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 Calculating upgrade… Done
 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
 root@host3:~# which lsyncd
 root@host3:~# apt-get -y install lsyncd
 Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 The following additional packages will be installed:
   liblua5.3-0 lua5.3
 The following NEW packages will be installed:
   liblua5.3-0 lsyncd lua5.3
 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
 Need to get 294 kB of archives.
 After this operation, 1,128 kB of additional disk space will be used.
 Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 liblua5.3-0 amd64 5.3.3-1.1ubuntu2 [116 kB]
 Get:2 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 lua5.3 amd64 5.3.3-1.1ubuntu2 [110 kB]
 Get:3 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 lsyncd amd64 2.2.3-1 [67.4 kB]
 Fetched 294 kB in 0s (1,146 kB/s)
 Selecting previously unselected package liblua5.3-0:amd64.
 (Reading database … 97037 files and directories currently installed.)
 Preparing to unpack …/liblua5.3-0_5.3.3-1.1ubuntu2_amd64.deb …
 Unpacking liblua5.3-0:amd64 (5.3.3-1.1ubuntu2) …
 Selecting previously unselected package lua5.3.
 Preparing to unpack …/lua5.3_5.3.3-1.1ubuntu2_amd64.deb …
 Unpacking lua5.3 (5.3.3-1.1ubuntu2) …
 Selecting previously unselected package lsyncd.
 Preparing to unpack …/lsyncd_2.2.3-1_amd64.deb …
 Unpacking lsyncd (2.2.3-1) …
 Setting up lua5.3 (5.3.3-1.1ubuntu2) …
 update-alternatives: using /usr/bin/lua5.3 to provide /usr/bin/lua (lua-interpreter) in auto mode
 update-alternatives: using /usr/bin/luac5.3 to provide /usr/bin/luac (lua-compiler) in auto mode
 Setting up liblua5.3-0:amd64 (5.3.3-1.1ubuntu2) …
 Setting up lsyncd (2.2.3-1) …
 Processing triggers for systemd (245.4-4ubuntu3.3) …
 Processing triggers for man-db (2.9.1-1) …
 Processing triggers for libc-bin (2.31-0ubuntu9.1) …
 root@host3:~#   

Step 3: Verify Install

root@host3:~# lsyncd --version
 Version: 2.2.3
 root@host3:~#

Configure lsyncd

Step 1: Define Directories to Sync

In this example, we will configure the /home/user1/public_html/ folder to sync to /home/backup/. You can select any combination of folders to suit your specific needs. Next, we will create a new directory to which lsyncd can write our changes.

root@host3:~# mkdir -p /source/to/copy 
root@host3:~# mkdir /dest/dir/

root@host3:~# mkdir /var/www/html/
root@host3:~# mkdir /backup/html/

Step 2: Create Configuration File and Log

Now, we will make a new lsyncd config file to store our information. This file will define both the source and destination directories we want to sync.

root@host3:~# vim /etc/lsyncd/lsyncd.conf.lua

Here is the information we want to add to the file that will define our folders, log info and status.

settings {
         logfile = "/var/log/lsyncd/lsyncd.log",
         statusFile = "/var/log/lsyncd/lsyncd.status",
    statusInterval = 30,
    nodaemon   = false
 }
 sync {
         default.rsync,
         source = "/var/www/html/",
         target = "/backup/html/"
 }

Now, save and close the file using :wq when finished editing. Next, we will create the log directories for the configuration.

root@host3:~# mkdir /var/log/lsyncd 
root@host3:~# touch /var/log/lsyncd/lsyncd.{log,status}

Step 3: Restart & Enable lsyncd

root@host3:~# systemctl start lsyncd
root@host3:~# systemctl enable lsyncd

Next, we check the status of the lsyncd service using the following command. You will see output similar to the following information.

root@host3:~# systemctl status lsyncd
 lsyncd.service - LSB: lsyncd daemon init script
 Loaded: loaded (/etc/init.d/lsyncd; generated)
 Active: active (exited) since Fri 2020-11-20 15:13:57 EST; 34min ago
   Docs: man:systemd-sysv-generator(8)
   Tasks: 0 (limit: 2178)
  Memory: 0B
  CGroup: /system.slice/lsyncd.service 
Nov 20 15:13:57 host3.g33k.fun systemd[1]: Starting LSB: lsyncd daemon init script…
Nov 20 15:13:57 host3.g33k.fun systemd[1]: Started LSB: lsyncd daemon init script.
root@host3:~# 

Enable Logging

Now, we will verify our log is set up correctly and producing output.

root@host3:~# mkdir /var/log/lsyncd/
root@host3:~# touch /var/log/lsyncd/lsyncd.log

Now, if a file is added to our default folder, it should automatically be synced over to our backup folder.

Testing

Now, lets add a new file to our /var/www/html/ folder.

touch /var/www/html/file101
root@host3:~# tail -f /var/log/lsyncd/lsyncd.log

Fri Nov 20 17:51:14 2020 Normal: --- Startup, daemonizing ---
 Fri Nov 20 17:51:14 2020 Normal: recursive startup rsync: /var/www/html/ -> /backup/html/

Fri Nov 20 17:51:14 2020 Normal: Startup of /var/www/html/ -> /backup/html/ finished.

Fri Nov 20 17:52:29 2020 Normal: Calling rsync with filter-list of new/modified files/dirs
/file101 <<< new file added and synced

Conclusion

And there you have it! A quick and easy way to utilize lsyncd to back up files. lsyncd also have the ability to sync files remotely as well. The only difference is we need to set up SSHKeys to log into the remote system and then add the following line to our sync section in the line

host = "123.456.789.0",

Get Started Today!

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our Support Teams are filled with experienced Linux technicians and talented system administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article.

Should you have any questions regarding this information, we are always available to answer any inquiries with issues related to this article, 24 hours a day, 7 days a week 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server, Managed Cloud Servers, or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone at @800.580.4985, a chat or support ticket to assisting you with this process.

Author Bio

About the Author: David Singer

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Refer a friend and get hosting credit!