I’ve started throwing some enthusiasm into my mongo db projects so I’ve decided I should start a serious backup plan for the content of the databases in it. My understanding is that this should be one of the selling points for mongo db as you can just export to json and store the file. I’m not currently trying to do anything crazy, like store images in the db (is that even possible?), so this should be straight forward.

Backup plan

SSH onto the box in question and run the following:

mongoexport --db <db-name> --collection <collection-name> --out output.json

My example:

mongoexport --db healthcheck --collection services --out services.json

In this database, I only have one collection (start simple), it would appear that we will have to back up each collection individually using this method. I’m ok with that for now as my databases aren’t huge. I did some reading about mongodump, which seems to be able to export the entire database with one command but there seems to be compatibility problems with older version of mongodb. As I’m currently running a relatively old version of mongodb on my pi (highest supported at the time of writing but still old), I will continue with this method.

Now store the output.json somewhere safe and feel good about having backed up your data.

Restore plan

mongoimport --db <database-name> --collection <collection-name> --file input.json

My example:

mongoimport --db healthcheck --collection services --file services.json

Update on this. It looks as though the import mechanism on earlier versions of mongo db does not support the date format created in later versions of mongodb. I may have to convert them to epoch (unix time). Theoretically if you export it from the db and then import it back into that db it should work ok. I did some testing from a later version export to an earlier version import and this is where I came unstuck.

Turn it into a script

There’s no way I will remember to ssh onto this box and run this command on a regular basis so I guess we should script it.

SSH back onto your box and navigate to /etc/cron.daily

cd /etc/cron.daily

Create a new script and make it executable:

sudo vi mongodb-backup
sudo chmod a+x mongodb-backup

Write your script:

#!/bin/bash
mongoexport --db <db-name> --collection <collection-name> --out output.json

My example:

#!/bin/bash
mongoexport --db healthcheck --collection services --out services.json

Save your script :wq and this should now run every day. You can test the script whilst you’re on the box to make sure it’s working:

./mongodb-backup

Now rest easy, safe in the knowledge that your data is safe…or maybe we should backup to another device on our network in case the pi sd card corrupts? Yes…yes we should. Back into the script:

#!/bin/bash
cd <backup-directory>
mongoexport --db <db-name> --collection <colection-name> --out output.json
rsync -ravz <backup-directory> <user>@<host>:<backup-directory>

My example:

#!/bin/bash
cd /pi/home/backup-mongodbs/healthcheck
mongoexport --db healthcheck --collection services --out services.json
rsync -ravz /pi/home/backup-mongodbs/ pi@192.168.1.27:/media/kodi/backup/

Ok, now we can…wait, what if that machine corrupts? I guess we have to draw the line somewhere.