2.3 KiB
mongodump
Mongodump is a utility tool that comes with MongoDB, which is used to create a backup of your data by capturing the BSON output from your MongoDB database. It is especially useful when you want to export data from MongoDB instances, clusters or replica sets for either backup purposes or to migrate data from one environment to another.
How it works
Mongodump connects to a running mongod
or mongos
process and extracts the BSON data from the database, which includes collections, their documents, and indexes. The tool stores the exported data in a binary format in a directory named dump
by default, with each collection's data placed inside a separate BSON file.
Usage
Here's a basic example of using mongodump
:
mongodump --uri "mongodb://username:password@host:port/database" --out /path/to/output/dir
Replace the values for username
, password
, host
, port
, and database
with your actual MongoDB credentials and target database. This command will create a backup of your specified database and will store it in the specified output directory.
Options
Mongodump offers a variety of options to customize your backups:
--uri
: The MongoDB connection string with authentication details.--out
: The path to save the output data.--db
: The specific database to backup.--collection
: The specific collection to backup.--query
: An optional query to export only matching documents.--oplog
: Include oplog data for a consistent point-in-time snapshot.--gzip
: Compress the backup files using gzip.--archive
: Write the output to a single archive file instead of individual files.
Restoring data with mongorestore
To restore data from a mongodump
backup, you can use the mongorestore
tool, which comes with MongoDB as well. Here's a basic example of using mongorestore
:
mongorestore --uri "mongodb://username:password@host:port/database" --drop /path/to/backup/dir
This command will restore the specified database from the backup directory, and the --drop
flag will remove any existing data in the target database before restoring the data.
In summary, mongodump
is a powerful utility for creating backups of your MongoDB data. Used in conjunction with mongorestore
, you can easily create, store, and restore data backups as needed.