Thursday, October 2, 2008

Synchronize directories/files between servers

I came across the scenario where, we have to synchronize files and directories between different servers. For example[Refer image1] my application update images in server1 only. But the same image shoule be synchronized in server2 also.







There is tool 'Rsync' for linux to acheive this scenario.
Install Rsync in two servers. We are going to sync server1 with server2. So here destination server is server2.


In server2 add the following configration files.

i) Create rsync.conf file in /etc/rsync.conf
Add the following code given in between two lines in this file.
#############################
motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[thumbnails]
path = /var/www/html/images
comment = Image Server
uid = apache
gid = apache
read only = no
list = yes

#############################
Explanation:
First four lines are log,pid,lock and motd files path

[thumbnails] is the label. This will refered in server1 to send the directories to server2.
path is the place where the directories and files will get stored.

ii) Create rsyncd.motd file in /ect/rsyncd.motd

Add some text. This will shown at the time of sync.
For expample add text like in between two lines.
#############################
File transfering.........
#############################

iii) By default rsync is disabled. So we have to enable Rsync. You can edit the file /etc/xinetd.d/rsync and chage the line as disable = no [by default disable = yes]

iv) Start the rsync server
/etc/init.d/xinetd start
Server2 [Destination server] configuration over.


In server1, only one line command will sync the file or directories to server2.

rsync -rptgou /absolute path/images 192.168.100.50::thumbnails

Explanation:
rsync ==> is the command
-rptgou ===> is the options
/absolute path/images ===> images is the directory we are going to transfer to server2
192.168.100.50::thumbnails ===> this is the server2 ip and thumbnails is the lable we have added in "rsync.conf" configuration file in server2

We can add the above command in crontab so that two servers in the sync.
Enjoy........

0 comments:

Post a Comment