While reading the mongodump documentation, I came across this information. "mongodump only captures the documents in the database in its backup data and does not include index data. mongorestore or mongod must then rebuild the indexes after restoring data."
Considering that indexes are also critical piece of the database puzzle and they form required to be rebuilt, why doesn't mongodump have an option of taking the backups with indexes?
I get that there are two advantages of not backing up indexes as a default option: 1. We save time which would otherwise be required for backup and restore of indexes. 2. We save space required for storing the backups.
But why not have it as an option at all?
Yes, mongodump does export the indexes created on the collection, and the indexes are restored with mongorestore along with the data.
mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.
Basic mongorestore syntax The basic way to restore a database is to use the mongorestore command to specify the backup directory (dump directory) without any options. This option is suitable for databases located in the localhost (127.0. 0.1) using the port 27017.
The mongodump command will overwrite the existing files within the given backup folder. The default location for backups is the dump/ folder. When the WiredTiger storage engine is used in a MongoDB instance, the output will be uncompressed data.
mongodump
creates a binary export of data from a MongoDB database (in BSON format). The index definitions are backed up in <dbname>.metadata.json
files, so mongorestore
can recreate the original data & indexes.
There are two main reasons that the actual indexes cannot be backed up with mongodump
:
Indexes point to locations in the data files. The data files do not exist if you are only exporting the documents in the data files (rather than taking a full file copy of the data files).
The format of indexes on disk is storage-engine specific, whereas mongodump
is intended to be storage-engine independent.
If you want a full backup of data & indexes, you need to backup by copying the underlying data files (typically by using filesystem or EBS snapshots). This is a more common option for larger deployments, as mongodump
requires reading all data into the mongod
process (which will evict some of your working set if your database is larger than memory).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With