database 如何创建数据库的 MongoDB 转储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4880874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a MongoDB dump of my database?
提问by TIMEX
What command do I use and run?
我使用和运行什么命令?
采纳答案by earldouglas
Use mongodump
:
使用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
回答by saimadhu.polamuri
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
要将备份文件导入 mongodb,您可以在终端上使用以下命令
mongorestore --db database_name path_to_bson_file
回答by r03
You can also use gzip
for taking backup of one collection and compressing the backup on the fly:
您还可以gzip
用于备份一个集合并动态压缩备份:
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:
或者当 mongodb 在 docker 中运行时:
docker exec <CONTAINER> sh -c 'exec mongodump --db somedb --gzip --archive' > dump_`date "+%Y-%m-%d"`.gz
回答by jatin
This command will make a dump of given database in json and bson format.
此命令将以 json 和 bson 格式转储给定的数据库。
mongodump -d <database name> -o <target directory>
回答by aditya_gaur
There is a utility called : mongodump On the mongo command line you can type :
有一个名为 mongodump 的实用程序在 mongo 命令行上,您可以键入:
>./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
看看:mongodump
回答by Jery
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:
您需要在安装 Mongo 的文件夹中以管理员身份打开命令提示符(在我的情况下:C:\Program Files\MongoDB\Server\3.4\bin)。如果你想转储整个数据库,你可以使用:
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
回答by ShahRokh
Backup/Restore Mongodb with timing.
定时备份/恢复Mongodb。
Backup:
备份:
sudo mongodump --db db_name --out /path_of_your_backup/`date +"%m-%d-%y"`
--db
argument for databse name
--db
数据库名称的参数
--out
argument for path of output
--out
输出路径的参数
Restore:
恢复:
sudo mongorestore --db db_name --drop /path_of_your_backup/01-01-19/db_name/
--drop
argument for drop databse before restore
--drop
还原前删除数据库的参数
Timing:
定时:
You can use crontabfor timing backup:
您可以使用crontab进行定时备份:
sudo crontab -e
It opens with editor(e.g. nano)
它用编辑器打开(例如 nano)
3 3 * * * mongodump --out /path_of_your_backup/`date +"%m-%d-%y"`
backup every day at 03:03 AM
每天凌晨 03:03 备份
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:
根据您的 MongoDB 数据库大小,您可能很快就会因备份过多而耗尽磁盘空间。这就是为什么还建议定期清理旧备份或压缩它们的原因。例如,要删除所有早于 7 天的备份,您可以使用以下 bash 命令:
3 1 * * * find /path_of_your_backup/ -mtime +7 -exec rm -rf {} \;
delete all the backups older than 7 days
删除所有超过 7 天的备份
Good Luck.
祝你好运。
参考:https: //www.digitalocean.com/community/tutorials/how-to-back-up-restore-and-migrate-a-mongodb-database-on-ubuntu-14-04
回答by Michael Horojanski
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"
- 主机 - 主机名端口
- 监听端口用户名
- db db 的用户名
- 数据库名称 ssl
- 安全连接输出
输出到具有名称的创建文件夹
mongodump --host --port --username --db --ssl --password --out _date+"%Y-%m-%d"
回答by Nanhe Kumar
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 trackingi have dump in dump folder
例如我的数据库名称正在跟踪我在转储文件夹中转储
mongodb -d tracking -o dump
Restoring dump
恢复转储
mongorestore -d <databasename> <dum_path>
mongorestore -d tracking dump/tracking
回答by arnav
cmd -->
命令行 -->
C:\Program Files\MongoDB\Server.2\bin>mongodump.exe --db Dintest