Table of Contents
情况一:误删部分数据,需要用最近一次备份覆盖
情况二:误删 table,表结构已经被drop了
Home Database Mysql Tutorial MySQL单表ibd文件恢复_MySQL

MySQL单表ibd文件恢复_MySQL

Jun 01, 2016 pm 01:39 PM
New surface

bitsCN.com

前言:

随着innodb的普及,innobackup也成为了主流备份方式。物理备份对于新建slave,全库恢复的需求都能从容应对。

但当面临单表数据误删,或者单表误drop的情况,如果使用物理全备进行恢复呢? 

下文将进行详细分析。 

恢复过程中需要用到的工具,percona data recover tool : https://launchpad.net/percona-innodb-recovery-tool

 

情况一:误删部分数据,需要用最近一次备份覆盖

来自同一台机器的ibd恢复覆盖,且备份后table没有被recreate过。

这种情况是最简单的,备份时的ibd文件(后称老ibd)中的space id和index id 与 新ibd的space id 和index id一致。

且和ibdata文件中的space id和index id一致。因此,物理文件可以直接覆盖做恢复。

以下是详细步骤

Step -1 : 物理备份

 innobackupex --defaults-file=/usr/local/mysql3321/my.cnf --socket=/xfs/mysql3321/mysql.sock --user=root --password=password /xfs/backup/

Step 0 : apply log

innobackupex --apply-log --defaults-file=/usr/local/mysql3321/my.cnf  /xfs/backup/2012-10-17_11-29-20/

Step 1 : 备份现在的ibd文件(可选)

cp -a testibd.ibd testibd.bak

Step 2 : 舍弃现在ibd文件

mysql> alter table testibd discard tablespace

Step 3 : 复制备份ibd文件

shell> cp /xfs/backup/2012-10-17_11-29-20/test/testibd.ibd /xfs/mysql3321/test/ 

shell> chown mysql:mysql /xfs/mysql3321/test/testibd.ibd

Step 4 : 导入ibd文件

mysql> alter table testibd import tablespace

 

情况二:误删 table,表结构已经被drop了

这种情况稍复杂,不过恢复过程还是比较容易操作的。由于table被drop后的space id会留空因此备份文件的space id不会被占用。

我们只需要重建表结构,然后把ibdata中该表的space id还原,物理文件可以直接覆盖做恢复了。

Step 1 : 重建表

mysql> create table testibd (UserID int);

Step 2 : 关闭mysql服务(必须)

shell> service mysqld3321 stop

Step 3: 准备ibd文件  apply log

shell> innobackupex --apply-log --defaults-file=/usr/local/mysql3321/my.cnf  /xfs/backup/2012-10-17_11-29-20/

Step 4 : 备份现在的ibd文件(可选)

cp -a testibd.ibd testibd.bak

Step 5 : 复制备份ibd文件

shell> cp -a /xfs/backup/2012-10-17_11-29-20/test/testibd.ibd /xfs/mysql3321/test/ 

shell> chown mysql:mysql /xfs/mysql3321/test/testibd.ibd

Step 6 : 使用percona recovery tool 修改ibdata

shell> /root/install/percona-data-recovery-tool-for-innodb-0.5/ibdconnect -o /xfs/mysql3321/ibdata1 -f /xfs/mysql3321/test/testibd.ibd -d test -t testibd

输出结果
Initializing table definitions...Processing table: SYS_TABLES - total fields: 10 - nullable fields: 6 - minimum header size: 5 - minimum rec size: 21 - maximum rec size: 555Processing table: SYS_INDEXES - total fields: 9 - nullable fields: 5 - minimum header size: 5 - minimum rec size: 29 - maximum rec size: 165Setting SPACE=1 in SYS_TABLE for `test`.`testibd`Check if space id 1 is already usedPage_id: 8, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 8 50 3 2 0 0 0 0 0 Db/table: infimumSpace id: 1768842857 (0x696E6669)Next record at offset: 8DRecord position: 8DChecking field lengths for a row (SYS_TABLES): OFFSETS: 16 11 17 24 32 36 40 48 52 52 Db/table: SYS_FOREIGNSpace id: 0 (0x0)Next record at offset: D5Record position: D5Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 16 22 29 37 41 45 53 57 57 Db/table: SYS_FOREIGN_COLSSpace id: 0 (0x0)Next record at offset: 122Record position: 122Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 12 18 25 33 37 41 49 53 53 Db/table: test/testibdSpace id: 2 (0x2)Next record at offset: 74Space id 1 is not used in any of the records in SYS_TABLESPage_id: 8, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 8 50 3 2 0 0 0 0 0 Db/table: infimumSpace id: 1768842857 (0x696E6669)Next record at offset: 8DRecord position: 8DChecking field lengths for a row (SYS_TABLES): OFFSETS: 16 11 17 24 32 36 40 48 52 52 Db/table: SYS_FOREIGNSpace id: 0 (0x0)Next record at offset: D5Record position: D5Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 16 22 29 37 41 45 53 57 57 Db/table: SYS_FOREIGN_COLSSpace id: 0 (0x0)Next record at offset: 122Record position: 122Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 12 18 25 33 37 41 49 53 53 Db/table: test/testibdSpace id: 2 (0x2)Updating test/testibd (table_id 17) with id 0x01000000SYS_TABLES is updated successfullyInitializing table definitions...Processing table: SYS_TABLES - total fields: 10 - nullable fields: 6 - minimum header size: 5 - minimum rec size: 21 - maximum rec size: 555Processing table: SYS_INDEXES - total fields: 9 - nullable fields: 5 - minimum header size: 5 - minimum rec size: 29 - maximum rec size: 165Setting SPACE=1 in SYS_INDEXES for TABLE_ID = 17Page_id: 11, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 50 7 2 0 0 0 0 TABLE_ID: 3798561113125514496SPACE: 1768842857Next record at offset: 8CRecord position: 8CChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 35 39 43 47 TABLE_ID: 11SPACE: 0Next record at offset: CERecord position: CEChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 36 40 44 48 TABLE_ID: 11SPACE: 0Next record at offset: 111Record position: 111Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 36 40 44 48 TABLE_ID: 11SPACE: 0Next record at offset: 154Record position: 154Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 35 39 43 47 TABLE_ID: 12SPACE: 0Next record at offset: 22CRecord position: 22CChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 44 48 52 56 TABLE_ID: 17SPACE: 2Updating SPACE(0x00000001 , 0x01000000) for TABLE_ID: 17sizeof(s)=4Next record at offset: 74SYS_INDEXES is updated successfully
Copy after login

 

Step 7 : 使用percona recovery tool 重新checksum ibdata

重复执行以下命令,直到程序没有输出为止。

shell> /root/install/percona-data-recovery-tool-for-innodb-0.5/innochecksum -f /xfs/mysql3321/ibdata1

输出结果
page 8 invalid (fails old style checksum)page 8: old style: calculated = 0xF4AD74CB; recorded = 0xEECB309Dfixing old checksum of page 8page 8 invalid (fails new style checksum)page 8: new style: calculated = 0x6F0C29B4; recorded = 0x3D02308Cfixing new checksum of page 8page 11 invalid (fails old style checksum)page 11: old style: calculated = 0x3908087C; recorded = 0xF9E8D30Cfixing old checksum of page 11page 11 invalid (fails new style checksum)page 11: new style: calculated = 0xB26CFD77; recorded = 0xDB25D39Dfixing new checksum of page 11
Copy after login

 

Step 8 : 启动mysql服务

shell> service mysqld3321 start

 

参考文档:

http://www.chriscalender.com/?p=28

http://www.mysqlperformanceblog.com/2011/05/13/connecting-orphaned-ibd-files/

http://blogs.innodb.com/wp/2012/04/innodb-transportable-tablespaces/

 

 

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to create a new simulator in the Lightning Simulator - How to create a new simulator in the Lightning Simulator How to create a new simulator in the Lightning Simulator - How to create a new simulator in the Lightning Simulator Mar 05, 2024 pm 03:04 PM

Recently, some users who have just downloaded and contacted the lightning simulator software asked the editor how to create a new simulator in the lightning simulator? The following content brings the method of creating a new simulator in the lightning simulator. Let us take a look below. Find the lightning simulator software. Double-click to open the Thunderbolt multi-opener. Click New/Copy Simulator. Click New Simulator. You can get a new simulator. Find the lightning simulator software, double-click to open the lightning multi-opener, click New/Copy Simulator, click New Simulator, and you will get a new simulator.

How to create a new Word document on your computer How to create a new Word document on your computer Dec 27, 2023 pm 10:05 PM

There are many ways to create a new word document. We can right-click the desktop and click New Doc Document or Docx Document. The specific methods are almost the same. Let’s take a look at them together. How to create a new word document using a computer: win11: 1. First, we right-click "New". 2. Then select the "docx or doc" file. 3. Finally, the creation can be completed. win10: 1. We right-click and select "New". 2. Then select one of the two documents. 3. Finally, the creation is completed. win7: 1. First, right-click and select "New". 2. Then select the word document. 3. Finally, the new creation can be completed.

How to implement the statement to view table data in MySQL? How to implement the statement to view table data in MySQL? Nov 08, 2023 pm 01:40 PM

Title: Statements and specific code examples for viewing table data in MySQL MySQL is an open source relational database management system that is widely used in applications of all sizes. In MySQL, viewing table data is a very basic operation. The following will introduce how to implement this operation through specific statements and code examples. First, we will introduce the statements and specific code examples for viewing table data through the MySQL command line tool. Suppose we have a table named "employees", the following is the pass

How to create a gray version of a new spanned volume on Win10 disk How to create a gray version of a new spanned volume on Win10 disk Jul 02, 2023 am 08:13 AM

What should I do if the newly created spanned volume on Win10 disk is grayed out? Sometimes there are multiple unused spaces in our hard disk. We can merge them and put them in a logical volume, so that the space on multiple hard disks can be used more efficiently. This is a spanned volume. However, some netizens said that their win10 system had problems when creating a new spanned volume. They found that the new spanned volume on the disk was not grayed out. What happened? How to create a new spanned volume? The editor below will teach you the correct method of creating a new cross-zone volume. Gray solution steps for creating a spanned volume on Win10 disk First, as shown in the figure below, the space on the two hard disks can use the same volume, and just create a spanned volume. Then the reason why the spanned volume cannot be grayed out may be due to the following factors: 1. If it contains a span

Step-by-step guide for creating folders on Realme mobile phone Step-by-step guide for creating folders on Realme mobile phone Mar 23, 2024 pm 03:51 PM

Step-by-Step Guide to Creating Folders on Realme Mobile Phones In our daily lives, we use our phones to store a variety of files, including photos, videos, documents, and more. However, as time goes by, the number of files on your phone may increase, causing insufficient memory on your phone or making it difficult to manage files. In order to better organize files and improve the efficiency of mobile phone use, the establishment of mobile phone folders has become particularly important. Realme Mobile is a popular smartphone brand with a powerful system and diverse features. Creating folders on Realme phones can help users better organize files

How to implement the statement of renaming table in MySQL? How to implement the statement of renaming table in MySQL? Nov 08, 2023 pm 12:11 PM

MySQL is a commonly used relational database management system that supports the operation of renaming tables. Normally, renaming a table carries certain risks, so you should be very careful when performing this operation. In this article, we will explore how to implement the rename table statement in MySQL and provide detailed code examples. In MySQL, you can use the ALTERTABLE statement to rename a table. The following is the basic syntax of the ALTERTABLE rename statement: ALTERTABLEo

Detailed method for creating a new monthly report on Enterprise WeChat Detailed method for creating a new monthly report on Enterprise WeChat Mar 25, 2024 pm 05:36 PM

1. Open Enterprise WeChat. 2. Click the [App] icon button on the left. 3. Click the [Report], [New], [Monthly Report] options. 4. Then you can see that you need to enter this month’s work, etc. 5. Enter the content of this month’s work. You may or may not fill in the rest, and click the [Submit] button. 6. Finally, you can see the details of your monthly report.

Solve the problem that there is no 'New' option on the right click of Win10 Solve the problem that there is no 'New' option on the right click of Win10 Jan 05, 2024 am 09:45 AM

When using win10 system, some friends find that there is no new option in the right-click menu when they right-click the mouse on the desktop. In fact, they only need to add an option to it through the registry to right-click and create a new one. What to do if there is no New option when right-clicking on Windows 10: 1. Press the key combination + Open Run, enter and click OK to open the Registry Editor. 2. Then expand the folders:. 3. Then find the folder. If there is no such folder, right-click the folder-- and name it. 4. After opening the file in the new folder, we change this numerical data to. 5. Then restart the computer to solve the problem.

See all articles