SlideShare a Scribd company logo
Percona Live Santa Clara 2014
Be the hero of the day with Data
recovery for InnoDB
Marco Tusa – Aleksandr Kuzminsky
April 2014
Who
• Marco ā€œThe Grinchā€
• Manager Rapid Response
• Former Pythian MySQL cluster
technical leader
• Former MySQL AB PS (EMEA)
• Love programming
• History of religions
• Ski; Snowboard; scuba diving;
Mountain trekking
2
What we will cover
• Recovery toolkit introduction
• Show how to extract data from IBD data file
• Attach ibd files after IBDATA corruption
• Recover deleted records
• Recover drop table
• Bad update
3
What is Percona Data recovery Tool for InnoDB
• Set of open source tools
• Work directly on the data files
• Recover lost data (no backup available)
• Wrappers (you can help)
4
A
What files?
• Server wide files
– <Table>.frm
• InnoDB files
– ibdata1
• InnoDB dictionary
• UNDO segment
• All tables if innodb_file_per_table=OFF
– <Table>.ibd
– Reads raw partition
5
A
InnoDB file format
• Antelope
– REDUNDANT (versions 4.x)
– COMPACT (5.X)
• Barracuda
– REDUNDANT, COMPACT
– New time formats in BARRACUDA
– COMPRESSED
6
A
What is a InnoDB Table space?
Tablespace consists of pages
• InnoDB page
– 16k by default
– page_id is file offset in 16k chunks
– Records never fragmented
– Types:
• FIL_PAGE_INDEX
• FIL_PAGE_TYPE_BLOB
• FIL_PAGE_UNDO_LOG
7
A
InnoDB index (B+ Tree) 8
A
Requirements and minimal skill to use the tool
• You need to know how to compile (make)
• MySQL you know what it is right?
• How to import data from a tab separated file
9
M
Show the tool in action - Process
Process:
10
data extraction
Extract data
From ibdataX
Read SYS_X
Tables
Generate Table
filters files
Extract data from
ibd Tbspaces
Validate Data
Import data back
Final clean up
Production
ready
Show the tool in action - page_parser
• Extract pages from InnoDB files
– (In case of innodb_file_per_table =0 it also extract real data)
– page_parser -f ibdata1 (or table space like employees.ibd)
11
Extract data from idbata
Show the tool in action – contraints_parser
• Extract data from InnoDB pages
• IE SYS_TABLE/INDEX/COLUMN
– bin/constraints_parser.SYS_TABLES -4Uf
FIL_PAGE_INDEX/0-1
– bin/constraints_parser.SYS_INDEXES -4Uf
FIL_PAGE_INDEX/0-3
12
Extract data from idbata
Show the tool in action - contraints_parser
Output
SYS_TABLES "employees/salaries" 811 
…
SYS_INDEXES 811 1889 "PRIMARYā€œ 
SYS_INDEXES 811 1890 "emp_no"
Table ID Index Id
13
Read SYS_Tables/Indexes
Show the tool in action - sys_parser
Why:
Lost FRM file
Two possible ways:
• Easy: copy it from slave/backup
• Less easy:
– Run sys_parser on a new create instance (info not accessible – require
dictionary table load)
14
Lost FRM and IBD (1)
Show the tool in action - sys_parser
Output:
./sys_parser -h192.168.0.35 -u stress -p tool –d <database>
employees/salaries
CREATE TABLE `salariesR`(
`emp_no` INT NOT NULL,
`salary` INT NOT NULL,
`from_date` DATE NOT NULL,
`to_date` DATE NOT NULL,
PRIMARY KEY (`emp_no`, `from_date`)) ENGINE=InnoDB;
15
Lost FRM and IBD (2)
Show the tool in action - ibdconnect
• Accidental removal of the IBDATA file
• IBDATA table space corruption
• Only file per table available (IE employees.ibd)
16
Attach Table from another source (1)
Show the tool in action - ibdconnect
What to do?
1. Start a new clean MySQL
2. Create empty structure
(same tables definition)
3. Copy over the table spaces
4. Run ibdconnect
5. Run
innochecksum_changer
17
Attach Table from another source (2)
./ibdconnect -o ibdata1 -f salaries.ibd -d
employees -t salaries
salaries.ibd belongs to space #15
Initializing table definitions...
Updating employees/salaries (table_id 797)
SYS_TABLES is updated successfully
Initializing table definitions...
Processing table: SYS_TABLES
…
Processing table: SYS_INDEXES
Setting SPACE=15 in SYS_INDEXES for TABLE_ID =
797
Show the tool in action – fix filters
Table filters use for:
• Identify the data inside the ibd
• Filter out the damage records
• Bound to table definition
• Must recompile for each table definition
• Generated with the tool create_def.pl
18
Generate Table filters (1)
Show the tool in action - page_parser filters
• Generated with the tool create_def.pl
create_defs.pl --db=$schema --table=$table >
$myPath/include/table_defs.${schema}.$table.defreco
very
• Create symbolic link to include/table_defs.h
• Compile again
19
Generate Table filters (1)
Show the tool in action - constraints_parser
The data is extracted by the tool specifying the table
space an possible BLOB directory.
/constraints_parser -5Uf FIL_PAGE_INDEX/0-${INDEXID} –b FIL_PAGE_TYPE_BLOB/ >
$DESTDIR/${SCHEMA}_${TABLE}.csvā€œ
FIL_PAGE_INDEX/0-${INDEXID} is the ID of the PK
FIL_PAGE_TYPE_BLOB is the directory containing the BLOB
21
Extract data from Table space (1)
Show the tool in action - constraints_parser
Example of the data:
-- Page id: 4, Format: COMPACT, Records list: Valid, Expected records: (164 164)
00000000150B 88000002260084 employees 10001 "1953-09-02ā€œ "G" "eorgiF" "(null)" "12722-11-12"
00000000150B 88000002260091 employees 10002 "1964-06-02" "B" "ezalelSā€œ "(null)" "14006-11-05"
00000000150B 8800000226009E employees 10003 "1959-12-03" "P" "artoB" "(null)" "14003-03-15"
00000000150B 880000022600AB employees 10004 "1954-05-01" "C" "hirstianK""(null)ā€œ "12598-03-09"
00000000150B 880000022600B8 employees 10005 "1955-01-21" "K" "yoichiM" "(null)" "13876-11-14"
<snip>
00000000150B 880000022608EE employees 10164 "1956-01-19" "J" "agodaB" "(null)" "12474-11-14"
-- Page id: 4, Found records: 164, Lost records: NO, Leaf page: YES
22
Validate data
Show the tool in action - LOAD DATA INFILE
How to import the data back?
Easy as:
LOAD DATA INFILE ā€˜PLMC_employees/employees' REPLACE INTO
TABLE `employees` FIELDS TERMINATED BY 't' OPTIONALLY
ENCLOSED BY '"' LINES STARTING BY 'employeest' (`emp_no`,
`birth_date`, `first_name`, `last_name`, `gender`,
`hire_date`);
Done
23
Import data back
How recover deleted record -
Identify the records just for this exercise:
select count(emp_no) from employeesR where hire_date > '1999-08-24';
+---------------+
| count(emp_no) |
+---------------+
| 279 |
+---------------+
1 row in set (0.23 sec)
And bravely delete them:
delete from employeesR where hire_date > '1999-08-24';
Query OK, 279 rows affected (0.55 sec)
24
Delete records
How recover deleted record -
To recover deleted record we must use the –D flag:
constraints_parser -5Df /FIL_PAGE_INDEX/0-1975 -b
/FIL_PAGE_TYPE_BLOB/ > employees_employeesDeleted.csv
cat employees_employeesDeleted.csv |grep -i -v -e "-- Page id"|wc -l
55680  Too many because I take unfiltered records
25
Recover delete records
How recover deleted record -
name: "employeesR",
{
{ /* int(11) */
name: "emp_no",
type: FT_INT,
fixed_length: 4,
has_limits: TRUE,
limits: {
can_be_null: FALSE,
int_min_val: 10001,
int_max_val: 499999
},
26
Use filters to clean up results
name: "first_name",
type: FT_CHAR,
min_length: 0,
max_length: 42,
has_limits: TRUE,
limits: {
can_be_null: FALSE,
char_min_len: 3,
char_max_len: 42,
char_ascii_only: TRUE
},
can_be_null: FALSE
},
name: "last_name",
type: FT_CHAR,
min_length: 0,
max_length: 48,
has_limits: TRUE,
limits: {
can_be_null: FALSE,
char_min_len: 3,
char_max_len: 48,
char_ascii_only: TRUE
},
How recover deleted record -
Now let us recompile and run the extraction again:
cat employees_employeesDeleted.csv |grep -i -v -e "-- Page
id"|wc -l
279 <------ Bingo!
27
Check if it fits and reload
How recover Drop tables -
• Different methods if using innodb_file_per_table=[0|1].
– Must act fast because files can be overwritten quickly
• In the first case pages are mark free for reuse
• In the second the file is removed and we need to scan
the device
28
How recover Drop tables -
What we need then ?
• The table definition
• The PK index
– Parse the dictionary with the ā€œDā€ flag
• constraints_parser.SYS_TABLES -4D
• Extract the InnoDB pages
29
How recover Drop tables -
Not using innodb_file_per_table method:
1. Extract the ibdataX as usual
2. Run contraints_parser
constraints_parser -5Uf ./FIL_PAGE_INDEX/0-1975 -b
./FIL_PAGE_TYPE_BLOB/ > employees_employeesDroped.csv
cat employees_employeesDroped.csv |grep -i -v -e "-- Page id"|wc -l
300024 <---- done
30
Not using file per table
How recover Drop tables -
Innodb_file_per_table=1 method:
What we need more?
• To know what device is containing the dropped
table
31
using file per table
How recover Drop tables -
Identify the PK id from dictionary:
cat SYS_TABLE.csv|grep employeesR
SYS_TABLES "employees/employeesR" 855
cat SYS_INDEX.csv|grep 855
SYS_INDEXES 855 1979 "PRIMARYā€
32
using file per table
How recover Drop tables -
This time we must run the page_parser against the device
not the file using the T option.
-T -- retrieves only pages with index id = NM (N - high
word, M - low word of id)
page_parser -t 100G -T 0:1979 -f /dev/mapper/EXT_VG-extlv
Parse in this case take longer.
33
Run the page extraction
How recover wrong updates -
Doing with the tool is possible when new data is larger
then the original, and will not fit in the original page,
otherwise the old one will be replaced, as such the only
way is to parse the undo segment.
Tools: s_indexes, s_tables recover the dictionary from the
undo.
34
How recover wrong updates -
Other method:
Possible to use the binary log for that
When in ROW format .
35
How recover wrong updates -
You can use binary log to recover your data if:
• Binlog format = ROW
• binlog_row_image = FULL (from 5.6 you can change it)
36
Prerequisite
How recover wrong updates -
Assume a table like :
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 |
| 10002 | 1964-06-02 | Bezalel | Simmel | F | 1985-11-21 |
| 10003 | 1959-12-03 | Parto | Bamford | M | 1986-08-28 |
+--------+------------+------------+-----------+--------+------------+
An action like :
update employeesR set last_name="WRONG-NAME" where emp_no <
10010;
37
Scenario (1)
How recover wrong updates -
You will have to recover something like:
+--------+------------+------------+------------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+------------+--------+------------+
| 10001 | 1953-09-02 | Georgi | WRONG-NAME | M | 1986-06-26 |
| 10002 | 1964-06-02 | Bezalel | WRONG-NAME | F | 1985-11-21 |
| 10003 | 1959-12-03 | Parto | WRONG-NAME | M | 1986-08-28 |
…
38
Scenario (2)
How recover wrong updates -
with a simple command like :
mysqlbinlog -vvv logs/binlog.000034 --start-datetime="2014-03-19
11:00:07"|grep -e "@1" -e "@4"|awk -F '/*' '{print $1}'|awk '{print
$2}'
@1=10001
@4='Facello'
@1=10001
@4='WRONG-NAME'
@1=10002
@4='Simmel'
@1=10002
@4='WRONG-NAME'..
39
Recover from binary log (2)
Reference and repositories
Main Percona branch:
bzr branch lp:percona-data-recovery-tool-for-innodb
Marco branch:
https://github.com/Tusamarco/drtools
40
Recover from binary log (2)
Q&A 41
Contacts 42
To contact Marco
marco.tusa@percona.com
marcotusa@tusacentral.net
To follow me
http://www.tusacentral.net/
https://www.facebook.com/marco.tusa.94
@marcotusa
http://it.linkedin.com/in/marcotusa/
To contact Aleksander
aleksandr.kuzminsky@percona.com
To follow me
http://www.mysqlperformanceblog.com/author/akuzminsk
y/
https://www.linkedin.com/in/akuzminsky

More Related Content

PDF
MySQL cluster 72 in the Cloud
PDF
Discard inport exchange table & tablespace
PDF
MySQL developing Store Procedure
PDF
MySQL overview
PDF
Oracle to MySQL 2012
PDF
My First 100 days with an Exadata (WP)
PPTX
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
PPTX
ConFoo MySQL Replication Evolution : From Simple to Group Replication
MySQL cluster 72 in the Cloud
Discard inport exchange table & tablespace
MySQL developing Store Procedure
MySQL overview
Oracle to MySQL 2012
My First 100 days with an Exadata (WP)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
ConFoo MySQL Replication Evolution : From Simple to Group Replication

What's hot (20)

PPTX
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
PDF
Managing Exadata in the Real World
PPTX
Indexing in Exadata
PDF
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
PDF
MySQL Advanced Administrator 2021 - ė„¤ģ˜¤ķ“ė”œė°”
PDF
Mysql database basic user guide
PDF
Advanced MySQL Query Optimizations
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
PDF
MariaDB 10.5 binary install (ė°”ģ“ė„ˆė¦¬ ģ„¤ģ¹˜)
PPTX
Understanding Query Optimization with ā€˜regular’ and ā€˜Exadata’ Oracle
PDF
Highload Perf Tuning
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
MySQL as a Document Store
PDF
MySQL database replication
PPTX
PDF
Percona xtra db cluster(pxc) non blocking operations, what you need to know t...
PDF
MySQL Indexierung CeBIT 2014
PDF
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Ā 
PDF
Dbvisit replicate: logical replication made easy
PDF
Meb Backup & Recovery Performance
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Managing Exadata in the Real World
Indexing in Exadata
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
MySQL Advanced Administrator 2021 - ė„¤ģ˜¤ķ“ė”œė°”
Mysql database basic user guide
Advanced MySQL Query Optimizations
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MariaDB 10.5 binary install (ė°”ģ“ė„ˆė¦¬ ģ„¤ģ¹˜)
Understanding Query Optimization with ā€˜regular’ and ā€˜Exadata’ Oracle
Highload Perf Tuning
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MySQL as a Document Store
MySQL database replication
Percona xtra db cluster(pxc) non blocking operations, what you need to know t...
MySQL Indexierung CeBIT 2014
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Ā 
Dbvisit replicate: logical replication made easy
Meb Backup & Recovery Performance
Ad

Similar to Plmce 14 be a_hero_16x9_final (20)

PDF
Undrop for InnoDB
PDF
Undrop for InnoDB
PPT
Perl DBI Scripting with the ILS
PPT
Les 11 Fb Queries
PDF
51881801 informatica-faq
PPT
Ms sql server architecture
PPT
Oracle-L11 using Oracle flashback technology-Mazenet solution
PPT
Lauri Pietarinen - What's Wrong With My Test Data
PDF
Data Purge Algorithm
PDF
What's New in MariaDB Server 10.3
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PDF
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitƤt
PDF
IBM DB2 for z/OS Administration Basics
Ā 
PPTX
Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...
PPT
1 Oracle Flashback study related with recyclebin.ppt
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DOC
Oracle SQL AND PL/SQL
Undrop for InnoDB
Undrop for InnoDB
Perl DBI Scripting with the ILS
Les 11 Fb Queries
51881801 informatica-faq
Ms sql server architecture
Oracle-L11 using Oracle flashback technology-Mazenet solution
Lauri Pietarinen - What's Wrong With My Test Data
Data Purge Algorithm
What's New in MariaDB Server 10.3
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitƤt
IBM DB2 for z/OS Administration Basics
Ā 
Wildcard13 - warmup slides for the "Roundtable discussion with Oracle Profess...
1 Oracle Flashback study related with recyclebin.ppt
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Oracle SQL AND PL/SQL
Ad

More from Marco Tusa (19)

PDF
My sql on kubernetes demystified
PDF
Comparing high availability solutions with percona xtradb cluster and percona...
PDF
Accessing data through hibernate: what DBAs should tell to developers and vic...
PDF
Best practice-high availability-solution-geo-distributed-final
PDF
MySQL innoDB split and merge pages
PDF
Robust ha solutions with proxysql
PDF
Fortify aws aurora_proxy_2019_pleu
PDF
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
PDF
Are we there Yet?? (The long journey of Migrating from close source to opens...
PPTX
Improve aws withproxysql
PDF
Fortify aws aurora_proxy
PDF
Mysql8 advance tuning with resource group
PDF
Proxysql sharding
PDF
Geographically dispersed perconaxtra db cluster deployment
PDF
Sync rep aurora_2016
PDF
Proxysql ha plam_2016_2_keynote
PPT
Empower my sql server administration with 5.7 instruments
PDF
Galera explained 3
PDF
Scaling with sync_replication using Galera and EC2
My sql on kubernetes demystified
Comparing high availability solutions with percona xtradb cluster and percona...
Accessing data through hibernate: what DBAs should tell to developers and vic...
Best practice-high availability-solution-geo-distributed-final
MySQL innoDB split and merge pages
Robust ha solutions with proxysql
Fortify aws aurora_proxy_2019_pleu
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Are we there Yet?? (The long journey of Migrating from close source to opens...
Improve aws withproxysql
Fortify aws aurora_proxy
Mysql8 advance tuning with resource group
Proxysql sharding
Geographically dispersed perconaxtra db cluster deployment
Sync rep aurora_2016
Proxysql ha plam_2016_2_keynote
Empower my sql server administration with 5.7 instruments
Galera explained 3
Scaling with sync_replication using Galera and EC2

Recently uploaded (20)

PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Computer network topology notes for revision
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PDF
Lecture1 pattern recognition............
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Database Infoormation System (DBIS).pptx
PDF
ā€œGetting Started with Data Analytics Using R – Concepts, Tools & Case Studiesā€
Clinical guidelines as a resource for EBP(1).pdf
Supervised vs unsupervised machine learning algorithms
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Business Ppt On Nestle.pptx huunnnhhgfvu
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Acceptance and paychological effects of mandatory extra coach I classes.pptx
1_Introduction to advance data techniques.pptx
Reliability_Chapter_ presentation 1221.5784
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Introduction-to-Cloud-ComputingFinal.pptx
Computer network topology notes for revision
Data_Analytics_and_PowerBI_Presentation.pptx
Lecture1 pattern recognition............
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Database Infoormation System (DBIS).pptx
ā€œGetting Started with Data Analytics Using R – Concepts, Tools & Case Studiesā€

Plmce 14 be a_hero_16x9_final

  • 1. Percona Live Santa Clara 2014 Be the hero of the day with Data recovery for InnoDB Marco Tusa – Aleksandr Kuzminsky April 2014
  • 2. Who • Marco ā€œThe Grinchā€ • Manager Rapid Response • Former Pythian MySQL cluster technical leader • Former MySQL AB PS (EMEA) • Love programming • History of religions • Ski; Snowboard; scuba diving; Mountain trekking 2
  • 3. What we will cover • Recovery toolkit introduction • Show how to extract data from IBD data file • Attach ibd files after IBDATA corruption • Recover deleted records • Recover drop table • Bad update 3
  • 4. What is Percona Data recovery Tool for InnoDB • Set of open source tools • Work directly on the data files • Recover lost data (no backup available) • Wrappers (you can help) 4 A
  • 5. What files? • Server wide files – <Table>.frm • InnoDB files – ibdata1 • InnoDB dictionary • UNDO segment • All tables if innodb_file_per_table=OFF – <Table>.ibd – Reads raw partition 5 A
  • 6. InnoDB file format • Antelope – REDUNDANT (versions 4.x) – COMPACT (5.X) • Barracuda – REDUNDANT, COMPACT – New time formats in BARRACUDA – COMPRESSED 6 A
  • 7. What is a InnoDB Table space? Tablespace consists of pages • InnoDB page – 16k by default – page_id is file offset in 16k chunks – Records never fragmented – Types: • FIL_PAGE_INDEX • FIL_PAGE_TYPE_BLOB • FIL_PAGE_UNDO_LOG 7 A
  • 8. InnoDB index (B+ Tree) 8 A
  • 9. Requirements and minimal skill to use the tool • You need to know how to compile (make) • MySQL you know what it is right? • How to import data from a tab separated file 9 M
  • 10. Show the tool in action - Process Process: 10 data extraction Extract data From ibdataX Read SYS_X Tables Generate Table filters files Extract data from ibd Tbspaces Validate Data Import data back Final clean up Production ready
  • 11. Show the tool in action - page_parser • Extract pages from InnoDB files – (In case of innodb_file_per_table =0 it also extract real data) – page_parser -f ibdata1 (or table space like employees.ibd) 11 Extract data from idbata
  • 12. Show the tool in action – contraints_parser • Extract data from InnoDB pages • IE SYS_TABLE/INDEX/COLUMN – bin/constraints_parser.SYS_TABLES -4Uf FIL_PAGE_INDEX/0-1 – bin/constraints_parser.SYS_INDEXES -4Uf FIL_PAGE_INDEX/0-3 12 Extract data from idbata
  • 13. Show the tool in action - contraints_parser Output SYS_TABLES "employees/salaries" 811  … SYS_INDEXES 811 1889 "PRIMARYā€œ  SYS_INDEXES 811 1890 "emp_no" Table ID Index Id 13 Read SYS_Tables/Indexes
  • 14. Show the tool in action - sys_parser Why: Lost FRM file Two possible ways: • Easy: copy it from slave/backup • Less easy: – Run sys_parser on a new create instance (info not accessible – require dictionary table load) 14 Lost FRM and IBD (1)
  • 15. Show the tool in action - sys_parser Output: ./sys_parser -h192.168.0.35 -u stress -p tool –d <database> employees/salaries CREATE TABLE `salariesR`( `emp_no` INT NOT NULL, `salary` INT NOT NULL, `from_date` DATE NOT NULL, `to_date` DATE NOT NULL, PRIMARY KEY (`emp_no`, `from_date`)) ENGINE=InnoDB; 15 Lost FRM and IBD (2)
  • 16. Show the tool in action - ibdconnect • Accidental removal of the IBDATA file • IBDATA table space corruption • Only file per table available (IE employees.ibd) 16 Attach Table from another source (1)
  • 17. Show the tool in action - ibdconnect What to do? 1. Start a new clean MySQL 2. Create empty structure (same tables definition) 3. Copy over the table spaces 4. Run ibdconnect 5. Run innochecksum_changer 17 Attach Table from another source (2) ./ibdconnect -o ibdata1 -f salaries.ibd -d employees -t salaries salaries.ibd belongs to space #15 Initializing table definitions... Updating employees/salaries (table_id 797) SYS_TABLES is updated successfully Initializing table definitions... Processing table: SYS_TABLES … Processing table: SYS_INDEXES Setting SPACE=15 in SYS_INDEXES for TABLE_ID = 797
  • 18. Show the tool in action – fix filters Table filters use for: • Identify the data inside the ibd • Filter out the damage records • Bound to table definition • Must recompile for each table definition • Generated with the tool create_def.pl 18 Generate Table filters (1)
  • 19. Show the tool in action - page_parser filters • Generated with the tool create_def.pl create_defs.pl --db=$schema --table=$table > $myPath/include/table_defs.${schema}.$table.defreco very • Create symbolic link to include/table_defs.h • Compile again 19 Generate Table filters (1)
  • 20. Show the tool in action - constraints_parser The data is extracted by the tool specifying the table space an possible BLOB directory. /constraints_parser -5Uf FIL_PAGE_INDEX/0-${INDEXID} –b FIL_PAGE_TYPE_BLOB/ > $DESTDIR/${SCHEMA}_${TABLE}.csvā€œ FIL_PAGE_INDEX/0-${INDEXID} is the ID of the PK FIL_PAGE_TYPE_BLOB is the directory containing the BLOB 21 Extract data from Table space (1)
  • 21. Show the tool in action - constraints_parser Example of the data: -- Page id: 4, Format: COMPACT, Records list: Valid, Expected records: (164 164) 00000000150B 88000002260084 employees 10001 "1953-09-02ā€œ "G" "eorgiF" "(null)" "12722-11-12" 00000000150B 88000002260091 employees 10002 "1964-06-02" "B" "ezalelSā€œ "(null)" "14006-11-05" 00000000150B 8800000226009E employees 10003 "1959-12-03" "P" "artoB" "(null)" "14003-03-15" 00000000150B 880000022600AB employees 10004 "1954-05-01" "C" "hirstianK""(null)ā€œ "12598-03-09" 00000000150B 880000022600B8 employees 10005 "1955-01-21" "K" "yoichiM" "(null)" "13876-11-14" <snip> 00000000150B 880000022608EE employees 10164 "1956-01-19" "J" "agodaB" "(null)" "12474-11-14" -- Page id: 4, Found records: 164, Lost records: NO, Leaf page: YES 22 Validate data
  • 22. Show the tool in action - LOAD DATA INFILE How to import the data back? Easy as: LOAD DATA INFILE ā€˜PLMC_employees/employees' REPLACE INTO TABLE `employees` FIELDS TERMINATED BY 't' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'employeest' (`emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date`); Done 23 Import data back
  • 23. How recover deleted record - Identify the records just for this exercise: select count(emp_no) from employeesR where hire_date > '1999-08-24'; +---------------+ | count(emp_no) | +---------------+ | 279 | +---------------+ 1 row in set (0.23 sec) And bravely delete them: delete from employeesR where hire_date > '1999-08-24'; Query OK, 279 rows affected (0.55 sec) 24 Delete records
  • 24. How recover deleted record - To recover deleted record we must use the –D flag: constraints_parser -5Df /FIL_PAGE_INDEX/0-1975 -b /FIL_PAGE_TYPE_BLOB/ > employees_employeesDeleted.csv cat employees_employeesDeleted.csv |grep -i -v -e "-- Page id"|wc -l 55680  Too many because I take unfiltered records 25 Recover delete records
  • 25. How recover deleted record - name: "employeesR", { { /* int(11) */ name: "emp_no", type: FT_INT, fixed_length: 4, has_limits: TRUE, limits: { can_be_null: FALSE, int_min_val: 10001, int_max_val: 499999 }, 26 Use filters to clean up results name: "first_name", type: FT_CHAR, min_length: 0, max_length: 42, has_limits: TRUE, limits: { can_be_null: FALSE, char_min_len: 3, char_max_len: 42, char_ascii_only: TRUE }, can_be_null: FALSE }, name: "last_name", type: FT_CHAR, min_length: 0, max_length: 48, has_limits: TRUE, limits: { can_be_null: FALSE, char_min_len: 3, char_max_len: 48, char_ascii_only: TRUE },
  • 26. How recover deleted record - Now let us recompile and run the extraction again: cat employees_employeesDeleted.csv |grep -i -v -e "-- Page id"|wc -l 279 <------ Bingo! 27 Check if it fits and reload
  • 27. How recover Drop tables - • Different methods if using innodb_file_per_table=[0|1]. – Must act fast because files can be overwritten quickly • In the first case pages are mark free for reuse • In the second the file is removed and we need to scan the device 28
  • 28. How recover Drop tables - What we need then ? • The table definition • The PK index – Parse the dictionary with the ā€œDā€ flag • constraints_parser.SYS_TABLES -4D • Extract the InnoDB pages 29
  • 29. How recover Drop tables - Not using innodb_file_per_table method: 1. Extract the ibdataX as usual 2. Run contraints_parser constraints_parser -5Uf ./FIL_PAGE_INDEX/0-1975 -b ./FIL_PAGE_TYPE_BLOB/ > employees_employeesDroped.csv cat employees_employeesDroped.csv |grep -i -v -e "-- Page id"|wc -l 300024 <---- done 30 Not using file per table
  • 30. How recover Drop tables - Innodb_file_per_table=1 method: What we need more? • To know what device is containing the dropped table 31 using file per table
  • 31. How recover Drop tables - Identify the PK id from dictionary: cat SYS_TABLE.csv|grep employeesR SYS_TABLES "employees/employeesR" 855 cat SYS_INDEX.csv|grep 855 SYS_INDEXES 855 1979 "PRIMARYā€ 32 using file per table
  • 32. How recover Drop tables - This time we must run the page_parser against the device not the file using the T option. -T -- retrieves only pages with index id = NM (N - high word, M - low word of id) page_parser -t 100G -T 0:1979 -f /dev/mapper/EXT_VG-extlv Parse in this case take longer. 33 Run the page extraction
  • 33. How recover wrong updates - Doing with the tool is possible when new data is larger then the original, and will not fit in the original page, otherwise the old one will be replaced, as such the only way is to parse the undo segment. Tools: s_indexes, s_tables recover the dictionary from the undo. 34
  • 34. How recover wrong updates - Other method: Possible to use the binary log for that When in ROW format . 35
  • 35. How recover wrong updates - You can use binary log to recover your data if: • Binlog format = ROW • binlog_row_image = FULL (from 5.6 you can change it) 36 Prerequisite
  • 36. How recover wrong updates - Assume a table like : +--------+------------+------------+-----------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+-----------+--------+------------+ | 10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 | | 10002 | 1964-06-02 | Bezalel | Simmel | F | 1985-11-21 | | 10003 | 1959-12-03 | Parto | Bamford | M | 1986-08-28 | +--------+------------+------------+-----------+--------+------------+ An action like : update employeesR set last_name="WRONG-NAME" where emp_no < 10010; 37 Scenario (1)
  • 37. How recover wrong updates - You will have to recover something like: +--------+------------+------------+------------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+------------+--------+------------+ | 10001 | 1953-09-02 | Georgi | WRONG-NAME | M | 1986-06-26 | | 10002 | 1964-06-02 | Bezalel | WRONG-NAME | F | 1985-11-21 | | 10003 | 1959-12-03 | Parto | WRONG-NAME | M | 1986-08-28 | … 38 Scenario (2)
  • 38. How recover wrong updates - with a simple command like : mysqlbinlog -vvv logs/binlog.000034 --start-datetime="2014-03-19 11:00:07"|grep -e "@1" -e "@4"|awk -F '/*' '{print $1}'|awk '{print $2}' @1=10001 @4='Facello' @1=10001 @4='WRONG-NAME' @1=10002 @4='Simmel' @1=10002 @4='WRONG-NAME'.. 39 Recover from binary log (2)
  • 39. Reference and repositories Main Percona branch: bzr branch lp:percona-data-recovery-tool-for-innodb Marco branch: https://github.com/Tusamarco/drtools 40 Recover from binary log (2)
  • 41. Contacts 42 To contact Marco marco.tusa@percona.com marcotusa@tusacentral.net To follow me http://www.tusacentral.net/ https://www.facebook.com/marco.tusa.94 @marcotusa http://it.linkedin.com/in/marcotusa/ To contact Aleksander aleksandr.kuzminsky@percona.com To follow me http://www.mysqlperformanceblog.com/author/akuzminsk y/ https://www.linkedin.com/in/akuzminsky