206

我应该使用什么命令来创建数据库的 MongoDB 转储?

4

20 回答 20

196

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-01T05:18:24.990 回答
133

您还可以gzip用于备份一个集合并即时压缩备份:

mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz

或在文件名中带有日期:

mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz

更新:
在日期文件夹中备份数据库的所有集合。文件被 gzip 压缩:

mongodump --db somedb --gzip --out /backups/`date +"%Y-%m-%d"`

或者对于单个存档:

mongodump --db somedb --gzip --archive > dump_`date "+%Y-%m-%d"`.gz

或者当 mongodb 在 docker 中运行时:

docker exec <CONTAINER> sh -c 'exec mongodump --db somedb --gzip --archive' > dump_`date "+%Y-%m-%d"`.gz
于 2013-07-15T13:51:42.820 回答
97

使用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

来源:http ://www.mongodb.org/display/DOCS/Import+Export+Tools

于 2011-02-02T22:54:55.080 回答
79

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

mongodump -d <database name> -o <target directory>
于 2015-02-13T10:58:40.700 回答
18

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-01T07:19:23.697 回答
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
于 2017-06-18T23:57:17.367 回答
14

有一个名为:mongodump 的实用程序在 mongo 命令行上,您可以键入:

>./mongodump

以上将创建本地主机上所有数据库的转储。要转储单个集合,请使用:

./mongodump --db blog --collection posts

看看:mongodump

于 2011-02-03T08:15:20.313 回答
12

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>
于 2020-05-05T13:58:10.453 回答
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
于 2018-05-14T10:14:18.753 回答
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"

于 2017-09-18T07:49:21.767 回答
5

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

mongodump -h SERVER_NAME:PORT -d DATABASE_NAME

If database user and password are there then you below code.

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

This worked very well in my Linux terminal.

于 2020-04-23T08:28:42.157 回答
4

cmd -->

C:\Program Files\MongoDB\Server\3.2\bin>mongodump.exe --db Dintest
于 2017-10-15T04:11:14.850 回答
4

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.

于 2018-11-24T17:41:14.397 回答
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-28T11:05:10.250 回答
1

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

于 2019-12-02T15:25:31.493 回答
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-02T09:56:50.157 回答
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-29T05:53:19.007 回答
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
于 2019-07-19T11:35:39.750 回答
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
于 2021-10-23T00:51:08.233 回答
-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-31T05:30:28.767 回答