Site icon IT Tutorial

MongoDB Backup and Restore Using MongoDump

In case if you feel that you want to take a backup of your files and folders in MongoDB then follow me through this article.

we will be using mongodumo and mongorestore for our backup and restore methodology. mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database. mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems. read more

Taking backup using mongodump:
[root@srv1 mongo]# mongodump --out=/home/mongoBackup --db=mughees
2019-10-21T13:32:48.421+0300 writing mughees.myNewCollection1 to 
2019-10-21T13:32:48.422+0300 writing mughees.myNewCollection2 to 
2019-10-21T13:32:48.425+0300 done dumping mughees.myNewCollection1 (3 documents)
2019-10-21T13:32:48.427+0300 writing mughees.myNewCollection3 to 
2019-10-21T13:32:48.429+0300 done dumping mughees.myNewCollection3 (0 documents)
2019-10-21T13:32:48.431+0300 done dumping mughees.myNewCollection2 (1 document)

–out ==> to provide the path where it will take backup output to.

–db ==> name of the database of which you wot take backup.

DROP mughees DB:
>show databases
admin 0.000GB
config 0.000GB
local 0.000GB
mughees 0.000GB


{ "dropped" : "mughees", "ok" : 1 }

Now we will create a mughees DB again and check if there is any collection available
> use mughees 
use mughees
switched to db mughees 
> show collectionsshow collections #no collection will be shown 

No collections have been shown as the database has been dropped

> show databases;
show databases;
admin 0.000GB
config 0.000GB
local 0.000GB

We do have created mughees DB but DB is not created until and unless you create any collection inside the DB.

Now restore Mughees DB:

Now lets resotre the our backup of mughees db make sure you have created the database with same.

[root@srv1 mongo]# mongorestore --db=mughees /home/mongoBackup/mughees

2019-10-21T13:41:34.773+0300 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-10-21T13:41:34.774+0300 building a list of collections to restore from /home/mongoBackup/mughees dir
2019-10-21T13:41:34.776+0300 reading metadata for mughees.myNewCollection1 from /home/mongoBackup/mughees/myNewCollection1.metadata.json
2019-10-21T13:41:34.783+0300 reading metadata for mughees.myNewCollection2 from /home/mongoBackup/mughees/myNewCollection2.metadata.json
2019-10-21T13:41:34.784+0300 reading metadata for mughees.myNewCollection3 from /home/mongoBackup/mughees/myNewCollection3.metadata.json
2019-10-21T13:41:34.828+0300 restoring mughees.myNewCollection1 from /home/mongoBackup/mughees/myNewCollection1.bson
2019-10-21T13:41:34.832+0300 no indexes to restore
2019-10-21T13:41:34.832+0300 finished restoring mughees.myNewCollection1 (3 documents, 0 failures)
2019-10-21T13:41:34.866+0300 restoring mughees.myNewCollection2 from /home/mongoBackup/mughees/myNewCollection2.bson
2019-10-21T13:41:34.869+0300 no indexes to restore
2019-10-21T13:41:34.871+0300 finished restoring mughees.myNewCollection2 (1 document, 0 failures)
2019-10-21T13:41:34.881+0300 restoring mughees.myNewCollection3 from /home/mongoBackup/mughees/myNewCollection3.bson
2019-10-21T13:41:34.895+0300 restoring indexes for collection mughees.myNewCollection3 from metadata
2019-10-21T13:41:34.921+0300 finished restoring mughees.myNewCollection3 (0 documents, 0 failures)
2019-10-21T13:41:34.921+0300 4 document(s) restored successfully. 0 document(s) failed to restore.
[root@srv1 mongo]#
Check the restore database:

Now let’s check it the db and the collections inside are available or not :

>show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
mughees 0.000GB
> > use mugheesuse mughees
switched to db mughees
> show collectionsshow collections

 1,735 views last month,  2 views today

Exit mobile version