Rolling My Own WordPress Blog Backup Strategy

General Add comments

I have been very complacent about implementing a backup strategy for my blog files but after reading about codinghorror.com and haacked.com amongst others recently falling foul to a hardware failure on the server that hosted their blogs and the struggles they had to try and retrieve their content again I realised that something needed to be done. In fact Phil Haack sums it up quite nicely in this tweet.

My blog is hosted on a shared Unix server so the first thing I did was look in my server control panel which is cPanel at what was available in the way of backups. Backups of various shapes and sizes were available but I wanted a bit more control on what was backed up and when so I turned to SuperUser.com and asked the question ‘How do I compress a directory on a Unix web server and email it on a sheduled basis?’ and got an interesting response from Chealion which involved the use of Tar and Crontab to zip up an archive on a scheduled basis. This is more like it!

I then decided that I wanted to backup just my blog files including my uploaded content such as images and also make a backup of the MySQL database my blog is using so that I can remove them to another location offline. I can always extend this further if I need to in the future but this would allow me to get my blog back online very quickly.

Creating the folder structure

I wanted to keep the database backup schema separate from the blog website files so I created the following structure on my web server. I didn’t need anything more than this because I intend to suffix the date and time to the filename.

image

Backing up the files

Compressing files using Tar is very simple. I just want to zip up all the files in my blog directory and save it with the date/time suffixed. The code below will give a filename of something like ‘blog_backups_website_files_YYYYmmddHHMMSS.tgz’.

tar czf "/path/to/blog_backups/website_files/blog_backups_website_files_`date +%Y%m%d%H%M%S`.tgz" /path/to/ianroke.co.uk/blog

Backing up the database

Backing up in MySQL is done with mysqldump which is a bit trickier to configure. I have to pass the username, password and the database name then give it somewhere to dump the output to. The code below gives you an idea of what is required. This bit was tricky because the database was automatically generated by cPanel so finding out the generated details was required first.

mysqldump -uUSERNAME -pPASSWORD DATABASE > "/path/to/blog_backups/database_schema/blog_backup_database_schema_`date +%Y%m%d%H%M%S`.sql"

Scheduling it to run periodically

Luckily cPanel has a cron section so I was able to quickly set up these tasks. For the purpose of testing it I wanted to set them to run every minute so I set it as such:

image

After committing changes I waited and nothing happened. Strange. I fired up my email to find that I was getting the following issue. After escaping the % characters and moving the quote marks it worked.

Great! Now what?

Now I have got it working I now need to decide how often I need to run backups. Cron has a great idea where you can specify every X so */2 means every X that is divisible by 2 so for example hours 2, 4, 6, 8, 10 and so on. I think for now I will put the website files at */6 which means every six hours it will backup all the files which is more than enough for the busiest of blogs and for the database backup I will put it at */2 so every two hours to make sure that I get all updates, comments, pings, trackbacks, etc. If there was a catastrophic failure then to loose 2 hours of database activity is not the end of the world in the grand scheme of things.

Next steps?

I would like to automate the process of moving the backup files to an offsite backup solution so I am not relying on logging in and moving the files myself. Has anybody had any experience of this? Could you post in the comments any ideas you might have? Cheers.

5 Responses to “Rolling My Own WordPress Blog Backup Strategy”

  1. Ian's Online Technical Notebook » Blog Archive Using Amazon S3 To Backup My Wordpress Blog – Ian's Online Technical Notebook Says:

    [...] Web Development Add comments A couple of days ago I wrote a post on my blog about implementing a backup strategy for archiving the website files and database related to this blog. Although I was happy with the [...]

  2. bolsas femininas Says:

    Loved reading this post, do you also have some sort of newsletter?

  3. Ian Says:

    I don’t have a newsletter just yet but I do publish my new blog posts on Twitter if you have that?

    Otherwise keep checking my blog and I will implement a newsletter very soon.

    Cheers, Ian.

  4. Rick Caldwell Says:

    You might look at the plugin WP-DBManager. It can backup your database to a file and also email a copy to you on a schedule so you have a off site copy.

    Rick

  5. Ian Says:

    Thanks for the comment Rick. That is indeed another good way of backing up your blog. I chose this method because it allows me to get back online after the event of failure quicker.

Leave a Reply

 

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in