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 - 200.200.200.201 (Command prompt S1#)
Share location say /home/commonfiles
Dubai server IP - 200.200.200.202 (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/id_rsa.pub root@200.200.200.202:/root/.ssh/authorized_keys [or]
S1#scp /root/.ssh/id_rsa.pub root@200.200.200.202:/tmp
S2#cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys

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

Shell script for rsync command execution:

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

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

Scheduling using crontab

S1#crontab -e [create as per your need]
* 0,5,10,15,20 * * * root /root/script.sh [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.

Logu
logu_microsoft@hotmail.com

No comments: