Setup Your Own Mirrored Web Server (Linux)

With a couple of easy techniques, you can setup a complete mirror of your web server. Why would you do this? Redundancy and Backups. Having a mirror of your entire website allows you to store a complete backup of your site in the case that your hardware malfunctions, data gets lost, or your server gets hacked. In addition, if your site goes down, you can quickly and easily raise your mirror instance to be the primary care taker by changing DNS, or doing an auto-detect server issue, and updating your DNS automatically. This is called fail-over.

And it will not cost you all that much. Even if you have dedicated servers out there, your mirror doesn’t require much to get running. If you are down for just a little bit or you want to simply have a backup, just setup a very inexpensive virtual private server (VPS) with limited resources. This will let you have all the access as your dedicated server, but in a cost enabled environment. And if you want to mirror your shared hosts, no problem there either. Just be sure you can get SSH and all the tools are most likely already there for you.

Step 1:
No matter what, it’s gravely more easy if you setup your mirror to be an exact replica of your primary host. If you are using Red Hat version whatever, use the same version as a base. If you are using CentOS or Mandrake, or which ever flavor, just make sure your mirror has the same. This goes the same for Plesk or cPanel users, use common control panels. Also match your script versions so that you don’t have issues on your script migrations. Your databases will have to be aligned as well. This example is specifically for a MySQL mirror.

Step 2:
Get SSH access. If you have dedicated or virtual private servers, you will already have this. If you are using a shared host, be sure to contact your provider and request that you can get in via SSH. This may require a different port, but that’s easy to overcome.

Step 3:
Setup SSH keys on both servers. SSH into your primary server and create a new SSH key.

  1. $ ssh-keygen
  2. Enter passphrase (empty for no passphrase):
  3. Enter same passphrase again:
  4.  
  5. scp ~/.ssh/id_rsa.pub mirroruser@mirror.com:.ssh/authorized_keys2

Step 4:
Setup Database Connections for mirrored site. You will need these for database restores from your primary server.

Step 5:
Create a new file using VI or PICO that you will use as a BASH shell script. Use this code to setup your sync. You will need to add your Database usernames and passwords. You will also have to ensure that those db usernames have proper access to the proper databases. Also your SSH statements are very important. You are basically taking your MySQL backup, moving it to the mirror site, unziping and then restoring. Finally, you perform an RSYNC over SSH, which is very fast if you have a good backbone on your server connections. Faster if you are in the same tier or internal network, but I would suggest your mirror is outside your primaries network context for natural disaster reasons.

Copy these to your new file /user/home/file.sh and make the proper changes.

  1. mysqldump –skip-extended-insert –user=username –password=password –all-databases | gzip > /home/user/backup/mysql.sql.gz
  2. scp /home/user/backup/mysql.sql mirroruser@mirror.com:/home/user/backup/mysql.sql
  3. ssh mirror.com -l mirroruser "gunzip /home/user/backup/mysql.sql.gz"
  4. ssh mirror.com -l mirroruser "mysql -u username -p[password] < /home/user/backup/mysql.sql"
  5. ssh mirror.com -l mirroruser "rm /home/user/backup/mysql.*"
  6. rsync -r -v -e ssh /home/user/public_html mirroruser@mirror.com:/home/user/

Step 6:
You will have to make this an executable, so on that file, do a:

  1. chmod +x /user/home/file.sh

Step 7:
Add an entry into your CRON jobs that will run this once a day, and you are set. Do this by typing

  1. crontab -e

When you edit the file, you will want to add this line, and make the proper adjustments to your file. This will run your Sync script every night at 3 AM.

  1. 0 3 * * * /user/home/file.sh

I know that I went through this very quickly. If you guys would like an even deeper post on this subject, let me know and I will go ahead and do it. I hope this helps!

About Phillihp Harmon

I'm Phillihp. My name can be spelled the same way forwards and backwards, so can my posts... if you wish. I'm out here exploring, learning, and sharing what I find. This is more for fun and personal growth, I aim to be as consistent as possible, so check back daily!
This entry was posted in ***, Cloud Computing, Clusters, DIY, howto, Internet, Linux. Bookmark the permalink.

One Response to Setup Your Own Mirrored Web Server (Linux)

  1. Rack Space says:

    Hey good tutorial exactly what i was looking for!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>