Knowledgebase Home | Glossary | Favorites Knowledgebase Home | Glossary | Favorites
Syncing files between multiple contianers.
Article Details

Last Updated
7th of June, 2011

Level
Intermediate

User Opinions (3 votes)
100% thumbs up 0% thumbs down

How would you rate this answer?
Helpful
Not helpful
If a single site is load-balanced across multiple containers an issue may arise where the files on one container do not match the files on the others. The Stackable panel offers a solution to this but it must be manually run whenever changes are made to files. A more efficient way of ensuring synchronous files is to make use of the linux tools rsync and crontab.

Table of Contents

  1. The Rsync Command
  2. Generating a GPG key pair
  3. Sharing the key pair
  4. Testing the key pair
  5. Configure the cron job

1. The Rsync Command

Since we will be using rsync to sync files from one container to another we will need to use rsync with SSH. Rsync will be run from the container you wish to sync files to or the "slave container".

rsync -avze ssh c-web1.wxyz.stackablehost.com:/remote/folder/ /local/folder/
*note: use your "master" container hostname instead of c-web1.wxyz.stackablehost.com(this can be found on the Stackable panel)

The command above inctructs rsync to ssh to the "master container", check the folder ~/sites/mystackable.com_env1/doc_root/ and ensure that the files in the local copy of that folder match the files on the "master container".

In running this command we notice that it prompts for a password everytime. If we plan on having cron run this script for us automatically, prompting for a password is undesirable.

2. Generating a GPG key pair

To get around this we will utilize a feature of SSH that allows us to create a pair of keys that containers share. This will allow us to SSH without a password prompt.



First we will create the keys on the "slave container" and set-up the proper permissions. SSH to the "slave container" and run these commands:

ssh-keygen
Press enter at all 3 prompts to use defaults and no passphrase.
chmod 700 ~/.ssh
chmod go-rwx ~/.ssh/*

3. Sharing the key pair

Once the keypair is created we will need to share the public key (~/.ssh/id_rsa.pub) with the "master container"
scp ~/.ssh/id_rsa.pub c-web1.wxyz.stackablehost.com:
Now we will need to append the public key to a special file on the "master container". SSH to the "master container" and run these commands:
if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi
if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi 
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

4. Testing the key pairs

Now we should be able to run rsync over ssh without a password prompt. From the "slave container run":
rsync -avze ssh c-web1.wxyz.stackablehost.com:~/sites/mystackable.com_env1/doc_root/ ~/sites/mystackable.com_env1/doc_root/
If everything worked correctly it should not for a password and tell you how much data was transferred. If it did not work check and make sure the permissions of the files are correct and that the the file authorized_keys is spelled correctly and is in ~/.ssh.


5. Configure the cron job

Now that we have verified that rsync works we can set-up a cron job. A cron job simply a way of telling Linux to run a certain command at a certain time. To set-up a cron job type on the "slave container":
crontab -e
Now we will need to tell crontab what to run and when to run it. To do this add a line like the following to the file.

*/5 * *  * * rsync -avze ssh stackablehostname:/remote/directory /local/directory

Once this line is added save and exit the file. This will tells cron to run the rsync command every 5 minutes.

At this point, the "slave containers" are syncing with the "master containers" every 5 minutes! Now we just need to repeat this process for any other "slave containers" we have.
Visitor Comments
  1. Comment #1 (Posted by Sukey)
    You have more useful info than the Brtisih had colonies pre-WWII.
  2. Comment #2 (Posted by Rosalinda)
    You clodun't pay me to ignore these posts!
Post Comment for "Syncing files between multiple contianers."
To post a comment for this article, simply complete the form below. Fields marked with an asterisk are required.
   Your Name:
* Your Comment:
* Enter the code below:
 
Related Articles
No related articles were found.
Attachments
No attachments were found.

Continue