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 ==> 10.0.0.236
Machine2 ==> Destination ==> 10.0.0.237
Folder to kept in sync: “/root/testing”
1) Install “pyinotify” python module in source machine
2) Enable ssh passwordless login from source(10.0.0.236) to destination(10.0.0.237)
[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:
[root@user1 testing]# ssh-copy-id -i /root/.ssh/id_rsa.pub email@example.com
The authenticity of host ‘10.0.0.237 (10.0.0.237)’ 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 ‘10.0.0.237’ (RSA) to the list of known hosts.
Now try logging into the machine, with “ssh ‘firstname.lastname@example.org′”, and check in:
to make sure we haven’t added extra keys that you weren’t expecting.
[root@user1 testing]# ssh email@example.com
Last login: Wed Dec 28 08:33:13 2011 from 10.0.0.28
3) Run “pyinotify.py” to sync the source and destination directory.
python pyinotify.py -v -r -s /root/testing -c "rsync -r -e \"ssh\" -v /root/testing/ firstname.lastname@example.org:/root/testing"
-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.
python pyinotify.py -v -r -s /root/testing -c "rsync -r --delete -e \"ssh\" -v /root/testing/ email@example.com:/root/testing"
Add the above command in /etc/rc.local to start it during system start-up.