Taking backup of a specific mongodb database or collection
In this recipe, we will be using mongodump utility to take backups for a specific database and/or collection.
Getting ready
You need a single node MongoDB installation, preferably with some data in it. Refer to the recipe Creating an index in Chapter 2, Understanding and Managing Indexes, for instructions on how to import sample data into a MongoDB instance.
How to do it...
- Open a mongo shell and examine the database:
use mydb show collections
- You should see the following output:
mockdata system.indexes system.profile
- Insert data into a random collection:
db.tmpcol.insert({foo:1})
- Use
mongodump
to take backup of specific database:
mongodump --gzip -d mydb
- You should see an output similar to this:
2017-10-04T12:05:24.352+0000 writing mydb.mockdata 2017-10-04T12:05:24.353+0000 writing mydb.tmpcol 2017-10-04T12:05:24.361+0000 done dumping mydb.tmpcol (1 document) 2017-10-04T12:05:25.098+0000 done dumping mydb.mockdata (100000 documents)
- Use
mongodump...