2 Way local folder synchronization with lsyncd

So I have a use case scenario at work where 2 way folder synchronization is of use for me. I tried using rsync but its not designed for 2 way.
Then some lovely people in #rsync on Freenode told me of some other tools, namely unison and lsyncd.

Unison does a good job of syncing between 2 dirs, and would solve the problem just fine, but it requires running the tool to do the sync process.

Ideally, I wanted something that would sync immediately on file change, which is exactly what lsyncd does.

However, it’s based on 1 way sync, but due to how it performs it doesn’t have deletion problems like my first attempts at rsync did.

So to provide 2 way with lsyncd, you simply run the tool twice, 1 for each direction.

I’ve wrote a quick bash script that can auto start the sync process for you, designed to be ran as a cronjob to ensure the daemons are running.

Create the file

~/bin/syncdirs

and paste the following in it

#!/usr/bin/env bash
#########################
## syncdirs
##
## syncs 2 directories with 2 directional sync using lsyncd
## apt-get install lsyncd
##
## written by aikar@aikar.co
## http://aikar.co/2011/03/07/2-local-folder-synchronization-lsyncd
##
#########################

sync="lsyncd --delay 0"
if [ $# == 2 ]; then
    if [ -d $1 ] && [ -d $2 ]; then
        d1="$(readlink -f $1)"
        d2="$(readlink -f $2)"
        
        sync1="$sync $d1 $d2"
        sync2="$sync $d2 $d1"
        
        found=0
        ps ax | grep -v grep | grep -q "$sync1" 
        if [ $? != 0 ]; then 
            $sync1
            found=1
        fi
        ps ax | grep -v grep | grep -q "$sync2"
        if [ $? != 0 ]; then 
            $sync2 
            found=1
        fi
        if [ $found == 1 ]; then
            echo "syncing $d1 and $d2"
        else
            echo "was already syncing"
        fi
    else
        echo "syncdirs: ERROR!"
        if [ ! -d $1 ]; then
            echo "$1 is not a directory"
        fi
        if [ ! -d $2 ]; then
            echo "$2 is not a directory"
        fi
    fi
else
    echo "syncdirs usage:"
    echo "syncdirs directory1 directory2"
fi

and then

chmod +x ~/bin/syncdirs

then if you have ~/bin in $PATH, type

syncdirs /path/to/sync /path/to/sync/with

and you should be told the dirs are now syncing.

to add ~/bin to PATH if not already done, edit ~/.bashrc and add

export PATH="$HOME/bin:$PATH"

Now to ensure they are always syncing, type

crontab -e

and add to bottom

* * * * * ~/bin/syncdirs /path/to/sync /path/to/sync/with >/dev/null

Now you should have the sync daemons auto start by cron on system start, and auto relaunch them if they die for any reason.
Disclaimer: I take no responsibility for what this tool does nor my starter script does to your files. (I didn’t even make lsyncd!)
make sure you have backups of your files before running these tools on them!!!

Tags: , , , , ,

I am very passionate about development, and communities. I am now living my dream of running my own gaming company, as I have now founded Starlis LLC and operate the Empire Minecraft service, the place where I met my lovely wife Mia!

Leave a Reply





I am Senior Software Engineer and Entrepeneur. I am an enthusiast and love creating things. I operate my own side company in my free time called Starlis LLC, working in Minecraft.

I enjoy doing things right and learning modern technologies.