MongoDB database maintenance is inseparable from the necessary backup, recovery operations, and generally does not go wrong, so we use the most of the time using backup and recovery operations. The principle of mongodump.exe backup is to get the current server snapshot through a single query and write the snapshot to disk, so this method is not saved in real time, because after the snapshot is taken, the server will also have data write, in order to ensure the security of the backup, we can also use the Fsync lock to temporarily write server data to the cache.
Efficient open source Database (MongoDB): http://www.jb51.net/softs/41751.html
Pre-backup Checks
> Show DBS
MyDB 0.0625GB
Admin (empty)
Bruce 0.0625GB
Local (empty)
Test 0.0625GB
> Use MyDB
Switched to DB MyDB
> Db.users.find ()
{"_id": ObjectId ("4e290aa39a1945747b28f1ee"), "a": 1, "B": 1}
{"_id": ObjectId ("4e2cd2182a65c81f21566318"), "a": 3, "B": 5}
>
Full Library Backup:
mongodump-h dbhost-d dbname-o dbdirectory
-h:mongdb server address, For example: 127.0.0.1, you can also specify a port number: 127.0.0.1:27017
-D: A database instance that needs to be backed up, such as: Test
-O: The location where the data is backed up, for example: C:\data\dump, of course, the directory needs to be established in advance, After the backup is complete, the system automatically establishes a test directory under the Dump directory, which contains the backup data for the database instance.
Official description of mongodump (can be viewed through mongodump --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)
-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)
Whole library recovery:
mongorestore -h dbhost -d dbname –directoryperdb dbdirectory
-h: MongoDB server address
-d: the database instance to be restored, for example: test, of course, this name can also be different from the backup, such as test2
–Directoryperdb: The location of the backup data, for example: c: \ data \ dump \ test. Why add a test here instead of dump during backup, readers should check the tips by themselves!
–Drop: When restoring, delete the current data first, and then restore the backup data. In other words, after recovery, the data added and modified after backup will be deleted, use with caution!
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
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)
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 (available View 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.
Other import and export operations:
1. mongoimport -d my_mongodb -c user user.dat
Parameter Description:
-d indicates the library used, in this case "my_mongodb"
-c indicates the table to be exported, in this case "user"
You can see that the table structure is implicitly created when the data is imported
2. mongoexport -d my_mongodb -c user -o user.dat
Parameter Description:
-d indicates the library used, in this case "my_mongodb"
-c indicates the table to be exported, in this case "user"
-o indicates the name of the file to be exported, in this case "user.dat"
As you can see from the above, the export method uses the JSON style.
Articles you may be interested in:
Steps to import sqlserver data into mongodb using mongovue
mongodb database operation-backup, restore, export, import
MongoDB whole database backup and restore and single collection backup and recovery method