如何使用“mongorestore”使用 TAR.GZ 文件恢复数据库

Met*_*iel 7 localhost mongodb mongorestore windows-10

简单的问题:

\n
$ mongorestore --gzip ./Mongo_DB_dump/restore-8f05kcbfhfbce745735eff49.tar.gz\n2020-07-09T18:10:48.207-0500    Failed: file .\\Mongo_DB_dump\\restore-8f05kcbfhfbce745735eff49.tar.gz does not have .bson extension\n2020-07-09T18:10:48.209-0500    0 document(s) restored successfully. 0 document(s) failed to restore.\n
Run Code Online (Sandbox Code Playgroud)\n

如果我不知道数据库的名称,如何使用 \xc2\xb4tar.gz\xc2\xb4 文件恢复数据库?\n我正在尝试将转储从生产实例 (MongoDB Cloud) 导入到我的数据库中当地环境。如果相关的话,我的本地没有任何以前版本的数据库。

\n

编辑

\n

我还尝试使用mongostore --gzip.tar.gz文件,并且尝试将内容提取到.tar文件中,但是,对于这两个文件,我收到以下错误:

\n
2020-07-09T22:33:07.127-0500    Failed: file .\\Mongo_DB_dump\\restore-8f05kcbfhfbce745735eff49.tar.gz does not have .bson extension\n2020-07-09T22:33:07.128-0500    0 document(s) restored successfully. 0 document(s) failed to restore.\n
Run Code Online (Sandbox Code Playgroud)\n

我也尝试使用/archive:<path-to-file>失败:

\n
$ mongorestore --gzip /archive:./Mongo_DB_dump/restore-8f05kcbfhfbce745735eff49.tar.gz\n2020-07-09T22:52:59.076-0500    Failed: stream or file does not appear to be a mongodump archive\n2020-07-09T22:52:59.077-0500    0 document(s) restored successfully. 0 document(s) failed to restore.\n
Run Code Online (Sandbox Code Playgroud)\n

我什至尝试盲目地将扩展名更改.tar.bson,但这当然没有成功:

\n
2020-07-09T22:08:24.175-0500    checking for collection data in Mongo_DB_dump\\restore-8f05kcbfhfbce745735eff49.bson\n2020-07-09T22:08:24.601-0500    restoring Mongo_DB_dump.restore-8f05kcbfhfbce745735eff49 from Mongo_DB_dump\\restore-8f05kcbfhfbce745735eff49.bson\n2020-07-09T22:08:24.664-0500    finished restoring Mongo_DB_dump.restore-8f05kcbfhfbce745735eff49 (0 documents, 0 failures)\n2020-07-09T22:08:24.664-0500    Failed: Mongo_DB_dump.restore-8f05kcbfhfbce745735eff49: error restoring from Mongo_DB_dump\\restore-8f05kcbfhfbce745735eff49.bson: reading bson input: invalid BSONSize: 1953719666 bytes\n2020-07-09T22:08:24.664-0500    0 document(s) restored successfully. 0 document(s) failed to restore.\n
Run Code Online (Sandbox Code Playgroud)\n

小智 16

以防万一其他人遇到这个问题:

我目前使用的是 db 版本v4.4.5并以 2 种不同的方式恢复备份:

  1. 如果您已经有转储的存档文件并将其压缩为dump.tar.gz,那么您可以先解压缩/提取存档文件,然后使用命令恢复它:

mongorestore --host=localhost --port=27017 --archive dump.tar

  1. 或者,您可以使用命令直接恢复dump.tar.gz

mongorestore --host=localhost --port=27017 --gzip --archive=dump.tar.gz

谢谢


Met*_*iel 1

.tar.gz我只是提取文件的内容C:\ProgramData\MongoDB\data\db并覆盖其中的所有内容...我对此不太确信,但至少我能够工作,因为这样我就可以获取Mongo Shell可见的数据库、集合和文档。如果您知道,请分享更好的答案。