MongoDB full library backup and restore and a single collection backup and restore method

  • 2020-05-06 11:57:36
  • OfStack

mongodump. The principle of exe backup is through a query access to current server snapshot, and in the snapshot will be written to disk, so this way is not real-time, because after the snapshots, the server will be writing data, in order to ensure the safety of the backup we can still use fsync lock to make same server data write to the cache for the time being.

High source database (mongodb) download address: / / www jb51. net softs / 41751. html

Check
before backing up > 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, of course, you can also specify the port number: 127.0.0.1:27017
-d: database instances that need to be backed up, e.g. test
-o: backup data storage location, such as: c:\data\dump, of course, the directory needs to be set up in advance, after the backup is completed, the system will automatically set up an test directory in the dump directory, which will hold the backup data of the database instance.

Official description of mongodump (available through mongodump --help) : options: --helpproduce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --versionprint the program's version and exit -h [ --host ] argmongo host to connect to ( /s1,s2 for sets) --port argserver port. Can also use --host hostname:port --ipv6enable IPv6 support (disabled by default) -u [ --username ] argusername -p [ --password ] argpassword --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 --journalenable 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 ] argjson 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)

full library restore:
Es110en-h dbhost d dbname, directoryperdb dbdirectory
-h: MongoDB server address
-d: the database instance that needs to be restored, e.g. test, although the name can be different from that of the backup, e.g. test2
directoryperdb: backup data location, e.g. c:\data\dump\test, why add an test instead of dump?
When restoring, first delete the current data and then restore the backup data. That is to say, after recovery, backup after adding modified data will be deleted, careful oh!

Official description of mongorestore (available through mongorestore --help) : options: --help produce help message -v [ --verbose ]be more verbose (include multiple times for more verbosity e.g. -vvvvv) --versionprint the program's version and exit -h [ --host ] argmongo 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 ] argusername -p [ --password ] argpassword --dbpath argdirectly 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 --directoryperdbif dbpath specified, each db is in a separate directory --journalenable journaling -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) --objcheck validate object before inserting --filter argfilter to apply before inserting --drop drop each collection before import --oplogReplayreplay oplog for point-in-time restore --oplogLimit argexclude oplog entries newer than provided timestamp (epoch[:ordinal]) --keepIndexVersion don't upgrade indexes to newest version --noOptionsRestore don't restore collection options --noIndexRestoredon'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: MongoDB server address
-d: database instance
that needs to be restored -c: collection
to recover -f: fields to be exported (omitted for all fields)
-o: represents the exported file name

Official description of mongoexport (available through mongoexport --help) : --helpproduce 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 argserver port. Can also use --host hostname:port --ipv6enable 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 ] argdatabase to use -c [ --collection ] argcollection to use (some commands) -f [ --fields ] argcomma separated list of field names e.g. -f name,age --fieldFile argfile with fields names - 1 per line -q [ --query ] argquery filter, as a JSON string --csv export to csv instead of json -o [ --out ] arg output file; if not specified, stdout is used --jsonArrayoutput 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 restoration:
Es231en-d dbhost c collectionname -type: indicates the file format to be imported -headerline: specifies that the first line is not imported because the first line is column name
-file: indicates the file path to be imported

Official description of mongoimport (available through mongoimport --help) : --help produce help message -v [ --verbose ]be more verbose (include multiple times for more verbosity e.g. -vvvvv) --versionprint the program's version and exit -h [ --host ] argmongo 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 ] argusername -p [ --password ] argpassword --dbpath argdirectly 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 --directoryperdbif dbpath specified, each db is in a separate directory --journalenable 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 --headerlineCSV,TSV only - use first line as headers --upsertinsert 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 --stopOnErrorstop 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 specifies the library to use, 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 specifies the library to use, in this case "my_mongodb"

-c indicates the table to be exported, in this case "user"

-o specifies the file name to export, in this case "user.dat"

As you can see above, the export method USES the JSON style.


Related articles: