删库不跑路,我含泪写下MySQL数据恢复大法
Java技术迷
共 11121字,需浏览 23分钟
·
2022-03-02 11:42
点击关注公众号,Java干货及时送达
作者:程淇铭
来源:https://segmentfault.com/a/1190000020116271
gzip -d backup.sql.gz | mysql -u<user> -h<host> -P<port> -p
# 步骤一:解压(如果没有压缩可以忽略这一步)
innobackupex --decompress <备份文件所在目录>
# 步骤二:应用日志
innobackupex --apply-log <备份文件所在目录>
# 步骤三:复制备份文件到数据目录
innobackupex --datadir=
--copy-back <备份文件所在目录>
chengqm-3306>>show create table mytest.mytest \G;
*************************** 1. row ***************************
Table: mytest
Create Table: CREATE TABLE `mytest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ctime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
[mysql@mysql-test ~]$ while true; do mysql -S /tmp/mysql.sock -e 'insert into mytest.mytest(ctime)values(now())';date;sleep 1;done
[mysql@mysql-test ~]$ mysqldump --opt --single-transaction --master-data=2 --default-character-set=utf8 -S /tmp/mysql.sock -A > backup.sql
[mysql25 backup.sql | grep 'CHANGE MASTER TO MASTER_LOG_FILE' -test ~]$ head -n
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000032', MASTER_LOG_POS=39654;
假设要恢复到 2019-08-09 11:01:54 这个时间点,我们从 binlog 中查找从 39654 到 019-08-09 11:01:54 的日志
[mysql39654 --stop-datetime='2019-08-09 11:01:54' /data/mysql_log/mysql_test/mysql-bin.000032 > backup_inc.sql -test ~]$ mysqlbinlog --start-position=
[mysql-83 ~]$ tail -n 20 backup_inc.sql -test
......
### INSERT INTO `mytest`.`mytest`
### SET
### @1=161 /* INT meta=0 nullable=0 is_null=0 */
### @2='2019-08-09 11:01:53' /* DATETIME(0) meta=0 nullable=1 is_null=0 */
......
-- 2019-08-09 11:01:54之前的数据条数
chengqm-3306 select count(*) from mytest.mytest where ctime < '2019-08-09 11:01:54';
+----------+
| count(*) |
+----------+
| 161 |
+----------+
1 row in set (0.00 sec)
-- 所有数据条数
chengqm-3306 select count(*) from mytest.mytest;
+----------+
| count(*) |
+----------+
| 180 |
+----------+
1 row in set (0.00 sec)
# 全量恢复
[mysql@mysql-test ~]$ mysql -S /tmp/mysql.sock < backup.sql
# 应用增量日志
[mysql@mysql-test ~]$ mysql -S /tmp/mysql.sock < backup_inc.sql
chengqm-3306 select count(*) from mytest.mytest;
+----------+
| count(*) |
+----------+
| 161 |
+----------+
1 row in set (0.00 sec)
chengqm-3306 select * from mytest.mytest order by id desc limit 5;
+-----+---------------------+
| id | ctime |
+-----+---------------------+
| 161 | 2019-08-09 11:01:53 |
| 160 | 2019-08-09 11:01:52 |
| 159 | 2019-08-09 11:01:51 |
| 158 | 2019-08-09 11:01:50 |
| 157 | 2019-08-09 11:01:49 |
+-----+---------------------+
5 rows in set (0.00 sec)
# 提取某个库的所有数据
sed -n '/^-- Current Database: `mytest`/,/^-- Current Database:/p' backup.sql > backup_mytest.sql
# 从库备份文件中提取建表语句
sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `mytest`/!d;q' backup_mytest.sql > mytest_table_create.sql
# 从库备份文件中提取插入数据语句
grep -i 'INSERT INTO `mytest`' backup_mytest.sql > mytest_table_insert.sql
# 恢复表结构到 mytest 库
mysql -u
-p mytest < mytest_table_create.sql
# 恢复表数据到 mytest.mytest 表
mysql -u
-p mytest < mytest_table_insert.sql
chengqm-3306 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| mytest |
| t_myisam |
+------------------+
2 rows in set (0.00 sec)
chengqm-3306 check table t_myisam;
+-----------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------+-------+----------+----------+
| mytest.t_myisam | check | status | OK |
+-----------------+-------+----------+----------+
1 row in set (0.00 sec)
起一个新实例 在实例上建一个和原来一模一样的表 执行 alter table t_innodb discard tablespace;,删除表空间,这个操作会把 t_innodb.ibd 删除 从备份文件中找到 t_innodb.ibd 这个文件,复制到对应的数据目录,并授权 执行 alter table t_innodb IMPORT tablespace; 加载表空间 执行 flush table t_innodb;check table t_innodb; 检查表 使用 mysqldump 导出数据,然后再导入到要恢复的数据库
chgnqm-3306 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
+------------------+
1 row in set (0.00 sec)
[mysql@mysql-test ~]$ head -n 25 backup.sql | grep 'CHANGE MASTER TO MASTER_LOG_FILE'
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000034', MASTER_LOG_POS=38414;
[.000034 | grep -i -B 3 'drop table `b`'; ]$ mysqlbinlog -vv /data/mysql_log/mysql_test/mysql-bin
SET TIMESTAMP=1566128910/*!*/;
DROP TABLE `b` /* generated by server */
# 第一条的 start-position 为备份文件的 pos 位置,stop-position 为 drop 语句的开始位置
mysqlbinlog -vv --start-position=38414 --stop-position=120629 /data/mysql_log/mysql_test/mysql-bin.000034 > backup_inc_1.sql
# 第二条的 start-position 为 drop 语句的结束位置
mysqlbinlog -vv --start-position=120747 /data/mysql_log/mysql_test/mysql-bin.000034 > backup_inc_2.sql
[mysql@mysql-test ~]$ mysql -S /tmp/mysql.sock < backup.sql
chgnqm-3306 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
| b |
+------------------+
2 rows in set (0.00 sec)
chgnqm-3306 select count(*) from a;
+----------+
| count(*) |
+----------+
| 71 |
+----------+
1 row in set (0.00 sec)
[mysql@mysql-test ~]$ mysql -S /tmp/mysql.sock < backup_inc_1.sql
[mysql@mysql-test ~]$ mysql -S /tmp/mysql.sock < backup_inc_2.sql
chgnqm-3306 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
| b |
+------------------+
2 rows in set (0.00 sec)
chgnqm-3306 select count(*) from a;
+----------+
| count(*) |
+----------+
| 274 |
+----------+
1 row in set (0.00 sec)
找出备份时的日志位置 找出执行了 drop table 语句的 GTID 值 导出备份时日志位置到最新的 binglog 日志 恢复备份文件 跳过这个 GTID
SET SESSION GTID_NEXT='对应的 GTID 值';
BEGIN; COMMIT;
SET SESSION GTID_NEXT = AUTOMATIC;
应用步骤 3 得到的增量 binlog 日志
localhost:3306 -> localhost:3307(delay 600)
slave status \G;
...
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 15524
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 22845
Relay_Master_Log_File: mysql-bin.000038
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
Seconds_Behind_Master: 600
...
chengqm-3307 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
| b |
+------------------+
chengqm-3306 drop table b;
Query OK, 0 rows affected (0.00 sec)
chengqm-3306 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
+------------------+
1 row in set (0.00 sec)
stop slave;
[.000039 | grep -i -B 10 'drop table `b`'; ]$ mysqlbinlog -vv /data/mysql_log/mysql_test/mysql-bin
...
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
use `mytest`/*!*/;
SET TIMESTAMP=1566186025/*!*/;
DROP TABLE `b` /* generated by server */
change master to master_delay=0;
start slave until master_log_file='mysql-bin.000039',master_log_pos=35134;
slave status \G;
...
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 65792
...
Slave_IO_Running: Yes
Slave_SQL_Running: No
Exec_Master_Log_Pos: 35134
...
Until_Log_File: mysql-bin.000039
Until_Log_Pos: 35134
set global sql_slave_skip_counter=1;
start slave;
chengqm-3307 show slave status \G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
1 row in set (0.00 sec)
chengqm-3307 show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| a |
| b |
+------------------+
2 rows in set (0.00 sec)
停止同步 找出执行了 drop table 语句的 GTID 执行这个 GTID 的事务
SET SESSION GTID_NEXT='对应的 GTID 值';
BEGIN; COMMIT;
SET SESSION GTID_NEXT = AUTOMATIC;
继续同步
wget https://github.com/danfengcao/binlog2sql/archive/master.zip -O binlog2sql.zip
unzip binlog2sql.zip
cd binlog2sql-master/
# 安装依赖
pip install -r requirements.txt
python binlog2sql/binlog2sql.py --flashback \
-h<host> -P<port> -u<user> -p'<password>' -d<dbname> -t<table_name>\
--start-file='<binlog_file>' \
--start-datetime='<start_time>' \
--stop-datetime='<stop_time>' > ./flashback.sql
python binlog2sql/binlog2sql.py --flashback \
-h<host> -P<port> -u<user> -p'<password>' -d<dbname> -t<table_name> \
--start-file='<binlog_file>' \
--start-position=<start_pos> \
--stop-position=<stop_pos> > ./flashback.sql
binlog格式必须为row,且 binlog_row_image=full 仅支持5.6与5.7 只能回滚DML(增、删、改)
# 依赖(centos)
yum install gcc* pkg-config glib2 libgnomeui-devel -y
# 下载文件
wget https://github.com/Meituan-Dianping/MyFlash/archive/master.zip -O MyFlash.zip
unzip MyFlash.zip
cd MyFlash-master
# 编译安装
gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
mv binary /usr/local/MyFlash
ln -s /usr/local/MyFlash/flashback /usr/bin/flashback
flashback --databaseNames=<dbname> --binlogFileNames=<binlog_file> --start-position=<start_pos> --stop-position=<stop_pos>
mysqlbinlog -vv binlog_output_base.flashback | mysql -u<user> -p
评论