Importing and Exporting MongoDB Databases
mysqldump
for mongodb?
It should have come as no surprise that the command I wanted was called mongodump
, really! There are lots of ways of importing/exporting mongodb databases, and there is some excellent documentation on the mongodb.org site about the various tools.
I moved one database from one server to another, and it was pretty straightforward so I thought I’d write down the commands that I used so I can refer to them later (I did this yesterday and already I’ve had to look at my bash history to remember how to do it!).
Exporting from MongoDB
To export the database, simply tell mongodump
which database (or collection) you want to export, and where to export it to. Mine was the pets database, so my command looks like this:
mongodump -d pets -o petsbackup
This dumps the pets
database into the petsbackup
directory. Take a look at what we have in that directory now:
pets ├── animals.bson └── system.indexes.bson 0 directories, 2 files
The only collection in my pets
database is the animals
collection, however you’ll see a .bson
file for each collection in your database, plus the system indexes collection. It is up to you whether you want to take individual collections, or a whole database, but bear in mind that your choice will dictate whether you get information about indexes etc when you import the data elsewhere.
Importing to MongoDB
To import, simply use the mongorestore
command, which accepts either a single .bson
file representing a collection, or a directory containing multiple files. Here’s my example:
mongorestore -d pets /path/to/pets
You can specify any database name and path to files you like, so for taking backups or restoring additional copies of a database, this can be really handy. The mongo commands are well-documented and I found them easy to work with – hopefully this helps you work with them too!
Thanks
Your article is very useful for me i am currently working in mongodb.
Thanks, it is very helpful ;)
I would recommend against using mongodump/mongorestore. It is very slow and once you get past 20/30GB of data it can take hours to restore. Moreover your data keeps changing during the backup – so you don’t get a consistent snapshot. Use a filesystem snapshot or cloud snapshot mechanism. At MongoDirector we use LVM snapshots to get a point in time copy of the database and we store it in S3 as a backup – http://blog.mongodirector.com/mongodb-backup-and-restore/
Thanks for your useful article, I want to know how to import database in remote server how is it possible?
I am trying to backup a remote db. I just connected to remote db using mongo hostname:27017 and now run these commands.
Thank you very much. It helps me.
Thank you very much! :)
yes thank you, It helps me to create database
Thank you. Nice and flexible for a newbie :)
Your article is very useful for me to export and import dbs
Thank you very much! :)
Thank you very much
Thank you, straight to the point. Thats gold.
Good post, exactly what I needed.