How to install lsyncd directory synchronisation on Ubuntu LTS
Table of contents
As a developer, sometimes you need to synchronize a directory multiple times simultaneously, e.g., when a new file is written to the directory. And of course, this should happen in (near) real-time without significant delay. This is common in web development.
Here's my resource-efficient solution for multiple synchronization, e.g. from directory /srv/www/original/src to web1 and simultaneously to web2:
/srv/www/original/src
sync to -> /srv/www/web1/src/
sync to -> /srv/www/web2/src/
For synchronization, I use the tool lsyncd (Live Syncing Daemon). It's available for almost every Linux distribution and macOS.
What is lsyncd?
Lsyncd (Live Syncing Daemon) is a powerful tool for real-time synchronization of directories on Linux and macOS systems. Unlike conventional synchronization tools like rsync, lsyncd continuously monitors changes in your directories and synchronizes them in near real-time. It uses the inotify subsystem of the Linux kernel to detect filesystem events and triggers synchronization only when actual changes occur, making it particularly resource-efficient.
Installation
sudo apt install lsyncd # Debian/Ubuntu
systemctl enable lsyncd
Test the installation:
lsyncd --version
Example output:
Version: 2.2.3
Configuration
Since there is no sample configuration in "/etc" after installation, we first need to find out what it's called on Ubuntu. You can find this information in "/etc/init.d/lsyncd".
cat /etc/init.d/lsyncd | grep CONFIG=
Example output:
CONFIG=/etc/lsyncd/lsyncd.conf.lua
On Ubuntu, the configuration file is: /etc/lsyncd/lsyncd.conf.lua (on Fedora: /etc/lsyncd.conf). Now let's create the configuration file:
vi /etc/lsyncd/lsyncd.conf.lua
In the following example, the directories: css/ and js/ are excluded.
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log"
}
-- Synchronize directories
sync {
default.direct,
source = "/srv/www/original/src/",
target = "/srv/www/web1/src/",
delay = 1,
exclude = {"css/*", "js/*"}
}
sync {
default.direct,
source = "/srv/www/original/src/",
target = "/srv/www/web2/src/",
delay = 1,
exclude = {"css/*", "js/*"}
}
Initial Synchronization
To avoid having everything synchronized with "lsyncd" during the first synchronization, we start with an initial synchronization using "rsync" to populate the directories. This is faster.
mkdir /srv/www/web1/src/
rsync -av --delete --exclude="css/" --exclude="js/" /srv/www/original/src/ /srv/www/web1/src/
mkdir /srv/www/web2/src/
rsync -av --delete --exclude="css/" --exclude="js/" /srv/www/original/src/ /srv/www/web2/src/
Start with the new configuration:
sudo systemctl restart lsyncd
Status:
sudo systemctl status lsyncd
Example output:
● lsyncd.service - LSB: lsyncd daemon init script
Loaded: loaded (/etc/init.d/lsyncd; generated)
Active: active (running) since Tue 2025-03-18 12:44:25 CET; 3h 22min ago
Docs: man:systemd-sysv-generator(8)
Process: 246897 ExecStart=/etc/init.d/lsyncd start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 115371)
Memory: 512.0M
CPU: 3.791s
CGroup: /system.slice/lsyncd.service
└─246905 /usr/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf.lua
Mar 18 12:44:25 ares systemd[1]: Starting LSB: lsyncd daemon init script...
Mar 18 12:44:25 ares lsyncd[246897]: * Starting synchronization daemon lsyncd
Mar 18 12:44:25 ares lsyncd[246904]: 12:44:25 Normal: --- Startup, daemonizing ---
Mar 18 12:44:25 ares lsyncd[246897]: ...done.
Mar 18 12:44:25 ares systemd[1]: Started LSB: lsyncd daemon init script.
Monitoring and Status
Check logfile:
tail -f /var/log/lsyncd.log
Status:
tail -f /var/log/lsyncd-status.log
Conclusion
Lsyncd offers excellent performance for synchronizing directories in near real-time. Through event-based monitoring, changes are detected immediately and synchronized with minimal delay, making it ideal for development environments and production servers. Resource usage remains minimal since synchronization only occurs when actual changes happen. Synchronization works not only locally but can also be set up between servers. For web developers working with multiple servers or directories, lsyncd is an indispensable tool that saves you a lot of manual work and ensures consistent data across multiple systems.
If you have questions or additions, simply write a comment here.
Regards,
Frank
Please also mark the comments that contributed to the solution of the article
Content-ID: 671386
Url: https://rootdb.com/tutorial/how-to-install-lsyncd-directory-synchronisation-on-ubuntu-lts-671386.html
Printed on: March 31, 2025 at 11:03 o'clock