mongodb tar gzip mongo dump 像 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24439068/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:45:42  来源:igfitidea点击:

tar gzip mongo dump like MySQL

mongodb

提问by frany

Is there anyway to tar gzip mongo dumps like you can do with MySQL dumps?

无论如何,是否可以像使用 MySQL 转储那样对 gzip mongo 转储进行 tar gzip mongo 转储?

For example, for mysqldumps, you can write a command as such:

例如,对于 mysqldumps,您可以编写如下命令:

mysqldump -u <username> --password=<password> --all-databases | gzip > all-databases.`date +%F`.gz

Is there an equivalent way to do the same for mongo dumps?

有没有对 mongo dumps 执行相同操作的等效方法?

For mongo dumps I run this command:

对于 mongo 转储,我运行以下命令:

mongodump --host localhost --out /backup

Is there a way to just pipe that to gzip? I tried, but that didn't work.

有没有办法将它通过管道传输到 gzip?我试过了,但这没有用。

Any ideas?

有任何想法吗?

回答by anthonygore

Version 3.2 introduced gzipand archiveoption:

引入了 3.2 版gziparchive选项:

mongodump --db <yourdb> --gzip --archive=/path/to/archive

mongodump --db <yourdb> --gzip --archive=/path/to/archive

Then you can restore with:

然后你可以恢复:

mongorestore --gzip --archive=/path/to/archive

mongorestore --gzip --archive=/path/to/archive

回答by Adam Comerford

Update (July 2015):TOOLS-675is now marked as complete, which will allow for dumping to an archive format in 3.2 and gzip will be one of the options in the 3.2 versions of the mongodump/mongorestoretools. I will update with the relevant docs once they are live for 3.2

更新(2015 年 7 月):TOOLS-675现在被标记为完整,这将允许转储到 3.2 中的存档格式,gzip 将成为 3.2 版本mongodump/mongorestore工具中的选项之一。一旦相关文档适用于 3.2,我将更新它们

Original answer (3.0 and below):

原始答案(3.0 及以下):

You can do this with a single collection by outputting mongodumpto stdout, then piping it to a compression program (gzip, bzip2) but you will only get data (no index information) and you cannot do it for a full database (multiple collections) for now. The relevant feature request for this functionality is SERVER-5190for upvoting/watching purposes.

您可以通过输出mongodump到单个集合来执行此操作stdout,然后将其通过管道传输到压缩程序(gzip、bzip2),但您将只获取数据(无索引信息),目前无法为完整数据库(多个集合)执行此操作. 此功能的相关功能请求是SERVER-5190,用于投票/观看目的。

Here is a quick sample run through of what is possible, using bzip2in this example:

以下是可能的快速示例,bzip2在此示例中使用:

./mongo
MongoDB shell version: 2.6.1
connecting to: test
> db.foo.find()
{ "_id" : ObjectId("53ad8a3eb74b5ae2ff0ec93a"), "a" : 1 }
{ "_id" : ObjectId("53ad8ba445be9c4f7bd018b4"), "a" : 2 }
{ "_id" : ObjectId("53ad8ba645be9c4f7bd018b5"), "a" : 3 }
{ "_id" : ObjectId("53ad8ba845be9c4f7bd018b6"), "a" : 4 }
{ "_id" : ObjectId("53ad8baa45be9c4f7bd018b7"), "a" : 5 }
> 
bye
$ ./mongodump -d test -c foo -o - | bzip2 - > foo.bson.bz2
connected to: 127.0.0.1
$ bunzip2 foo.bson.bz2 
$ ./bsondump foo.bson
{ "_id" : ObjectId( "53ad8a3eb74b5ae2ff0ec93a" ), "a" : 1 }
{ "_id" : ObjectId( "53ad8ba445be9c4f7bd018b4" ), "a" : 2 }
{ "_id" : ObjectId( "53ad8ba645be9c4f7bd018b5" ), "a" : 3 }
{ "_id" : ObjectId( "53ad8ba845be9c4f7bd018b6" ), "a" : 4 }
{ "_id" : ObjectId( "53ad8baa45be9c4f7bd018b7" ), "a" : 5 }
5 objects found

Compare that with a straight mongodump(you get the same foo.bson but the extra foo.metadata.json describing the indexes is not included above):

将其与直接进行比较mongodump(您得到相同的 foo.bson,但上面不包括描述索引的额外 foo.metadata.json):

$ ./mongodump -d test -c foo -o .
connected to: 127.0.0.1
2014-06-27T16:24:20.802+0100 DATABASE: test  to     ./test
2014-06-27T16:24:20.802+0100    test.foo to ./test/foo.bson
2014-06-27T16:24:20.802+0100         5 documents
2014-06-27T16:24:20.802+0100    Metadata for test.foo to ./test/foo.metadata.json
$ ./bsondump test/foo.bson 
{ "_id" : ObjectId( "53ad8a3eb74b5ae2ff0ec93a" ), "a" : 1 }
{ "_id" : ObjectId( "53ad8ba445be9c4f7bd018b4" ), "a" : 2 }
{ "_id" : ObjectId( "53ad8ba645be9c4f7bd018b5" ), "a" : 3 }
{ "_id" : ObjectId( "53ad8ba845be9c4f7bd018b6" ), "a" : 4 }
{ "_id" : ObjectId( "53ad8baa45be9c4f7bd018b7" ), "a" : 5 }
5 objects found