背景介绍:
Red Hat Enterprise Linux Server release 7.9 (Maipo)
Server version: 10.4.25-MariaDB
操作过程:
MariaDB [(none)]> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> select count(*) from dep;
+----------+
| count(*) |
+----------+
| 540000 |
+----------+
1 row in set (17.078 sec)
MariaDB [test]>
MariaDB [test]> checksum table test.dep;
+----------+------------+
| Table | Checksum |
+----------+------------+
| test.dep | 4285120342 |
+----------+------------+
1 row in set (20.768 sec)
MariaDB [test]> show create table test.dep\G;
*************************** 1. row ***************************
Table: dep
Create Table: CREATE TABLE `dep` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`depno` mediumint(8) unsigned NOT NULL DEFAULT 0,
`depname` varchar(255) NOT NULL DEFAULT '',
`memo` varchar(255) NOT NULL DEFAULT '',
`memo1` varchar(255) NOT NULL DEFAULT '',
`memo2` varchar(255) NOT NULL DEFAULT '',
`memo3` varchar(255) NOT NULL DEFAULT '',
`memo4` varchar(255) NOT NULL DEFAULT '',
`memo5` varchar(255) NOT NULL DEFAULT '',
`memo6` varchar(255) NOT NULL DEFAULT '',
`memo7` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1266421 DEFAULT CHARSET=latin1
1 row in set (0.001 sec)
MariaDB [test]> delete from test.dep where id<40001;
Query OK, 20000 rows affected (7.088 sec)
MariaDB [test]> checksum table test.dep;
+----------+------------+
| Table | Checksum |
+----------+------------+
| test.dep | 1733503517 |
+----------+------------+
1 row in set (20.203 sec)
root@tc58:tools# ./my2sql -h
my2back V1.0
-U prefer to use unique key instead of primary key to build where condition for delete/update sql
-add-extraInfo
Works with -work-type=2sql|rollback. Print database/table/datetime/binlogposition...info on the line before sql, default false
-big-trx-row-limit int
transaction with affected rows greater or equal to this value is considerated as big transaction. Valid values range from 1 to 30000, default 10 (default 10)
-databases string
only parse these databases, comma seperated, default all.
-do-not-add-prifixDb
Prefix table name witch database name in sql,ex: insert into db1.tb1 (x1, x1) values (y1, y1).
-file-per-table
One file for one table if true, else one file for all tables. default false. Attention, always one file for one binlog
-full-columns
For update sql, include unchanged columns. for update and delete, use all columns to build where condition.
default false, this is, use changed columns to build set part, use primary/unique key to build where condition
-host string
mysql host, default 127.0.0.1 . (default "127.0.0.1")
-ignore-databases string
ignore parse these databases, comma seperated, default null
-ignore-primaryKey-forInsert
for insert statement when -workType=2sql, ignore primary key
-ignore-tables string
ignore parse these tables, comma seperated, default null
-local-binlog-file string
local binlog files to process, It works with -mode=file
-long-trx-seconds int
transaction with duration greater or equal to this value is considerated as long transaction. Valid values range from 0 to 3600, default 1 (default 1)
-mode string
valid options are: repl,file. repl: as a slave to get binlogs from master. file: get binlogs from local filesystem. default repl (default "repl")
-mysql-type string
valid options are: mysql,mariadb. server of binlog, mysql or mariadb, default mysql (default "mysql")
-output-dir string
result output dir, default current work dir. Attension, result files could be large, set it to a dir with large free space
-output-toScreen
Just output to screen,do not write to file
-password string
mysql user password.
-port uint
mysql port, default 3306. (default 3306)
-print-interval int
works with -w='stats', print stats info each PrintInterval. Valid values range from 1 to 600, default 30 (default 30)
-server-id uint
this program replicates from mysql as slave to read binlogs. Must set this server id unique from other slaves, default 1113306 (default 1113306)
-sql string
valid options are: insert,update,delete. only parse these types of sql, comma seperated, valid types are: insert, update, delete; default is all(insert,update,delete)
-start-datetime string
Start reading the binlog at first event having a datetime equal or posterior to the argument, it should be like this: "2020-01-01 01:00:00"
-start-file string
binlog file to start reading
-start-pos uint
start reading the binlog at position (default 4)
-stop-datetime string
Stop reading the binlog at first event having a datetime equal or posterior to the argument, it should be like this: "2020-12-30 01:00:00"
-stop-file string
binlog file to stop reading
-stop-pos uint
Stop reading the binlog at position (default 4)
-tables string
only parse these tables, comma seperated, DONOT prefix with schema, default all.
-threads uint
Works with -workType=2sql|rollback. threads to run (default 2)
-tl string
time location to parse timestamp/datetime column in binlog, such as Asia/Shanghai. default Local (default "Local")
-user string
mysql user.
-v print version
-work-type string
valid options are: 2sql,rollback,stats. 2sql: convert binlog to sqls, rollback: generate rollback sqls, stats: analyze transactions. default: 2sql (default "2sql")
root@tc58:tools#
#解析binglog文件
./my2sql -user tc_mon -password tc_mon -host tc58 -port 3306 -databases test -tables dep -mode repl -work-type 2sql -start-file binlog.000190 -output-dir /tmp
root@tc58:tools# ./my2sql -user tc_mon -password tc_mon -host tc58 -port 3306 -databases test -tables dep -mode repl -work-type 2sql -start-file binlog.000190 -output-dir /tmp
[2022/11/25 15:14:50] [info] events.go:221 start thread to write redo/rollback sql into file
[2022/11/25 15:14:50] [info] events.go:61 start thread 1 to generate redo/rollback sql
[2022/11/25 15:14:50] [info] events.go:61 start thread 2 to generate redo/rollback sql
[2022/11/25 15:14:50] [info] stats_process.go:166 start thread to analyze statistics from binlog
[2022/11/25 15:14:50] [info] binlogsyncer.go:164 create BinlogSyncer with config {1113306 mysql tc58 3306 tc_mon utf8 false false <nil> false Local false 0 0s 0s 0 false false 0 <nil> 0xc0000900c0 0x637c00}
[2022/11/25 15:14:50] [info] binlogsyncer.go:400 begin to sync binlog from position (binlog.000190, 4)
[2022/11/25 15:14:50] [info] repl.go:16 start to get binlog from mysql
[2022/11/25 15:14:50] [info] binlogsyncer.go:816 rotate to (binlog.000190, 4)
[2022/11/25 15:14:50] [info] events.go:255 finish processing binlog.000190 10488958
[2022/11/25 15:14:50] [info] events.go:255 finish processing binlog.000190 20977346
[2022/11/25 15:14:50] [info] events.go:255 finish processing binlog.000190 31465734
[2022/11/25 15:14:55] [info] repl.go:84 deadline exceeded.
[2022/11/25 15:14:55] [info] repl.go:18 finish getting binlog from mysql
[2022/11/25 15:14:55] [info] stats_process.go:266 exit thread to analyze statistics from binlog
[2022/11/25 15:14:55] [info] events.go:196 exit thread 1 to generate redo/rollback sql
[2022/11/25 15:14:55] [info] events.go:196 exit thread 2 to generate redo/rollback sql
[2022/11/25 15:14:55] [info] events.go:285 finish writing redo/forward sql into file
[2022/11/25 15:14:55] [info] events.go:288 exit thread to write redo/rollback sql into file
root@tc58:tools#
root@tc58:tools# ll /tmp
total 478068
-rw-r--r-- 1 root root 255 Nov 25 15:14 biglong_trx.txt
-rw-r--r-- 1 root root 288 Nov 25 15:14 binlog_status.txt
-rw-r--r-- 1 root root 854449 Nov 25 15:14 forward.190.sql
root@tc58:tools# cat /tmp/biglong_trx.txt
binlog starttime stoptime startpos stoppos rows duration tables
binlog.000190 2022-11-25_10:55:50 2022-11-25_10:55:50 395 35870601 20000 0 [test.dep(inserts=0, updates=0, deletes=20000)]
root@tc58:tools#
root@tc58:tools# cat /tmp/biglong_trx.txt
binlog starttime stoptime startpos stoppos rows duration tables
binlog.000190 2022-11-25_10:55:50 2022-11-25_10:55:50 395 35870601 20000 0 [test.dep(inserts=0, updates=0, deletes=20000)]
root@tc58:tools#
more -22 /tmp/forward.190.sql
root@tc58:tools# more -22 /tmp/forward.190.sql
DELETE FROM `test`.`dep` WHERE `id`=2;
DELETE FROM `test`.`dep` WHERE `id`=4;
DELETE FROM `test`.`dep` WHERE `id`=6;
DELETE FROM `test`.`dep` WHERE `id`=8;
DELETE FROM `test`.`dep` WHERE `id`=10;
DELETE FROM `test`.`dep` WHERE `id`=12;
DELETE FROM `test`.`dep` WHERE `id`=14;
DELETE FROM `test`.`dep` WHERE `id`=16;
DELETE FROM `test`.`dep` WHERE `id`=18;
DELETE FROM `test`.`dep` WHERE `id`=20;
DELETE FROM `test`.`dep` WHERE `id`=22;
DELETE FROM `test`.`dep` WHERE `id`=24;
DELETE FROM `test`.`dep` WHERE `id`=26;
DELETE FROM `test`.`dep` WHERE `id`=28;
DELETE FROM `test`.`dep` WHERE `id`=30;
DELETE FROM `test`.`dep` WHERE `id`=32;
DELETE FROM `test`.`dep` WHERE `id`=34;
DELETE FROM `test`.`dep` WHERE `id`=36;
DELETE FROM `test`.`dep` WHERE `id`=38;
DELETE FROM `test`.`dep` WHERE `id`=40;
DELETE FROM `test`.`dep` WHERE `id`=42;
DELETE FROM `test`.`dep` WHERE `id`=44;
DELETE FROM `test`.`dep` WHERE `id`=46;
# 生成闪回sql文件
./my2sql -user tc_mon -password tc_mon -host tc58 -port 3306 -databases test -tables dep -mode repl -work-type rollback -start-file binlog.000190 -output-dir /tmp/flashback/
root@tc58:tools# ./my2sql -user tc_mon -password tc_mon -host tc58 -port 3306 -databases test -tables dep -mode repl -work-type rollback -start-file binlog.000190 -output-dir /tmp/flashback/
[2022/11/25 15:20:37] [info] events.go:221 start thread to write redo/rollback sql into file
[2022/11/25 15:20:37] [info] events.go:61 start thread 1 to generate redo/rollback sql
[2022/11/25 15:20:37] [info] events.go:61 start thread 2 to generate redo/rollback sql
[2022/11/25 15:20:37] [info] binlogsyncer.go:164 create BinlogSyncer with config {1113306 mysql tc58 3306 tc_mon utf8 false false <nil> false Local false 0 0s 0s 0 false false 0 <nil> 0xc0000900c0 0x637c00}
[2022/11/25 15:20:37] [info] stats_process.go:166 start thread to analyze statistics from binlog
[2022/11/25 15:20:37] [info] binlogsyncer.go:400 begin to sync binlog from position (binlog.000190, 4)
[2022/11/25 15:20:37] [info] repl.go:16 start to get binlog from mysql
[2022/11/25 15:20:37] [info] binlogsyncer.go:816 rotate to (binlog.000190, 4)
[2022/11/25 15:20:38] [info] events.go:255 finish processing binlog.000190 10488958
[2022/11/25 15:20:38] [info] events.go:255 finish processing binlog.000190 20977346
[2022/11/25 15:20:38] [info] events.go:255 finish processing binlog.000190 31465734
[2022/11/25 15:20:43] [info] repl.go:84 deadline exceeded.
[2022/11/25 15:20:43] [info] repl.go:18 finish getting binlog from mysql
[2022/11/25 15:20:43] [info] stats_process.go:266 exit thread to analyze statistics from binlog
[2022/11/25 15:20:43] [info] events.go:196 exit thread 2 to generate redo/rollback sql
[2022/11/25 15:20:43] [info] events.go:196 exit thread 1 to generate redo/rollback sql
[2022/11/25 15:20:43] [info] events.go:270 finish writing rollback sql into tmp files, start to revert content order of tmp files
[2022/11/25 15:20:43] [info] rollback_process.go:15 start thread 1 to revert rollback sql files
[2022/11/25 15:20:43] [info] rollback_process.go:41 start to revert tmp file /tmp/flashback/.rollback.190.sql into /tmp/flashback/rollback.190.sql
[2022/11/25 15:20:43] [info] rollback_process.go:156 finish reverting tmp file /tmp/flashback/.rollback.190.sql into /tmp/flashback/rollback.190.sql
[2022/11/25 15:20:43] [info] rollback_process.go:25 exit thread 1 to revert rollback sql files
[2022/11/25 15:20:43] [info] events.go:283 finish reverting content order of tmp files
[2022/11/25 15:20:43] [info] events.go:288 exit thread to write redo/rollback sql into file
root@tc58:tools#
more -22 /tmp/flashback/rollback.190.sql
root@tc58:tools# more -22 /tmp/flashback/rollback.190.sql
INSERT INTO `test`.`dep` (`id`,`depno`,`depname`,`memo`,`memo1`,`memo2`,`memo3`,`memo4`,`memo5`,`memo6`,`memo7`) VALUES (40000,20001
,'iJVBVWYcopGozFDczrkbAwKhISoSyLmasKylSzWhWkkxJaYSriQFBTPqiWhXrQeWwuFVMXDZmmxEBUVUNcXIwlMfmVSCijuyivBvzkAZuaRhmOhvGSvyglMaSmFpLIkbBE
rUwAoRtsGfGOdYJABdMAqfBtlhXqNSaztvVthCrbhFGoALdkPrsONAlEoHsS','kzPDwwUhdVrVGrSmHxtBBazxNyeZItVvqrMJldJiOtEQqgHStolyFDbxhtwbSkvzmKPTX
hSQDsdplxxWqNRWhWlzHNUjmICPrlqRlCgWpIwlNhyTauZKEQoZeAmKNJjTQzXrOUihkFtcgBlCfQRKYMRVfMwWuhAioVLSfaIrIrJwgpgKgBmHzzbJrGfJdpmthDvtHd','
vSbFVNddfpljlFrSlNYFfNwVlCXiVdELSfaJwgqmluqwiCoLNGTzRJXIzyUdGVMUqUyMlcxixNxaJvYHoyAimNcXEhWkmGsVAQDuokgecWDarGiYpFhUbxixODzMkREwtCGx
wRRJTrfxaIqBNkKWCYljkBZvdelbuTjlMXAJVyISpXQmPkHHoyAfZJvaPYyyVidQWl','qVDhZzBilNdddkPrsLBxLmcCEoFmthHLMyjBcIiPBlCfSXjeThdUmDeLqz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izbGdvWsbepllEhXrTrevPNtDOiyS');
INSERT INTO `test`.`dep` (`id`,`depno`,`depname`,`memo`,`memo1`,`memo2`,`memo3`,`memo4`,`memo5`,`memo6`,`memo7`) VALUES (39998,20000
,'hfcXEdGTzUWZhPAhkFvlUPphPCsiLfruVwxWpLMzlFvofKjQBioUEpJDOiySUVSCikyOAjtrCOkJQZCOoakZtVyCrclhUfQMmaqDULYHsQaHgMqsRgdbVAOtAxNuJmhaHi
UXeEHxrpBKWEhYuemXbrBJOSYpCRClIzyTYllwBpWNaPWoGpDWZgJckQyXqO','UfUifdZNQRKZTusDPpgJdldGUHyvKprOVmwwTfWoFkjoTDifbRggkJQYywKmfPIUAUWZg
IWKKtOPFIBHFfLquaUvwTgZBMcdhClKLylLToMTjlGwqovohSUSHIrLEOiwLolmElpTwEDhbIlrSoSyLmZmmxBpWNbTlLNIaeuMArfCwzislltkSHLIfCwCxGLMDDgYzA','
fbOPJaackTMgswfkINNBuuQRMjJVBTNjJQdRbFVPmPpfEIETFxuHagHPeaPZCOlWTDlvtGcsImklLQXpFgPHQglMbVxCtmcAxLojdOMtHcsHiRMkPqloIwkKUtiJVDdIgGOa
JwfmTFyBjslmwuLtNHVHAFzISmKLCDjmHzCpObORTPuFRqhMlfLlkkutKtPSUTKWzK','aXMTfUgZFamjilSxLmcylUKQZzxPFIyuCDhdSfYCOpgNtETCgaHkfUkpTATRFAN
lbtQUdIgGMPRLfvLrADqSpYXSvBuvThcSgb','OPHTusHfHUATSFAJWDdGXUElAOsyoZfApXTzTSHKCIGiYsSlDfUfUhZCPrpAGEeIevQSOuEPlKTlzPBlKNJfzhlRuxgoal
fPKgCqWKKwdaSoPglTHEapwphRNqqHowswegtCDjlMUoMPPFIzyUcBzTWcvXAHMMArgFJETGBOodBvygmRvCyOyaGgLlYlhfcXFkjrgDzLgzdVqQirlgWpKGZduLyivDEkoO
cU','qUBYoyCoNWvlWZiSNmVRzTYkfXtdmYfIabhGJDPlTGxwQNrvcZQhmMZLJmhbJqBKXHtVyEzNpmlFoCXdxfizXlAKbcfuGXVIFZjXiXrOTdJlbzsrDTHDVUKSjrgFGsU
vvOIackREumZowsvaPXtdhCoNWuhDtlXhPGKFVPlVLWzHKHdvRZxphRKZUxGNVmBWbuSdQRPwNBshLgAh','ivCzPAipbliaGeArfBshIUAQCoMUoJDPpdxecaUvwUkoRpZd
qxpgLlYhQIRjuwZHluiFEeLpsUvtGZgGLMAtqxpjYpDWamjlATUQtwbTpQmMWzGIyqkdLwXzGHqHozFDatRaDPqlfTdMxcTkxEFoEdGUEltmcDHBLbXKHeAlPkGCRBhgjEsd
lURzXpIsSjqewWrWGqNQPCtkVXakZsQbKuTjqZclhUfSYmpObQWoEeG','WOfkIJwiyRRHKDOiAcLykEqPbPVjgfjCkurzvHYVHAEvppDVUNfocvVrYObPUgTcGYYVKOOCwB
pXTzPBpSuuNJetGbolALdjMiEuoiZAAcKvVsckPvJhJXMSbERyQHRjwFKLzrhMokijvDDfSWfOBqbjSJUwzfcXFjfZEYgMlgSUVUJORR','KaWGrOUifcVupmrYRpZbhHPeb
VyGKIkcEQrkYpEbvXBOqlihlLUqUDhYywKolwtEOhwJgDxFIxoeDElBSIMOFLPPHQjvBtrCKUqYRqdrBIItRfYBMiBktqvhAdOMuLxdYKDLVwvPMlhUeMxbNNAoTzTUSErWK
IjZtWCWaorTsgCsgCtmcBzSRFCTMemUPoZgGMRZvgsxjBgbPUcDLTlzOtFVKPSUTLaTtlXfHXRrhL');
INSERT INTO `test`.`dep` (`id`,`depno`,`depname`,`memo`,`memo1`,`memo2`,`memo3`,`memo4`,`memo5`,`memo6`,`memo7`) VALUES (39996,19999
,'AAcLzmKReVoKIjUYiUZlkoRqfBskYljlMVulVTIJylTFvlbtNLomvqqJvbXFklwDyKfvMxbOSVYhOyZCNhxTXjaCJPXrQfdbVBSLZSoPiwHWLPRRGFhXoBSKXGoyExDxDy
IVFmvrveiExzeXCSFzFDcAuwYCRDqSqaexaJrIrHlkqYWOdaQbNIbkVYfGNZ','HltftDMXEeIbgEEgUeOGOaLFTCg'...............
.................省略
root@tc58:tools# ls -lrt /tmp/flashback
total 37668
-rw-r--r-- 1 root root 255 Nov 25 15:20 biglong_trx.txt
-rw-r--r-- 1 root root 288 Nov 25 15:20 binlog_status.txt
-rw-r--r-- 1 root root 38563347 Nov 25 15:20 rollback.190.sql
root@tc58:tools# cat /tmp/flashback/binlog_status.txt
root@tc58:tools# cat /tmp/flashback/biglong_trx.txt
binlog starttime stoptime startpos stoppos rows duration tables
binlog.000190 2022-11-25_10:55:50 2022-11-25_10:55:50 395 35870601 20000 0 [test.dep(inserts=0, updates=0, deletes=20000)]
root@tc58:tools#
root@tc58:tools# cat /tmp/flashback/binlog_status.txt
binlog starttime stoptime startpos stoppos inserts updates deletes database table
binlog.000190 2022-11-25_10:55:50 2022-11-25_10:55:50 495 35870570 0 0 20000 test dep
root@tc58:tools#
# 应用rollback文件进行数据恢复
mysql -htc58 -utc_mon -ptc_mon test < /tmp/flashback/rollback.190.sql
root@tc58:tools# mysql -htc58 -utc_mon -ptc_mon test < /tmp/flashback/rollback.190.sql
root@tc58:tools#
# 检查恢复情况
MariaDB [test]> select count(*) from test.dep;
+----------+
| count(*) |
+----------+
| 540000 |
+----------+
1 row in set (11.256 sec)
MariaDB [test]> checksum table test.dep;
+----------+------------+
| Table | Checksum |
+----------+------------+
| test.dep | 4285120342 |
+----------+------------+
1 row in set (20.973 sec)