Few months back , I came to know about the tool Binlog2sql . The tool has very cool features like .
- The tool can extract SQL’s from MySQL Binary log .
- The tool can generate the rollback SQL’s for PITR .
In this blog, I am going to explain, how the above two features can be achieved using the tool binlog2sql .
Installation :
The tool has been developed by Mr. Cao Danfeng . Great Job Mr. Cao Dancing . The tool can be downloaded from the GitHub .
https://github.com/danfengcao/binlog2sql
git clone https://github.com/danfengcao/binlog2sql.git
cd binlog2sql
pip install -r requirements.txt
Make sure, the machine should have the Python for execute the tool .
MySQL Configuration :
The MySQL server should have the following configuration to make the tool working effectively .
[mysqld]
server_id = 100 #mandatory
log_bin = sakthi #mandatory
max_binlog_size = 1G
binlog_format = row #mandatory
binlog_row_image = full #mandatory
How to extract the SQL statements from MySQL binary logs ?
- flush logs
- created the table binlog2sql
- Inserted some records

Extracting SQL from binary log ,

- By default binlog2sql tool will prints the start / end position of the particular SQL statement and the time .
- You can ignore them by adding the AWK command as shown in the screenshot .
cmd :
python /root/binlog2sql/binlog2sql/binlog2sql.py –user=root –port=3306 -p –host=’localhost’ –start-file=’sakthi.000003′
python /root/binlog2sql/binlog2sql/binlog2sql.py –user=root –port=3306 -p –host=’localhost’ –start-file=’sakthi.000003′ | awk -F ‘\\;’ ‘{print $1″;”}’
Additionally, you can also get the similar output by enabling the variable “binlog_rows_query_log_events”

cmd :
mysqlbinlog –no-defaults –base64-output=decode-rows -vvv sakthi.000003 | grep ‘create\|insert\|update\|delete’
Some useful options to play :

Point in time recovery ( PITR ) :
This portion have 2 cases, which will explain the methods to recover the data from binary logs using –flashback option .
Case 1 : ( recover the data from wrong UPDATE )
- Mistakenly made the wrong UPDATE without WHERE clause on table binlog2sql.
- Need to rollback all the data to previous state on that particular table ( binlog2sql ).

step 1: find the binary log position which has affected by the UPDATE statement .


start position : 1358
end position : 1731
step 2 : reverting back to the old data with option “–flashback” .

cmd :
python /root/binlog2sql/binlog2sql/binlog2sql.py –user=root –port=3306 –password=’Jesus@7sakthI’ –host=’localhost’ –start-file=’sakthi.000003′ –start-position=’1358′ –stop-position=’1731′ –databases=’jesus’ –flashback –tables=’binlog2sql’ | awk -F ‘\\;’ ‘{print $1″;”}’ | mysql
The old data ( before UPDATE ) has been reverted now .
Case 2 : ( recover the data from wrong DELETE )
- Mistakenly made the wrong DELETE without WHERE clause on table binlog2sql.
- Need to rollback the data to previous state on that particular table ( binlog2sql ).
Going to perform the same steps here as well . But, here the INSERT’s will be generated for recovery purpose ( instead of UPDATE ).

step 1: find the binary log positions which has affected by the DELETE statement .
start position : 4476
end position : 4744
step 2 : Reverting back to old data with option “–flashback” .

cmd :
python /root/binlog2sql/binlog2sql/binlog2sql.py –user=root –port=3306 –password=’Jesus@7sakthI’ –host=’localhost’ –start-file=’sakthi.000003′ –start-position=’4476′ –stop-position=’4744′ –databases=’jesus’ –tables=’binlog2sql’ –flashback | awk -F ‘\\;’ ‘{print $1″;”}’ | mysql
My point of view PITR is the very required and important skill for the database administrator . Hope this blog will help someone who is looking for the recovery plan . I will come up with my next blog as much as possible .
Thanks !!!