16 Mar 2015, 13:20 Services

MongoDB automated database backups

Even though the Bluemix MongoDB service is still in the experimental phase, it was a requirement for one of the client projects that we’ve recently had in the London Garage. In spite of all the warnings associated with an experimental service, MongoDB worked well and performance was satisfactory. Because of the experimental nature of the service, we have chosen to regularly backup the data to an IBM supported Cloudant service. This is the simplest possible automated solution that we’ve come up with.

The free Cloudant plans gives us sufficent data storage for our needs (20GB) and provides a mature, highly-available and clustered environment. Storing MongoDB backups in Cloudant is done through a simple HTTP call. Same for retrieving specific backups. Cloudant’s Dashboard gives us another alternative to browse all available MongoDB backups.

The backup app itself is designed to be run as a standalone app on Bluemix. It consists of a few shell scripts which call unix commands such as tar, curl and statically compiled Linux 64bit binaries such as mongodump & jq. Before deploying to Bluemix, we need to set the correct MongoDB & Cloudant service names in manifest.backup.yml. In the same cf manifest, we can configure the backup interval in seconds via the MONGO_BACKUP_INTERVAL environment variable. Once configured, the app is deployed to Bluemix as a worker app (i.e. no-route option) via cf push -f manifest.backup.yml

A handy behaviour of the backup utility app is that it runs the MongoDB backup on startup. This means that when we want to perform an ad-hoc MongoDB backup to Cloudant, all we have to do is restart the app. Otherwise, the next backup will run when MONGO_BACKUP_INTERVAL is reached.

This utility app would only be half as useful without a restore mechanism for the MongoDB backups. The restore functionality is part of the same code but deployed to Bluemix as a separate app. Service instance configuration is done the same as for the backup app, but in the manifest.restore.yml file. To restore a specific backup, first download the backup.tgz from Cloudant, and then place the file in the same directory as the cf manifest. With this backup file in place, you’re ready to just cf push -f manifest.restore.yml and the bound MongoDB service instance will be restored from this backup.

If you would like to get automated MongoDB database backups working on Bluemix today, feel free to download and deploy our mongo-backup-restore utility app to your Bluemix account.