0% found this document useful (0 votes)
23 views

Backup +recovery

The document discusses MongoDB backup and recovery utilities including mongoexport, mongoimport, mongodump, and mongorestore. Mongoexport and mongoimport are used to export and import data to and from MongoDB in JSON or other formats. Mongodump takes a backup of all collections while mongorestore is used to restore from backups taken by mongodump. Each tool has various options to target specific databases, collections, or formats.

Uploaded by

srinu katams
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Backup +recovery

The document discusses MongoDB backup and recovery utilities including mongoexport, mongoimport, mongodump, and mongorestore. Mongoexport and mongoimport are used to export and import data to and from MongoDB in JSON or other formats. Mongodump takes a backup of all collections while mongorestore is used to restore from backups taken by mongodump. Each tool has various options to target specific databases, collections, or formats.

Uploaded by

srinu katams
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Backup & Recovery

Backup and Recovery Utilities


• mongoexport
• Mongoimport
• mongodump
• mongorestore
Mongoexport
• One of the primary methods to extract the data
• Extract the data in JSON format
• Useful for moving the data to other databases
• https://www.quackit.com/json/tutorial/list_of_json_databases.cfm
• mongoexport must run on a single specified database and collection.
• You cannot run mongoexport on an entire database or multiple
collections.
Mongoexport
• [root@mongoserver4 ~]# mongoexport -u dba -p oracle -d moviedb
2021-04-06T12:19:30.312-0400 must specify a collection
2021-04-06T12:19:30.312-0400 try 'mongoexport --help' for more information
• mongoexport -u dba -p oracle --authenticationDatabase=admin -d
moviedb -c movies -o movie.json
• At the end of the export , the number of records exported will be
listed
Mongoexport
• Exporting the data in CSV format
• $ mongoexport -u dba -p oracle --authenticationDatabase=admin -d
moviedb -c movies -o movie.csv --type CSV --fields name
• The option --type CSV and list fields specified using --fields
• --sort: This works similar to a query level sort, sorting documents by
some keys.
• --limit: This works similar to a query level limit, limiting the number of
documents outputted.
Exporting based on a query
• --query can be used
• Eg
• mongoexport -u dba -p oracle --authenticationDatabase=admin -d moviedb -
c movies -q='{"name":"test"}'

• You must enclose the query document in single quotes ('{ ... }') to ensure that
it does not interact with your shell environment
Mongoimport
• Used to import the data into mongodb
• This tool is designed to take an output of mongoexport as input
• The tool supports JSON, CSV and TSV formats
• mongoexport, mongoimport operates on a single target collection
within the specified database. This means that if you wish to import
data into multiple collections, you must separate the data into
individual files.
Mongoimport
• mongoimport -u dba -p oracle --authenticationDatabase=admin -d
moviedb -c newmovies --file movies.json
• With mongoimport, we can use the --drop option to drop the
collection before the import takes place.
• Other important arguments
• --stopOnError: If specified, the import will stop on the first error it
encounters
• --type: This can be either JSON, CSV, or TSV to specify what type of
file will be imported, but the default type is JSON
Mongoimport
• --ignoreBlanks TSV and CSV only, this will ignore empty fields in your
import file.
• --headerline : TSV and CSV only, this will assume the first line of your
import file is a list of field names.
• --fields: TSV and CSV only, this will specify a comma-separated list of
keys in your documents for CSV and TSV formats. This is only needed
if you do not have a header line.
• $ mongoimport -u dba -p oracle --authenticationDatabase=admin -d
movies -c contacts --type CSV --file contacts.csv --ignoreBlanks --
headerline
Mongodump
• Take the backup of all collections in the mongodb instance
• Mongodump created a ,bson and a json file for each collection
• mongodump -u dba -p oracle
Important Options
--db This allows you to specify a single database for the command to backup,
by default it will back up all databases.
--collection: specify a single collection to backup, by default it will back up all
collections.
--excludeCollection: This allows you to specify a collection to exclude from
the backup.
$ mongodump -u dba -p oracle --authenticationDatabase=admin --db
moviedb --excludeCollection=newmovies
mongorestore
Used to restore the database/collections created by mongodump
• $ mongorestore -u dba -p oracle --drop dump
• --dryRun: This allows you to see the output of running a mongorestore
without actually changing the information in the database, this is an
excellent way to test your command before executing potentially
dangerous operations.
• --drop: Similar to mongoimport, the --drop option will drop the
collections to be restored before restoring them, allowing you to
ensure no old data remains after the command has run.
mongorestore
• --stopOnError: If enabled, the process stops as soon as a single error
occurs.
• --nsInclude: Instead of providing a database and collection specifically,
this option allows you to define which namespaces (databases and
collections) should be imported from the dump file.
• --nsExclude: This is the complimentary option for nsInclude, allowing
you to provide a namespace pattern that is not imported when
running the restore.
mongorestore
Restore a specific collection using “mongorestore”
• $ mongorestore -u dba -p oracle --drop --
nsInclude="movies.contacts" dump
Restore a database using “mongorestore”
• $ mongorestore -u dba -p oracle --drop --nsInclude="movies.* " dump
Restore the collections from one db to another.
• mongorestore -u dba -p oracle --authenticationDatabase=admin --
drop --nsFrom="moviedb" --nsTo="backup" --db="backup"
dump/moviedb

You might also like