MongoDB whole library backup and restore and single collection backup and recovery method
Check before backup
> show dbsMyDB 0.0625GBadmin (empty) bruce 0.0625GBlocal (empty) test 0.0625GB > use MyDBswitched 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 address, for example: 127.0.0.1. Of course, you can also specify the port number: 127.0.0.1 dbname 27017murd: the database instance that needs to be backed up, such as: test-o: the data storage location of the backup, for example: C:\ data\ dump. Of course, this directory needs to be established in advance. After the backup is completed, the system automatically creates a test directory under the backup directory. This directory stores the backup data of the database instance. The official description of mongodump (available at 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 host 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, such as test. Of course, this name can also be different from that at the time of backup, such as test2-directoryperdb: the location of the backup data, for example: C:\ data\ dump\ test, why add an extra test here instead of the dump at the time of backup? -drop: when restoring, delete the current data first, and then restore the backed-up data. That is to say, after recovery, the data added and modified after backup will be deleted, so use it with caution. The official description of mongorestore (available at 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 host 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 don't upgrade indexes to newest version-- noOptionsRestore don't restore collection options-- noIndexRestore don'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 of MongoDB-d: database instance to be restored-c: collection to be restored-f: fields to be exported (omitted all fields)-o: official description of the exported file name mongoexport (available 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: indicates the file format to be imported-headerline: indicates not to import the first line Because the first line is the column name-file: official description indicating the path of the file to be imported (available 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" can implicitly create a table structure when you see the imported data. 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 file name to be exported, and in this case it is "user.dat".
You can see from the above that the export method uses the JSON style.