您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL单表ibd文件恢复_MySQL

2024/12/27 13:19:54发布20次查看
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
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
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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product