35

I have a server that is having trouble talking to the outside world. I would like to get its mongodb contents to another server--but since the servers cannot talk to eachother db.copyDatabase() won't do.

Is there something like mysqldump where I could dump the db into a binary file, scp that somewhere, and then use that to populate another mongodb server?

Community
  • 1
  • 1
Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103

3 Answers3

38

If you're using Ubuntu/Linux, run the following commands. First, mongodump on the origin server:

mongodump --db DataBaseName

Copy the generated dump/DataBaseName folder to the new machine. Then, import using mongorestore:

mongorestore --db DataBaseName /path/to/DataBaseName 

Note that /path/to/DataBaseName should be a directory filled with .json and .bson representations of your data.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Tom G
  • 3,650
  • 1
  • 20
  • 19
  • 3
    To save to a specific location add --out like: mongodump --db DataBaseName --out ~/backups/ – Chris Aug 20 '18 at 19:11
34

Use the mongodump and mongorestore commands.

mongodump --db test --collection collection
mongorestore --collection collection --db test dump/

You can also gzip. The documentation has more examples.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • mongodump--so obvious in hindsight. – Aaron Silverman Aug 29 '11 at 18:19
  • Does this work for old versions? I have mongo v2.4.9 and it says: ` mongodump is not defined`. – user2173353 Apr 05 '16 at 09:01
  • i know this but if my data is huge with many collection now can i copy and paste whole database from file manager to another PC? – suresh pareek Jul 01 '17 at 16:16
  • I meet some pb with the mongorestore (`root directory must be a dump of a single database`) I use : `mongorestore --db myDB .\dump\myDB` as you can read in [Mongodb monogorestore “root directory must be a dump of a single database”](https://stackoverflow.com/a/27923656/608772) or here under – JPBlanc Jul 11 '17 at 08:18
4

If you want to transfer database to another system, then you must use the following commands.

First dump the database to the output directory:

mongodump --db DatabaseName -o ./DirectoryName

then copy that directory and put it into your machine and issue this command:

mongorestore --db DBName ./DirectoryName
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42