MongoDB backup and restoration are crucial tasks for ensuring data integrity, disaster recovery, and system stability. MongoDB provides tools and methods for creating backups and restoring data. Let's explore how to perform MongoDB backup and restoration.
For this blog post, we will take a backup of the builddevops database and attempt to restore it. Below is the current status of our databases and the collection of the builddevops database.
builddevops> show dbs
admin 40.00 KiB
builddevops 72.00 KiB
config 60.00 KiB
local 40.00 KiB
builddevops> use builddevops
already on db builddevops
builddevops> db.mycollection.find()
[
{ _id: ObjectId('6564d366ebf0acf9947c70c3'), server: 'dell' },
{ _id: ObjectId('6564d37cebf0acf9947c70c4'), server: 'IBM' }
]
builddevops>
MongoDB Backup:
In MongoDB, the mongodump tool is used to take data backups. It simply dumps all the stored data into a dump directory in MongoDB. The backed-up data is in BSON format, also known as BSON data dumps. By default, the backup is stored in the mongodb's bin\dump folder. To specify a different output directory, we can use the --out option. Mongodump can be used in two ways: with or without arguments.
Without Argument:
Without any arguments, mongodump connects to the MongoDB instance on the local system on port 27017 and creates a backup of every database and every collection.
[root@siddhesh ~]# mongodump
With Argument: By specifying the database in the argument by which we can limit the amount of data stored in the database dump.
[root@siddhesh ~]# mongodump --db databaseName --collection collectionName
Example 1: Backing up all the databases
[root@siddhesh ~]# mongodump --out /opt/mongodump
2023-11-27T23:17:40.029+0530 writing admin.system.version to /opt/mongodump/admin/system.version.bson
2023-11-27T23:17:40.030+0530 done dumping admin.system.version (1 document)
2023-11-27T23:17:40.030+0530 writing builddevops.mycollection to /opt/mongodump/builddevops/mycollection.bson
2023-11-27T23:17:40.031+0530 done dumping builddevops.mycollection (2 documents)
[root@siddhesh ~]# ls -lhrt /opt/mongodump/
total 0
drwxr-xr-x 2 root root 69 Nov 27 23:17 admin
drwxr-xr-x 2 root root 65 Nov 27 23:17 builddevops
[root@siddhesh ~]#
The mongodump process is writing the data from the admin.system.version collection to /opt/mongodump/admin/system.version.bson.
It then indicates that it has completed the dump for admin.system.version with 1 document.
Next, it writes the data from the builddevops.mycollection collection to /opt/mongodump/builddevops/mycollection.bson.
It completes the dump for builddevops.mycollection with 2 documents.
In summary, this mongodump command is creating a backup of the specified collections (admin.system.version and builddevops.mycollection) and storing them in the /opt/mongodump directory.
Example 2: Backing up the specified collection
[root@siddhesh ~]# mongodump --db builddevops --collection mycollection --out /opt/mycollection_backup
2023-11-27T23:21:38.906+0530 writing builddevops.mycollection to /opt/mycollection_backup/builddevops/mycollection.bson
2023-11-27T23:21:38.911+0530 done dumping builddevops.mycollection (2 documents)
[root@siddhesh ~]# ls -lhrt /opt/mycollection_backup/builddevops/
total 8.0K
-rw-r--r-- 1 root root 179 Nov 27 23:21 mycollection.metadata.json
-rw-r--r-- 1 root root 77 Nov 27 23:21 mycollection.bson
[root@siddhesh ~]#
The mongodump process is writing the data from the builddevops.mycollection collection to /opt/mycollection_backup/builddevops/mycollection.bson.
It then indicates that it has completed the dump for builddevops.mycollection with 2 documents.
In summary, this mongodump command is creating a backup of the specified collection (mycollection) from the builddevops database and storing it in the /opt/mycollection_backup directory.
MongoDB Restoration:
In case of data loss or system failure, use mongorestore to restore data from the backup.
So let's drop a database builddevops & try to restore it from the backup which we took in the above example. Database Drop:
test> use builddevops
switched to db builddevops
builddevops> db.dropDatabase()
{ ok: 1, dropped: 'builddevops' }
builddevops> show dbs
admin 40.00 KiB
config 84.00 KiB
local 40.00 KiB
builddevops>
Database Restore:
[root@siddhesh ~]# mongorestore --db builddevops /opt/mycollection_backup/builddevops/
2023-11-27T23:28:44.946+0530 building a list of collections to restore from /opt/mycollection_backup/builddevops dir
2023-11-27T23:28:44.946+0530 reading metadata for builddevops.mycollection from /opt/mycollection_backup/builddevops/mycollection.metadata.json
2023-11-27T23:28:44.958+0530 restoring builddevops.mycollection from /opt/mycollection_backup/builddevops/mycollection.bson
2023-11-27T23:28:44.970+0530 finished restoring builddevops.mycollection (2 documents, 0 failures)
2023-11-27T23:28:44.970+0530 no indexes to restore for collection builddevops.mycollection
2023-11-27T23:28:44.970+0530 2 document(s) restored successfully. 0 document(s) failed to restore.
[root@siddhesh ~]#
Verify Data:
test> show dbs
admin 40.00 KiB
builddevops 8.00 KiB
config 84.00 KiB
local 40.00 KiB
test> use builddevops
switched to db builddevops
builddevops> db.mycollection.find()
[
{ _id: ObjectId('6564d366ebf0acf9947c70c3'), server: 'dell' },
{ _id: ObjectId('6564d37cebf0acf9947c70c4'), server: 'IBM' }
]
builddevops>
1件のコメント