Introduction to backup and recovery of mongodb
I. introduction of the installation environment:
The installation environment of the demo mongo:
Binary installation package mongoDB3.6.16
Authorize the Super Admin account to mongodb:
Mongo-- host 127.0.0.1-- port 6068db.createUser ({user: 'root', pwd:'TdLLQ6689', roles: [{role:' root', db: 'admin'}]}); use admindb.auth ("root", "TdLLQ6689")
Build database, build table and simulate data:
Use dbtest001db.chenji.insert ({"name": "floret", "grade": "sophomore", "gender": "male", "hobby": "learning"}) use dbtest002db.xiangmu.insert ({"name": "floret", "grade": "sophomore", "gender": "male", "hobby": "learning"}) db.mumu.insert ({"name": "floret", "grade": "second grade", "gender": "male" "hobby": "study"})
Tip: this demonstration backup and recovery mainly uses the super account root to demonstrate.
2. Mongodump backup command introduction: 2.1Syntax and parameter description:
Mongodump-h dbhost-u xxx-p xxx-d dbname-o dbdirectory
Parameter description:
-h indicates the IP of the database host
-- port indicates the port of the database
-u indicates the user name of the database
-p indicates the password of the database
-d indicates the name of the database
-c specify the name of collection
-o indicate the name of the file to be exported
-Q indicates the filtering criteria for the exported data
-- name of authenticationDatabase authentication data
-- compress gzip during backup
-- oplog use oplog for taking a point-in-time snapshot
Create a backup directory for the demo in advance:
Mkdir full # full backup
Mkdir single_db # single library backup
Table backup in mkdir single_db_col # Library
Mkdir single_db.gzip # Library backup Compression
Table backup Compression in mkdir single_db_col.gzip # Library
2.2 full backup: [root@localhost ~] # mongodump-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-o / root/full2020-01-04T18:13:18.271+0800 writing admin.system.users to 2020-01-04T18:13:18.272+0800 done dumping admin.system.users (7 documents) 2020-01-04T18:13:18.272+0800 writing admin.system.version to 2020-01-04T18:13:18.273+0800 done dumping admin.system.version ( 2 documents) 2020-01-04T18:13:18.273+0800 writing dbtest001.chenji to 2020-01-04T18:13:18.273+0800 writing dbtest002.mumu to 2020-01-04T18:13:18.273+0800 writing dbtest002.xiangmu to 2020-01-04T18:13:18.274+0800 done dumping dbtest002.mumu (1 document) 2020-01-04T18:13:18.274+0800 done dumping dbtest001.chenji (2 documents) 2020-01-04T18:13:18.290+0800 done dumping dbtest002.xiangmu (2 documents) [root@ Localhost ~] # cd full/ [root@localhost full] # lsadmin dbtest001 dbtest0022.3 single database backup:
Failed to use super account root backup backup:
[root@localhost] # mongodump-h 127.0.0.1 root-p TdLLQ6689-d dbtest001-o / root/single_db2020-01-04T18:22:20.087+0800 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
The solution is as follows:
The first method: add-- authenticationDatabase admin
[root@localhost] # mongodump-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest001-o / root/single_db2020- 01-04T18:37:46.926+0800 writing dbtest001.chenji to 2020-01-04T18:37:46.927+0800 done dumping dbtest001.chenji (2 documents) [root@localhost] # mongodump-h 127.0.0.1 root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-o / root/single_db2020- 01-04T18:37:54.253+0800 writing dbtest002.xiangmu to 2020-01-04T18:37:54.253+0800 writing dbtest002.mumu to 2020-01-04T18:37:54.254+0800 done dumping dbtest002.xiangmu (2 documents) 2020-01-04T18:37:54.254+0800 done dumping dbtest002.mumu (1 document) [root@localhost] # tree / root/single_db/root/single_db ├── dbtest001 │ ├── chenji.bson │ └── chenji.metadata.json └─ ─ dbtest002 ├── mumu.bson ├── mumu.metadata.json ├── xiangmu.bson └── xiangmu.metadata.json2 directories 6 files
The second method: backup for read and write permissions granted by a separate library
[root@localhost ~] # mongo-- host 127.0.0.1-- port 6068MongoDB shell version v3.6.16connecting to: mongodb://127.0.0.1:6068/?gssapiServiceName=mongodbImplicit session: session {"id": UUID ("93f83e64-71ca-4b1d-b599-508b7fba8722")} MongoDB server version: 3.6.16 > use dbtest001 > db.auth ("root", "TdLLQ6689") > use dbtest001 > db.createUser ({user: 'backupuser', pwd:'TdLLQ6689', roles: [{role:' readWrite' Db: 'dbtest001'}]}) > use dbtest002 > db.createUser ({user: 'backupuser', pwd:'TdLLQ6689', roles: [{role:' readWrite', db: 'dbtest002'}]}) [root@localhost] # mongodump-h 127.0.0.1 TdLLQ6689 6068-u backupuser-p TdLLQ6689-d dbtest001-o / root/single_db2020-01-04T18:24:15.243+0800 writing dbtest001.chenji to 2020-01-04T18:24:15.244+0800 done dumping dbtest001.chenji (2 documents) # ll / root/single_db/dbtest001/chenji.*-rw-r--r-- 1 root root 192 January 4 18:24 / root/single_db / dbtest001/chenji.bson-rw-r--r-- 1 root root 130 January 4 18:24 / root/single_db/dbtest001/ chenji.metadata.json [root @ localhost ~] # mongodump-h 127.0.0.1 root/single_db/dbtest001/ 6068-u backupuser-p TdLLQ6689-d dbtest002-o / root/single_db2020-01-04T18:28:39.523+0800 writing dbtest002.xiangmu to 2020-01-04T18:28:39.523+0800 writing dbtest002.mumu to 2020-01-04T18:28:39. 5240800 done dumping dbtest002.xiangmu (2 documents) 2020-01-04T18:28:39.524+0800 done dumping dbtest002.mumu (1 document) [root@localhost ~] # cd / root/single_ DB [root @ localhost single_db] # lsdbtest001 dbtest0022.4 backup single table specified in the single library: [root@localhost ~] # mongodump-h 127.0.0.1 root 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c xiangmu-o / root/single_db_col2020-01- 04T18:34:30.260+0800 writing dbtest002.xiangmu to 2020-01-04T18:34:30.261+0800 done dumping dbtest002.xiangmu (2 documents) [root@localhost ~] # ll / root/single_db_col/dbtest002/ Total usage 8MerrwWhitWhile-1 root root 192 January 4 18:34 xiangmu.bson-rw-r--r-- 1 root root 131 January 4 18:34 xiangmu.metadata.json2.5 database backup compression: 2.5.1 library Backup compression: [root@localhost ~] # mongodump-h 127.0.0.1 TdLLQ6689 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-o / root/single_db-- gzip2020-01-04T18:44:28.558+0800 writing dbtest002.xiangmu to 2020-01-04T18:44:28.559+0800 writing dbtest002.mumu to 2020-01-04T18:44:28.560+0800 done dumping dbtest002.xiangmu (2 documents) 2020-01-04T18:44:28.561+0800 done dumping dbtest002. Mumu (1 document) [root@localhost ~] # tree single_dbsingle_db └── dbtest002 ├── mumu.bson.gz ├── mumu.metadata.json.gz ├── xiangmu.bson.gz └── xiangmu.metadata.json.gz1 directory 4 Table backup compression in files2.5.2 library: [root@localhost ~] # mongodump-h 127.0.0.1 root 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest001-o / root/single_db_col.gzip-- gzip2020-01-04T18:41:08.459+0800 writing dbtest001.chenji to 2020-01-04T18:41:08.460+0800 done dumping dbtest001.chenji (2 documents) [root@localhost dbtest001] # ll / root/single_db_col.gzip/dbtest001 Total amount 8 root 4 18:41 chenji.metadata.json.gz-1 root root 4 18:41 chenji.metadata.json.gz 3, Mongorestore restore backup data command introduction 3.1 syntax and parameters introduction:
Mongorestore-h-d dbname
-- host,-h: server address where mongoDB resides. Default is: localhost:27017.
-h indicates the IP of the database host
-u indicates the user name of the database
-p indicates the password of the database
-d indicate the name of the database # # database to use when restoring from a BSON file
-c indicates the name of collection # # collection to use when restoring from a BSON file
-o indicate the name of the file to be exported
-Q indicates the filtering criteria for the exported data
-- name of authenticationDatabase authentication data
-- compress gzip during backup
-- oplog use oplog for taking a point-in-time snapshot
-- drop the previous collection when drop recovers
3.2 restore to the library using a full backup:
Tip: using a full backup to restore directly to the online library will prompt key conflicts. When restoring, you should use the parameter-- drop to drop the previous collection.
[root@localhost] # mongorestore-h 127.0.0.1 root-p TdLLQ6689-- authenticationDatabase admin. / full/2020-01-04T18:55:43.409+0800 preparing collections to restore from2020-01-04T18:55:43.414+0800 reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json2020-01-04T18:55:43.414+0800 restoring dbtest001.chenji from full/dbtest001/chenji.bson2020-01-04T18:55:43.415+0800 reading metadata for dbtest002.xiangmu from full / dbtest002/xiangmu.metadata.json2020-01-04T18:55:43.415+0800 reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json2020-01-04T18:55:43.415+0800 restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson2020-01-04T18:55:43.416+0800 restoring dbtest002.mumu from full/dbtest002/mumu.bson2020-01-04T18:55:43.418+0800 error: multiple errors in bulk operation:-E11000 duplicate key error collection: dbtest002.xiangmu index: _ id_ dup key: {: ObjectId ('5e0f16191083b09e85237cb2')}-E11000 duplicate key error collection: dbtest002.xiangmu index: _ id_ dup key: {: ObjectId (' 5e0f161d1083b09e85237cb3')} 2020-01-04T18:55:43.418+0800 no indexes to restore2020-01-04T18:55:43.418+0800 finished restoring dbtest002.xiangmu (2 documents) 2020-01-04T18:55:43.436+0800 error: multiple errors in bulk operation:-E11000 duplicate key error collection: dbtest001.chenji index: _ id_ dup key: {: ObjectId ('5e0f150150fe973d9e9051f4')}-E11000 duplicate key error collection: dbtest001.chenji index: _ id_ dup key: {: ObjectId (' 5e0f150750fe973d9e9051f5')} 2020-01-04T18:55:43.436+0800 no indexes to restore2020-01-04T18:55:43.436+0800 finished restoring dbtest001.chenji (2 documents) 2020-01-04T18:55:43.436+0800 error: E11000 duplicate key error collection: dbtest002.mumu index: _ id_ dup key: {: ObjectId ('5e0f162d1083b09e85237cb4') ')} 2020-01-04T18:55:43.436+0800 no indexes to restore2020-01-04T18:55:43.436+0800 finished restoring dbtest002.mumu (1 document) 2020-01-04T18:55:43.436+0800 restoring users from full/admin/system.users.bson2020-01-04T18:55:43.447+0800 done
Delete the database and restore:
> use dbtest001switched to db dbtest001 > db.dropDatabase () {"dropped": "dbtest001", "ok": 1} > use dbtest002switched to db dbtest002 > db.dropDatabase () {"dropped": "dbtest002" "ok": 1} [root@localhost ~] # mongorestore-h 127.0.0.1 mongorestore-u root-p TdLLQ6689-- authenticationDatabase admin. / full/2020-01-04T18:59:29.633+0800 preparing collections to restore from2020-01-04T18:59:29.639+0800 reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json2020-01-04T18:59:29.639+0800 reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json2020-01-04T18:59: 29.640 0800 reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json2020-01-04T18:59:29.646+0800 restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson2020-01-04T18:59:29.648+0800 no indexes to restore2020-01-04T18:59:29.648+0800 finished restoring dbtest002.xiangmu (2 documents) 2020-01-04T18:59:29.664+0800 restoring dbtest001.chenji from full/dbtest001/chenji.bson2020-01-04T18:59:29.677+0800 restoring dbtest002.mumu from full/ Dbtest002/mumu.bson2020-01-04T18:59:29.690+0800 no indexes to restore2020-01-04T18:59:29.690+0800 finished restoring dbtest001.chenji (2 documents) 2020-01-04T18:59:29.690+0800 no indexes to restore2020-01-04T18:59:29.690+0800 finished restoring dbtest002.mumu (1 document) 2020-01-04T18:59:29.690+0800 restoring users from full/admin/system.users.bson2020-01-04T18:59:29.701+0800 done
Add the parameter-- drop to restore:
[root@localhost] # mongorestore-h 127.0.0.1 root-p TdLLQ6689-- authenticationDatabase admin-- drop. / full/2020-01-04T19:03:04.854+0800 preparing collections to restore from2020-01-04T19:03:04.859+0800 reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json2020-01-04T19:03:04.864+0800 restoring dbtest001.chenji from full/dbtest001/chenji.bson2020-01-04T19:03:04.866+0800 no indexes to restore2020-01 -04T19:03:04.866+0800 finished restoring dbtest001.chenji (2 documents) 2020-01-04T19:03:04.879+0800 reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json2020-01-04T19:03:04.880+0800 reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json2020-01-04T19:03:04.884+0800 restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson2020-01-04T19:03:04.898+0800 restoring dbtest002.mumu from full/dbtest002/mumu.bson2020- 01-04T19:03:04.899+0800 no indexes to restore2020-01-04T19:03:04.899+0800 finished restoring dbtest002.xiangmu (2 documents) 2020-01-04T19:03:04.915+0800 no indexes to restore2020-01-04T19:03:04.915+0800 finished restoring dbtest002.mumu (1 document) 2020-01-04T19:03:04.915+0800 restoring users from full/admin/system.users.bson2020-01-04T19:03:04.928+0800 done3.3 restores the backed up database table files to the specified database table:
-d indicates the name of the database and the data file of the backed up database must be a file in BSON format
[root@localhost] # mongorestore-h 127.0.0.1 root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002. / full/dbtest002/2020-01-05T07:36:24.293+0800 the-- db and-- collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future Use-- nsInclude instead2020-01-05T07:36:24.293+0800 building a list of collections to restore from full/dbtest002 dir2020-01-05T07:36:24.294+0800 reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json2020-01-05T07:36:24.295+0800 reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json2020-01-05T07:36:24.299+0800 restoring dbtest002.mumu from full/dbtest002/mumu.bson2020-01-05T07:36:24.303+0800 no indexes to restore2020 -01-05T07:36:24.303+0800 finished restoring dbtest002.mumu (1 document) 2020-01-05T07:36:24.319+0800 restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson2020-01-05T07:36:24.321+0800 no indexes to restore2020-01-05T07:36:24.321+0800 finished restoring dbtest002.xiangmu (2 documents) 2020-01-05T07:36:24.321+0800 done
-c indicates the name of the collection and the collection data file of the backed up database must be a file in BSON format
[root@localhost] # mongorestore-h 127.0.0.1 root 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu. / full/dbtest002/mumu.bson2020- 01-05T07:45:53.367+0800 checking for collection data in full/dbtest002/mumu.bson2020- 01-05T07:45:53.368+0800 reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json2020-01-05T07:45:53.373+0800 restoring dbtest002.mumu from full/dbtest002/mumu.bson2020- 01-05T07:45:53.436+0800 no indexes to restore2020-01-05T07:45:53.436+0800 finished restoring dbtest002.mumu (1 document) 2020-01-05T07:45:53.436+0800 done [root@localhost ~] # [root@localhost ~] # mongorestore-h 127.0.0.1 document 6068-u root-p TdLLQ6689-- authenticationDatabase admin-d dbtest002-c mumu. / full/dbtest002/xiangmu.bson 2020-01-05T07:46:35.517+0800 checking for collection data in full/dbtest002 / xiangmu.bson2020-01-05T07:46:35.519+0800 reading metadata for dbtest002.mumu from full/dbtest002/xiangmu.metadata.json2020-01-05T07:46:35.519+0800 restoring dbtest002.mumu from full/dbtest002/xiangmu.bson2020-01-05T07:46:35.582+0800 no indexes to restore2020-01-05T07:46:35.582+0800 finished restoring dbtest002.mumu (2 documents) 2020-01-05T07:46:35.582+0800 done