Last couple of months I was wandering through the world of MongoDb, both as a developer and as a sysadmin. I won’t say how happy I’m with it or how much I got disappointed , I will just note that you don’t know how deep or serious you dived into a software product till the moment when you feel the need for a backup. And that day came, and I did something about it.
I’m not so much into installing backup software that will just do the things for me, I always start with the idea that I can do it with my own simple implementation that will fill my needs for saving data or doing something else. Mongo has become a huge software/database monster and there are a lot of different approaches regarding this question. Off course the right way for you depends of several factors. Some of them are: the infrastructure of the Mongo deployment, the importance of the data, the quantity of the data, the performance factor etc.
For my needs that are associated with a single cluster instance of Mongo database without replication or sharding I realized that using the “classical” dumping method will fulfil .
One of the things I like about Mongo is its nicely done documentation. A lot of information about backuping MongoDb can be found on its official site:
http://docs.mongodb.org/manual/core/backups/
The documentation is like a guide showing you the different ways of backuping. Generally there are 3 different approaches: doing file system snaphosts, using mongodump command and using MongoDB Management Service.
For my dumping approach I wrote simple and primitive bash shell script that you can use for local backups or as a push towards the idea how to backup mongo data. It is oriented around dumping databases. Mongo data can be dumped as whole instance, whole collection or part collection and as a database. Here is the scirpt, the product of it are BSON and metadata JSON files at the designated directory.
#!/bin/bash # # GNRP LICENSE: Script licensed by GOD # # @author: Igor Ivanovski <igor at genrepsoft.com> # # March, 2015 # # # Filesystem directory variables # # Now in format: YYYY-MM DATEYYMM=`date +"%Y-%m"` # Now in format: DD DATEDD=`date +%d` # Backup directory BACKUPDIR="/opt/backup/mongodumps" # Daily backup directory BACKUPDIRDAILY="$BACKUPDIR/$DATEYYMM/$DATEDD/" # # List of databases to backup # DBs=" admin someDb "; # ### Mongo Server Setup ### # # Don't forget to add adequate roles if you are using authenticatio #use someDb db.createUser({user:"backup",pwd:"pwd",roles:["readWrite"]}) #use admin db.createUser({user:"backup",pwd:"pwd",roles:["backup"]}) # Mongo backup username MUSER="backup" # Mongo backup password MPASS="pwd" # Mongo HOST name MHOST="localhost" # Mongo PORT number MPORT="27017" # Mongo dump binary # Check if mongodump is installed STATUS=0 [ "Y`which mongodump`" != "Y" ] && STATUS=$? || STATUS=$? [ $STATUS == 1 ] && echo "No mongodump found. Exiting"; exit 1 MONGODUMP=`which mongodump`; # Starting to dump databases one by one if [ "X$DBs" != "X" ]; then for db in $DBs do echo "Backing up database $db" $MONGODUMP --host $MHOST --port $MPORT --username $MUSER --password $MPASS --out $BACKUPDIRDAILY --db $db done else echo "All listed mongo databases dumped. Bye" fi
Thank you for reading me, here is a bonus from me for swinging 😉
Bonus is unavailable :D.
LikeLike