Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SSRS Properties - ColumnsSSRS Properties - Background Image »
    comments

    Today it is time to learn how to backup and restore databases in MongoDB. You do have jobs setup that automatically create backups right? If you do not, please close this window and go set that up first, your data is the most important part of the organization, without data you got nothing, just ask ma.gnolia. That site is not around anymore because they didn't back up their database. Enough of that let's get started.
    Connect to mongodb, create a new database named blog. You can do that by just executing use blog

    Now insert the following 5 items in that database

    db.Blog.insert( { name : "Denis",  age : 20, city : "Princeton" } )
    db.Blog.insert( { name : "Abe",    age : 30, city : "Amsterdam" } )
    db.Blog.insert( { name : "John",   age : 40, city : "New York"  } )
    db.Blog.insert( { name : "Xavier", age : 10, city : "Barcelona" } )
    db.Blog.insert( { name : "Zen",    age : 50, city : "Kyoto"     } )

    You have now a collection named Blog with those 5 items.

    If you want to list all the collections for a database, you can use db.getCollectionNames()

    db.getCollectionNames()

    Here is the output

    [ "Blog", "system.indexes" ]

    Let's get some stats for that collection

    db.Blog.stats()

    Here is the output

    {
            "ns" : "blog.Blog",
            "count" : 5,
            "size" : 356,
            "avgObjSize" : 71.2,
            "storageSize" : 8192,
            "numExtents" : 1,
            "nindexes" : 1,
            "lastExtentSize" : 8192,
            "paddingFactor" : 1,
            "systemFlags" : 1,
            "userFlags" : 0,
            "totalIndexSize" : 8176,
            "indexSizes" : {
                    "_id_" : 8176
            },
            "ok" : 1
    }


    Backing Up

    Time to do the backup. To do a backup you can't run if you are connected to mongodb. Open up a new command/shell window, navigate to the bin directory inside the mongodb folder. In my case this is D:\mongodb\bin. To do a backup we are going to call the mongodump executable inside the bin directory. Here is what the syntax will look like

    mongodump --db {Database name}

    You need to change the database name to something that you have. Running the command will create a directory named dump

    mongodump --db blog

    Here is what the output looks like

    connected to: 127.0.0.1
    Wed Jan 30 13:55:56 DATABASE: blog       to     dump/blog
    Wed Jan 30 13:55:56     blog.Blog to dump/blog/Blog.bson
    Wed Jan 30 13:55:56              5 objects
    Wed Jan 30 13:55:56     Metadata for blog.Blog to dump/blog/Blog.metadata.json
    
    D:\mongodb\bin>

    The Blog.bson file is the actual backup of the database

    As you can see the backup also creates a metadata file Blog.metadata.json

    All that the manifest file has in it is the following
    { "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "blog.Blog", "name" : "_id_" } ] }

    As you can see it has the database as well as the collection name

    We will talk more about the .bson file in tomorrow's post

    Now that we have a backup we can do a restore. But first let's drop the collection :-)

    Execute the following in the command window connected to mongodb

    db.Blog.drop()

    Here is the output

    true

    Now if we check the stats again, there will be an error

    db.Blog.stats()
    { "errmsg" : "ns not found", "ok" : 0 }


    Restore

    Now it is time to do a restore, use the command/shell window where you did the backup. The restore process uses an executable as well that needs to be executed from a command window outside the MongoDB process.

    The command will look like the following

    mongorestore directory/database
    In our case the command will look like the following

    mongorestore dump/blog

    Execute the following

    mongorestore dump/blog

    Here is what the output looks like

    connected to: 127.0.0.1
    Wed Jan 30 13:59:58 dump/blog/Blog.bson
    Wed Jan 30 13:59:58     going into namespace [blog.Blog]
    5 objects found
    Wed Jan 30 13:59:58     Creating index: { key: { _id: 1 }, ns: "blog.Blog", name: "_id_" }

    Now, let's see if we have the collection back

    db.Blog.stats()
    {
            "ns" : "blog.Blog",
            "count" : 5,
            "size" : 356,
            "avgObjSize" : 71.2,
            "storageSize" : 8192,
            "numExtents" : 1,
            "nindexes" : 1,
            "lastExtentSize" : 8192,
            "paddingFactor" : 1,
            "systemFlags" : 1,
            "userFlags" : 0,
            "totalIndexSize" : 8176,
            "indexSizes" : {
                    "_id_" : 8176
            },
            "ok" : 1
    }

    As you can see the collection is there again, the restore worked.

    You can also restore the backup to a new database or another database. If the database does not exists it will create one for you. If I run the same command from before but use --db RestoredDB you will get the following output

    mongorestore -db RestoredDB dump/blog
    connected to: 127.0.0.1
    Wed Jan 30 14:05:26 dump/blog/Blog.bson
    Wed Jan 30 14:05:26     going into namespace [RestoredDB.Blog]
    5 objects found
    Wed Jan 30 14:05:26     Creating index: { key: { _id: 1 }, ns: "RestoredDB.Blog", name: "_id_" }
    

    Now connecting to MongoDB again we can check that the new database is there with the same collection

    use RestoredDB
    switched to db RestoredDB
    db.Blog.find()
    { "_id" : ObjectId("510961b1e5247980b903c30c"), "name" : "Denis", "age" : 20, "city" : "Princeton" }
    { "_id" : ObjectId("510961b3e5247980b903c30d"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }
    { "_id" : ObjectId("510961b3e5247980b903c30e"), "name" : "John", "age" : 40, "city" : "New York" }
    { "_id" : ObjectId("510961b3e5247980b903c30f"), "name" : "Xavier", "age" : 10, "city" : "Barcelona"
    { "_id" : ObjectId("510961b5e5247980b903c310"), "name" : "Zen", "age" : 50, "city" : "Kyoto" }

    As you can see it it pretty straightforward to backup and restore databases. What if you just want to restore a collection? Aha...that will be tomorrow's post: MongoDB: How to restore collections

    That is all for this post, if you are interested in my other MongoDB posts, you can find them here:
    Install MongoDB as a Windows Service
    UPSERTs with MongoDB
    How to sort results in MongoDB
    Indexes in MongoDB: A quick overview
    Multidocument updates with MongoDB
    MongoDB: How to include and exclude the fields you want in results
    MongoDB: How to limit results and how to page through results

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2897 views
    InstapaperVote on HN

    1 comment

    Comment from: Dharshan Rangegowda [Visitor] Email · http://blog.mongodirector.com
    Dharshan Rangegowda I would recommend against using mongodump/mongorestore. It is very slow and once you get past 20/30GB of data it can take hours to restore. Moreover your data keeps changing during the backup - so you don't get a consistent snapshot. Use a filesystem snapshot or cloud snapshot mechanism. At MongoDirector we use LVM snapshots to get a point in time copy of the database and we store it in S3 as a backup - http://blog.mongodirector.com/mongodb-backup-and-restore/
    03/10/13 @ 19:33

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)