简体   繁体   English

如何将转储恢复到正在运行的 mongodb

[英]How to restore the dump into your running mongodb

I want to load data/restore dump data in mongoDB using mongorestore.我想使用 mongorestore 在 mongoDB 中加载数据/恢复转储数据。 I am trying to command我试图命令

mongorestore dump

but it giving me error但它给了我错误

Sat Sep 21 16:12:33.403 JavaScript execution failed: SyntaxError: Unexpected identifier

How can we restore or put data into mongoDB??我们如何将数据恢复或放入 mongoDB?? Please give me the steps.请给我步骤。

mongodump: To dump all the records: mongodump:转储所有记录:

mongodump --db databasename

To limit the amount of data included in the database dump, you can specify --db and --collection as options to mongodump.要限制数据库转储中包含的数据量,您可以指定 --db 和 --collection 作为 mongodump 的选项。 For example:例如:

mongodump --collection myCollection --db test

This operation creates a dump of the collection named myCollection from the database 'test' in a dump/ subdirectory of the current working directory.此操作从当前工作目录的 dump/ 子目录中的数据库“test”创建名为 myCollection 的集合的转储。 NOTE: mongodump overwrites output files if they exist in the backup data folder.注意: mongodump 会覆盖备份数据文件夹中存在的输出文件。


mongorestore: To restore all data to the original database: mongorestore:将所有数据恢复到原始数据库:

1) mongorestore --verbose \path\dump

or restore to a new database:或恢复到新数据库:

2) mongorestore --db databasename --verbose \path\dump\<dumpfolder>

Note: Both requires mongod instances.注意:两者都需要 mongod 实例。

Dump DB by mongodump通过mongodump转储数据库

mongodump --host <database-host> -d <database-name> --port <database-port> --out directory

Restore DB by mongorestore通过mongorestore恢复数据库

With Index Restore使用索引还原

mongorestore --host <database-host> -d <database-name> --port <database-port> foldername

Without Index Restore无索引还原

mongorestore --noIndexRestore --host <database-host> -d <database-name> --port <database-port> foldername

Import Single Collection from CSV [1st Column will be treat as Col/Key Name]从 CSV 导入单个集合 [第一列将被视为列/键名称]

mongoimport --db <database-name> --port <database-port> --collection <collection-name> --type csv --headerline --file /path/to/myfile.csv

Import Single Collection from JSON从 JSON 导入单个集合

mongoimport --db <database-name> --port <database-port> --collection <collection-name> --file input.json

The directory should be named 'dump' and this directory should have a directory which contains the .bson and .json files.该目录应命名为“转储”,并且该目录应有一个包含 .bson 和 .json 文件的目录。 This directory should be named as your db name.此目录应命名为您的数据库名称。

eg: if your db name is institution then the second directory name should be institution.例如:如果您的数据库名称是机构,那么第二个目录名称应该是机构。

After this step, go the directory enclosing the dump folder in the terminal, and run the command这一步之后,进入终端中转储文件夹所在的目录,运行命令

mongorestore --drop. mongorestore --drop。

Do see to it that mongo is up and running.请务必确保 mongo 已启动并运行。

This should work fine.这应该可以正常工作。

To restore a single database:恢复单个数据库:

1. Backup the 'users' database
$ mongodump --db users

2. Restore the 'users' database to a new database called 'users2'
$ mongorestore --db users2 dump/users

To restore all databases:恢复所有数据库:

1. Backup all databases
$ mongodump

2. Restore all databases
$ mongorestore dump

Follow this path.走这条路。

C:\Program Files\MongoDB\Server\4.2\bin

Run the cmd in bin folder and paste the below command在 bin 文件夹中运行 cmd 并粘贴以下命令

mongorestore --db <name-your-database-want-to-restore-as> <path-of-dumped-database>

For Example:例如:

mongorestore --db testDb D:\Documents\Dump\myDb

You can take a dump to your local machine using this command:您可以使用以下命令将转储到本地计算机:

mongodump -h <host>:<port> -u <username> -p <password> -d ubertower-new -o /path/to/destination/directory

You can restore from the local machine to your Mongo DB using this command您可以使用此命令从本地机器恢复到您的 Mongo DB

mongorestore -h <host>:<port> -u <username> -p <password> -d <DBNAME> /path/to/destination/directory/<DBNAME>
  1. Start mongod启动mongod
  2. Navigate to folder where you have extracted "enron.zip" in OS shell(cmd in case of windows)导航到您在 OS shell 中提取“enron.zip”的文件夹(在 Windows 的情况下为 cmd)
  3. Then type ">mongorestore -d your_db_name dump/enron"然后输入“> mongorestore -d your_db_name dump/enron”

对于 mongoDB 数据库还原,请在此处使用此命令

mongorestore --db databasename --drop dump file path

You can also restore your downloaded Atlas Backup .wt WiredTiger files (which unzips or untar as a restore folder) to your local MongoDB.您还可以将下载的 Atlas Backup .wt WiredTiger 文件(作为restore文件夹解压缩或解压) restore到本地 MongoDB。

First, make a backup of your /data/db path.首先,备份您的/data/db路径。 Call it /data_20200407/db .称之为/data_20200407/db Second, copy paste all the .wt files from your Atlas Backup restore folder into your local /data/db path.其次,将 Atlas Backup 还原文件夹中的所有.wt文件复制粘贴到本地/data/db路径中。 Restart your Ubuntu or MongoDB server.重新启动您的 Ubuntu 或 MongoDB 服务器。 Start your Mongo shell and you should have those restored files there.启动你的 Mongo shell,你应该在那里有那些恢复的文件。

I have been through a lot of trouble so I came up with my own solution, I created this script, just set the path inside script and db name and run it, it will do the trick我遇到了很多麻烦所以我想出了自己的解决方案,我创建了这个脚本,只需在脚本和数据库名称中设置路径并运行它,它就可以解决问题

#!/bin/bash

FILES= #absolute or relative path to dump directory
DB=`db` #db name
for file in $FILES
do

    name=$(basename $file)
    collection="${name%.*}"
    echo `mongoimport --db "$DB" --file "$name" --collection "$collection"`

done

mongodump --host test.mongodb.net --port 27017 --db --username --password --authenticationDatabase admin --ssl --out mongodump --host test.mongodb.net --port 27017 --db --username --password --authenticationDatabase admin --ssl --out

mongorestore --db --verbose mongorestore --db --verbose

If you are okay to drop the existing collections then then following command works fine for me.如果你可以删除现有的集合,那么下面的命令对我来说很好用。

  • Okay to drop the existing collections可以删除现有的集合
$ uri_complete="your_complete_uri"
$ restoreFileName="your_restore_filename"
$ mongorestore --uri=$uri_complete -v --gzip --archive=$restoreFileName --drop
  • Not okay to drop existing collections不能删除现有的集合
$ uri_complete="your_complete_uri"
$ restoreFileName="your_restore_filename"
$ mongorestore --uri=$uri_complete -v --gzip --archive=$restoreFileName

If your database is running on localhost on default port without authentication then the following will work.如果您的数据库在没有身份验证的情况下在 localhost 上的默认端口上运行,则以下操作将起作用。

$ mongorestore -v --gzip --archive=restorefile.gzip --drop

More details - mongorestore更多详情 - mongorestore

Process of Backup MongoDb Collections.备份 MongoDb 集合的过程。

  1. Find the MongoDb folder in ProgramFiles or ProgramFiles32.在 ProgramFiles 或 ProgramFiles32 中找到 MongoDb 文件夹。
  2. Navigate to bin Folder.Find the path like this.导航到bin 文件夹。找到这样的路径。 C:\\Program Files\\MongoDB\\Server\\4.2\\bin C:\\Program Files\\MongoDB\\Server\\4.2\\bin
  3. Make you have to Install Mongo Dump Tools.使您必须安装 Mongo Dump Tools。 在此处输入图片说明
  4. If you are not find any such tools , kindly go through this link and install it.如果您没有找到任何此类工具,请通过此链接进行安装。 MongDb Database Tools MongDb 数据库工具
  5. inside bin Folder open CMD or navigate to MongdDb Server Bin folder.在 bin 文件夹内打开 CMD 或导航到 MongdDb Server Bin 文件夹。
  6. Run this Command mongodump --db yourdatabasename .运行此命令mongodump --db yourdatabasename after running this command Dump creation process will start and it will create and one Dump folder in side bin.运行此命令后,转储创建过程将开始,它将在 side bin 中创建一个转储文件夹。

Restore Process还原过程

  1. open the CMD prompt in Mongdb Server bin Folder.在 Mongdb Server bin 文件夹中打开 CMD 提示。

  2. after that Run this Command **mongorestore --db mydababse --verbose之后运行此命令 **mongorestore --db mydababse --verbose

  3. D:\\MongoDbBackup\\mydababse\\Dec28-21**(this this path where I have store my backup collection). D:\\MongoDbBackup\\mydababse\\Dec28-21**(这是我存储备份集合的路径)。

  4. some time mongDb Restore program required folder permission to restore.有一段时间 mongDb 恢复程序需要文件夹权限才能恢复。

For mongoDB database restore use this command here .对于 mongoDB 数据库恢复,请在此处使用此命令。 First go to your mongodb database location such as For Example : cd Downloads/blank_db/v34000 After that Enter mongorestore -d v34000 ./首先转到您的 mongodb 数据库位置,例如: cd Downloads/blank_db/v34000之后输入 mongorestore -d v34000 ./

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM