MongoDB: backup/restore

04/05/2014 20:55 | DB, MongoDB, OS X

System: OS: OS X 10.9

Mongo: MongoDB shell version 2.4.9

Link: http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/

MongoDB_1

Generally according to MongoDB docs three backup methods can be used for DB dump:

  • MongoDB Management Service (MMS)
  • Copying Underlying Data Files
  • Dump/restore using mongodump/mongorestore tool

In this topic we will focus on only one of this dump methods. In practice during development process or during deployment real project on production environment most useful mongodump/mongorestore tool.

If you have Mongo installed on your computer the mongodump/mongorestore tool already included to the package and can be used simply from command line.

The result of running mongodump command you will get bunch of BSON file with all you DB data that is should be result of your back-up and source for future restore. In real life that is one of the easiest and fastest ways to dump your data. But there one important limitation in a case when you need dump really huge DB that is not a best way to do it and you need use other ways for dumping DB. However the mongodump/mongorestore are the best way to work with DB during development, make dump of relatively small DB. Personally I tested mongodump/mongorestore with 20GB DB. I had situation when I needed to dump 20GB database from server to my local laptop and restore it for local test. Finally I spend few hours to make this dump and few hours to restore it but its works!

IMPORTANT! mongodump/mongorestore not best way to work with huge DBs. Successfully tested with 20GB database. While running mongodump/mongorestore you should have admin access rights.

Mongodump

By default mongodump using address 127.0.0.1 or localhost and port 27017. Thats mean if you will not specify host and port manually Mongo will use default one.

Simplest way to dump db on your local computer:

As a result of this command you will get in the folder from where you executed this command new folder dump/project_test_db/ with bunch of BSON files with indexes and data from source DB. Also as you running command on your local machine you  no need to set password or user name even host/port not required:

For more comfortable use of dump tool I would recommend to use —out parameter for specifying destination folder for dump:

As result your will get result instead of dump/project_test_db folder in mongodump/db/project_test_db folder. And finally for dumping data from server if DB protected with user/password authentication use full command:

Note! that hostname can be specified without any prefixes like “http://” or “www”.

In a case if you want limit your dump and you need only some parts of database use –collection parameter to specify collection for dump:

Mongorestore

Simplest mongorestore example if you need to restore db on your local:

In this command mongodump/db/project_test_db/ path to your BSON files.

Full version of restore looks like that:

About all functionality I would recommend to read Mongo docs for this topic. That was only small overview of main features of such functionality.

© 2014 Artem Gryn personal blog