误删数据恢复

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 数据平台开发误删数据,但是不知道具体时间点,就给了一个模糊时间,同事使用rollback逆向恢复(解析binlog产生反向SQL),我使用备份集恢复apply binlog正向恢复,尽快解决问题 【原正向恢复方式】 选择数据出问题时间之前最近的备份集恢复在一个新实例(restorebak.pl恢复工具支持,操作方便) 将备份集pos与数据出问题pos之间的binlog拷贝

数据平台开发误删数据,但是不知道具体时间点,就给了一个模糊时间,同事使用rollback逆向恢复(解析binlog产生反向SQL),我使用备份集恢复apply binlog正向恢复,尽快解决问题

【原正向恢复方式】
  • 选择数据出问题时间之前最近的备份集恢复在一个新实例(restorebak.pl恢复工具支持,操作方便)
  • 将备份集pos与数据出问题pos之间的binlog拷贝到新实例上(拷贝binlog操作繁琐)
  • 使用mysqlbinlog –stop-datetime来apply binlog到新实例,以便恢复到数据出问题前的一致性数据快照(mysqlbinlog每次只支持单个binlog apply ,操作繁琐)

 

其中关于pos点的查找一般都是通过类似mysqlbinlog –no-defaults –stop-datetime=”2012-11-11 11:11:11″ |grep “具体操作”  -C 3方式来查找的,先通过大体时间段确定一个范围,然后在范围内的精确查找具体的pos

 

其实mysql有个函数master_pos_wait 可以指定具体pos并返回期间执行事件,于是改进恢复过程,将新实例直接指向原主库拉binlog

【新正向恢复方式】
  • 选择数据出问题时间之前最近的备份集恢复在一个新实例(恢复工具支持,操作方便)
  • start slave;select MASTER_POS_WAIT(‘mysql-bin.000396′,67698920,0);stop slave; (relay 到指定pos后 触发stop slave操作)
  • show slave status\G;  (保存复制关系)
  • change master to  master_host=’ ‘, master_user=’slave’,master_password=’slave’; (取消复制关系,防止误操作开启复制)
【问题】
这种方式在新实例上会多执行几个事件,所以需要将MASTER_POS_WAIT设置的pos提前一些,只适用于基于时间恢复的粗略场景,但是这种场景占绝大多数情况

 MASTER_POS_WAIT(log_name,log_pos[,timeout])

This function is useful for control of master/slave synchronization. It blocks until the slave has read and applied all updates up to the specified position in the master log. The return value is the number of log events the slave had to wait for to advance to the specified position. The function returns NULL if the slave SQL thread is not started, the slave’s master information is not initialized, the arguments are incorrect, or an error occurs. It returns -1 if the timeout has been exceeded. If the slave SQL thread stops while MASTER_POS_WAIT() is waiting, the function returns NULL. If the slave is past the specified position, the function returns immediately.

If a timeout value is specified, MASTER_POS_WAIT() stops waiting when timeout seconds have elapsed. timeoutmust be greater than 0; a zero or negative timeout means no timeout.

This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when binlog_format is set to STATEMENT. (Bug #47995)

 

【分享】
后来同事分享其实start slave直接可以指定停止的pos,汗
START SLAVE [SQL_THREAD] UNTIL MASTER_LOG_FILE = ‘ log_name‘, MASTER_LOG_POS =  log_pos

An UNTIL clause may be added to specify that the slave should start and run until the SQL thread reaches a given point in the master binary log or in the slave relay log. When the SQL thread reaches that point, it stops. If the SQL_THREAD option is specified in the statement, it starts only the SQL thread. Otherwise, it starts both slave threads. If the SQL thread is running, the UNTIL clause is ignored and a warning is issued.

For an UNTIL clause, you must specify both a log file name and position. Do not mix master and relay log options.

Any UNTIL condition is reset by a subsequent STOP SLAVE statement, a START SLAVE statement that includes noUNTIL clause, or a server restart.

The UNTIL clause can be useful for debugging replication, or to cause replication to proceed until just before the point where you want to avoid having the slave replicate an event. For example, if an unwise DROP TABLE statement was executed on the master, you can use UNTIL to tell the slave to execute up to that point but no farther. To find what the event is, use mysqlbinlog with the master binary log or slave relay log, or by using a SHOW BINLOG EVENTS statement.

If you are using UNTIL to have the slave process replicated queries in sections, it is recommended that you start the slave with the –skip-slave-start option to prevent the SQL thread from running when the slave server starts. It is probably best to use this option in an option file rather than on the command line, so that an unexpected server restart does not cause it to be forgotten.

【最终正向恢复方式】
  • 选择数据出问题时间之前最近的备份集恢复在一个新实例(恢复工具支持,操作方便)
  • start slave until master_log_file=’mysql-bin.000396′,master_log_pos=67698920;
  • show slave status\G;  (保存复制关系)
  • change master to  master_host=’ ‘, master_user=’slave’,master_password=’slave’; (取消复制关系,防止误操作开启复制)
【改进】
可以直接把start slave until语句直接做到恢复工具中,很大的收获
感谢印风和智盛的idea,感谢维西的分享

【转载地址】
开发误删数据恢复

PS: 偶平时也就这样做了一下.
shell>mysqlbinlog  -d xxx --stop-datetime="xxxx-xx-xx xx:xx:xx" mysqlbin.000004 >> restore.sql
shell>mysql -u root -p --default-character-set=utf-8 < restore.sql
....如果没有建库的SQL,估计就得进库先建,然后 source restore.sql;...

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
SQL 数据库 数据安全/隐私保护
误删数据怎么办?
误删数据怎么办?
|
存储 安全
u盘文件损坏怎么恢复数据 ,兔八哥爱分享轻松带你恢复数据
随着电脑和移动设备的广泛使用,U盘已经成为了人们在数据传输和备份中不可或缺的重要工具之一。特别是在计算机方面,一天要用的数据很多。但有时,我们可能会遇到U盘文件损坏的情况,导致无法访问里面的数据,又不清楚是什么原因造成的,这也是很让人烦恼的。那我们可以用什么方法呢?其实先了解u盘出现损坏的原因再对症下药会简单很多。今天兔八哥爱分享就给大家分享一些 u盘恢复数据方法,操作方法简单实用,大家可以试试!
2342 0
u盘文件损坏怎么恢复数据 ,兔八哥爱分享轻松带你恢复数据
|
存储 编解码 搜索推荐
EasyRecovery2023数据恢复好用吗?
误删重要文件这样的事想必大家都不陌生,有时粗心起来可能经常会出现这样的事情。现代人的生活和工作基本离不开电脑等多媒体设备,每天需要处理的文件量和数据量也是呈指数增长,所以一款能够实现误删数据修复的软件可以说是当代人的电脑必装了。本以为在电脑中都找不到的文件肯定是恢复不了,没想到易恢复文件恢复软件能给恢复好。第一次用这个文件恢复软件,操作简单很喜欢,回收站删除的文件都给恢复了,功能很强大,赞!
135 0
|
存储 Windows
不小心把u盘里的文件删除了怎么恢复丢失怎么办?,用什么数据恢复软件恢复
自从手机和电脑可以无线传输文件后,U盘就被遗忘在包里,偶尔看见,但基本上没再用过。昨天突然想看看U盘里还有什么文件资料,结果,插在电脑上却读不出来,这是怎么回事呢?u盘在电脑上读不出来数据怎么修复?紧急求救身边的懂电脑小哥哥,他教我一招,就轻松解决了这个难题。
203 0
不小心把u盘里的文件删除了怎么恢复丢失怎么办?,用什么数据恢复软件恢复
|
存储 大数据 数据库
Ontrack EasyRecovery易恢复最好用的数据恢复软件
Ontrack EasyRecovery易恢复是由全球著名数据厂商Kroll Ontrack出品的一款专业级数据文件恢复软件,号称全球最好的数据恢复软件!
543 0
|
存储 监控 固态存储
EasyRecovery数据恢复可以轻松恢复删除和丢失的任何数据
Ontrack EasyRecovery易恢复是由全球著名数据厂商Kroll Ontrack出品的一款专业级数据文件恢复软件,号称最好的数据恢复软件!
181 0
|
关系型数据库 数据库 文件存储
数据恢复比备份费时的五个原因
备份数据可能很快,但由于访问备份并将其恢复到实时网络上需要几个步骤,恢复速度可能会很慢。
130 0

相关实验场景

更多