How do I create a MongoDB dump of my database?
Asked Answered
L

21

241

What command should I use to create a MongoDB dump of my database?

Leavis answered 2/2, 2011 at 22:52 Comment(1)
Just a single mongodump without any flags and you get dump folderCalcareous
A
101

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

Arrest answered 2/2, 2011 at 22:54 Comment(3)
To put the results in a single compressed file, see unix.stackexchange.com/questions/93139/…Lucre
At mongodb server at which place the database will be stored?Grooms
Please refer to this link. #44997006 @spaceearthEyrir
D
226

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
Deserve answered 1/4, 2015 at 5:18 Comment(3)
What is the significance of metadata.json for restoring?Tory
dba.stackexchange.com/questions/113017/…Than
Can it be than stared as a compressed file?Olivarez
A
161

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
Apanage answered 2/2, 2011 at 22:52 Comment(10)
This looks very nice, but how do you import this gzip file ?Navigate
You need to 'gunzip' them, and then use mongorestoreDenson
says: ERROR: don't know what to do with file! Gunizpped and tried `mongorestore --db db_name 'gunzipped file'Fariss
typo : "-db" => "--db"Writ
In version 3.2 of mongodump or higher you can use the --gzip option to do that: mongodump_manpage and same option for mongorestoreSelina
@r03, Is it possible to remove automatically old backups? For example remove backups which older than 1 week.Trifling
@Pyrejkee: #13489898Heeltap
@r03, do you happen to know why template for date date +"%Y-%m-%d" doesn't work on windows console?Trifling
@Pyrejkee, better ask new questions for these things instead of a comment. (date on windows is very different)Heeltap
How do I restore this single archive? from mongodump --db somedb --gzip --archive > dump_date "+%Y-%m-%d".gzGift
A
101

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

Arrest answered 2/2, 2011 at 22:54 Comment(3)
To put the results in a single compressed file, see unix.stackexchange.com/questions/93139/…Lucre
At mongodb server at which place the database will be stored?Grooms
Please refer to this link. #44997006 @spaceearthEyrir
D
81

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

mongodump -d <database name> -o <target directory>
Demonstrative answered 13/2, 2015 at 10:58 Comment(2)
And how can we restore all the collections (bson file) in single command?Quantitative
Use this command to restore all the collections - mongorestore --db <database name> <source directory(bson files)>Cha
P
31

Edit: Updated commands based on latest version of MongoDB v4.4.2

to export

mongodump -d <database name> -o <backup-folder>

to import

mongorestore -d <database name> --dir <backup-folder>
Pitchfork answered 5/5, 2020 at 13:58 Comment(1)
still work mongodump version: 100.7.0 git version: 17946f45f5fabfcdd99f8960b9109f976d370631 Go version: go1.19.3 os: linux arch: amd64 compiler: gcHomosporous
P
20

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

Premature answered 1/1, 2019 at 7:19 Comment(0)
L
17

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
Lucan answered 18/6, 2017 at 23:57 Comment(0)
B
15

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

Brocatel answered 3/2, 2011 at 8:15 Comment(0)
O
9

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
Oxysalt answered 14/5, 2018 at 10:14 Comment(0)
E
9

If your database is in the local system. Then you can type the below command. for Linux terminal

mongodump -h SERVER_NAME:PORT -d DATABASE_NAME

If database has username and password then you can use below code.

mongodump -h SERVER_NAME:PORT -d DATABASE_NAME -u DATABASE_USER -p PASSWORD

This worked very well in my Linux terminal.

Ethnomusicology answered 23/4, 2020 at 8:28 Comment(0)
C
8

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"

Chemise answered 18/9, 2017 at 7:49 Comment(2)
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.Southern
Is there any chance to have an example of how a functioning command with all parameters should look?Gon
L
7

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

Omitting --collection COLLECTION_NAME will dump entire DB.

Liebowitz answered 24/11, 2018 at 17:41 Comment(0)
A
6

cmd -->

C:\Program Files\MongoDB\Server\3.2\bin>mongodump.exe --db Dintest
Ambrosio answered 15/10, 2017 at 4:11 Comment(2)
Easy and quickest optionTrinomial
Thanks a lot Arnav, really appreciated.Columella
V
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

Viscosity answered 28/2, 2017 at 11:5 Comment(0)
S
1

use "path" for windows else it gives the error as: positional arguments not allowed

Spade answered 2/12, 2019 at 15:25 Comment(0)
T
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 !!!!!
Towline answered 2/8, 2016 at 9:56 Comment(0)
D
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
Domella answered 29/9, 2016 at 5:53 Comment(0)
K
0
 Use -v to see progress of backup data
    mongodump -v --db dbname --out /pathforbackup/NewFolderforBackupData

 you can use it for restore also
    mongorestore -v --db dbname --drop /pathforbackup/NewFolderforBackupData/dbname

with multile v like -vvvv you will get more information
Kick answered 19/7, 2019 at 11:35 Comment(0)
B
0

I created a library called BackSync which helps to backup and sync MongoDB databases (also local files/directories and some other known databases), you can also schedule it with pm2 to run the process automatically.

Install:

npm i -g @ayoubamine/backsync

Add your MongoDB backup source:

backsync add backup

Add your sync source (Local, Google Drive, ...):

backsync add sync

Then, run the process: 🙌

backsync run
Bee answered 23/10, 2021 at 0:51 Comment(1)
Nice. I will try and contribute to this opensource repo.Exudate
C
0

I had tough time to dump mongodb database and was able to extract metadata, but records are not coming up for some reason. But, adding --forceTableScan helped to dump both metadata and records from db with all collections - see the script below,

mongodump --forceTableScan --host=HOST_NAME_OR_IP --port=PORT --username=USER_NAME --password="PASSWORD" --db=DB_NAME --out=FILE_NAME_BACKUP

above command will dump metadata and records of all collections in the db.

Cannonry answered 22/3 at 6:17 Comment(0)
B
-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

Basaltware answered 31/3, 2017 at 5:30 Comment(1)
1. mongodump - is a command to create a mongo dump along with we need input about specicification. 2. -h represents your mongodb hostname. 3. -u represents your mongodb username. 4. -p represents passsword. 5. --db represents the databasename tha we need to take dump. 6. --port represents the port your mongo is running. 7. --out represents the destination of your dump with name.Basaltware

© 2022 - 2024 — McMap. All rights reserved.