Backup and restore MongoDB database

In this tutorial, I will guide you all how to backup and restore your MongoDB database.

First of all is the backup.

We will use the mongodump tool which installed with MongoDB.

The syntax for MongoDB database backup is as follows:

Inside:

  • database_need_backup is the MongoDB database that you need to back up.
  • folder_will_contain_backup_file is the directory that will contain the backup file.

For example, I have a database named “qm” in my MongoDB server.

Backup and restore MongoDB database

To backup this database, I will run the following statement:

Result:

Backup and restore MongoDB database

Check the directory that you just passed in the command above, you will see a folder named MongoDB database name with some .json, .bson files is the content of this database.

Next we will talk about how to restore.

We will also use the mongorestore tool which installed with the MongoDB server.

The syntax for the restore part is as follows:

Inside:

  • database_will_be_restored is the new database that you need to restore database. If this database does not exist in MongoDB server then it will be created automatically.
  • location_to_folder_containing_mongodb_database is the directory containing the database content that we need to restore.

Assuming I need to restore the “qm” database that I just backed up above to a new database, I will execute the following command:

Result:

Backup and restore MongoDB database

Check the list of MongoDB database, you will see the results as follows:

Backup and restore MongoDB database

Add Comment