有没有像tar转储一样tar gzip mongo转储?
例如,对于mysqldumps,您可以这样编写命令:
mysqldump -u--password= --all-databases | gzip > all-databases.`date +%F`.gz
有没有相同的方法来为mongo转储做同样的事情?
对于mongo转储,我运行此命令:
mongodump --host localhost --out /backup
有没有办法把它管道到gzip?我试过了,但那没用.
有任何想法吗?
更新(2015年7月):
TOOLS-675现已标记为完成,这将允许转储为3.2中的存档格式,gzip将是3.2版本mongodump/mongorestore
工具中的选项之一.一旦他们为3.2生活,我将更新相关文档
原始答案(3.0及以下):
您可以通过输出mongodump
到单个集合来执行此操作stdout
,然后将其传递给压缩程序(gzip,bzip2),但是您只能获取数据(没有索引信息),而现在您无法为完整数据库(多个集合)执行此操作.此功能的相关功能请求是SERVER-5190,用于提升/观看目的.
下面是一个快速示例,介绍了可能的内容,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
将其与直线进行比较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
版本3.2介绍gzip
和archive
选项:
mongodump --db <yourdb> --gzip --archive=/path/to/archive
然后你可以恢复:
mongorestore --gzip --archive=/path/to/archive