MongoDB offers backup support so that you can back up your data and can reuse it if the data is lost. The backup can be performed on all the MongoDB-based databases present on your system or you can target a specific database or a single collection as well.
In this MongoDB tutorial series, we will guide you to create a backup of your database(s)/collection(s) and how you can retrieve the data.
How to create a backup in MongoDB
This section provides several ways to create a backup in MongoDB. Furthermore, this section is broken down into several subsections to compile an informative section.
How to create a backup of all databases
The command used to create a backup in MongoDB is “mongodump“. The following syntax will help you in this regard:
To create a backup of all the databases and associated collections, you must execute the following command in your Ubuntu terminal:
You may have noticed that the command is executed in the Ubuntu terminal. It is a universal command that can be executed on several operating systems and is used to create backups in MongoDB.
The collections and associated documents present on your MongoDB server are backed up.
How to backup a database in MongoDB
The syntax to create a backup of a database is provided below:
For instance, we want to create a backup for “linuxhint” database. To do so, we have executed the below-stated command in Ubuntu’s terminal:
It can be observed from the output that, all the collections and documents associated with linuxhint database are successfully backed up after this execution.
How to backup a collection of a database
Sometimes the size of the database is very large and creating a backup for these databases consumes time as well as storage. In such conditions, the administrator will prefer to create a backup of important collections. The mongodump command also allows you to create a backup of a single collection only. To create a backup of a single collection; one must follow the syntax provided below:
Furthermore, the command written below creates the backup of a collection “staff” and this collection belongs to “linuxhint” database:
As discussed earlier, the default dump address of the MongoDB database and collections is your home directory. However, you can change the backup address by using the “–out” option in “mongodump” command.
For example, we have used the following command to get the backup of “linuxhint” database at our desired address:
How to restore backed up databases/collections in MongoDB
Once the backup is created, you must know the way to restore it. In this section, we will demonstrate the way to restore data that includes databases and collections.
The command used to restore the backup has the following syntax:
A single backup command creates backups for all databases. Similarly, a single restore command retrieves all the databases from the backup directory to your MongoDB server. The command stated below restores all the databases at once:
Database Management Systems are used to manage the data of an organization and MongoDB is one of the well-known database management systems. The backup phenomenon has a key role in DBMS’s and almost all database administrators update the backup directory regularly. In this descriptive post, we have provided the possible ways to create a backup in MongoDB. Additionally, we have also provided ways to restore the backed-up data. Wrapping up, this guide is equally important for all users of MongoDB. Lastly, it is recommended to create backups of your data, either you are working on databases or any operations related to data management.