DevEasy
Nov 14, 2021

How to create a Mongodb backups and restore the db to use in the local from Mobaxterm

To Backup the MongoDB:-

If you are using the server and want to take the baackup of mongodb, then run below command:-

mongodumb -d dbname -o file-na

me

Ex:- Let my dbname is product and want to take the backup of db in mongoBackups file, then,

mongodump -d product -o mongoBackups

To restore the db in local:-

If you have created the backup of mongo in the container, then run this command to copy that file from mongo container to a certain folder.

docker cp mongo:/filename

Ex:- If you want to copy that mongoBackups file to Myproject folder, then run this command in Myproject folder. 

docker cp mongo:/mongoBackups 

To restore the db in local, copy the mongoBackups in the local and run this command inside that folder. Before restoring the db, check if the new db name and old db name is same or not, if it is same, then rename the existing db using following command:-

db.copyDatabase("old db name", "new db name")

Then remove the existing db by going into that db.

Eg:- use dbname then db.dropDatabase();
mongorestore -d  dbname dbname

If ypu have the latest mongo version >4.2 , then use this command to restore

mongorestore -d new_db_name mongodump/old_db_name

Aamir

Aamir

A handful guide to make the developerslife easy.

Leave a Reply

Related Posts

Categories

© Copyright, 2023