Last Updated: 04 August, 2023
It may be possible that our important and valuable data can be removed, crashed, or corrupted from our server or our local machine accidentally or by any mistake. So it is very important to take a backup of our data from the MongoDB database.
MongoDB provides several options for backup and restore operations, allowing us to protect our data and recover it in case of data loss or system failures.
Below, I'll explain the basic steps for performing backups and restores in MongoDB.
In MongoDB, mongodump is a command-line tool that is used to create binary backups of our MongoDB data. It creates a dump of the entire database or specific collections and stores the data in BSON format.
Example:
Take a Backup of all Databases together
mongodump
Take a Backup of single Database
mongodump --db schooldb
Here, schooldb is the database; it will create a dump folder along with a backup file inside the mongodb bin folder.
Take a Backup of a single Database and store the backup at the specified location
mongodump --db schooldb -o \mydbdumpfolder
Here, schooldb is the database, and it will create a backup file in the mydbdumpfolder directory.
Take a Backup of a database collection
mongodump --db schooldb --collection students
Here, schooldb is the database, and students is the collection.
Take a Backup of a Collection of any Database and store backup on specified location
mongodump --db schooldb -o \mydbdumpfolder --collection students
Here, schooldb is the database and students is the collection and it will create backup file in mydbdumpfolder directory.
The restore utility uses the mongorestore command to restore a binary backup created by mongodump. The mongorestore command can restore either an entire database backup or a subset of the backup.
Restore a backup of all Databases together.
mongorestore
Restore Backup of single Database
mongorestore --db schooldb
Here, schooldb is the database; it will create a dump folder along with a backup file inside the mongodb bin folder.
Take a Backup of a single Database and store the backup at the specified location.
mongorestore --db schooldb -o \mydbdumpfolder
Note: Here schooldb is the database; it will create a backup file in the mydbdumpfolder directory.
Take a backup of the database collection
mongorestore --db schooldb --collection students
Here, schooldb is the database, and students is the collection.
Take a Backup of a Collection of any Database and store the backup at a specified location.
mongorestore --db schooldb -o \mydbdumpfolder --collection students
Here, schooldb is the database and students is the collection, and it will create a backup file in the mydbdumpfolder directory.
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com