In an effort to boost my website's reliability, I created the following remote mirror system for my website. This system allows for either host to go down without hosting being impacted. With any luck, this will prevent most website outages. This system consists of my primary webserver, hosted in North Bend, WA, and a secondary host: my dorm server running here at Cornell.
My website is essentially a wordpress installation in addition to some other small php pages. In order to sync the two websites, I use the following script which syncs both the website's filesystem as well as wordpress's MySQL database. This script takes advantage of rsync, so only differences between the two websites are synced. This is essential since my home server has severely limited bandwidth and can't afford a massive transfer every hour.
#!/bin/sh #====================================== #=== Website synchronization script === #====================================== echo "Syncing website directory with rsync" rsync -e ssh -avz --exclude="local" --exclude="Stats.html" --exclude="wp-config.php" --bwlimit=15 /var/www/ nexus:/var/www/ echo "Syncing blog database" #Export the wordpress database mysqldump -u root -p$PASSWORD blog > /dbtmp/txfr.sql #Transfer database via rsync rsync -e ssh -avz --bwlimit=15 /dbtmp/ nexus:/dbtmp/ #Install the remote database ssh nexus 'mysql -u root -p$PASSWORD blog < /dbtmp/txfr.sql'
A cron job on my home server runs this script every hour (at 5 minutes past the hour). This can be accomplished by running crontab -e and inserting the line:
5 * * * * /root/sync_website.sh
In order to switch jeffheidel.com to my backup server when my home server goes down, I use the following python script:
#!/usr/bin/python
#Script checks if jeffheidel.com is up
#If down, we reassign the DNS records
import urllib
import sys
import os
def reset():
print "Executing DNS bypass proceedure"
os.system("/root/bin/updateip_home.sh")
sys.exit(0)
try:
a=urllib.urlopen('http://www.jeffheidel.com').getcode()
if(a!=200):
reset()
except:
reset()
print "All's good"
The script /root/bin/updateip_home.sh uses FreeDNS's dyndns wget script to set the record for jeffheidel.com to the current IP address. The python script above script runs periodically using cron. Similarly, on my home server, I have a script which periodically runs this wget script so that the DNS record is restored once my home server is restored.
If things are running normally (both servers are up) you will be able to see my primary server at jeffheidel.com and my backup server at cornell.jeffheidel.com. The only difference between the two websites will be the Server Statistics section.



