How do I create a MongoDB dump of my database?

Go To StackoverFlow.com

113

What command do I use and run?

2011-02-02 22:52
by TIMEX
Just a single mongodump without any flags and you get dump folde - Ivan Aracki 2018-06-26 14:56


66

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

2011-02-02 22:54
by earldouglas
To put the results in a single compressed file, see http://unix.stackexchange.com/questions/93139/can-i-zip-an-entire-folder-using-gzi - Donal Lafferty 2015-08-27 08:52
At mongodb server at which place the database will be stored - space earth 2017-06-15 10:09


104

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
2015-04-01 05:18
by saimadhu.polamuri
What is the significance of metadata.json for restoring - Nabin 2018-05-08 14:21


61

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
2013-07-15 13:51
by r03
This looks very nice, but how do you import this gzip file - fahim ayat 2014-07-14 10:30
You need to 'gunzip' them, and then use mongorestor - Meny Issakov 2014-08-06 10:22
says: ERROR: don't know what to do with file! Gunizpped and tried `mongorestore --db db_name 'gunzipped file - amitchhajer 2015-06-10 06:54
typo : "-db" => "--db - Vivien 2015-10-01 08:33
In version 3.2 of mongodump or higher you can use the --gzip option to do that: mongodump_manpage and same option for mongorestoreBoop 2016-05-30 13:48


51

This command will make a dump of given database in json and bson format.

mongodump -d <database name> -o <target directory>
2015-02-13 10:58
by jatin
Only this worked for m - Jamie Hutber 2016-11-30 00:27


12

There is a utility called : mongodump On the mongo command line you can type :

>./mongodump

The above will create a dump of all the databases on your localhost. To make dump of a single collection use:

./mongodump --db blog --collection posts

Have a look at : mongodump

2011-02-03 08:15
by aditya_gaur


11

You need to open command prompt as an administrator in a folder where your Mongo is installed (in my case: C:\Program Files\MongoDB\Server\3.4\bin). If you want to dump your whole database, you can just use:

mongodump --db database_name

You also have posibilities to dump only certain collection(s), or to dump all but certain collection(s).

If you want to dump only one collection (for example users):

mongodump  --db database_name --collection users

If you want to dump all but users collection:

mongodump  --db database_name --excludeCollection=users

It is also possible to output the dump to an archive file:

mongodump --archive=test.archive --db database_name
2017-06-18 23:57
by Jery


7

Following command connect to the remote server to dump a database:

<> optional params use them if you need them

  • host - host name port
  • listening port username
  • username of db db
  • db name ssl
  • secure connection out
  • output to a created folder with a name

    mongodump --host --port --username --db --ssl --password --out _date+"%Y-%m-%d"

2017-09-18 07:49
by Michael Horojanski
For those who are getting this error after running above suggested query- error parsing command line options: unknown option "ssl". Try to run above query after removing --ssl . it worked for me.Thanks - Anurag_BEHS 2018-07-09 19:44


6

You can dump your database and restore with bellow command

mongodb  -d <Your_db_name> -o <path of your folder>

for example my database name is tracking i have dump in dump folder

mongodb  -d tracking -o dump

Restoring dump

mongorestore -d <databasename> <dum_path>

mongorestore -d tracking  dump/tracking
2018-05-14 10:14
by Nanhe Kumar


3

cmd -->

C:\Program Files\MongoDB\Server\3.2\bin>mongodump.exe --db Dintest

2017-10-15 04:11
by arnav
Easy and quickest optio - Ignacio Ara 2018-07-30 16:26
Thanks a lot Arnav, really appreciated - HassanSh__3571619 2019-02-18 20:33


1

Below command will work to take dump of mongo db .

mongodump -d -o

On Windows : try this one where c:\mongodump is dump file location , It will create metadata in json, and backup in bson format

C:\MongoDB\bin>mongodump -d -o c:\mongodump

2017-02-28 11:05
by Bhasker The Navigator


1

Backup/Restore Mongodb with timing.

Backup:

sudo mongodump --db db_name --out /path_of_your_backup/`date +"%m-%d-%y"`

--db argument for databse name

--out argument for path of output

Restore:

sudo mongorestore --db db_name --drop /path_of_your_backup/01-01-19/db_name/

--drop argument for drop databse before restore

Timing:

You can use crontab for timing backup:

sudo crontab -e

It opens with editor(e.g. nano)

3 3 * * * mongodump --out /path_of_your_backup/`date +"%m-%d-%y"`

backup every day at 03:03 AM

Depending on your MongoDB database sizes you may soon run out of disk space with too many backups. That's why it's also recommended to clean the old backups regularly or to compress them. For example, to delete all the backups older than 7 days you can use the following bash command:

3 1 * * * find /path_of_your_backup/ -mtime +7 -exec rm -rf {} \;

delete all the backups older than 7 days

Good Luck.

ref: https://www.digitalocean.com/community/tutorials/how-to-back-up-restore-and-migrate-a-mongodb-database-on-ubuntu-14-04

2019-01-01 07:19
by ShahRokh


0

Or you can make backup script on Windows, remember to add Winrar to %PATH%

bin\mongodump --db=COL1 -o D:\BACK\COL1
rar.exe a -ep1 -r COL1.rar COL1
rename COL1.rar "COL1_%date:~10,4%_%date:~7,2%_%date:~4,2%_%time:~0,2%_%time:~3,2%.rar"

#rmdir /s /q COL1 -> don;t run this on your mongodb/ dir !!!!!
2016-08-02 09:56
by user956584


0

take mongodb backup for particular db and delete 7 days old backup using bin sh command :-

#!/bin/bash

MONGO_DATABASE="nexgtv_16"
APP_NAME="test"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/mongodbbackups/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
$MONGODUMP_PATH -d $MONGO_DATABASE
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
find /home/mongodbbackups/backups/test/ -mindepth 1 -mtime +7 -delete
2016-09-29 05:53
by manoj tiwari


0

Mongo dump and restore with uri to local

mongodump --uri "mongodb://USERNAME:PASSWORD@IP_OR_URL:PORT/DB_NAME" --collection COLLECTION_NAME -o LOCAL_URL

If you do not specify --colletion COLLECTION_NAME, it will dump entire DB.

2018-11-24 17:41
by smartworld-dm


-4

mongodump -h hostname -u dbusername -p dbpassword --db dbname --port portnumber --out /path/folder

mongodump -h hostname -u dbusername -p dbpassword --db dbname --port portnumber --out /path/folder.gz

2017-03-31 05:30
by Anjankumar H N
Please add an explanation around what these commands do - Steve 2017-03-31 05:51
>
  • mongodump - is a command to create a mongo dump along with we need input about specicification.

  • -h represents your mongodb hostname.

  • -u represents your mongodb username.

  • -p represents passsword.

  • --db represents the databasename tha we need to take dump.

  • --port represents the port your mongo is running.
  • --out represents the destination of your dump with name.
  • - Anjankumar H N 2017-03-31 06:07
    Ads