Backup +recovery
Backup +recovery
• 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