Dump and Restore a MongoDB Database

Dump a MongoDB database:

$ $MONGO_HOME/bin/mongodump --db messageservice --out /tmp/messageservice-$(date +"%Y-%m-%d-%S")
connected to: 127.0.0.1
Wed Sep 14 22:18:21.960 DATABASE: messageservice     to     /tmp/messageservice-2014-09-17-21/messageservice
Wed Sep 14 22:18:21.961     messageservice.system.indexes to /tmp/messageservice-2014-09-17-21/messageservice/system.indexes.bson
Wed Sep 14 22:18:21.961          3 objects
Wed Sep 14 22:18:21.961     messageservice.messages to /tmp/messageservice-2014-09-17-21/messageservice/messages.bson
Wed Sep 14 22:18:21.963          1 objects
Wed Sep 14 22:18:21.963     Metadata for messageservice.messages to /tmp/messageservice-2014-09-17-21/messageservice/messages.metadata.json

Restore the database from the dump created with the previous command:

$ $MONGO_HOME/bin/mongorestore --db messageservice /tmp/messageservice-2014-09-17-21/messageservice/messages.bson
connected to: 127.0.0.1
Wed Sep 17 22:24:05.439 /tmp/messageservice-2014-09-17-21/messageservice/messages.bson
Wed Sep 17 22:24:05.439     going into namespace [messageservice.messages]
Wed Sep 17 22:24:05.445     Created collection messageservice.messages with options: { "create" : "messages" } 1 objects found
Wed Sep 17 22:24:05.445     Creating index: { key: { _id: 1 }, ns: "messageservice.messages", name: "_id_" }
Wed Sep 17 22:24:05.445     Creating index: { key: { messageBox: 1 }, ns: "messageservice.messages", name: "messageBox_1" }
Wed Sep 17 22:24:05.449     Creating index: { key: { creationDate: 1 }, ns: "messageservice.messages", name: "creationDate_1" }

Visit mongodump and mongorestore for more information.