淘先锋技术网

首页 1 2 3 4 5 6 7

从整库备份的sql文件中导出某个表的sql语句时,vim查找到表的第一条INSERT语句后,按上下换行键计数INSERT语句的条数,然后按n yy复制,退出vim后,
再新建一个文件,按p粘贴刚才的n条INSERT语句,再进行source导入。

----------------------------------------------------------------

一、登录数据库刷新binlog

1.1)查看当前的binlog
MySQL> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| test-150-bin.000003 |  2895377 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

1.2)刷新binlog
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

1.3)确认刷新binlog成功
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| test-150-bin.000004 |      120 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
用show master status 命令查看当前的binlog已经由test-150-bin.000003变为test-150-bin.000004,
证明binlog已经刷新成功。

二、查询二进制日志位置
mysql> show variables like'log_bin%';
+---------------------------------+------------------------------------------+
| Variable_name                   | Value                                    |
+---------------------------------+------------------------------------------+
| log_bin                             | ON                                       |
| log_bin_basename             | /db/mysql5.6/data/test-150-bin       |
| log_bin_index                    | /db/mysql5.6/data/test-150-bin.index |
| log_bin_trust_function_creators  | ON                                       |
| log_bin_use_v1_row_events       | OFF                                      |
+---------------------------------+------------------------------------------+

三、从二进制日志中获取表被删除的时间
$ mysqlbinlog test-150-bin.000003 | grep -i DROP  -A3 -B4
COMMIT;
# at 122869
#140126 17:03:35 server id 150  end_log_pos 122989 CRC32 0x8707c4c4     Query   thread_id=12519 exec_time=0     error_code=0
SET TIMESTAMP=1390727015;
DROP TABLE `test`
;
# at 122989
#140126 17:04:06 server id 150  end_log_pos 123039 CRC32 0x43476aad     Rotate to test-150-bin.000004  pos: 4

drop语句的前两行表名drop语句的执行时间是在 17:03:35
mysql> SELECT from_unixtime('1390727015');
+-----------------------------+
| from_unixtime('1390727015')  |
+-----------------------------+
| 2014-01-26 17:03:35.000000 |
+-----------------------------+
1 row in set

四、从binlog中获取指定数据库的改变数据
用mysqlbinlog 恢复ivr数据库在二进制日志test-150-bin.000003中的数据
假设从上一次mysql备份后,只有一个binlog产生,即登录数据库时查询到binlog test-150-bin.000003

mysqlbinlog -d mydb --stop-datetime='2014-01-26 17:03:35' test-150-bin.000003 > recover_mydb.sql

如果从上次备份刷新binlog,到发现表被删掉的过程中产生了多个binlog。则要按照binlog产生的顺序进行恢复,那
么恢复的次序应该是按照binglog的产生的序号,从小到大依次恢复。
假如从上次备份,到发现表被删除,共有两个binlog文件,分别是test-150-bin.000002,test-150-bin.000003 ,
则按照binlog序号从小到大的排列,恢复的顺序应该是:

mysqlbinlog -d mydb  test-150-bin.000002  > recover_mydb.sql

mysqlbinlog -d mydb --stop-datetime='2014-01-26 17:03:35' test-150-bin.000003  >> recover_mydb.sql

由于恢复的文件recover_mydb.sql中包含了整个mydb数据库的所有表,我们只要恢复指定的表mytable,还要对恢复出来的sql进行过滤。

cat recover_mydb.sql | grep  -A1 -B3 -i -E '^insert|^update|^delete|^replace|^alter' | grep -A1 -B3 mytable  > mytable.sql

可以先使用more确认一下
;
;
;
DELIMITER ;
--
# at 122721
#140126 17:03:28 server id 150  end_log_pos 122721 CRC32 0xe0f851bb     Intvar
SET INSERT_ID=4;
#140126 17:03:28 server id 150  end_log_pos 122838 CRC32 0x9efcc7b2     Query   thread_id=12578 exec_time=0     error_code=0
use `ivr`;
SET TIMESTAMP=1390727008;
INSERT INTO `testtuo` (`name`) VALUES ('d')
;
# at 122838

将过滤后的结果保存为sql脚本,恢复到数据库即可。

ps备注脚本

python /www/binlog2sql/binlog2sql/binlog2sql.py -h localhost -u root -p 123456 -d 库名 -t 表名 --start-file mysql-bin.000009 --start-datetime '2021-07-01 00:00:00' --stop-datetime '2021-07-01 23:59:59'> /www/backup/sql/01.sql 【此方式好像没有用】

/www/server/mysql/bin/mysqlbinlog -d 库名 --start-datetime="2021-07-01 00:00:00" --stop-datetime="2021-07-12 23:59:59" /www/server/data/mysql-bin.000009 > /www/backup/sql/backup.sql
cat /www/backup/sql/backup.sql | grep  -A1 -B3 -i -E '^insert|^update|^delete|^replace|^alter' | grep -A1 -B3 t_tk_exp  > /www/backup/sql/xx1.sql
cat /www/backup/sql/backup.sql | grep  -A1 -B3 -i -E '^insert|^update|^delete|^replace|^alter' | grep -A1 -B3 t_tk_exp  > /www/backup/sql/xx2.sql
cat /www/backup/sql/backup.sql | grep  -A1 -B3 -i -E '^insert|^update|^delete|^replace|^alter' | grep -A1 -B3 t_tk_exp  > /www/backup/sql/xx3.sql
source /www/backup/sql/xx1.sql
source /www/backup/sql/xx2.sql
source /www/backup/sql/xx3.sql