Thursday, August 30, 2007

Rsync for synchronizing directories b/w remote machines

Hi friends
Since having a file stores for accessing the files is common in organization. Obviously ftp, samba or some shared repository plays the role of above one. Nowadays company are multi-branched and also located at different geographical location. My case is to have a common filestore that should be accessed by both instance say Los angels lan and Dubai lan and this store is purely for internal access alone. The simple and effective answer for the above case is to have two servers in respective location and to enable synchronization b/w shares. Synchronization can be one way and can be used to pull or push. But we can configure this at both end and first time sync alone takes much time and successive sync is just updates and more over like incremental stuff.

Lets us consider
Los angels server IP - (Command prompt S1#)
Share location say /home/commonfiles
Dubai server IP - (Command prompt S2#)
Share location say /home/commonfiles

Password-less SSH authentication:
why suddenly to authentication? The answer is we need to provide the password for synchronizing data from one server to other remote server over ssh and also i need to schedule this synchronization for regular intervals.Henceforth i need to suppress this password prompting and at the same time to have secure way to use ssh. Lets do this with rsa generated public/private key option.

S1#ssh-keygen -t dsa [allow default]

S1#scp /root/.ssh/ root@ [or]
S1#scp /root/.ssh/ root@
S2#cat /tmp/ >> /root/.ssh/authorized_keys

S1#ssh root@ [result automatically logs in without asking for password]

Shell script for rsync command execution:

S1#vi /root/ [with the content below]
rsync -e ssh -avuzp /home/commonfiles root@ > /var/log/crontab.log

S1#chmod ugo+x /root/ [to make executable]

Scheduling using crontab

S1#crontab -e [create as per your need]
* 0,5,10,15,20 * * * root /root/ [daily midnight,5 am,10 am, 3 pm, 8 pm]

finally we have done... boom.... its sync the folders from Los angels to Dubai.If you need to update the entries added in the Dubai server to Los angels do the same as in the opposite server and schedule for alternate timing and make sure that the interval time is enough.

Please do post if you have any queries.


No comments: