Maintaining backups is significant for each organization. Data backups act as a security measure. Where data are often recovered or restored just in case of an emergency. The Dump MongoDB function enables backups and restore. Generally, we create backups are copying the database. To do this we generally use :
- Built-in tools
- Specialized external backup services
MongoDB also offers multiple inbuilt backup options counting on the MongoDB deployment method you employ. So, what are we waiting for?? Lets get started with the topic !!
Mongodump, a simple MongoDB backup utility that makes hi-fi BSON files from an underlying database. These files are often thus, restored using the mongorestore utility. It is hence, a perfect backup solution for little MongoDB instances thanks to its simple use and portability.
File system backups
- In this method, you simply keep copies of the underlying data files of a MongoDB installation. We will utilize snapshots if the filing system supports it.
- Another way is to use a tool like rsync. Here we will directly copy the info files to a backup directory.
So, What is MongoDump ?
The mongodump may be a utility for creating database backups. The utility are often used for creating binary export of the database contents. Mongodump can export data from both mongod and mongos instances, allowing you to make backups from:
- A standalone, replica set
- A sharded cluster of MongoDB deployments
MongoDB Database Tools
The Database Tools are a set of command-line utilities that help with the upkeep and administration of a MongoDB instance. It is compatible with:
Here’s an article on How MongoDB Index Works !!!!
Using the Dump MongoDB methods — Mongodump
The Basic mongodump Syntax is :
The most basic method to make a backup is to use the mongodump command with none options. This generally assumes that the database is found in the localhost (127.0.0.1) and using port 27017 with no authentication requirements. The backup process will create a dump folder within the current directory.
How to backup a foreign MongoDB instance
We can specify a number and a port using the –uri connection string.
To connect using the uri option:
mongodump --uri="mongodb://:" [additional options]
To connect using the host option:
mongodump --host=":" [additional options]
Connect using host and port options:
mongodump --host="" --port= [additional options]
The following example demonstrates the way to create a backup of remote MongoDB instance:
mongodump --host="10.10.10.59" --port=27017
How to backup a secure MongoDB instance
If we would like to attach to a MongoDB instance with access-control, then, we will be required to provide, thus :
- Authentication database options
# Authentication Syntax mongodump --authenticationDatabase= -u= -p= [additional options] #For a foregin instance having username and password, we use mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="shammy" -p="testpassword"
Working with Databases and Collections
Using the –db and –collection options, we will indicate a database and a set to be protected . The –db option are often a standalone option, but to pick a set a database must be specified. In order to make a case for a set from the backup process, we will use the –excludeCollection option. So, lets see how we do all these.
To select a database :
mongodump --db= [additional options]
Select a collection :
mongodump --db= --collection= [additional options]
How to Exclude a collection :
mongodump --db= --excludeCollection= [additional options] # Lets also see how to set a target backup mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="shammy" -p="testpassword" --db=vehicles --collection=shubhamfiles # in the above examples we have set the "shubhamfiles" collection as our backup target
Backup Directory Change
To change the backup directory, we make use of the –out option. As in,
mongodump --out= [additional options] # Now suppose, we want the backup folder to be the "shubham" folder mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="shammy" -p="testpassword" --out=shubham
The mongodump utility allows us to make an archive file. The –archive specifies the file. Also, if no file is specify the output is going to write to plain output (stdout). The –archive option can’t be utilized in conjunction with the –out option.
mongodump --archive= [additional options] # To define an archive file mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="shammy" -p="testpassword" --archive=db.archive
The backup files are often compressed using the –gzip option. This feature will thus, compress the individual JSON and BSON files.
mongodump --gzip [additional options] #Additionally, to compress the whole database, we use mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="shammy" -p="testpassword" --gzip
Expected Behaviour and Limitations of the Mongodump utility :
- The “local” DB is generally exclude from the backup option.
- Only metadata is capture during read-only view backups.
- Extended JSON v2.0 is use for greater compatibility.
- The mongodump command must execute from the direction shell only. It is so because it may be a separate utility.
- Incremental backups are no options. The MongoDB backups are a complete copy of the whole database.
This post has been a honest effort and a culmination of tremendous hardwork. Through this post, I have thus, tried to bring to you the various methods and ways. That, we can use Dump in MongoDB. We also got to see the use of Mongo Dump. As we have seen and are aware by now. Mongodump is thus the one and only thing that we need for our complete backup and restoring solutions. We have hence, seen ways to select and backup collections, databases and various other files.
In case you face any problems. Then, do feel free to post it in the comments section to get your answers. Thus, hoping, in anticipation that this is the ultimate post. For you for all your backing and restoring solutions in MongoDB. See- ya !!