第48章:MongoDB-备份和恢复

①文件系统快照(snapshot)

生成文件系统快照这个方法需要满足两个条件:

1:文件系统本身支持快照技术

2:运行mongod时必须开启日记系统

   其恢复就是快照的恢复,当然,恢复的时候,确保mongod没有开启

②复制文件系统

冷备份:就是把mongod停了,然后拷贝相应的数据文件,最好是把一个数据库相应的数据文件夹完整的拷出,然后恢复的时候完整的拷入。--最简单的

 

热备份:就是mongod在运行中,可以使用db.fsyncLock();来锁定数据库,然后进行数据文件的拷贝,拷贝完成后,使用db.fsyncUnLock();解除锁定。恢复的时候,需要把mongod停了,然后把文件拷入。

 

③使用mongodump来备份

这个方式有些缺点,比如速度慢,处理副本集的时候也很容易出问题。但对于单独的数据库和集合还是一个好选择的。

 

mongodump的参数与mongoexport的参数基本一致 

参数

参数说明

-h

指明数据库宿主机的IP

-u

指明数据库的用户名

-p

指明数据库的密码

-d

指明数据库的名字

-c

指明collection的名字

-o

指明到要导出的文件名

-q

指明导出数据的过滤条件

--authenticationDatabase

验证数据的名称

--gzip

备份时压缩

--oplog

use oplog for taking a point-in-time snapshot

mongodump参数实践

全库备份

mongodump -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -o /home/mongod/backup/full

备份test库

mongodump -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -o /home/mongod/backup/

备份test库下的vast集合

mongodump -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -c vast -o /home/mongod/backup/

压缩备份库

mongodump -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -o /home/mongod/backup/ --gzip

压缩备份单表

mongodump -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -c vast -o /home/mongod/backup/ --gzip

 

 

④使用mongorestore来恢复

 

mongorestore与mongoimport参数类似 

参数

参数说明

-h

指明数据库宿主机的IP

-u

指明数据库的用户名

-p

指明数据库的密码

-d

指明数据库的名字

-c

指明collection的名字

-o

指明到要导出的文件名

-q

指明导出数据的过滤条件

--authenticationDatabase

验证数据的名称

--gzip

备份时压缩

--oplog

use oplog for taking a point-in-time snapshot

--drop

恢复的时候把之前的集合drop

全库备份中恢复单库(基于之前的全库备份)

mongorestore -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test --drop /home/mongod/backup/full/test/

恢复test库

mongorestore -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test /home/mongod/backup/test/

恢复test库下的vast集合

mongorestore -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -c vast /home/mongod/backup/test/vast.bson

--drop参数实践恢复

# 恢复单库 mongorestore -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test --drop /home/mongod/backup/test/ # 恢复单表 mongorestore -h 10.0.0.152:27017 -uroot -proot --authenticationDatabase admin -d test -c vast --drop /home/mongod/backup/test/vast.bson

 

⑤mongodb的备份准则

只针对replica或master/slave,满足这些准则MongoDB就可以进行point-in-time恢复操作:

1任意两次数据备份的时间间隔(第一次备份开始到第二次备份结束)不能超过oplog时间窗口覆盖范围。

2在上次数据备份的基础上,在oplog时间窗口没有滑出上次备份结束的时间点前进行完整的oplog备份。请充分考虑oplog备份需要的时间,权衡服务器空间情况确定oplog备份间隔。

实际应用中的注意事项:

  1. 考虑到oplog时间窗口是个变化值,请关注oplog时间窗口的具体时间。
  2. 在靠近oplog时间窗口滑动出有效时间之前必须要有足够的时间dump出需要的oplog.rs,请预留足够的时间,不要顶满时间窗口再备份。
  3. 当灾难发生时,第一件事情就是要停止数据库的写入操作,以往oplog滑出时间窗口。特别是像上述这样的remove({})操作,瞬间就会插入大量d记录从而导致oplog迅速滑出时间窗口。

分片集群的备份注意事项

1、备份什么?

  (1)configserver

  (2)每一个shard节点

2、备份需要注意什么?

  (1)元数据和真实数据要有对等性(blancer迁移的问题,会造成config和shard备份不一致),要进行分片集备份,需要先关闭均衡器。

   (2)不同部分备份结束时间点不一样,恢复出来的数据就是有问题的。建议对备份节点进行备份,最好是采用复制文件系统的方式。

 

附录:Aliyun备份策略

1 MongoDB云数据库备份/恢复


 

备份策略:

  1. 从hidden节点备份
  2. 每天一次全量备份
  3. 持续拉取oplog增量备份
  4. 定期巡检备份有效性
  5. 恢复时克隆到新实例

2 全量备份方法

 

3 逻辑备份流程 - mongodump

 

特点:

  1. 全量遍历所有数据、
  2. 备份、恢复慢
  3. 对业务影响较大
  4. 无需备份索引、恢复时重建
  5. 通用性强

4 物理备份流程


 

备份特点

  1. 拷贝数据目录所有文件,效率高
  2. 备份、恢复快
  3. 对业务影响较小
  4. 跟数据库版本、配置强关联

5 逻辑备份 vs 物理备份

 

逻辑备份

物理备份

备份效率

数据库接口读取数据

拷贝物理文件

恢复效率

下载备份集 +  导入数据 +  建立索引

下载备份集 +  启动进程

备份影响

直接与业务争抢资源

备份集大小

比原库小

无需备份索引数据

与原库相同

兼容性

兼容绝大部分版本

可跨存储引擎

依赖存储布局

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Lucky-stars/p/10598426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值