By default, mongorestore looks for a database backup in mongodb's bin\dump folder which is also the default folder for mongodump command for dumping the backup. Example: In this example, we are using a database GeeksForGeeks which has 4 collections.
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
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
This command will make a dump of given database in json and bson format.
mongodump -d <database name> -o <target directory>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With