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:
bashmongodump --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:
mongodumpcreates a backup of the entirelibraryDBdatabase.- The
--outoption 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:
bashmongorestore --uri="mongodb://localhost:27017" /path/to/backup/libraryDB/3. The database will be restored to the MongoDB instance.
Explanation:
mongorestorerestores thelibraryDBdatabase 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:
bashmongodump --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=booksspecifies the collection to back up.- The
--outoption 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:
bashmongorestore --uri="mongodb://localhost:27017" --collection=books /path/to/backup/libraryDB/books.bson3. The books collection will be restored from the backup.
Explanation:
--collection=booksensures that only thebookscollection is restored.- The path
/path/to/backup/libraryDB/books.bsonpoints 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:
bashmongoexport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --out=students.json3. The students.json file will contain the exported data from the students collection.
Explanation:
mongoexportis used to export the collection in JSON format.--out=students.jsonspecifies 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:
bashmongoimport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --file=students.json3. The data from students.json will be imported into the students collection.
Explanation:
mongoimportis used to import data from a JSON file.- The
--fileoption 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:
bashmongoexport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --type=csv --out=students.csv --fields=name,email,age3. The students.csv file will be created containing the data from the students collection.
Explanation:
- The
--type=csvoption specifies the export format. --fields=name,email,agespecifies 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:
bashmongoimport --uri="mongodb://localhost:27017" --db=studentsDB --collection=students --file=students.json3. The data from students.json will be restored to the students collection.
Explanation:
mongoimportis used for importing data into a MongoDB collection.- The
--fileoption 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:
bash0 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 * * *). mongodumpwill create a backup of thestudentsDBdatabase 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:
bashmongodump --uri="mongodb://localhost:27017/libraryDB" --out=/path/to/backup/ --gzip3. The backup will be stored in a compressed .gz file in the specified directory.
Explanation:
- The
--gzipoption compresses the backup files to save disk space.
Summary
- These assignments cover the basics of backing up, restoring, and exporting data in MongoDB.
mongodumpandmongorestoreare used for backing up and restoring databases, whilemongoexportandmongoimportare 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.
