Backup MongoDB – mongodump

Backup MongoDB – To backup data in MongoDB, MongoDB provides mongodump utility to backup data at different levels. Using mongodump, you may backup only a particular collection of a database, or single database, or all databases of a specified MongoDB instance.

In this tutorial, we will learn how to use mongodump utility to take backup of MongoDB Databases/Collections.

Using mongodump utility

mongodump is an utility that could be run from a Terminal.

Following are the usages of mongodump utility.

1. To create backup for a collection in database, use mongodump with options: Collection Name and Database Name.

mongodump --collection COLLECTION_NAME --db DB_NAME

2. To create backup for a database, use mongodump with options: Database Path and Output Directory for Backup.

mongodump --dbpath DB_PATH --out /OUTPUT/DIRECTORY/FOR/BACKUP

3. To create backup all the databases linked to a specific MongoDB instance, use mongodump with options: Host Name and Port Number at which MongoDB instance is running.

mongodump --host HOST_NAME --port PORT_NUMBER

4. Running mongodump utility without any options creates backup for all databases linked to the MongoDB running at localhost (127.0.0.1) and port 27017.

mongodump

Note : When no –out option is specified, a directory named dump is created at the location from which mongodump utility is run.

Examples for MongoDB Backup

Make sure MongoDB Instance is running. If not, start one as shown below :

~$ sudo mongod -dbpath /var/lib/mongodb/

Backup MongoDB

Open a Terminal and run mongodump utility with no arguments

~$ mongodump
2018-01-07T18:06:41.181+0530	writing admin.system.version to 
2018-01-07T18:06:41.182+0530	done dumping admin.system.version (1 document)
2018-01-07T18:06:41.182+0530	writing school.students to 
2018-01-07T18:06:41.182+0530	writing tutorialkart.people to 
2018-01-07T18:06:41.182+0530	writing tutorialkart.stores to 
2018-01-07T18:06:41.183+0530	writing tutorialkart.customers to 
2018-01-07T18:06:41.183+0530	done dumping school.students (9 documents)
2018-01-07T18:06:41.183+0530	writing tutorialkart.webpages to 
2018-01-07T18:06:41.184+0530	done dumping tutorialkart.people (8 documents)
2018-01-07T18:06:41.184+0530	writing newdb.users to 
2018-01-07T18:06:41.184+0530	done dumping tutorialkart.customers (5 documents)
2018-01-07T18:06:41.184+0530	writing school.totals to 
2018-01-07T18:06:41.184+0530	done dumping newdb.users (4 documents)
2018-01-07T18:06:41.184+0530	writing fruits.seasonal to 
2018-01-07T18:06:41.185+0530	done dumping tutorialkart.stores (5 documents)
2018-01-07T18:06:41.185+0530	writing tutorialkart.myNewCollection to 
2018-01-07T18:06:41.185+0530	done dumping school.totals (3 documents)
2018-01-07T18:06:41.185+0530	done dumping tutorialkart.webpages (5 documents)
2018-01-07T18:06:41.185+0530	done dumping fruits.seasonal (1 document)
2018-01-07T18:06:41.186+0530	done dumping tutorialkart.myNewCollection (0 documents)
~$ cd dump/
~/dump$ ls
admin  fruits  newdb  school  tutorialkart
~/dump$ 

Backup MongoDB Collection

In this example, we shall backup “webpages” collection of “tutorialkart” db.

~$ mongodump --collection webpages --db tutorialkart
2018-01-07T18:24:16.928+0530	writing tutorialkart.webpages to 
2018-01-07T18:24:16.953+0530	done dumping tutorialkart.webpages (5 documents)
~$ cd dump
~/dump$ ls
tutorialkart

Backup MongoDB Database

In this example, we shall backup “tutorialkart” db.

~$ mongodump --db tutorialkart
2018-01-07T18:25:43.148+0530	writing tutorialkart.people to 
2018-01-07T18:25:43.149+0530	writing tutorialkart.customers to 
2018-01-07T18:25:43.149+0530	writing tutorialkart.stores to 
2018-01-07T18:25:43.149+0530	writing tutorialkart.webpages to 
2018-01-07T18:25:43.149+0530	done dumping tutorialkart.people (8 documents)
2018-01-07T18:25:43.150+0530	writing tutorialkart.myNewCollection to 
2018-01-07T18:25:43.150+0530	done dumping tutorialkart.customers (5 documents)
2018-01-07T18:25:43.150+0530	done dumping tutorialkart.stores (5 documents)
2018-01-07T18:25:43.150+0530	done dumping tutorialkart.myNewCollection (0 documents)
2018-01-07T18:25:43.150+0530	done dumping tutorialkart.webpages (5 documents)
~$ cd dump/
~/dump$ ls
tutorialkart
~/dump$ cd tutorialkart/
~/dump/tutorialkart$ ls
customers.bson           myNewCollection.bson           people.bson           stores.bson           webpages.bson
customers.metadata.json  myNewCollection.metadata.json  people.metadata.json  stores.metadata.json  webpages.metadata.json

Backup MongoDB Databases linked to a specific MongoDB instance

In this example, we shall backup all databases linked to the MongoDB instance running at 127.0.0.1:27017

~$ mongodump --host 127.0.0.1 --port 27017
2018-01-07T18:06:41.181+0530	writing admin.system.version to 
2018-01-07T18:06:41.182+0530	done dumping admin.system.version (1 document)
2018-01-07T18:06:41.182+0530	writing school.students to 
2018-01-07T18:06:41.182+0530	writing tutorialkart.people to 
2018-01-07T18:06:41.182+0530	writing tutorialkart.stores to 
2018-01-07T18:06:41.183+0530	writing tutorialkart.customers to 
2018-01-07T18:06:41.183+0530	done dumping school.students (9 documents)
2018-01-07T18:06:41.183+0530	writing tutorialkart.webpages to 
2018-01-07T18:06:41.184+0530	done dumping tutorialkart.people (8 documents)
2018-01-07T18:06:41.184+0530	writing newdb.users to 
2018-01-07T18:06:41.184+0530	done dumping tutorialkart.customers (5 documents)
2018-01-07T18:06:41.184+0530	writing school.totals to 
2018-01-07T18:06:41.184+0530	done dumping newdb.users (4 documents)
2018-01-07T18:06:41.184+0530	writing fruits.seasonal to 
2018-01-07T18:06:41.185+0530	done dumping tutorialkart.stores (5 documents)
2018-01-07T18:06:41.185+0530	writing tutorialkart.myNewCollection to 
2018-01-07T18:06:41.185+0530	done dumping school.totals (3 documents)
2018-01-07T18:06:41.185+0530	done dumping tutorialkart.webpages (5 documents)
2018-01-07T18:06:41.185+0530	done dumping fruits.seasonal (1 document)
2018-01-07T18:06:41.186+0530	done dumping tutorialkart.myNewCollection (0 documents)
~$ cd dump/
~/dump$ ls
admin  fruits  newdb  school  tutorialkart
~/dump$ 

Backup to a particular directory

–out option could be used to create backup at a particular directory

~$ mongodump --out /home/tk/mongo_dump/
2018-01-07T18:37:19.793+0530	writing admin.system.version to 
2018-01-07T18:37:19.795+0530	done dumping admin.system.version (1 document)
2018-01-07T18:37:19.795+0530	writing school.students to 
2018-01-07T18:37:19.795+0530	writing tutorialkart.people to 
2018-01-07T18:37:19.795+0530	writing tutorialkart.stores to 
2018-01-07T18:37:19.795+0530	writing tutorialkart.customers to 
2018-01-07T18:37:19.796+0530	done dumping school.students (9 documents)
2018-01-07T18:37:19.796+0530	writing tutorialkart.webpages to 
2018-01-07T18:37:19.797+0530	done dumping tutorialkart.people (8 documents)
2018-01-07T18:37:19.797+0530	writing newdb.users to 
2018-01-07T18:37:19.797+0530	done dumping tutorialkart.stores (5 documents)
2018-01-07T18:37:19.797+0530	writing school.totals to 
2018-01-07T18:37:19.798+0530	done dumping tutorialkart.customers (5 documents)
2018-01-07T18:37:19.798+0530	writing fruits.seasonal to 
2018-01-07T18:37:19.798+0530	done dumping fruits.seasonal (1 document)
2018-01-07T18:37:19.799+0530	writing tutorialkart.myNewCollection to 
2018-01-07T18:37:19.799+0530	done dumping school.totals (3 documents)
2018-01-07T18:37:19.799+0530	done dumping newdb.users (4 documents)
2018-01-07T18:37:19.799+0530	done dumping tutorialkart.myNewCollection (0 documents)
2018-01-07T18:37:19.800+0530	done dumping tutorialkart.webpages (5 documents)
~$ cd /home/tk/mongo_dump/
~/mongo_dump$ ls
admin  fruits  newdb  school  tutorialkart

Conclusion

In this MongoDB Tutorial, we have learnt to Backup MongoDB Databases and the control provided by mongodump utility for various levels in backup : Collection , Single Database, All Databases linked to a MongoDB Instance.