背景:

  最近后端基于mongo的项目越来越多,MySQL基于冷备份+binlog可以恢复至任意时间点,那么mongo是否有同样的功能呢?经过调研发现可以通过dump+oplog可以实现粒度更细致的恢复。

官方文档关于oplog的说明 : <a class="mongo-link" href="https://docs.mongodb.com/manual/core/replica-set-oplog/"></a>

  以下是实验步骤,附脚本内容。

1、 制作测试数据并且获取冷备

2、模拟线上两个冷备份之间的数据写入

3、备份oplog

4、冷备+oplog恢复至临时实例,确认没问题后,通过临时实例恢复到主库

1、

dump比较重要的几个参数
#mongodump --help
Usage:
mongodump <options> Export the content of a running server into .bson files. Specify a database with -d and a collection with -c to only dump that database or collection. See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information. connection options:
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port> server port (can also use --host hostname:port) authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use --gzip compress archive our collection output with Gzip
--oplog use oplog for taking a point-in-time snapshot

  

test_1234:PRIMARY> show dbs
admin .000GB
local .724GB
test_1234:PRIMARY> use test
switched to db test
test_1234:PRIMARY> for (var i=;i<=;i++)(db.test.insert({"test":i}))
WriteResult({ "nInserted" : })
test_1234:PRIMARY> db.test.count() 当前test.test有5001条数据,获取冷备份并查看当前时间戳

#date +%s
1553499626


2、 这个时候模拟下冷备之后的数据写入,删除一些数据并记录oplog

test_1234:PRIMARY> use test
switched to db test
test_1234:PRIMARY>
test_1234:PRIMARY>
test_1234:PRIMARY> show tables
test
test_1234:PRIMARY> for (var i=0;i<=5000;i++)(db.after.insert({"after":i}))
WriteResult({ "nInserted" : 1 })
test_1234:PRIMARY> db.test.remove({"test":{$gt:1500}})
WriteResult({ "nRemoved" : 3500 })
test_1234:PRIMARY> db.test.count()
1501
test_1234:PRIMARY> db.after.count()
5001
test_1234:PRIMARY>

#date +%s
1553499686

  

3、备份oplog

./mongoha_dump_oplog.sh 

options:
-S [Start-BSON-Timestamp]
-E [End-BSON-Timestamp]
is running like mongoha_dump_oplog.sh -S -E #

 

./mongoha_dump_oplog.sh -S 1553499626 -E 1553499686
2019-03-25T15:42:07.579+0800 writing local.oplog.rs to
2019-03-25T15:42:10.576+0800 local.oplog.rs 0
2019-03-25T15:42:13.575+0800 local.oplog.rs 0
2019-03-25T15:42:16.575+0800 local.oplog.rs 0
2019-03-25T15:42:19.575+0800 local.oplog.rs 0
2019-03-25T15:42:22.575+0800 local.oplog.rs 0
2019-03-25T15:42:25.576+0800 local.oplog.rs 0
2019-03-25T15:42:26.267+0800 local.oplog.rs 8507
2019-03-25T15:42:26.267+0800 done dumping local.oplog.rs (8507 documents)
Mongo oplog backup [2019-03-25 15:40:26 - 2019-03-25 15:41:26] -->Sucess

  

4、 新建临时实例利用刚才冷备+oplog恢复,确认数据没问题之后,恢复至主库。

1)新建临时端口
mongo_install.sh -T 1 -P 6666 -M 1
2019-03-25 15:32:57 start install
mongodb-linux-x86_64-rhel62-3.4.0/README
mongodb-linux-x86_64-rhel62-3.4.0/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel62-3.4.0/MPL-2
mongodb-linux-x86_64-rhel62-3.4.0/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodump
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongorestore
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongoexport
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongoimport
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongostat
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongotop
mongodb-linux-x86_64-rhel62-3.4.0/bin/bsondump
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongofiles
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongooplog
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongoreplay
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongoperf
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongos
mongodb-linux-x86_64-rhel62-3.4.0/bin/mongo
/usr/local/mongodb is exists ..
mongo data,log dir create sucess 2019-03-25 15:32:57 ----> mongodb6666 start sucess
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 8394
child process started successfully, parent exiting 2)恢复冷备+oplog

mongoha_fullrestore.sh

options:
-P specify the port
-p specify the restore db path ,must abspath
is running like mongoha_backup.sh -P 27017 -p /test

mongoha_fullrestore.sh -P 6666 -p /data1/backup/mongodb/2019/03/25/_2019-03-25_15\:15/
at 2019-03-25_15:34 to do restore for mongo
2019-03-25T15:34:48.505+0800 preparing collections to restore from
2019-03-25T15:34:48.506+0800 reading metadata for test.test from /data1/backup/mongodb/2019/03/25/_2019-03-25_15:15/test/test.metadata.json
2019-03-25T15:34:48.544+0800 restoring test.test from /data1/backup/mongodb/2019/03/25/_2019-03-25_15:15/test/test.bson
2019-03-25T15:34:48.573+0800 restoring indexes for collection test.test from metadata
2019-03-25T15:34:48.573+0800 finished restoring test.test (5001 documents)
2019-03-25T15:34:48.573+0800 replaying oplog
2019-03-25T15:34:48.573+0800 done
Mongo restore() -->Sucess

冷备恢复成功

临时节点查看下数据

恢复到冷备之前的数据了,但是之前删除的数据及后插入的after document还未恢复,这时候我们恢复oplog

./mongoha_restore_oplog.sh

options:
-P specify the port
-p specify the restore db path ,must abspath
is running like ./mongoha_restore_oplog.sh -P 临时端口 -p /data1/backup/mongodb/oplog/2019/03/25/local/oplog.rs.bson

再次查看临时库数据

临时库数据以恢复,这时候就可以备份出来恢复至线上了。

附mongoha_fullbackup.sh脚本

 #!/bin/bash
#auth liding@zlongame.com dat=`date "+%Y-%m-%d_%H:%M"`
day=`date "+%Y/%m/%d" -d now`
#localip=`ip a | grep "\binet\b" |awk '{print $2}'| egrep -e "^10|172" | awk -F"/" '{print $1; exit;}'`
basedir="/data1/backup/mongodb/$day" helpfunc(){
echo
echo "options:"
echo " -P specify the port"
echo " is running like mongoha_backup.sh -P 27017 -D test,test1"
} while getopts "P:" Option
do
case $Option in
P) ports=$OPTARG;;
*) helpfunc; exit 1; ;;
esac
done dbname=`echo $dbname | sed 's/,/ /g'`
dobackup(){
echo "at $dat to do bakcup for mongo$i"
mkdir -p "$basedir"
#mongodump -u$bacuser -p$pswd --port $i --oplog -o "$basedir"/mongo"$i"
if /usr/local/mongodb/bin/mongodump -h 127.0.0.1 --port $ports --oplog -o "$basedir"/"$i"_$dat;then
echo "Mongo Backup($i) -->Sucess"
else
echo "Mongo Backup($i) -->faild"
fi } check(){
person=`whoami`
if [ "$person" != "root" ]; then
echo "the user is now $person, checkout to 'root' to continue"
exit 1
fi if [ -z "$ports" ] ;then
helpfunc
exit 1
fi } cleanbackup(){
rmdate=`date "+%Y/%m/%d" -d "7 days ago"`
if [ -d $1 ] ;then
rm -rf $1
echo "rm -$1"
elif [ -d $basedir/$rmdate ] ;then
rm -rf $basedir/$rmdate
echo "rm -rf $basedir/$rmdate"
else
rm -rf $basedir/mongo$1
echo "rm -rf $basedir/mongo$1"
exit 1
fi
}
main(){ check
dobackup
}
main

  

附mongoha_fullrestore.sh脚本

mongoha_fullrestore.sh
#!/bin/bash dat=`date "+%Y-%m-%d_%H:%M"`
day=`date "+%Y/%m/%d" -d now`
#localip=`ip a | grep "\binet\b" |awk '{print $2}'| egrep -e "^10|172" | awk -F"/" '{print $1; exit;}'`
basedir="/data1/backup/mongodb/$day" helpfunc(){
echo
echo "options:"
echo " -P specify the port"
echo " -p specify the restore db path ,must abspath"
echo " is running like mongoha_backup.sh -P 27017 -p $backdir/test"
} while getopts "P:p:" Option
do
case $Option in
P) ports=$OPTARG;;
p) path=$OPTARG;;
*) helpfunc; exit 1; ;;
esac
done #mongorestore -h 127.0.0.1:27017 -d test test/
dorestore(){
echo "at $dat to do restore for mongo${dbname}"
if /usr/local/mongodb/bin/mongorestore --drop --oplogReplay -h 127.0.0.1:${ports} $path;then
echo "Mongo restore(${dbname}) -->Sucess"
else
echo "Mongo restore(${dbname}) -->faild"
fi } check(){
person=`whoami`
if [ "$person" != "root" ]; then
echo "the user is now $person, checkout to 'root' to continue"
exit 1
fi if [ -z "$ports" ] ;then
helpfunc
exit 1
fi } main(){ check
dorestore
}
main

附mongoha_dump_oplog.sh 脚本

cat mongoha_dump_oplog.sh
#!/bin/bash dat=`date "+%Y-%m-%d_%H:%M"`
day=`date "+%Y/%m/%d" -d now` helpfunc(){
echo
echo "options:"
echo " -S [Start-BSON-Timestamp]"
echo " -E [End-BSON-Timestamp] "
echo " is running like mongoha_dump_oplog.sh -S -E "
}
if [ $# -lt 2 ] ;
then
helpfunc
exit 1
else while getopts "S:E:" Option
do
case $Option in
S) start_t=$OPTARG;;
E) stop_t=$OPTARG;;
*) helpfunc; exit 1; ;;
esac
done
fi start_time="Timestamp(${start_t}, 0)"
stop_time="Timestamp(${stop_t}, 0)"
date_now=`date "+%Y%m%d%H_%M" -d now`
backdir="/data1/backup/mongodb/oplog"
#dump
function data_fetch(){
mkdir -p $backdir
query='{ts:{$gt:'$start_time',$lt: '$stop_time' }}'
flags='-d local -c oplog.rs'
if /usr/local/mongodb/bin/mongodump $flags --query "$query" -o $backdir/$date_now;then
echo "Mongo oplog backup [`date -d @${start_t} "+%Y-%m-%d %H:%M:%S"` - `date -d @${stop_t} "+%Y-%m-%d %H:%M:%S"`] -->Sucess"
else
echo "Mongo oplog backup [`date -d @${start_t} "+%Y-%m-%d %H:%M:%S"` - `date -d @${stop_t} "+%Y-%m-%d %H:%M:%S"`] -->Faild"
fi } main(){
data_fetch
}
main

   附mongoha_restore_oplog.sh 脚本

cat mongoha_restore_oplog.sh
#!/bin/bash dat=`date "+%Y-%m-%d_%H:%M"`
day=`date "+%Y/%m/%d" -d now`
#localip=`ip a | grep "\binet\b" |awk '{print $2}'| egrep -e "^10|172" | awk -F"/" '{print $1; exit;}'`
backdir="/data1/backup/mongodb/oplog/$day" helpfunc(){
echo
echo "options:"
echo " -P specify the port"
echo " -p specify the restore db path ,must abspath"
echo " is running like $0 -P 临时端口 -p $backdir/local/oplog.rs.bson"
} if [ $# -lt 2 ] ;
then
helpfunc
exit 1
else
while getopts "P:p:" Option
do
case $Option in
P) port=$OPTARG;;
p) path=$OPTARG;;
*) helpfunc; exit 1; ;;
esac
done
fi function data_restore(){
flags='-d local -c oplog.rs'
if /usr/local/mongodb/bin/mongorestore --port $port $flags $path;then
echo "Mongo restore[$port] -->Sucess"
else
echo "Mongo restore[$port] -->Faild"
fi }
data_restore

  

 

Mongodb基于oplog恢复至任意时间的更多相关文章

  1. 2020-08-01:MySQL 的数据如何恢复到任意时间点?

    福哥答案2020-08-01: 恢复到任意时间点以定时的做全量备份,以及备份增量的 binlog 日志为前提.恢复到任意时间点首先将全量备份恢复之后,再此基础上回放增加的 binlog 直至指定的时间 ...

  2. MongoDB 定位 oplog 必须全表扫描吗?

    MongoDB oplog (类似于 MySQL binlog) 记录数据库的所有修改操作,除了用于主备同步:oplog 还能玩出很多花样,比如 全量备份 + 增量备份所有的 oplog,就能实现 M ...

  3. mysqlbinlog工具基于日志恢复详细解释

    如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是DBA维护mysql的一个重要工作 1)RESET MASTER在上面查看日志存放的文件夹中 ...

  4. 基于FPGA的DDS任意波形发生器设计

    一.简介       DDS技术最初是作为频率合成技术提出的,由于其易于控制,相位连续,输出频率稳定度高,分辨率高, 频率转换速度快等优点,现在被广泛应用于任意波形发生器(AWG).基于DDS技术的任 ...

  5. Python: 处理mongodb文档,怎么让UTC时间转换为本地时间?

    存储数据到MongoDB数据库时,一般我们会加一个更新数据的时间update_time.这时在python代码中 会用到datetime模块以便获取当前系统时间,但是存入到MongoDB数据库时,存储 ...

  6. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

  7. dataguard 归档丢失(主库中无此丢失归档处理),备库基于SCN恢复

    dataguard 归档丢失(主库中无此丢失归档处理),备库基于SCN恢复 环境: OS: CentOS 6.5 DB: Oracle 10.2.0.5 1.主备库环境 主库: SQL> sel ...

  8. C#- 将秒数转化成任意时间格式

    将秒数转化成任意时间格式,可以使用C#的一个函数TimeSpan,看示例: TimeSpan ts = new TimeSpan(0, 0, 3661); richTextBox2.Text = ts ...

  9. mongodb的oplog遇到的问题

    mongodb调整oplog的大小的方法 关闭当前服务器,将服务器以单机模式启动.这是一种方法,还有没有其他方法? mongodb实时扫描oplog,判断记录到哪个地方了 如果扫描oplog的程序挂掉 ...

随机推荐

  1. MFC程序如何修改icon图标

    场景: Visual Studio写MFC应用程序,默认的程序左上角图标是自带的(如下图),虽说也不丑,但是对于程序员来说,还是缺乏个性了. 你知道,C.C++.java系程序员最常干的事情就是定义. ...

  2. Hibernate学习7—Hibernate 映射继承

    需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...

  3. SpringBoot中RedisTemplate订阅发布对象

    解说 RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布.订阅配置都必须在这里面实现 addMessageListener(MessageListe ...

  4. hadoop Partiton中的字符串Hash函数改进

    最近的MapReduce端的Partition根据map生成的Key来进行哈希,导致哈希出来的Reduce端处理任务数量非常不均匀,有些Reduce端处理的数据量非常小(几分钟就执行完成,而最后的pa ...

  5. jquery Autocomplete函数

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)

    随着OS X 10.9 于 2013年6月10日在旧金山WWDC(world wide developer conference)上发布.是首个不使用猫科动物命名的系统,而转用加利福尼亚的产物. 该系 ...

  7. python‘s second day for me

    in     not in 主要用来检测一些字符串是否存在,或者避免一些字符串 while True: comment = input('请输入你的评论') if '顾清秋' in comment: ...

  8. Tkinter简易教程

    支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...

  9. python中nltk的下载安装方式

    首先去http://nltk.org/install.html下载相关的安装程序,然后 在cmd窗口中,进入到python的文件夹内的 Scripts内,运行easy_install pip 安装Py ...

  10. JavaScript Post提交数据并跳转到页面(模拟Form表单提交)

    function GotoWatchTicketCode() { var orderID='@ViewBag.OrderInfo.OrderID'; var phoneNum='@ViewBag.Or ...