How to backup and restore MongoDB ?

From PheonixSolutions
Jump to navigation Jump to search

Introduction to MongoDB

[edit]

MongoDB is a widely used open-source, NoSQL database management system designed for scalability, performance, and flexibility in handling unstructured or semi-structured data. It falls under the category of document-oriented databases, where data is stored in flexible, JSON-like documents.

Prerequisties

[edit]

To backup and restore MongoDB, ensure you have MongoDB installed and properly configured on your system. Additionally, make sure you have the necessary permissions to perform backup and restore operations.

Create one Example Data

[edit]

Before performing the backup operation, you may optionally create some example data in the test database. You can do this by importing data from a JSON file, such as countries.json, using the following command:

mongoimport --drop --jsonArray -d test -c countries countries.json

The Following output will be generated if import data is successful.

Check Data

[edit]

Upon successful import, you should see output indicating the number of documents imported.

To check the imported data, open a command prompt window and connect to the test database using the MongoDB shell:

Command: Mongo test

Check the total records of the countries Collection.

Command: db.countries.count()

It will return output as below:

245

Backup Data

[edit]

Backup test database and export the data to c: \mongodb\backup directory

Command: mongodump -h localhost --port 27017 -d test -o c: \mongo\backup

The following output will be generated if backup data is successful.

You can ignore the -h and --port options if you are connect to a local MongoDB server with default port 27017

You can backup all the database in MongoDB Server.

Command: mongodump -h localhost --port 27017 -o c: \mongodb\backup

You can backup only Countries Collection in MongoDB Server

Command: mongodump -h localhost --port 27017 -d test -c countries -o C: \mongodb\backup

You can backup a database with authentication.

Command: mongodump -h localhost --port 27017 -u user1 -p pass1 -d test -o C: \mongodb\backup

Restore Data

[edit]

Restore all the data from C: \mongodb\backup directory back to MongoDB Server

Command: mongorestore -h localhost --port 27017 --drop C: \mongodb\backup

The following output will be generated if restore data is successful.

You can ignore the -h and --port options if you are connect to a local MongoDB server with default port 27017

You can restore a database with authentication.

Command: mongodump -h localhost --port 27017 -u user1 -p pass1 --drop C: \mongodb\backup

Verify Data

[edit]

After database restored, Verify the data of countries collection

Open Command prompt window to connect test database with MongoDB Shell.

Command: mongo test

Check the total records of countries collection

Command: db.countries.count()

You will get the same total records count as below:

245

Conclusions

[edit]

Backing up and restoring MongoDB databases is a crucial aspect of database management. By following the outlined steps and ensuring prerequisites are met, you can effectively safeguard your data against loss or corruption. Regular backups ensure data integrity and provide a means of recovery in the event of unforeseen issues. Additionally, verifying data integrity after restoration helps ensure that the backup process was successful and that your data is intact and accessible.