database backup

I manage a lot of MySQL databases on various servers, and one of my responsibilities is to ensure we have a nightly backup of the data.

Over the years I have refined my approach.

I started with a great rake task written by Craig Ambrose. I made a few modifications and was ready to go.

That worked for a while, but then I realized I should be uploading the backups to redundant cloud storage rather than merely storing them on the server’s HD.

In the midst of modifying the rake task, I realized it would be better to extract all of the logic into a PORO (plain old Ruby class) and call it from the rake task.

Later, I needed highly similar functionality in other Rails applications.  I also ended up needing to backup databases for some PHP applications.

I realized there was no reason to embed the code within the Rails application itself — all I needed was a standalone Ruby script that I could execute from cron.

I decided to extract the core functionality into two gems — one for making and rotating the backups, and one for uploading them to Rackspace CloudFiles.

Then I made a third gem to cover the basic Ruby needed to tie these two gems together along with basic YAML configuration.

Now, any time I have a MySQL database that needs a nightly backup, regardless of whether it’s a Rails app, PHP app, or otherwise, I just have to clone db-archive, setup a YAML file, and call db-archive from cron.