MongoDB - Basics. GitHub Gist: instantly share code, notes, and snippets. Download ZIP. MongoDB - Basics Raw. ###Restoring MongoDB data The mongorestore utility restores a binary backup created by mongodump. By default, mongorestore looks for a database backup in the dump/ directory. Connect with mongodump ΒΆ. The Binary Import and Export section of the Command Line Tools tab displays a copyable template with the minimum required options for connecting mongodump to your Atlas cluster. For instructions on finding the Command Line Tools tab, see Access the Command Line Tools Tab. The template includes placeholder values for certain options.
Use mongodump and mongorestore to prevent data loss
Effectively managing data, services, and information is crucial to a company's success. And in a world of increasingly growing data, choosing the right data management solution is more important than ever. MongoDB is an emerging, open source NoSQL database that you can use for massive data management. It provides high performance, high availability, and easily scalable features. A critical element of data management is performing backups regularly to prevent the loss of data, and this is not something you should do manually. There are several different methods that you can use to perform backups in MongoDB, but this article shows you how to configure and run the backup scripts using
mongodump
and mongorestore
.Set up a MongoDB instance
Use the following steps to design MongoDB backup scripts that run automatically with a cron job. You can customize the backup script based on your business requirements.
Note: To follow the steps in this tutorial, create a separate user (named
mongo
, for example) to perform the administrative operations in MongoDB.- Install MongoDB from the MongoDB Download Center. The MongoDB binaries are located in the /opt/mongodb/mongodb/bin/ directory. Use the
root
credentials when you install MongoDB. - Log in as
root
, and then create a group and user: - Add the user
mongo
into the groupmongogrp
that you just created: - Verify that the user was created:
- Set the password for the user
mongo
:
Invoke a MongoDB shell
- Open a new session and log in as the user
mongo
: - Locate the bin directory of the MongoDB installation:
- To run these utilities, the bin directory must have proper access to the user
mongo
. Use theroot
credentials and provide the proper access to themongo
user on the bin directory. - Verify that the utilities shown in Step 2 are under the bin directory.
- You have two options when invoking a MongoDB shell: One uses SSL and one does not. The instructions for both are below.
- To invoke a MongoDB shell, run the
mongo
command with these options: - To invoke a MongoDB shell using SSL, run the
mongo
command with these options:Where:
- To invoke a MongoDB shell, run the
Note: Here, you will get MongoDB shell version 3.2. You can now perform various operations.
This article executes MongoDB commands and utilities using SSL.
MongoDB backup and restore functions
To back up the MongoDB database, you use the
mongodump
utility, which is located in the bin directory. This backs up all data into the dump folder at the default location /bin/dump. MongoDB uses a default port 27017. You can use the utility mongodump
for both hot (online) and cold (offline) backup.Offline MongoDB backup
The MongoDB server has a primary daemon process, called
mongod
. Mongod
manages data access, data requests, and background operations. To perform an offline backup, you first stop the mongod
service, which stops the MongoDB
instance. Then, perform the backup and start the MongoDB
instance.To create an offline MongoDB backup:
- Create a backup directory. Based on the size of the MongoDB database, create the backup directories at the respective backup locations. Here, you create the backup directories as /mongo_data/backup:
- Change the owner to the user
mongo
: - Stop the
mongod
instance: - Perform the offline backup. First, log in to
mongo
user and locate the bin directory: - Start the
mongod
instance by typingservice mongod start
.
Online MongoDB backup
To perform an online backup, run the
mongodump
command without stopping, and restart the mongod
instance. You can run this command with or without SSL. Both commands are shown below.To perform the
mongodump
command using SSL, your command looks something like this:To perform the
mongodump
command without using SSL, your command looks like this:Small sharded cluster backup with mongodump
If your sharded cluster holds a small set of data, you can connect to a
mongos
using mongodump
.n a MongoDB sharded cluster,
mongos
is a routing service that processes queries from the application layer. To perform the operation, it determines the location of this data in the sharded cluster.You can create the MongoDB cluster backup if the infrastructure can perform the entire backup in a reasonable amount of time and a storage system can hold the complete MongoDB data set. By default,
mongodump
issues its queries to the non-primary nodes.To perform the backup of a sharded cluster, use
mongodump
as shown in the following command:Note: Applications can continue to modify data while
mongodump
captures the output. For replica sets, mongodump
provides the --oplog option to include in its output oplog entries that occur during the mongodump
operation. This allows the corresponding mongorestore
operation to replay the captured oplog. To restore a backup created with --oplog, use mongorestore
with the --oplogReplay option. However, for replica sets, consider MongoDB Cloud Manager or Ops Manager.Recovery scenarios
You can better plan for and avoid failure scenarios if you understand how failures occur and how you can recover from them. The following sections simulate different types of failures and present a series of steps you can follow if one of them occurs in your environment.
Scenario 1. An entire database is accidentally dropped or becomes corrupted
A manual error or hardware failure can damage or corrupt an entire database. If this happens, you can recover the complete database by applying the last full backup of
mongodump
and restore it using the mongorestore
utility.In this scenario, we assume the database as testdb1 with the collections (for example, users and students) having some records, as shown below.
Step 1. Verify the database and the collections
- Log in as a mongo user:
- Locate the bin directory:
- Invoke the MongoDB shell:
- Verify the database and collections:
Step 2. Back up the entire database
Use the following code to back up the entire database:
Step 3. Simulate a failure
To simulate a failure scenario, you need to completely drop the database.
- Connect to the database:
- Verify the collections:
- Verify the current database:
- Drop the database:
Step 4. Restore database testdb1
Restore the backup image using the utility
mongorestore
as demonstrated below. In this example, you restore the latest backup image available at the backup location /mongo_data/backup/testdb1
.Step 5. Verify the database and collections
To verify that the database is restored, connect to it and query the collections:
Scenario 2. A collection is accidentally dropped
Sometimes, especially when there are thousands of collections in a database, a collection is dropped by mistake. To recover a collection that's accidentally dropped, you need the latest backup of the collection (for instance,
collection.name.bson
) at the backup location. In this scenario, I show you how to perform these functions using the collection student
, which is dropped and recovered using the mongorestore
utility.Step 1. Verify the collection
Use the following code to verify the collection:
Step 2. Back up the collection
Perform the backup only at the collection level:
Note: Make sure that at the backup location (for instance, mongo_data/backup), a file student.bson is created.
Step 3. Simulate the failure
To simulate this failure, drop the collection completely.
- Connect to the database:
- Drop the collection:
- Verify that the contents are dropped:
Step 4. Restore the collection
Restore the latest backup image of the collection (for example,
student.bson
) from the mongo_data/backup
location as shown:Note: You can perform database-level backup using
mongodump
, and you can only restore the required collection from the backup directory if required.Step 5. Verify the collections
To verify that the collection is restored, connect to the database and query the collection:
Create and run a backup script using a cron job
To set up the backup strategy based on the business requirement, you need to set up the customized backup script:
run_backup.sh
. You also need to set up a cron job to run this backup.Create the backup script
The following sample backup script,
run_backup.sh
, is based on the following conditions.- The script first deletes the backup images older than 30 days from the backup location. It performs the daily backup of all the databases at the backup location.
- The script writes the comments into the log file backup.log about what the backup action deleted and when it was performed. By default,
mongodump
does not capture the contents of the local database. You should add it separately if required. To uniquely identify the individual database backup with the application name, you must define some parameters in the backup script. This is shown in the next section.
Create a backup directory
- Log in as
root
user: - Change the owner as
mongo
user; - Log in as mongo user:
- Create a backup script:
- The following code shows sample content of the backup script:
Grant permission
Save the file and grant it 755 permission:
Create a cron job to run the backup script
Create a new cron job, named mycron.txt, at the
mongo_data/backup
location and schedule it according to the backup strategy.- Log in as the
mongo
user: - Check the existing cron jobs for the
mongo
user: - Create a new cron job:
- Add the backup schedule to run this cron job according to your backup strategy. For example:
- Set the cron job and verify it:
Conclusion
You've configured and run the backup scripts for MongoDB database servers, and you have a better understanding of how to use the
mongodump
and mongorestore
utilities for backup and restore purposes. You scheduled and ran the backup script using a cron job. You can use the backup script to schedule, maintain, and manage the backup of your MongoDB database servers.Downloadable resources
Related topics
Comments
Sign in or register to add and subscribe to comments.
- bsondump - display BSON files in a human-readable format
- mongoimport - Convert data from JSON, TSV or CSV and insert them into a collection
- mongoexport - Write an existing collection to CSV or JSON format
- mongodump/mongorestore - Dump MongoDB backups to disk in .BSON format, or restore them to a live database
- mongostat - Monitor live MongoDB servers, replica sets, or sharded clusters
- mongofiles - Read, write, delete, or update files in GridFS
- mongotop - Monitor read/write activity on a mongo server
- mongoreplay - Capture, observe, and replay traffic for MongoDB
Report any bugs, improvements, or new feature requests at https://jira.mongodb.org/browse/TOOLS
Building Tools
To build the tools, you need to have Go version 1.9 and up.
go get
will not work; youneed to clone the repository to build it.To use build/test scripts in the repo, you MUST set GOROOT to your Go root directory.
Quick build
The
build.sh
script builds all the tools, placing them in the bin
directory. Pass any build tags (like ssl
or sasl
) as additional commandline arguments.Manual build
Source
set_goenv.sh
and run the set_goenv
function to setup your GOPATH andarchitecture-specific configuration flags:Pass tags to the
go build
command as needed in order to build the tools withsupport for SSL and/or SASL. For example:Contributing
See our Contributor's Guide.
Documentation
See the MongoDB packages documentation.