MongoDB – Backup and Restoration


MongoDB – Backup and Restoration

Data backup is one of the most highly required processes for any database management system as data can be lost or corrupted. To overcome these drawbacks, we need database backup. Database backup is a copy of a database that already exists. In MongoDB, the mongodump tool is used to take the data backup. And mongorestore tool is used to restore the backup data.

Data Backup

In MongoDB, the mongodump tool is used to take the data backup. It simply dumps all the data stored into a dump directory of MongoDB. Backed-up data is in BSON format, also known as BSON data dumps. By default, the backup is stored in mongodb’s bin\dump folder. To specify a different output directory, we can use the –out option. Mongodump is used in two ways, with or without argument.

Without Argument:

Without any arguments, mongodump connects with MongoDB instance on the local system on port 27017 and creates a backup of every database and collection.

With Argument:

By specifying the database in the argument by which, we can limit the amount of data stored in the database dump

Data Restoration

In MongoDB, the mongorestore utility is used to restore the backup data. It restores the binary backup created by the mongodump utility(i.e., BSON data dumps). It can restore either an entire database backup or a subset of the backup. It also restores the indexes created for any collection inside that database. By default, mongorestore looks for a database backup in mongodb’s bin\dump folder, the default folder for the mongodump command for dumping the backup.

MongoDB Backup and Restore

You should follow some best practices when using the MongoDB backup and restore services for your MongoDB clusters.

  • MongoDB uses both regular JSON and Binary JSON (BSON) file formats. It's better to use BSON when backing up and to restore. While JSON is easy to work with, it doesn't support all of the data types that BSON supports, and it may lead to the loss of fidelity.
  • You don’t need to explicitly create a MongoDB database, as it will be automatically created when you specify a database to import from. Similarly, a structure for a collection will be created whenever the first document is inserted into the database.
  • When creating a new cluster, you have the option to turn on cloud backup. While you can also enable cloud backups when modifying an existing cluster, you should turn this feature on by default, as it will prevent data loss.
  • If a snapshot fails, Atlas will automatically attempt to create another snapshot. While you can use a fallback snapshot to restore a cluster, it should only be done when necessary. Fallback snapshots are created using a different process, which may have inconsistent data.
  • Use secondary servers for backups as this helps avoid degrading the performance of the primary node.
  • Time the backup of data sets around periods of low bandwidth/traffic. Backups can take a long time, especially if the data sets are quite large.
  • Use a replica set connection string when using unsupervised scripts. A standalone connection string will fail if the MongoDB host proves unavailable.