Lecture Notes Of Day 20: MongoDB Backup and Restore

Rashmi Mishra
0

 

Lecture Notes Of Day 20: MongoDB Backup and Restore

Objective:

Learn how to back up and restore MongoDB databases.

Outcome:

Students will be able to create backups of MongoDB databases and restore them when needed.


Introduction to MongoDB Backup and Restore

MongoDB is a popular NoSQL database, which is used for handling large volumes of data in a flexible, scalable way. Just like any other database, it’s important to create backups to protect your data and ensure recovery in case of failure. MongoDB provides tools and utilities to back up and restore databases efficiently. Understanding how to perform backups and restores is a crucial skill for database management.

Backup and Restore Methods

MongoDB provides two main ways to back up and restore databases:

1.  Using mongodump (Backup) and mongorestore (Restore) tools

2.  Using mongoexport and mongoimport tools (Used for exporting and importing data in JSON or CSV format)

In this lesson, we will focus on the most commonly used tools: mongodump for backup and mongorestore for restoring data.


1. MongoDB Backup Using mongodump

What is mongodump?

mongodump is a utility that creates a binary export of the contents of a MongoDB database. It backs up the entire database or specific collections, depending on your needs.

Syntax:

bash
mongodump --uri="mongodb://<username>:<password>@<host>:<port>/<database>"

Here’s a breakdown of the command:

  • --uri="mongodb://<username>:<password>@<host>:<port>/<database>": Specifies the MongoDB URI, where:
    • <username>: Your MongoDB username.
    • <password>: Your MongoDB password.
    • <host>: The MongoDB server’s hostname or IP address.
    • <port>: The port number where MongoDB is running (default is 27017).
    • <database>: The name of the database you want to back up.

Example:

To back up a database named studentsDB running locally on the default MongoDB port:

bash
mongodump --uri="mongodb://localhost:27017/studentsDB"

By default, this command will create a dump directory containing all the backups in a directory named dump.

Options:

  • --out=<directory>: Allows you to specify the directory where the backup should be saved. Example: mongodump --uri="mongodb://localhost:27017/studentsDB" --out=/path/to/backup/
  • --gzip: Compresses the backup files using gzip to reduce the size of the backup files.
  • --archive=<filename>: Creates a single compressed archive file instead of a directory of files. Example: mongodump --uri="mongodb://localhost:27017/studentsDB" --archive=studentsDB_backup.gz --gzip

2. MongoDB Restore Using mongorestore

What is mongorestore?

mongorestore is a utility that allows you to restore a backup created by mongodump. You can restore a whole database, specific collections, or even individual documents.

Syntax:

bash
mongorestore --uri="mongodb://<username>:<password>@<host>:<port>/<database>" <backup_directory>

Example:

If you want to restore a backup of the studentsDB database:

bash
mongorestore --uri="mongodb://localhost:27017" /path/to/backup/studentsDB/

Options:

  • --drop: Drops the existing database or collection before restoring. Use this option with caution, as it will overwrite any existing data. Example: mongorestore --uri="mongodb://localhost:27017" --drop /path/to/backup/studentsDB/
  • --gzip: Decompresses a gzipped backup before restoring. Example: mongorestore --uri="mongodb://localhost:27017" --gzip --archive=studentsDB_backup.gz
  • --nsInclude=<namespace>: Restores only specific collections from a backup. Example: mongorestore --uri="mongodb://localhost:27017" --nsInclude="studentsDB.students" /path/to/backup/

3. Exporting and Importing Data Using mongoexport and mongoimport

While mongodump and mongorestore work with binary data, mongoexport and mongoimport allow for exporting and importing data in JSON or CSV format. These tools are useful for working with smaller datasets or exporting data for use in other applications.

MongoDB Export (mongoexport)

You can export collections into JSON or CSV format.

Example (Export as JSON):

bash
mongoexport --uri="mongodb://localhost:27017" --collection=students --db=studentsDB --out=students.json

MongoDB Import (mongoimport)

You can import data into a MongoDB database from JSON or CSV files.

Example (Import from JSON):

bash
mongoimport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --file=students.json

4. Backup and Restore Best Practices

Here are some best practices to follow when performing backups and restores with MongoDB:

  • Automate Backups: Set up a cron job or scheduled task to automate regular backups of your MongoDB databases.
  • Store Backups Off-Site: Always store backups in a different location, preferably off-site or on cloud storage, to avoid data loss due to hardware failure.
  • Test Backups: Periodically restore backups to ensure they are working and can be used in case of data loss.
  • Backup Configuration Files: Don't forget to back up your MongoDB configuration files (like mongod.conf), as they contain important settings for your MongoDB instance.

5. Practical Example: Performing a Full Backup and Restore

Step 1: Backup a Database

Let's assume we have a database studentsDB that contains a collection students. To back it up, we use the following command:

bash
mongodump --uri="mongodb://localhost:27017/studentsDB" --out=/path/to/backup/

Step 2: Restore the Database

After backing up the database, we can restore it to a new MongoDB instance or on the same instance if needed. To restore the backup, use:

bash
mongorestore --uri="mongodb://localhost:27017" /path/to/backup/studentsDB/

Step 3: Verify the Restoration

After restoring, you can connect to MongoDB and check if the data is successfully restored:

bash
mongo
use studentsDB
db.students.find()

Conclusion

In this lesson, you have learned how to back up and restore MongoDB databases using the mongodump and mongorestore utilities. You’ve also been introduced to the mongoexport and mongoimport tools for exporting and importing data in JSON or CSV format. Regular backups are crucial for maintaining the safety and integrity of your data, so it's important to incorporate these practices into your routine database management.


Key Takeaways

  • Use mongodump to back up MongoDB databases and collections.
  • Use mongorestore to restore a database or collection from a backup.
  • Make sure to automate backups and store them securely.
  • Periodically test backups to ensure they can be restored when needed.

Exercise

1.  Backup a MongoDB database using mongodump and restore it using mongorestore.

2.  Create a backup of a collection in JSON format using mongoexport and then import it back using mongoimport.

You said:

Give solution ...Exercise Backup a MongoDB database using mongodump and restore it using mongorestore. Create a backup of a collection in JSON format using mongoexport and then import it back using mongoimport.

ChatGPT said:

ChatGPT

Exercise Solution


1. Backup a MongoDB Database Using mongodump and Restore it Using mongorestore

Step 1: Backup the Database Using mongodump

Let's assume we have a MongoDB database called studentsDB. We will back up this database using mongodump.

1.  Open a terminal or command prompt.

2.  Run the following command to back up the studentsDB database:

bash
mongodump --uri="mongodb://localhost:27017/studentsDB" --out=/path/to/backup/
  • Replace /path/to/backup/ with the actual path where you want to store the backup files.
  • This command will create a directory called studentsDB in the specified backup folder, and it will contain the backup of all collections in the studentsDB database.

Step 2: Restore the Database Using mongorestore

To restore the studentsDB backup, you need to run the following command:

1.  Run this command to restore the database:

bash
mongorestore --uri="mongodb://localhost:27017" /path/to/backup/studentsDB/
  • This will restore the studentsDB database from the backup directory /path/to/backup/studentsDB/ to your MongoDB instance running on localhost.

Step 3: Verify the Restoration

To verify that the database was restored successfully, you can connect to the MongoDB shell and query the studentsDB database:

1.  Open the MongoDB shell:

bash
mongo

2.  Switch to the studentsDB database:

javascript
use studentsDB

3.  List the collections to ensure that all collections were restored:

javascript
show collections

4.  Query the students collection (or any other collection you have):

javascript
db.students.find()

If the data appears, the restoration was successful.


2. Create a Backup of a Collection in JSON Format Using mongoexport and Import it Back Using mongoimport

Step 1: Export a Collection Using mongoexport

Assume we have a collection named students in the studentsDB database. We will export this collection into a JSON file.

1.  Open a terminal or command prompt.

2.  Run the following command to export the students collection from the studentsDB database:

bash
mongoexport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --out=students.json
  • This command will create a file called students.json that contains the exported data from the students collection.

Step 2: Import the Collection Back Using mongoimport

Now, you can import the students.json file back into the MongoDB database.

1.  Run the following command to import the students.json file back into MongoDB:

bash
mongoimport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --file=students.json
  • This will import the data from students.json into the studentsDB database's students collection.

Step 3: Verify the Import

To verify the import, open the MongoDB shell and run the following commands:

1.  Switch to the studentsDB database:

javascript
use studentsDB

2.  Check if the students collection exists:

javascript
show collections

3.  Query the students collection:

javascript
db.students.find()

If you see the data from the JSON file, the import was successful.


Summary of the Commands Used

  • Backup a database using mongodump:
bash
mongodump --uri="mongodb://localhost:27017/studentsDB" --out=/path/to/backup/
  • Restore a database using mongorestore:
bash
mongorestore --uri="mongodb://localhost:27017" /path/to/backup/studentsDB/
  • Export a collection using mongoexport:
bash
mongoexport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --out=students.json
  • Import a collection using mongoimport:
bash
mongoimport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --file=students.json

Post a Comment

0Comments

Post a Comment (0)

About Me