分布式文档存储数据库之MongoDB备份与恢复的实践详解

时间:2021-05-02

  前文我们聊了下分布式文档存储数据库之MongoDB访问控制,回顾请参考http://www.zzvips.com/article/126699.html;今天我们来了解下mongodb的备份与恢复

  为什么要备份?

  备份的目的是对数据做冗余的一种方式,它能够让我们在某种情况下保证最少数据的丢失;之前我们对mongodb做副本集也是对数据做冗余,但是这种在副本集上做数据冗余仅仅是针对系统故障或服务异常等一些非人为的故障发生时,保证数据服务的可用性;它不能够避免人为的误操作;为了使得数据的安全,将数据损失降低到最小,我们必须对数据库周期性的做备份;

  常用备份方法

  提示:上图主要描述了mongodb数据库上常用备份策略,我们可以逻辑备份,逻辑备份是将数据库中的数据导出成语句,通常使用专用工具导出和导入来完成一次备份与恢复;其次我们也可以物理备份,简单讲物理备份就是把数据库文件打包,备份;恢复时直接将对应的数据库文件解压恢复即可;另外一种快速物理备份的方式就是给数据拍快照,拍快照可以将数据保存为当前拍快照时的状态;如果我们要进行恢复直接恢复快照即可;

  mongodb逻辑备份和物理备份比较

  提示:从上图描述可以看出总体上来看物理备份效率和恢复效率要高于逻辑;物理备份效率高于逻辑备份,其主要原因是逻辑备份是通过数据库接口将数据读取出来,然后保存为对应格式的文件,而物理备份只需要将数据文件直接打包备份,不需要一条一条的读取数据,然后写入到其他文件,这中间就省去了读写过程,所以物理备份效率高;恢复也是类似的过程,物理恢复也是省去了读写的过程;

  mongodb逻辑备份工具

  在mongodb中使用逻辑备份的工具有两组,第一组是mongodump/mongorestore,使用mongodump/mongorestore这组工具来逻辑的备份数据,它备份出来的数据是BSON格式,BSON是一种二进制格式,通常无法使用文本编辑器直接打开查看其内容,对人类的可读性较差,但它的优点是保存的文件体积要小;使用这组命令导出的数据,在恢复是依赖mongodb版本,不同版本导出的BSON格式略有不同,所以恢复时,可能存在版本不同而导致恢复数据失败的情况;另外一组是mongoexport/mongoimport,这组工具导出的数据是json格式的数据,通常我们可以使用文本编辑器打开直接查看,对人类的可读性较好,但体积相对BSON格式的数据要大,恢复时不依赖版本;所以跨版本备份要先查看下对应版本的兼容性,如果兼容使用mongodump/mongorestore,不兼容的话建议使用mongoexport/mongoimport;这里需要注意一点,JSON格式虽然可读性很好,也很通用,但是它只是保留了数据部分,而没有保留索引,账户等基础信息,在使用是应该注意;

  使用mongodump备份数据

  插入数据

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > use testdb switched to db testdb > for(i=1;i<=1000;i++) db.test.insert({id:i,name:"test"+i,age:(i%120),classes:(i%25)}) WriteResult({ "nInserted" : 1 }) > show tables test > db.test.findOne() { "_id" : ObjectId("5fb130da012870b3c8e3c4ad"), "id" : 1, "name" : "test1", "age" : 1, "classes" : 1 } > db.test.count() 1000 >

  备份所有数据库

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -o ./node12_mongodb_full_backup 2020-11-15T21:47:45.439+0800 writing admin.system.users to node12_mongodb_full_backup/admin/system.users.bson 2020-11-15T21:47:45.442+0800 done dumping admin.system.users (4 documents) 2020-11-15T21:47:45.443+0800 writing admin.system.version to node12_mongodb_full_backup/admin/system.version.bson 2020-11-15T21:47:45.447+0800 done dumping admin.system.version (2 documents) 2020-11-15T21:47:45.448+0800 writing testdb.test to node12_mongodb_full_backup/testdb/test.bson 2020-11-15T21:47:45.454+0800 done dumping testdb.test (1000 documents) [root@node11 ~]# ls node12_mongodb_full_backup [root@node11 ~]# ll node12_mongodb_full_backup/ total 0 drwxr-xr-x 2 root root 128 Nov 15 21:47 admin drwxr-xr-x 2 root root 49 Nov 15 21:47 testdb [root@node11 ~]# tree node12_mongodb_full_backup/ node12_mongodb_full_backup/ ├── admin │ ├── system.users.bson │ ├── system.users.metadata.json │ ├── system.version.bson │ └── system.version.metadata.json └── testdb ├── test.bson └── test.metadata.json 2 directories, 6 files [root@node11 ~]#

  提示:-u用于指定用户,-p指定对应用户的密码,-h指定数据库地址,--authenticationDatabase 指定验证用户和密码对应的数据库 -o指定要存放备份文件的目录名称;

  只备份单个testdb数据库

? 1 2 3 4 5 6 7 8 9 10 11 [root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -o ./node12_testdb 2020-11-15T21:53:36.523+0800 writing testdb.test to node12_testdb/testdb/test.bson 2020-11-15T21:53:36.526+0800 done dumping testdb.test (1000 documents) [root@node11 ~]# tree ./node12_testdb ./node12_testdb └── testdb ├── test.bson └── test.metadata.json 1 directory, 2 files [root@node11 ~]#

  提示:-d用户指定要备份的数据库名称;

  只备份testdb下的test集合

? 1 2 3 4 5 6 7 8 9 10 11 [root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -c test -o ./node12_testdb_test-collection 2020-11-15T21:55:48.217+0800 writing testdb.test to node12_testdb_test-collection/testdb/test.bson 2020-11-15T21:55:48.219+0800 done dumping testdb.test (1000 documents) [root@node11 ~]# tree ./node12_testdb_test-collection ./node12_testdb_test-collection └── testdb ├── test.bson └── test.metadata.json 1 directory, 2 files [root@node11 ~]#

  提示:-c用于指定要备份的集合(collection)名称;

  压缩备份testdb库

? 1 2 3 4 5 6 7 8 9 10 11 [root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb --gzip -o ./node12_mongodb_testdb-gzip 2020-11-15T22:00:52.268+0800 writing testdb.test to node12_mongodb_testdb-gzip/testdb/test.bson.gz 2020-11-15T22:00:52.273+0800 done dumping testdb.test (1000 documents) [root@node11 ~]# tree ./node12_mongodb_testdb-gzip ./node12_mongodb_testdb-gzip └── testdb ├── test.bson.gz └── test.metadata.json.gz 1 directory, 2 files [root@node11 ~]#

  提示:可以看到使用压缩,只需要加上--gzip选项即可,备份出来的数据就是.gz后缀结尾的压缩文件;

  压缩备份testdb库下的test集合

? 1 2 3 4 5 6 7 8 9 10 11 [root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -c test --gzip -o ./node12_mongodb_testdb-test-gzip 2020-11-15T22:01:31.492+0800 writing testdb.test to node12_mongodb_testdb-test-gzip/testdb/test.bson.gz 2020-11-15T22:01:31.500+0800 done dumping testdb.test (1000 documents) [root@node11 ~]# tree ./node12_mongodb_testdb-test-gzip ./node12_mongodb_testdb-test-gzip └── testdb ├── test.bson.gz └── test.metadata.json.gz 1 directory, 2 files [root@node11 ~]#

  使用mongorestore恢复数据

  在node12上删除testdb

? 1 2 3 4 5 6 7 8 9 > db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 } > show dbs admin 0.000GB config 0.000GB local 0.000GB >

  全量恢复所有数据库

? 1 2 3 4 5 6 7 8 9 [root@node11 ~]# mongorestore -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin --drop ./node12_mongodb_full_backup 2020-11-15T22:07:35.465+0800 preparing collections to restore from 2020-11-15T22:07:35.467+0800 reading metadata for testdb.test from node12_mongodb_full_backup/testdb/test.metadata.json 2020-11-15T22:07:35.475+0800 restoring testdb.test from node12_mongodb_full_backup/testdb/test.bson 2020-11-15T22:07:35.486+0800 no indexes to restore 2020-11-15T22:07:35.486+0800 finished restoring testdb.test (1000 documents, 0 failures) 2020-11-15T22:07:35.486+0800 restoring users from node12_mongodb_full_backup/admin/system.users.bson 2020-11-15T22:07:35.528+0800 1000 document(s) restored successfully. 0 document(s) failed to restore. [root@node11 ~]#

 提示:--drop用于指定,恢复是如果对应数据库或者colleciton存在,则先删除然后在恢复,这样做的目的是保证恢复的数据和备份的数据一致;

  验证:登录192.168.0.52:27017查看对应testdb数据库是否恢复?

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 [root@node11 ~]# mongo -utom -p123456 192.168.0.52:27017/admin MongoDB shell version v4.4.1 connecting to: mongodb://192.168.0.52:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("af96cb64-a2a4-4d59-b60a-86ccbbe77e3e") } MongoDB server version: 4.4.1 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com --- The server generated these startup warnings when booting: 2020-11-15T20:42:23.774+08:00: ***** SERVER RESTARTED ***** 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' --- --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB testdb 0.000GB > use testdb switched to db testdb > show collections test > db.test.count() 1000 > db.test.findOne() { "_id" : ObjectId("5fb130da012870b3c8e3c4ad"), "id" : 1, "name" : "test1", "age" : 1, "classes" : 1 } >

  恢复单个库

  删除testdb库

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 > show dbs admin 0.000GB config 0.000GB local 0.000GB testdb 0.000GB > use testdb switched to db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 } > show dbs admin 0.000GB config 0.000GB local 0.000GB >

 使用mongorestore恢复testdb库

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [root@node11 ~]# mongorestore -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb --drop ./node12_testdb/testdb/ 2020-11-15T22:29:03.718+0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-15T22:29:03.718+0800 building a list of collections to restore from node12_testdb/testdb dir 2020-11-15T22:29:03.719+0800 reading metadata for testdb.test from node12_testdb/testdb/test.metadata.json 2020-11-15T22:29:03.736+0800 restoring testdb.test from node12_testdb/testdb/test.bson 2020-11-15T22:29:03.755+0800 no indexes to restore 2020-11-15T22:29:03.755+0800 finished restoring testdb.test (1000 documents, 0 failures) 2020-11-15T22:29:03.755+0800 1000 document(s) restored successfully. 0 document(s) failed to restore. [root@node11 ~]# mongo -utom -p123456 192.168.0.52:27017/admin MongoDB shell version v4.4.1 connecting to: mongodb://192.168.0.52:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("f5e73939-bb87-4d45-bf80-9ff1e7f6f15d") } MongoDB server version: 4.4.1 --- The server generated these startup warnings when booting: 2020-11-15T20:42:23.774+08:00: ***** SERVER RESTARTED ***** 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' --- --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB testdb 0.000GB > use testdb switched to db testdb > show tables test > db.test.count() 1000 > db.test.findOne() { "_id" : ObjectId("5fb130da012870b3c8e3c4ad"), "id" : 1, "name" : "test1", "age" : 1, "classes" : 1 } >

  恢复单个集合

  删除testdb下的test集合

? 1 2 3 4 5 6 7 8 > db testdb > show collections test > db.test.drop() true > show collections >

  使用mongorestore恢复testdb下的test集合

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 [root@node11 ~]# mongorestore -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -c test --drop ./node12_testdb_test-collection/testdb/test.bson 2020-11-15T22:36:15.615+0800 checking for collection data in node12_testdb_test-collection/testdb/test.bson 2020-11-15T22:36:15.616+0800 reading metadata for testdb.test from node12_testdb_test-collection/testdb/test.metadata.json 2020-11-15T22:36:15.625+0800 restoring testdb.test from node12_testdb_test-collection/testdb/test.bson 2020-11-15T22:36:15.669+0800 no indexes to restore 2020-11-15T22:36:15.669+0800 finished restoring testdb.test (1000 documents, 0 failures) 2020-11-15T22:36:15.669+0800 1000 document(s) restored successfully. 0 document(s) failed to restore. [root@node11 ~]# mongo -utom -p123456 192.168.0.52:27017/admin MongoDB shell version v4.4.1 connecting to: mongodb://192.168.0.52:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("27d15d9e-3fdf-4efc-b871-1ec6716e51e3") } MongoDB server version: 4.4.1 --- The server generated these startup warnings when booting: 2020-11-15T20:42:23.774+08:00: ***** SERVER RESTARTED ***** 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' --- --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB testdb 0.000GB > use testdb switched to db testdb > show collections test > db.test.count() 1000 > db.test.findOne() { "_id" : ObjectId("5fb130da012870b3c8e3c4ad"), "id" : 1, "name" : "test1", "age" : 1, "classes" : 1 } >

 使用压缩文件恢复数据库

  删除testdb数据库

? 1 2 3 4 5 6 7 8 9 > db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 } > show dbs admin 0.000GB config 0.000GB local 0.000GB >

  使用mongorestore工具加载压缩文件恢复数据库

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 [root@node11 ~]# mongorestore -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb --gzip --drop ./node12_mongodb_testdb-gzip/testdb/ 2020-11-15T22:39:55.313+0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-15T22:39:55.313+0800 building a list of collections to restore from node12_mongodb_testdb-gzip/testdb dir 2020-11-15T22:39:55.314+0800 reading metadata for testdb.test from node12_mongodb_testdb-gzip/testdb/test.metadata.json.gz 2020-11-15T22:39:55.321+0800 restoring testdb.test from node12_mongodb_testdb-gzip/testdb/test.bson.gz 2020-11-15T22:39:55.332+0800 no indexes to restore 2020-11-15T22:39:55.332+0800 finished restoring testdb.test (1000 documents, 0 failures) 2020-11-15T22:39:55.332+0800 1000 document(s) restored successfully. 0 document(s) failed to restore. [root@node11 ~]# mongo -utom -p123456 192.168.0.52:27017/admin MongoDB shell version v4.4.1 connecting to: mongodb://192.168.0.52:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("73d98c33-f8f7-40e3-89bd-fda8c702e407") } MongoDB server version: 4.4.1 --- The server generated these startup warnings when booting: 2020-11-15T20:42:23.774+08:00: ***** SERVER RESTARTED ***** 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' --- --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB testdb 0.000GB > use testdb switched to db testdb > show collections test > db.test.count() 1000 > db.test.findOne() { "_id" : ObjectId("5fb130da012870b3c8e3c4ad"), "id" : 1, "name" : "test1", "age" : 1, "classes" : 1 } >

  提示:使用mongorestore恢复单个库使用-d选线指定要恢复的数据库,恢复单个集合使用-c指定集合名称即可,以及使用压缩文件恢复加上对应的--gzip选项即可,总之,备份时用的选项在恢复时也应当使用对应的选项,这个mongodump备份使用的选项没有特别的不同;

  使用mongoexport备份数据

  新建peoples数据库,并向peoples_info集合中插入数据

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 > use peoples switched to db peoples > for(i=1;i<=10000;i++) db.peoples_info.insert({id:i,name:"peoples"+i,age:(i%120),classes:(i%25)}) WriteResult({ "nInserted" : 1 }) > show dbs admin 0.000GB config 0.000GB local 0.000GB peoples 0.000GB testdb 0.000GB > db.peoples_info.count() 10000 > db.peoples_info.findOne() { "_id" : ObjectId("5fb13f35012870b3c8e3c895"), "id" : 1, "name" : "peoples1", "age" : 1, "classes" : 1 } >

  使用mongoexport工具peoples库下的peoples_info集合

? 1 2 3 4 5 6 7 8 9 [root@node11 ~]# mongoexport -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d peoples -c peoples_info --type json -o ./peoples-peopels_info.json 2020-11-15T22:54:18.287+0800 connected to: mongodb://192.168.0.52:27017/ 2020-11-15T22:54:18.370+0800 exported 10000 records [root@node11 ~]# ll total 1004 -rw-r--r-- 1 root root 1024609 Nov 15 22:54 peoples-peopels_info.json [root@node11 ~]# head -n 1 peoples-peopels_info.json {"_id":{"$oid":"5fb13f35012870b3c8e3c895"},"id":1.0,"name":"peoples1","age":1.0,"classes":1.0} [root@node11 ~]#

  提示:使用--type可以指定导出数据文件的格式,默认是json格式,当然也可以指定csv格式;这里还需要注意mongoexport这个工具导出数据必须要指定数据库和对应集合,它不能直接对整个数据库下的所有集合做导出;只能单个单个的导;

  导出csv格式的数据文件

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@node11 ~]# mongoexport -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d peoples -c peoples_info --type csv -o ./peoples-peopels_info.csv 2020-11-15T22:58:30.495+0800 connected to: mongodb://192.168.0.52:27017/ 2020-11-15T22:58:30.498+0800 Failed: CSV mode requires a field list [root@node11 ~]# mongoexport -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d peoples -c peoples_info --type csv -f id,name,age -o ./peoples-peopels_info.csv 2020-11-15T22:59:26.090+0800 connected to: mongodb://192.168.0.52:27017/ 2020-11-15T22:59:26.143+0800 exported 10000 records [root@node11 ~]# head -n 1 ./peoples-peopels_info.csv id,name,age [root@node11 ~]# head ./peoples-peopels_info.csv

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章