简体   繁体   English

如何以最简单的方式备份MongoDB GridFS数据库?

[英]How can I backup a MongoDB GridFS database the easiest way?

Like the title says, I have a MongoDB GridFS database with a whole range of file types (eg, text, pdf, xls), and I want to backup this database the easiest way. 就像标题所说,我有一个包含各种文件类型的MongoDB GridFS数据库(例如,text,pdf,xls),我想以最简单的方式备份这个数据库。

Replication is not an option. 复制不是一种选择。 Preferably I'd like to do it the usual database way of dumping the database to file and then backup that file (which could be used to restore the entire database 100% later on if needed). 我希望以通常的数据库方式将数据库转储到文件然后备份该文件(如果需要可以用于100%以后恢复整个数据库)。 Can that be done with mongodump ? 可以用mongodump完成吗? I also want the backup to be incremental. 我还希望备份是增量备份。 Will that be a problem with GridFS and mongodump ? 这会是GridFSmongodump的问题吗?

Most importantly, is that the best way of doing it? 最重要的是,这是最好的方式吗? I am not that familiar with MongoDB , will mongodump work as well as mysqldump does with MySQL ? 我对MongoDB不是很熟悉, mongodumpMySQL mysqldump一样吗? Whats the best practice for MongoDB GridFS and incremental backups? 什么是MongoDB GridFS和增量备份的最佳实践?

I am running Linux if that makes any difference. 我正在运行Linux如果这有任何区别。

GridFS stores files in two collections: fs.files and fs.chunks. GridFS将文件存储在两个集合中:fs.files和fs.chunks。

More information on this may be found in the GridFS Specification document: http://www.mongodb.org/display/DOCS/GridFS+Specification 有关这方面的更多信息可以在GridFS规范文档中找到: http//www.mongodb.org/display/DOCS/GridFS+Specification

Both collections may be backed up using mongodump, the same as any other collection. 两个集合都可以使用mongodump备份,与任何其他集合相同。 The documentation on mongodump may be found here: http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongodump 关于mongodump的文档可以在这里找到: http//www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongodump

From a terminal, this would look something like the following: 从终端,这看起来如下所示:

For this demonstration, my db name is "gridFS": 对于此演示,我的数据库名称是“gridFS”:

First, mongodump is used to back the fs.files and fs.chunks collections to a folder on my desktop: 首先,mongodump用于将fs.files和fs.chunks集合备份到桌面上的文件夹:

$ bin/mongodump --db gridFS --collection fs.chunks --out /Desktop
connected to: 127.0.0.1
DATABASE: gridFS     to     /Desktop/gridFS
    gridFS.fs.chunks to /Desktop/gridFS/fs.chunks.bson
         3 objects
$ bin/mongodump --db gridFS --collection fs.files --out /Desktop
connected to: 127.0.0.1
DATABASE: gridFS     to     /Desktop/gridFS
    gridFS.fs.files to /Users/mbastien/Desktop/gridfs/gridFS/fs.files.bson
         3 objects

Now, mongorestore is used to pull the backed-up collections into a new (for the purpose of demonstration) database called "gridFScopy" 现在,mongorestore用于将备份的集合拉入一个名为“gridFScopy”的新数据库(用于演示)

$ bin/mongorestore --db gridFScopy --collection fs.chunks /Desktop/gridFS/fs.chunks.bson 
connected to: 127.0.0.1
Thu Jan 19 12:38:43 /Desktop/gridFS/fs.chunks.bson
Thu Jan 19 12:38:43      going into namespace [gridFScopy.fs.chunks]
3 objects found
$ bin/mongorestore --db gridFScopy --collection fs.files /Desktop/gridFS/fs.files.bson 
connected to: 127.0.0.1
Thu Jan 19 12:39:37 /Desktop/gridFS/fs.files.bson
Thu Jan 19 12:39:37      going into namespace [gridFScopy.fs.files]
3 objects found

Now the Mongo shell is started, so that the restore can be verified: 现在启动Mongo shell,以便可以验证还原:

$ bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use gridFScopy
switched to db gridFScopy
> show collections
fs.chunks
fs.files
system.indexes
> 

The collections fs.chunks and fs.files have been successfully restored to the new DB. fs.chunks和fs.files集合已成功还原到新数据库。

You can write a script to perform mongodump on your fs.files and fs.chunks collections periodically. 您可以编写脚本以定期对fs.files和fs.chunks集合执行mongodump。

As for incremental backups, they are not really supported by MongoDB. 至于增量备份,MongoDB并不真正支持它们。 A Google search for "mongodb incremental backup" reveals a good mongodb-user Google Groups discussion on the subject: http://groups.google.com/group/mongodb-user/browse_thread/thread/6b886794a9bf170f 谷歌搜索“mongodb增量备份”显示了一个很好的mongodb用户Google Groups关于这个主题的讨论: http//groups.google.com/group/mongodb-user/browse_thread/thread/6b886794a9bf170f

For continuous back-ups, many users use a replica set. 对于连续备份,许多用户使用副本集。 (Realizing that in your original question, you stated that this is not an option. This is included for other members of the Community who may be reading this response.) A member of a replica set can be hidden to ensure that it will never become Primary and will never be read from. (意识到在您的原始问题中,您声明这不是一个选项。这包括在社区中可能正在阅读此响应的其他成员。)可以隐藏副本集的成员以确保它永远不会成为小学,永远不会被读。 More information on this may be found in the "Member Options" section of the Replica Set Configuration documentation. 有关此内容的更多信息,请参见“副本集配置”文档的“成员选项”部分。 http://www.mongodb.org/display/DOCS/Replica+Set+Configuration#ReplicaSetConfiguration-Memberoptions http://www.mongodb.org/display/DOCS/Replica+Set+Configuration#ReplicaSetConfiguration-Memberoptions

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

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