Assignements Of Day 20: MongoDB Backup and Restore

Rashmi Mishra
0

 

Assignements Of Day 20: MongoDB Backup and Restore


Assignment 1: Backing Up a MongoDB Database

Objective: Learn how to back up a MongoDB database using mongodump.

Task: Create a backup of the libraryDB database.

Steps:

1.  Open the terminal or command prompt.

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

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

o    Replace /path/to/backup/ with the desired backup directory.

3.  After running the command, the backup will be stored in the specified directory.

Explanation:

  • mongodump creates a backup of the entire libraryDB database.
  • The --out option specifies where to store the backup files.

Assignment 2: Restoring a MongoDB Database

Objective: Learn how to restore a MongoDB database using mongorestore.

Task: Restore the libraryDB database from the backup created in Assignment 1.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to restore the libraryDB database:

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

3.  The database will be restored to the MongoDB instance.

Explanation:

  • mongorestore restores the libraryDB database from the backup directory.
  • The path /path/to/backup/libraryDB/ points to the backup directory created in Assignment 1.

Assignment 3: Backing Up a Specific Collection

Objective: Learn how to back up a specific collection from a MongoDB database.

Task: Back up the books collection from the libraryDB database.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to back up the books collection:

bash
mongodump --uri="mongodb://localhost:27017/libraryDB" --collection=books --out=/path/to/backup/

3.  The backup of the books collection will be stored in the specified directory.

Explanation:

  • --collection=books specifies the collection to back up.
  • The --out option specifies where to store the backup.

Assignment 4: Restoring a Specific Collection

Objective: Learn how to restore a specific collection from a backup.

Task: Restore the books collection from the backup directory.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to restore the books collection:

bash
mongorestore --uri="mongodb://localhost:27017" --collection=books /path/to/backup/libraryDB/books.bson

3.  The books collection will be restored from the backup.

Explanation:

  • --collection=books ensures that only the books collection is restored.
  • The path /path/to/backup/libraryDB/books.bson points to the backup file.

Assignment 5: Exporting Data in JSON Format

Objective: Learn how to export a MongoDB collection in JSON format.

Task: Export the students collection from the studentsDB database to a students.json file.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to export the students collection:

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

3.  The students.json file will contain the exported data from the students collection.

Explanation:

  • mongoexport is used to export the collection in JSON format.
  • --out=students.json specifies the output file.

Assignment 6: Importing Data from JSON

Objective: Learn how to import data from a JSON file into a MongoDB collection.

Task: Import the students.json file back into the studentsDB database's students collection.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to import the students.json file:

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

3.  The data from students.json will be imported into the students collection.

Explanation:

  • mongoimport is used to import data from a JSON file.
  • The --file option specifies the file to import.

Assignment 7: Exporting Data in CSV Format

Objective: Learn how to export a MongoDB collection in CSV format.

Task: Export the students collection to a CSV file called students.csv.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to export the students collection:

bash
mongoexport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --type=csv --out=students.csv --fields=name,email,age

3.  The students.csv file will be created containing the data from the students collection.

Explanation:

  • The --type=csv option specifies the export format.
  • --fields=name,email,age specifies which fields to include in the CSV file.

Assignment 8: Restoring Data from JSON File

Objective: Learn how to restore data into a MongoDB collection from a JSON file.

Task: Restore the students.json file into the studentsDB database's students collection.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to restore the students.json file:

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

3.  The data from students.json will be restored to the students collection.

Explanation:

  • mongoimport is used for importing data into a MongoDB collection.
  • The --file option specifies the file containing the data.

Assignment 9: Automating Database Backups

Objective: Learn how to automate MongoDB database backups using cron jobs (Linux) or Task Scheduler (Windows).

Task: Set up a cron job to automatically back up the studentsDB database every day at midnight.

Steps:

1.  Open the terminal and type crontab -e to edit the cron jobs.

2.  Add the following line to back up the studentsDB database at midnight every day:

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

3.  Save and exit the editor. The backup will run every day at midnight.

Explanation:

  • The cron job is set to run at midnight every day (0 0 * * *).
  • mongodump will create a backup of the studentsDB database at the specified location.

Assignment 10: Creating a Backup with Compression

Objective: Learn how to back up a MongoDB database and compress the backup using gzip.

Task: Create a compressed backup of the libraryDB database using gzip.

Steps:

1.  Open the terminal or command prompt.

2.  Run the following command to create a compressed backup:

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

3.  The backup will be stored in a compressed .gz file in the specified directory.

Explanation:

  • The --gzip option compresses the backup files to save disk space.

Summary

  • These assignments cover the basics of backing up, restoring, and exporting data in MongoDB.
  • mongodump and mongorestore are used for backing up and restoring databases, while mongoexport and mongoimport are used for exporting and importing collections in JSON or CSV format.
  • Students also learned how to automate backups and use compression to reduce the backup size.

Post a Comment

0Comments

Post a Comment (0)

About Me