If you're looking for a way to synchronize files and folders between computers automatically, lsyncd is a pretty great option. The only downside for beginners? You have to configure everything at the command line, and text files.
Even so, it is a program worth learning for any sysadmin.
The best description of lsyncd, comes from its own man page. Slightly paraphrased, lsyncd is a light-weight live mirror solution that is not hard to install. It does not require new filesystems or block devices, and does not hamper local filesystem performance. In short, it mirrors files.
lsyncd watches a local directory tree's event monitor interface (inotify). It aggregates and combines events for a few seconds, and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync.
For the purposes of this guide, you will call the system with the original files the "source", and the one that we are synchronizing to will be the "target". It is actually possible to completely mirror a server using lsyncd by very carefully specifying directories and files that you want to synchronize. It's pretty sweet!
There are actually two ways to install lsyncd. We will include them both here. The RPM tends to lag behind the source packages by a little, but only by a little. The version installed by the RPM method at the time of this writing is 2.2.2-9, whereas the source code version is now 2.2.3. That said, we want to give you both options and let you choose.
Installing the RPM version is not hard. The only thing you will need to install first is the EPEL software repository from Fedora. This can be done with a single command:
dnf install -y epel-release
Then, we just need to install lsyncd and any missing dependencies will be installed along with it:
dnf install lsyncd
Set up the service to start on boot, but don't start it just yet:
We will need some dependencies: a few that are required by lsyncd itself, and a few that are required to build packages from source. Use this command on your Rocky Linux computer to ensure you have the dependencies you need. If you are going to be building from source, it is a good idea to have all of the development tools installed:
dnf groupinstall 'Development Tools'
For Rocky Linux 9.0
lsyncd has been fully tested in Rocky Linux 9.0, and will work as expected. In order to get all of the needed dependencies installed, however, you will need to enable an additional repository:
dnf config-manager --enable crb
Doing this in 9 before the next steps, will allow you to finish the build without backtracking.
And here are the dependencies we need for lsyncd itself, and its build process:
With the RPM install method, the systemd service will be installed for you, but if you choose to install from source, you will need to create the systemd service. While you can start the binary without the systemd service, we want to ensure that it does start on boot. If not, a server reboot would stop your synchronization effort. If you forgot to start it again, which is highly likely, that would be a problem for any systems administrator!
Creating the systemd service is not terribly difficult, though, and will save you time in the long run.
Whichever method you choose for installing lsyncd, you will need a configuration file: /etc/lsyncd.conf. The next section will tell you how to build a configuration file, and test it.
Here's an example of a simplistic configuration file that synchronizes /home to another computer. Our target computer is going to be a local IP address: 192.168.1.40
The logfile and statusFile will be automatically created when the service starts.
The statusInterval is the number of seconds to wait before writing to the statusFile.
maxProcesses is the number of processes lsyncd is allowed to spawn. Honestly, unless you are running this on a super busy computer, 1 process is enough.
In the sync section default.rsyncssh says to use rsync over SSH
The source= is the directory path we are syncing from.
The host= is our target computer that we are syncing to.
The excludeFrom= tells lsyncd where the exclusions file is. It must exist, but can be empty.
The targetdir= is the target directory we are sending files to. In most cases this will be equal to the source, but not always.
Then we have the rsync = section, and these are the options that we are running rsync with.
Finally we have the ssh = section, and this specifies the SSH port that is listening on the target computer.
If you are adding more than one directory to sync, then you need to repeat the entire "sync" section including all the opening and closing brackets for each directory.
As noted earlier, the excludeFrom file must exist, so let's create that now:
touch /etc/lsyncd.exclude
If you were syncing the /etc folder on our computer, there would be many files and directories that you should exclude. Each excluded file or directory is listed in the file, one per line, like this:
Now that everything else is set up, you can test it all. For starters, lets ensure our systemd lsyncd.service will start:
systemctl start lsyncd
If no errors appear after executing this command, check the status of the service, just to ensure:
systemctl status lsyncd
If it shows the service running, use tail to see the ends of the two log files, and ensure everything shows up OK:
tail /var/log/lsyncd.log
And then:
tail /var/log/lsyncd-status.log
Assuming that this all looks correct, navigate to the /home/[user] directory, where [user] is a user on the computer and create a new file there with touch.
touch /home/[user]/testfile
Now go to the target computer and see if the file shows up. If so, everything is working as it should. Set the lsyncd.service to start on boot with:
Anytime you are synchronizing a set of files or directories to another computer, think carefully about the effect it will have on the target computer. If you go back to The lsyncd.exclude File in our example above, can you imagine what might happen if /etc/fstab is synchronized?
For newbies, fstab is the file that is used to configure storage drives on any Linux computer. The disks and labels are almost certainly different. The next time the target computer was rebooted it would likely fail to boot entirely.
lsyncd is a powerful tool for directory synchronization between computers. As you've seen, it is not hard to install, and it is not complex to maintain going forward. You can not ask for more than that.