Sync folders between servers

In this small tutorial I will explain how you can have folders synchronised between different computers using lsyncd for MacOsX

What is lsyncd?

When managing a web server or an application, there are many situations that require synchronization between directories. While a number of tools can help you accomplish this, lsyncd is a strong candidate.

This article will discuss how to configure lsyncd to mirror changes between directories on a single machine and how to mirror between remote hosts.

For this guide, we will be using an OSX 10.9.2, but any modern distribution should function in a similar way.

How to Install lsyncd

Fortunately, Brew repo for OSX includes lsyncd.

We can install lsyncd with the following commands:

This will install lsync, but it will not provide you with a default configuration. We will create one later in the article.

How to Find lsyncd Configuration Examples

While lsyncd does not provide a configuration file by default, it does include some examples that we can look at to get ideas.

See the examples by checking out the files in the “/usr/local/share/doc/lsyncd” directory:

You can look at these text files to get a sense of what can be done with configuration.

The lsync configuration file is written in the Lua programming language. You can learn more about Lua here.

Setting Up the Environment

We can create a log directory and some files for lsyncd to use:

Next, we can create the lsyncd configuration directory:

We will create a configuration file inside of this directory called “lsyncd.conf.lua”, with vim:

How to Configure Remote Syncing with lsyncd

With a few changes to our configuration file, we can configure remote syncing.

First we need to be able to sign into the mirroring machine from the original machine through password-less ssh. How to Log Into the Remote Machine with SSH Keys

Make a key pair for your root user, since the lsyncd service will run as root. You can then copy the key file to the remote mirror machine with the following commands:

Your key file should now allow you to log into the remote mirroring server as the root user.

Try it now so to test that it works and to create a destination directory on the remote host:

We can open the lsyncd configuration file again with the following command:

Save the file and exit.

Exclude directories

Sometimes you will want to exclude some directories. You can create the file lsyncd.dev.exclude and add exclusions per line like this example:

Testing Remote Syncing

We can now start the lsyncd service by issuing the following command:

If we ssh into our remote mirror, we should be able to see the changes in the remote “/var/www/lighttpd” directory:

You should see all of the files that we added to the local “/Users/cesar/PhpstormProjects/YourProject” directory.

Taking it Further

The lsyncd service is a great way to synchronize files across directories or systems. Due to its Lua-based configuration files, it can be very powerful and flexible. The Lua documentation and the lsync documentation are great resources that will allow you to develop more complex syncing operations.

Check out some of the other examples in “/usr/local/share/doc/lsyncd” directory for more ideas.

More info: Official github documentation