通过 binlog 时间区间 / 偏移量进行恢复
mysqlbinlog [选项] binlog 日志文件名 | MySQL -uroot -p
选项 |
---|
--start-datetime="yyyy-mm-dd hh:mm:ss" |
--stop-datetime="yyyy-mm-dd hh:mm:ss" |
--strat-position = 偏移量 |
--stop-position = 偏移量 |
恢复思路:
第一步首先通过上次的完整备份恢复到完整备份之前的状态.
第二步通过 binlog 定位到完整备份之后到当前删库之前的偏移量得到起始偏移量和结束偏移量(或者时间).
第三步准确恢复校对数据.
思路校验:
先创建测试库测试表然后通过循环进行插入数据.
- MySQL -uroot -p12345 -e "create database testdb charset=utf8;"
- MySQL -uroot -p12345 -e "create table testdb.testtb1(id int);"
- for i in {
- 1..1000
- };do MySQL -uroot -p12345 -e "insert into testdb.testtb1(id) values($i);" ;done
然后通过 mysqldump 对数据库 testdb 进行完整备份.
- mysqldump -uroot -p12345 --flush-logs -B testdb> /tmp/testdb.sql
- # 参数 - B 和不加有何区别?
- #1. 区别就在于加参数 B 后可以指定备份数据库列表.
- #2. 生成的数据库备份文件中含有创建数据库和选择数据库的指令.
- #CREATE DATABASE /*!32312 IF NOT EXISTS*/ `testdb` /*!40100 DEFAULT CHARACTER SET utf8 */;
- #USE `testdb`;
模拟用户写入数据 1001 条.
for i in {3000..4000};do MySQL -uroot -p12345 -e "insert into testdb.testtb1(id) values($i);" ;done && MySQL -uroot -p12345 -e "insert into testdb.testtb1(id) values(666);" ;
然后测试删库
MySQL -uroot -p12345 -e "drop database testdb;"
然后使用完整备份记录进行恢复.
MySQL -uroot -p12345 < testdb.sql
使用上次完整备份的记录恢复后, 中间还有用户写入的 1000 条数据, 可以通过 binlog 偏移量 (时间差) 进行恢复, 先定位上次备份结束的位置
- [[email protected] tmp]# MySQL -uroot -p12345 -e "select count(*) from testdb.testtb1"
- MySQL: [Warning] Using a password on the command line interface can be insecure.
- +----------+
- | count(*) |
- +----------+
- | 1000 |
- +----------+
- # at 869709
- #191015 0:19:55 server id 1 end_log_pos 875708 CRC32 0x9b4fd429 Query thread_id=3032 exec_time=0 error_code=0
- SET TIMESTAMP=1571069995/*!*/;
- ******************************************************************************************
内容过多省略
- ******************************************************************************************
- # at 875708
- #191015 0:19:56 server id 1 end_log_pos 296078 CRC32 0x84203c10 Query thread_id=4040 exec_time=0 error_code=0
- SET TIMESTAMP=1571071195/*!*/;
- insert into testdb.testtb1(id) values(666)
- /*!*/;
通过偏移量进行恢复(也可以指定 binlog 文件和结束偏移量恢复到删库前一条, 不带开始偏移量.)
mysqlbinlog --strat-position=869709 --stop-position=875708 /logfile/lqh.000001 | MySQL -uroot -p12345
来源: http://www.bubuko.com/infodetail-3240809.html