October 2010 Archive

Backup your site!

October 9th, 2010

In my previous post I mentioned how I didn’t back up or migrate any of my data before we stopped paying the hosting company, so it’s all lost.

This has me thinking how much of a shame it would be if I build a wealth of information or a blog that I can use to identify myself and my work, only to have it go if the machine it’s hosted on dies. That would be bad, so this post is a short tutorial on how perform nightly backups of your website without you having to lift a finger.

About
This method uses rsync to transfer files (including a dump of the database) to another machine so you’ve got a live backup should things turn pear shaped. In this short tutorial, we’ll be transfering files from our ‘webhost‘ server to our backuphost server

Step 1. SSH RSA KEYS
We will be using ssh to transfer, securely, the data between our two hosts. Because this is a scripted and automated method, we can’t be there to type in a password each time we wish to run the backups. So we’ll be using a RSA public/private key pair for secure authentication. This will allow us password-less authentication for ssh sessions, transferring files with scp, etc.
First, log in to the webhost where we can generate the local public and private keys.

$ ssh-keygen -t rsa

This will prompt you for a location to save the key and a passphrase, you can just enter past those and accept all the defaults.

Our next step is to transfer the public key across to our backup server.

$ ssh-copy-id -i ~/.ssh/id_rsa.pub backupUser@backupserver.com

Site Note on using OSX or disto where you don’t have ssh-copy-id.. This will work just as well

cat ~/.ssh/id_rsa.pub | ssh backups@backupserver.com “cat – >> ~/.ssh/authorized_keys”

This will prompt you for the login password for the host, then copy the
keyfile for you, creating the correct directory and fixing the permissions as necessary.

Now test to make sure you can log into the backupserver without requiring a password.

Step 2. Creating the backup script
I made a backup script (backup.sh, me sure to ‘chmod u+x backup.sh’ once it’s been created) to run through these few backup procedures. The first step is to do a dump of the database to file. You could very well host an SQL server on the other side and mirror the database to get things up and running faster, but that was overkill for my needs.. Besides, always remember there are many ways to skin a cat and this one seemed to work nice for all intents and purposes.

The backup script has two parts in it, basically do a dump of the database, then copy that over to the server as well as all the public_html or htdocs or where you put your public hosted files.

echo “Running backup script…”

echo “Running a dump of the database..”

mysqldump –user=scottyob –password=somePassword –all-databases > /var/www/scottyob.com/backup/database.dump #backup of database

echo “Syncing the backup directory..”

rsync -a -e ssh /var/www/scottyob.com/backup/ backups@backuphost.com:backups/
rsync -a -e ssh /var/www/scottyob.com/htdocs/ backups@backuphost.com:htdocs/

echo “Backup made on ” `date` >> /var/www/scottyob.com/backuplog.txt

And there you have it, a script to backup your website to a user ‘backups’ on the server ‘backuphost.com’

Step 3. Automated..ness

This is no good for me unless it’s automated. I just ran this script under a cron job to automate this procedure

Edit your cron file using

$ crontab -e

Then I told it that at one minute past midnight, it should backup my website every day (add the following line)

1 0 * * * /var/www/scottyob.com/backup.sh

And there we have it folks, my backup procedure now so my site will never have to be started from scratch again.


A new blog

October 9th, 2010

Hello everyone. Ok, so this is a brand new blog. What happened to all my old content? I’d like to say I just wanted to start fresh, but truth is, I was lazy and didn’t back up any data from my hosting company before my term ended with them.. so I lost my database and content. I’ll re-post the good stuff, hopefully more in-depth on this site though. Stay tuned.

This blog is going to be a lot more personal then my last. It’s going to have technical and personal posts where I’ll post about anything and everything that takes my fancy. Good times :)