How can I back up the GridFS MongoDB database in the easiest way?

As in the header, I have a MongoDB GridFS with a full set of file types (e.g. text, pdf, xls), and I want to backup this database in the easiest way.

Replication is not an option. Preferably, I would like to do this in the usual way of a database to dump the database into a file and then back up this file (which can be used to restore the entire database 100% later if necessary). Can this be done with mongodump ? I also want the backup to be incremental. Will this be a problem with GridFS and mongodump ?

Most importantly, what is the best way to do this? I am not familiar with MongoDB , does mongodump and mysqldump does it with MySQL ? What is the best practice for MongoDB GridFS and incremental backups?

I am running Linux if that matters.

+8
mongodb gridfs backup
source share
1 answer

GridFS stores files in two collections: fs.files and fs.chunks.

More information on this can be found in the GridFS specification document: http://www.mongodb.org/display/DOCS/GridFS+Specification

Both collections can be copied using mongodump, like any other collection. The mongodump documentation can be found here: http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongodump

From the terminal, it looks something like this:

For this demonstration, my db name is "gridFS":

Firstly, mongodump is used to return the fs.files and fs.chunks collections to a folder on my desktop:

 $ 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 

Mongorestore is now used to pull backup collections into a new (for demonstration) database called "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 

The Mongo shell is now running, so recovery can be verified:

 $ 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 fs.chunks and fs.files collections were successfully restored to the new database.

You can write a script to run mongodump periodically in the fs.files and fs.chunks collections.

As for incremental backups, they are not supported by MongoDB. A Google search for "incremental mongodb backup" reveals a good discussion by the Google Group on mongodb users on this issue: http://groups.google.com/group/mongodb-user/browse_thread/thread/6b886794a9bf170f

For continuous backups, many users use a set of replicas. (Understanding that in your original question you stated that this is not an option. This is included for other members of the Community who can read this answer.) A member of a replica set can be hidden to ensure that it never becomes Primary and never will be be considered. More information about this can be found in the "Member Settings" section of the replica configuration documentation. http://www.mongodb.org/display/DOCS/Replica+Set+Configuration#ReplicaSetConfiguration-Memberoptions

+15
source share

All Articles