65

I created a backup of all my databases using mongodump command. Now I want to restore a specific database using mongorestore command.

How is this possible, I use this command: --db option then mongodb doesn't restore a specific database.

94

To restore a single database you need to provide the path to the dump directory as part of the mongorestore command line.

For example:

# Backup the training database
mongodump --db training

# Restore the training database to a new database called training2
mongorestore --db training2 dump/training

The --db option for mongodump specifies the source database to dump.

The --db option for mongorestore specifies the target database to restore into.

|improve this answer|||||
  • I backup using mongodump --archive to create .gz file. May I know how we restore from .gz file and also specify new database name? My .gz file size is 2Gb, so extracting it is very time-consuming. – Nam G VU Nov 2 '17 at 3:05
  • @NamGVU Can you post a new question including your version of mongodump and the exact command line you are using? The --archive command creates a special archive format which is different from --gzip. Depending on your mongodump version and command-line options used, there are different recommendations for renaming databases or collections with mongorestore. – Stennie Nov 2 '17 at 6:57
  • I have make the question as you required, please help. Thank you! dba.stackexchange.com/q/189928/52550 – Nam G VU Nov 2 '17 at 8:13
  • May you help us here dba.stackexchange.com/q/189928/52550 – Nam G VU Nov 3 '17 at 1:25
  • Thank you Stennie, your suggestion worked perfectly for me. – user752746 Feb 9 '18 at 18:50
16

Use the following command to restore mongo db:

mongorestore -d dbname dbpath
|improve this answer|||||
10
$ mongorestore --drop -d <database-name> <dir-of-backup-files>
  • --drop Drop is necessary if you are replacing an existing db
  • -d <database-name> The name of the database to create/replace
  • <dir-of-backup-files> For some reason this is necessary even if it's the current directory

enter image description here

Reference: https://coderwall.com/p/3hx06a/restore-a-mongodb-database-mongorestore

|improve this answer|||||
  • 1
    this worked for me - just be careful as it will drop the db – Alexander Mills Sep 1 '17 at 20:56
  • Helpful details here. – Wylliam Judd Oct 25 '18 at 23:21
1

Restoring using mongorestore

After making sure that mongod and mongo are running, go to the dump's parent directory in a new terminal. And use the command mongorestore dump. Where dump is the folder name in which the database dump is present.

Restoring using mongorestore

|improve this answer|||||
-1

Maybe slightly unrelated, but based on the answers here i got the following

Using mongodump and mongorestore archive feature I comprised a oneliner:

mongodump --host H --port P --username U --password PWD --archive | mongorestore --username U1 --password PWD1 --archive

The above example will :

  1. Dump data from host H with port P logged in with user U and password PWD
  2. "Stream" that data into your local database with user U1 and password PWD1
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.