mongodump
Backup MongoDB database to files
TLDR
Create a dump of all databases (this will place the files inside a directory called "dump")
Specify an output location for the dump
Create a dump of a given database
Create a dump of a given collection within a given database
Connect to a given host running on a given port, and create a dump
Create a dump of a given database with a given username; user will be prompted for password
Create a dump from a specific instance; host, user, password and database will be defined in the connection string
SYNOPSIS
mongodump [options]
Example: mongodump --host localhost --port 27017 --db mydatabase --out /data/backup
Example with URI: mongodump --uri 'mongodb://localhost:27017/mydatabase' --archive=mydump.archive --gzip
PARAMETERS
--help
Displays a help message describing command-line options and usage.
--version
Displays the version of the mongodump utility.
--uri <uri>
Specifies the MongoDB connection string URI. This is an alternative to specifying --host, --port, etc.
--host <hostname>
Specifies the hostname or IP address of the mongod instance to connect to.
--port <port>
Specifies the port number of the mongod instance to connect to.
--db <database>
Specifies the database to dump. If omitted, mongodump dumps all databases (excluding local and admin unless specified).
--collection <collection>
Specifies the collection to dump. Must be used with --db. If omitted, all collections in the specified database are dumped.
--out <directory>
Specifies the output directory for the dump files. Defaults to dump/ in the current working directory.
--username <username>
Specifies the username for authentication.
--password <password>
Specifies the password for authentication. If omitted and a username is provided, mongodump will prompt for a password.
--gzip
Compresses the output BSON files using gzip. The output files will have a .gz extension.
--query <json-query>
Provides a JSON document as a query to filter documents in the collection to be dumped. Requires --db and --collection.
--oplog
For replica sets, includes the oplog entries from the beginning of the dump to the end. Essential for point-in-time recovery with mongorestore --oplogReplay.
--archive=<file>
Dumps the database to a single archive file instead of a directory of BSON files. Can be used with --gzip for compression.
DESCRIPTION
mongodump is a utility within the MongoDB Database Tools that creates a binary export of data from a MongoDB instance. It connects to a running mongod process and writes all data, or a subset, into a binary BSON format, suitable for later restoration using mongorestore. By default, it exports all databases to a dump/ directory, with each collection stored as a separate .bson file alongside .json metadata files. This utility is fundamental for database backup and migration strategies. It allows users to dump an entire MongoDB instance, specific databases, or even individual collections. Advanced options enable filtering documents with a query, compressing the output, or including oplog entries for point-time recovery in replica sets. While older versions might have imposed significant read locks, modern MongoDB versions with the WiredTiger storage engine allow mongodump to run with minimal impact on application availability.
CAVEATS
When dumping a running mongod instance, mongodump generally creates a consistent snapshot without blocking reads/writes for modern MongoDB versions (WiredTiger storage engine).
For replica sets, using the --oplog option is crucial for enabling point-in-time recovery, as it captures all operations during the dump process. Without --oplog, a restore operation would only recover data up to the start time of the dump.
mongodump does not back up indexes or user-defined roles/privileges; these are restored by mongorestore from the system collections' metadata. It is generally not suitable for backing up the entire filesystem structure of a MongoDB data directory.
BEST PRACTICES
Always test your mongodump backups by performing a full mongorestore operation on a separate instance to ensure data integrity and recoverability. For replica sets, consistently use the --oplog option to enable precise point-in-time recovery. Regularly schedule dumps, preferably during off-peak hours for very large databases, and store backups in a secure, off-site location.
When dumping sharded clusters, connect mongodump to a mongos instance to ensure a consistent view of the cluster data, or dump each shard individually for more granular control (though this complicates consistency).
OUTPUT STRUCTURE
By default, mongodump creates a directory named dump/ (or the directory specified by --out) in the current working directory. Inside, there are subdirectories for each dumped database, and within those, .bson files for each collection (containing the actual data) and corresponding .metadata.json files (containing index definitions, collection options, etc.). Using --archive consolidates all data into a single, compact archive file, which simplifies transfer and storage.
HISTORY
mongodump has been a core utility of the MongoDB Database Tools since the early days of MongoDB, serving as the primary method for binary database backups. Its functionality has evolved significantly: initial versions might have caused global read locks during the dump, but advancements in MongoDB storage engines (like WiredTiger) allow for a non-blocking or minimally blocking dump process. The introduction of options like --oplog enhanced its capabilities for replica set recovery, and --archive streamlined output management. It continues to be actively developed, adapting to new MongoDB features and best practices for data management.
SEE ALSO
mongorestore(1), mongo(1), mongostat(1), tar(1), gzip(1)