Last Updated: February 25, 2016
·
285
· lewdev

Lesson learned: Backup your database and here's how!

About two years ago, while running several Wordpress websites, I found that one of those websites suddenly stopped working. I panicked and looked into what was wrong. Turns out, the configuration database table was broken. Luckily it wasn't my content table, or else that would have been the end of that website.

Fixing the Problem

I simply backed up what I could and created a new database with all tables except that configuration table was empty. Then I started the Wordpress installation with existing content. The damage was minimal: I only had to reconfigure the theme, menus, links, and widgets. However, it still caused me stress and a couple of hours of work which all could have been avoided.

After that, it was clear, I should have simply backed up the database and the recovery would been instant saved me so much time!

My Solution

I searched for a script to automate the database backup, but I didn't find what I needed: Backup the database, but save the previous backup too.

I am lazy, like a good programmer, so I wanted to set it and forget it. I don't visit my Wordpress websites very often, so it's not like a simple daily backup would be good because I could be backing up a broken database if I wait too long; which is why I backup daily, weekly, and monthly.

Backup MySQL Database Script (GitHub)

I created a script, which I think is easy to setup an use.

Setting up the cron job

CPanel cron job setup (image).
Sorry, I don't have the cron job commands, but here's a screenshot for those who use CPanel, or in my case, HostMonster to setup the cron job. It's really easy to use and they will email you if any errors or text gets printed. Add in an "echo" command if you want to be notified after each backup.

I hope this helps!