Home > HA > Linux: Live Sync directories in two remote machines

Linux: Live Sync directories in two remote machines

I came across an article at cyberciti which explains the steps to monitor directories for changes and take action when a new inode event occurs. The author mentions “inotify” for monitoring directories. One limitation of this method was, it doesn’t monitor the sub-directories. On searching I could find a python module named “pyinotify” which supports monitoring sub-directories recursively. This article mentions the steps to keep directories in two remote machines in Live sync using “pyinotify”.

Machine1 ==> Source ==>
Machine2 ==> Destination ==>
Folder to kept in sync: “/root/testing”

1) Install “pyinotify” python module in source machine

cd /usr/local/src/
wget https://nodeload.github.com/seb-m/pyinotify/zipball/master
unzip master
cd seb-m-pyinotify-d5d471e/
python setup.py install

2) Enable ssh passwordless login from source( to destination(

[root@user1 testing]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
6e:11:82:9b:8c:2f:d6:b1:a6:29:07:a6:ea:17:e9:3f root@user.testserver.com
[root@user1 testing]#
[root@user1 testing]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@
The authenticity of host ‘ (’ can’t be established.
RSA key fingerprint is f4:cd:cd:e9:51:08:11:68:1c:90:b5:84:9a:c4:6b:1d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘’ (RSA) to the list of known hosts.
root@’s password:
Now try logging into the machine, with “ssh ‘root@′”, and check in:


to make sure we haven’t added extra keys that you weren’t expecting.

[root@user1 testing]# ssh root@
Last login: Wed Dec 28 08:33:13 2011 from
[root@user2 ~]#

3) Run “pyinotify.py” to sync the source and destination directory.

cd /usr/local/src/seb-m-pyinotify-d5d471e/python2/
python pyinotify.py -v -r -s /root/testing -c "rsync -r -e \"ssh\" -v /root/testing/ root@"


-v : displaying verbose messages
-r : recursively monitor the directories
-s : source directory
-c : command to execute when an inode notification orrcurs

Use the “–delete” option in rsync to remove files/folders in destination when they get deleted in source.

cd /usr/local/src/seb-m-pyinotify-d5d471e/python2/
python pyinotify.py -v -r -s /root/testing -c "rsync -r --delete -e \"ssh\" -v /root/testing/ root@"

Add the above command in /etc/rc.local to start it during system start-up.

Categories: HA
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: