Automating DB Backups, with a Twist

Here is a geeky post that, for once, might be pertinent to more than two or three readers. Read on if:

  1. You have a website.
  2. This website uses a database (most blog platforms, for example, do).
  3. You would really hate to lose all your data in an accident.

It think there is no need to be an übergeek to satisfy all three points above.

So the problem is about backups. Backups are a beautiful and easy thing, in theory. In practice, nobody, but the more anal retentive among us and those paid to do it, will ever go through the pain of manually backing up their data on more than episodic occasions. And since Murphy’s Law applies more than ever here, you can be sure your next server failure will happen the day before your next backup.

Solution is to automate your backup. Have a script schedule an automated backup of your whole database, through the use of Unix utilities such as cron. While using cron, especially if your host provides you with a Control Panel to do so, isn’t that hard, there is still a problem with this method: your backups are usually copied on the server itself, which both generates risks of exceeding your storage quota (if you have a big database, a small disk space allowance and set it for regular auto-backups) and also kinda defeats the whole purpose of backups, since there is a decent amount of chance that whatever disaster would wipe out your database will also wipe out the server it’s running on (and your backup files with it).

And that’s where this really nifty idea comes in.

As with most cool tricks, it is blindingly simple, yet not that universal, since none of the friends I talked to had ever heard of it. So here goes:

Instead of setting your DB backups to be saved on the server, as most scripts will do, use a script that emails the DB backup file to a gMail account! Hell, given how easy these accounts are to come by nowadays, go wild and have your program redundantly email backups to two gMail accounts, just in case something happen to one of them.

In effect, you are now running a completely automated backup system with redundant copies of your backups hosted for free on the servers of one of the most respected Internet company in the world. As an extra bonus, these backups are accessible from anywhere, just the same as your website (as opposed to, say, removable media that usually end up lost, long before you ever need them for a restoration one day).

As for the specifics of the implementation, it depends entirely on your database and the tools that are installed on your server. For example, I use mySQL and my host runs cPanel, which offers some easy ways to automate tasks (basically an interface to setup cron) and do full DB backups. All I had to do was slightly tweak a script I found to fit my needs. If you google around with your combination of DB type, hosting server and the requirement of “send by mail”, you are bound to find hundreds of 10 line PHP or Perl scripts to do the same for you.

At the current rate, and even accounting for reasonable growth (of my site, not even a potential increase of gMail quota), I have calculated that the gMail accounts that hold my daily backup will only start reaching capacity around 2008, at the earliest. Which gives me quite some room to plan something else (such as purging old backups). Until then, I no longer have to worry about DB screw-ups…

Filed under: Geek

2 comments

  1. that one sounds so cool that I will actually look into it … having no idea about php or pearl might be an obsticle .. but heck …

Comments are closed.