MongoDB Whole library Backup restore single table collection backup restore
CD D:\MongoDB\bin
1 backup of the whole library:
Mongodump-h dbhost-d Dbname-o dbdirectory
-H:mongdb server address, for example:127.0.0.1:27017
-D: The database instance that needs to be backed up, for example:wlwdb
-O: The backup data storage location, for example:D:\MongoDB\data\dump, of course, the directory needs to be established in advance, after the backup is completed, the system automatically in the dump Directory to create a wlwdb directory in which to store backup data for the database instance.
eg
Mongodump-h 127.0.0.1-d Wlwdb-o D:\MongoDB\data\dump
You can view the backup directory:
Mongodump--help
Official description of mongodump:
options:
--help produce help message
-v [--verbose] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program ‘s version and exit
-h [--host] arg mongo host to connect to (/ s1, s2 for
sets)
--port arg server port. Can also use --host hostname: port
--ipv6 enable IPv6 support (disabled by default)
-u [--username] arg username
-p [--password] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server-
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [--db] arg database to use
-c [--collection] arg collection to use (some commands)
-o [--out] arg (= dump) output directory or "-" for stdout
-q [--query] arg json query
--oplog Use oplog for point-in-time snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use $ snapshot)
2 Whole library recovery:
mongorestore -h dbhost -d dbname –directoryperdb dbdirectory
-h: MongoDB server address
-d: The database instance that needs to be restored, for example: wlwdb (may be different from the backup)
–Directoryperdb: the location of the backup data, for example: D: \ MongoDB \ data \ dump \ wlwdb
–Drop: When restoring, delete the current data first, and then restore the backup data.
eg:
mongorestore -h 127.0.0.1 -d wlwdb --directoryperdb D: \ MongoDB \ data \ dump \ wlwdb
The official description of mongorestore (can be viewed through mongorestore --help):
options:
--help produce help message
-v [--verbose] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program ‘s version and exit
-h [--host] arg mongo host to connect to (/ s1, s2 for sets)
--port arg server port. Can also use --host hostname: port
--ipv6 enable IPv6 support (disabled by default)
-u [--username] arg username
-p [--password] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server-
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [--db] arg database to use
-c [--collection] arg collection to use (some commands)
--objcheck validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--oplogReplay replay oplog for point-in-time restore
--oplogLimit arg exclude oplog entries newer than provided timestamp
(epoch [: ordinal])
--keepIndexVersion do n‘t upgrade indexes to newest version
--noOptionsRestore do n‘t restore collection options
--noIndexRestore do n‘t restore indexes
--w arg (= 1) minimum number of replicas per write
3 Single collection backup:
mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory
-h: server address where MongoDB is located
-d: need to restore the database instance
-c: collection to be restored
-f: fields that need to be exported (omitted for all fields)
-o: indicates the exported file name
Official description of mongoexport (can be viewed through mongoexport --help):
--help produce help message
-v [--verbose] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program ‘s version and exit
-h [--host] arg mongo host to connect to (/ s1, s2 for
sets)
--port arg server port. Can also use --host hostname: port
--ipv6 enable IPv6 support (disabled by default)
-u [--username] arg username
-p [--password] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server-
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [--db] arg database to use
-c [--collection] arg collection to use (some commands)
-f [--fields] arg comma separated list of field names e.g. -f
name, age
--fieldFile arg file with fields names-1 per line
-q [--query] arg query filter, as a JSON string
--csv export to csv instead of json
-o [--out] arg output file; if not specified, stdout is used
--jsonArray output to a json array rather than one object per
line
-k [--slaveOk] arg (= 1) use secondaries for export if available, default
true
--forceTableScan force a table scan (do not use $ snapshot)
4 Single collection recovery:
mongoimport -d dbhost -c collectionname –type csv –headerline –file
-type: specify the file format to be imported
-headerline: indicate that the first line is not imported because the first line is the column name
-file: specify the file path to be imported Official description of mongoimport (can be viewed through mongoimport --help):
--help produce help message
-v [--verbose] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program ‘s version and exit
-h [--host] arg mongo host to connect to (/ s1, s2 for sets)
--port arg server port. Can also use --host hostname: port
--ipv6 enable IPv6 support (disabled by default)
-u [--username] arg username
-p [--password] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server-
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [--db] arg database to use
-c [--collection] arg collection to use (some commands)
-f [--fields] arg comma separated list of field names e.g. -f name, age
--fieldFile arg file with fields names-1 per line
--ignoreBlanks if given, empty fields in csv and tsv will be ignored
--type arg type of file to import. default: json (json, csv, tsv)
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--headerline CSV, TSV only-use first line as headers
--upsert insert or update objects that already exist
--upsertFields arg comma-separated fields for the query part of the
upsert. You should make sure this is indexed
--stopOnError stop importing at first error rather than continuing
--jsonArray load a json array, not one item per line. Currently
limited to 16MB.
MongoDB whole database backup and restore single table collection backup and restore