How do I create a MongoDB dump of my database?
Use mongodump
:
$ ./mongodump --host prod.example.com
connected to: prod.example.com
all dbs
DATABASE: log to dump/log
log.errors to dump/log/errors.bson
713 objects
log.analytics to dump/log/analytics.bson
234810 objects
DATABASE: blog to dump/blog
blog.posts to dump/log/blog.posts.bson
59 objects
DATABASE: admin to dump/admin
Source: http://www.mongodb.org/display/DOCS/Import+Export+Tools
To dump your database for backup you call this command on your terminal
mongodump --db database_name --collection collection_name
To import your backup file to mongodb you can use the following command on your terminal
mongorestore --db database_name path_to_bson_file
You can also use gzip
for taking backup of one collection and compressing the backup on the fly:
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
or with a date in the file name:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
Update:
Backup all collections of a database in a date folder. The files are gziped:
mongodump --db somedb --gzip --out /backups/`date +"%Y-%m-%d"`
Or for a single archive:
mongodump --db somedb --gzip --archive > dump_`date "+%Y-%m-%d"`.gz
Or when mongodb is running inside docker:
docker exec <CONTAINER> sh -c 'exec mongodump --db somedb --gzip --archive' > dump_`date "+%Y-%m-%d"`.gz
This command will make a dump of given database in json and bson format.
mongodump -d <database name> -o <target directory>