Technical Blog Of JackCHAN

June 21, 2010

Synchronize data between load balance servers

Filed under: linux — Tags: , , — kaisechen @ 7:14 am

It always faces similiar business requirment when developing web application using load balance server, e.g upload image to one server and need to synchronize images to another server.

One solution is building a shell script  and put it into crontab , every five or ten minutes run the script.

In the script , use rsync or scp linux command.

Such as :

myaccount@myserver1.com~>rsync ./ -avz myaccount@myserver2.com:/home/myaccount/html/ -e ssh –safe-links

Of course, it should configue public/private key in myserver1.com.

1) produce public/private key pair

myaccount@myserver1.com~>ssh-keygen -b 2048 -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/myaccount/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/myaccount/.ssh/id_rsa.
Your public key has been saved in /home/myaccount/.ssh/id_rsa.pub.
The key fingerprint is:
74:78:91:ca:c7:20:14:08:9b:68:cf:15:32:18:7d:21 myaccount@myserver1.com

Notice: don’t input passphrase, because it is the password for private key.

2)copy the public key file to remote server

myaccount@myserver1.com~>scp .ssh/id_rsa.pub myaccount@myserver2.com:/home/myaccount

3)add the content of public key file into authorized_keys in remote server

myaccount@myserver2.com:~> mv id_rsa.pub .ssh/authorized_keys

or

myaccount@myserver2.com:~>cat id_rsa.pub >> .ssh/authorized_keys

4)copy data from local server to remote server now

myaccount@myserver1.com:~>rsync ./ -avz myaccount@myserver2.com:/home/myaccount/html/ -e ssh –safe-links                      building file list … done
./
test.txt

sent 182 bytes  received 48 bytes  460.00 bytes/sec
total size is 60  speedup is 0.26

Create a free website or blog at WordPress.com.