1. Introduction to Lsyncd

Lsyncd (Live Syncing Daemon) is a tool that combines file system monitoring using inotify with the file synchronization capabilities of rsync. It’s ideal for near real-time synchronization between servers, especially when a simple and efficient solution is needed.

2. Installing Lsyncd

To install Lsyncd on a Debian/Ubuntu-based system, use the following commands:

sudo apt-get update
sudo apt-get install lsyncd

On Red Hat/CentOS-based systems, use:

sudo yum install epel-release
sudo yum install lsyncd

3. Basic Configuration of Lsyncd

Lsyncd is configured using a Lua configuration file. The most common configuration file is located at /etc/lsyncd/lsyncd.conf.lua.

Basic configuration example:

settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd-status.log",
nodaemon = false,
}

sync {
default.rsyncssh,
source = "/local/path/to/sync",
host = "user@remote_server",
targetdir = "/remote/destination/path",
rsyncOpts = {"-avz"},
ssh = {
port = 22,
password = "user_password"
}
}

4. Explanation of Key Parameters

  • logfile: File where Lsyncd logs are stored.
  • statusFile: File where synchronization status is saved.
  • nodaemon: If set to false, Lsyncd runs in the background.
  • source: Local folder to be synchronized.
  • host: User and address of the remote server.
  • targetdir: Destination directory on the remote server.
  • rsyncOpts: Options passed to rsync for synchronization.
  • ssh: SSH settings, such as port and password.

5. Running and Monitoring Lsyncd

To start Lsyncd, simply run:

sudo lsyncd /etc/lsyncd/lsyncd.conf.lua

You can verify that it’s working by checking the log file specified in the configuration (/var/log/lsyncd/lsyncd.log).

6. Advanced Usage

If you need more advanced configurations, you can add exclusions, perform custom actions before or after synchronization, or handle multiple synchronizations simultaneously in the configuration file.

7. Security Considerations

It’s important to consider the security of password authentication. For a more secure environment, it’s recommended to use SSH key-based authentication instead of passwords.

8. Conclusion

Lsyncd is a powerful and flexible tool for real-time file synchronization between servers. With proper configuration, you can keep your servers efficiently synchronized.