Preface
 
    We all know that Xtrabackup is a backup tool of percona for innodb or Xtradb.It's usually used to back up whole databases physically.But how to restore a single innodb table from a full Xtrabackup after dropping it by accident?We can also make use of the feature of transportable tablespace to accomplish it.
    I've demonstrated how to restore a table on a server to another using transportable tablespace in my yesterday's blog.Today,we'll use another tool called "mysqlfrm" to fetch the structure of table in .frm files.
 
Introduce
 
    mysqlfrm is a tool designed to diagnose information in .frm file when in recovery scenario.mysqlfrm provides two modes of operatins.By default,it creates a new instance referencing the base directory using "--basedir" it also need a port specified by "--port" which ought to be diffrent from the one used in the original instance.The other mode is connecting to the already exist instance using "--server".The new instance will be shutdown and all temperary files will be deleted after it reads data in .frm files.Further more,there're two exclusions when using mysqlfrm,one is foreign key constraints,the other one is auto increment number sequences.
 
Example
 
Install mysqlfrm tool.
  1. [root@zlm1 :: ~]
  2. #yum install mysql-utilities
  3.  
  4. Installed:
  5. mysql-utilities.noarch :1.3.-.el7
  6.  
  7. Dependency Installed:
  8. mysql-connector-python.noarch :1.1.-.el7
  9.  
  10. Complete!
Generate a Xtrabackup backup.
  1. [root@zlm1 :: ~]
  2. #innobackupex --defaults-file=/data/mysql/mysql3306/my3306.cnf --host=localhost --user=root --password=Passw0rd --sock=/tmp/mysql3306.sock /data/backup
  3. :: innobackupex: Starting the backup operation
  4.  
  5. IMPORTANT: Please check that the backup run completes successfully.
  6. At the end of a successful backup run innobackupex
  7. prints "completed OK!".
  8.  
  9. //Omitts the intermedia output.
  10.  
  11. :: Finished backing up non-InnoDB tables and files
  12. :: [] Writing xtrabackup_binlog_info
  13. :: [] ...done
  14. :: Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
  15. xtrabackup: The latest check point (for incremental): ''
  16. xtrabackup: Stopping log copying thread.
  17. . :: >> log scanned up to ()
  18.  
  19. :: Executing UNLOCK TABLES
  20. :: All tables unlocked
  21. :: [] Copying ib_buffer_pool to /data/backup/--06_10--/ib_buffer_pool
  22. :: [] ...done
  23. :: Backup created in directory '/data/backup/2018-07-06_10-09-22'
  24. MySQL binlog position: filename 'mysql-bin.000071', position '', GTID of the last change '2a4b3562-2ab6-11e8-be7a-080027de0e0e:1-12715494'
  25. :: [] Writing backup-my.cnf
  26. :: [] ...done
  27. :: [] Writing xtrabackup_info
  28. :: [] ...done
  29. xtrabackup: Transaction log of lsn () to () was copied.
  30. :: completed OK!
  31.  
  32. [root@zlm1 :: ~]
  33. #cd /data/backup
  34.  
  35. [root@zlm1 :: /data/backup]
  36. #ls -l
  37. total
  38. drwxr-x--- root root Jul : --06_10--
  39.  
  40. [root@zlm1 :: /data/backup]
  41. #cd --06_10--/
  42.  
  43. [root@zlm1 :: /data/backup/--06_10--]
  44. #ls -l
  45. total
  46. drwxr-x--- root root Jul : aaron8219
  47. -rw-r----- root root Jul : backup-my.cnf
  48. drwxr-x--- root root Jul : -help
  49. -rw-r----- root root Jul : ib_buffer_pool
  50. -rw-r----- root root Jul : ibdata1
  51. drwxr-x--- root root Jul : mysql
  52. drwxr-x--- root root Jul : performance_schema
  53. drwxr-x--- root root Jul : sys
  54. drwxr-x--- root root Jul : sysbench
  55. -rw-r----- root root Jul : xtrabackup_binlog_info
  56. -rw-r----- root root Jul : xtrabackup_checkpoints
  57. -rw-r----- root root Jul : xtrabackup_info
  58. -rw-r----- root root Jul : xtrabackup_logfile
  59. drwxr-x--- root root Jul : zlm
Prepare the backup.
  1. [root@zlm1 :: /data/backup/--06_10--]
  2. #innobackupex --defaults-file=/data/mysql/mysql3306/my3306.cnf --host=localhost --user=root --password=Passw0rd --sock=/tmp/mysql3306.sock --apply-log /data/backup/--06_10--/
  3. :: innobackupex: Starting the apply-log operation
  4.  
  5. IMPORTANT: Please check that the apply-log run completes successfully.
  6. At the end of a successful apply-log run innobackupex
  7. prints "completed OK!".
  8.  
  9. //Omitts the intermedia output.
  10.  
  11. InnoDB: Database was not shutdown normally!
  12. InnoDB: Starting crash recovery.
  13. InnoDB: xtrabackup: Last MySQL binlog file position , file name mysql-bin.
  14. InnoDB: Removed temporary tablespace data file: "ibtmp1"
  15. InnoDB: Creating shared tablespace for temporary tables
  16. InnoDB: Setting file './ibtmp1' size to MB. Physically writing the file full; Please wait ...
  17. InnoDB: File './ibtmp1' size is now MB.
  18. InnoDB: redo rollback segment(s) found. redo rollback segment(s) are active.
  19. InnoDB: non-redo rollback segment(s) are active.
  20. InnoDB: Waiting for purge to start
  21. InnoDB: 5.7. started; log sequence number
  22. xtrabackup: starting shutdown with innodb_fast_shutdown =
  23. InnoDB: page_cleaner: 1000ms intended loop took 10865ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
  24. InnoDB: FTS optimize thread exiting.
  25. InnoDB: Starting shutdown...
  26. InnoDB: Shutdown completed; log sequence number
  27. :: completed OK!
Drop two tables to mimic misoperation.
  1. root@localhost:mysql3306.sock [(none)]>show tables from zlm;
  2. +----------------+
  3. | Tables_in_zlm |
  4. +----------------+
  5. | customer |
  6. | goods |
  7. | semi_sync_test |
  8. | test_flashbk |
  9. | test_myisam |
  10. +----------------+
  11. rows in set (0.00 sec)
  12.  
  13. root@localhost:mysql3306.sock [(none)]>show tables from sysbench;
  14. +--------------------+
  15. | Tables_in_sysbench |
  16. +--------------------+
  17. | sbtest1 |
  18. | sbtest10 |
  19. | sbtest2 |
  20. | sbtest3 |
  21. | sbtest4 |
  22. | sbtest5 |
  23. | sbtest6 |
  24. | sbtest7 |
  25. | sbtest8 |
  26. | sbtest9 |
  27. +--------------------+
  28. rows in set (0.00 sec)
  29.  
  30. root@localhost:mysql3306.sock [(none)]>drop table zlm.test_flashbk,sysbench.sbtest1;
  31. Query OK, rows affected (0.11 sec)
Diagnose .frm file from Xtrabackup using mysqlfrm.
  1. [root@zlm1 :: /data/backup/--06_10--]
  2. #mysqlfrm --basedir=/usr/local/mysql --port= --diagnostic /data/backup/--06_10--/sysbench/sbtest1.frm /data/backup/--06_10--/zlm/zlm:test_flashbk.frmsbtest1.frm /data/backup/--06_10--/zlm/zlm:tes
  3. # WARNING The --port option is not used in the --diagnostic mode.
  4. # WARNING: Cannot generate character set or collation names without the --server option.
  5. # CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
  6. # Reading .frm file for /data/backup/--06_10--/sysbench/sbtest1.frm:
  7. # The .frm file is a TABLE.
  8. # CREATE TABLE Statement:
  9.  
  10. CREATE TABLE `sysbench`.`sbtest1` (
  11. `id` int() NOT NULL AUTO_INCREMENT,
  12. `k` int() NOT NULL,
  13. `c` char() NOT NULL,
  14. `pad` char() NOT NULL,
  15. PRIMARY KEY `PRIMARY` (`id`),
  16. KEY `k_1` (`k`)
  17. ) ENGINE=InnoDB;
  18.  
  19. # Reading .frm file for /data/backup/--06_10--/zlm/test_flashbk.frm:
  20. # The .frm file is a TABLE.
  21. # CREATE TABLE Statement:
  22.  
  23. CREATE TABLE `zlm`.`test_flashbk` (
  24. `id` bigint() NOT NULL AUTO_INCREMENT,
  25. `name` varchar() NOT NULL,
  26. PRIMARY KEY `PRIMARY` (`id`)
  27. ) ENGINE=InnoDB;
  28.  
  29. #...done.
  30.  
  31. //If you want to get the information of character set,"--server" is indispensable.
  32. //you can either use <dbname>:<tablename>.frm or just <tablename>.frm.
  33. //"--port" can be omitted.
Create vacant table using above create statement.
  1. root@localhost:mysql3306.sock [(none)]>CREATE TABLE `sysbench`.`sbtest1` (
  2. -> `id` int() NOT NULL AUTO_INCREMENT,
  3. -> `k` int() NOT NULL,
  4. -> `c` char() NOT NULL,
  5. -> `pad` char() NOT NULL,
  6. -> PRIMARY KEY `PRIMARY` (`id`),
  7. -> KEY `k_1` (`k`)
  8. -> ) ENGINE=InnoDB;
  9. ERROR (): Column length too big for column 'c' (max = ); use BLOB or TEXT instead
  10. root@localhost:mysql3306.sock [(none)]>CREATE TABLE `zlm`.`test_flashbk` (
  11. -> `id` bigint() NOT NULL AUTO_INCREMENT,
  12. -> `name` varchar() NOT NULL,
  13. -> PRIMARY KEY `PRIMARY` (`id`)
  14. -> ) ENGINE=InnoDB;
  15. Query OK, rows affected (0.02 sec)
  16.  
  17. //We get an error when creating table sysbench.sbtest1 beause of the overload value of char.
Check the structure of  sbtest2 table in sysbench.
  1. root@localhost:mysql3306.sock [(none)]>show create table sysbench.sbtest2\G
  2. *************************** . row ***************************
  3. Table: sbtest2
  4. Create Table: CREATE TABLE `sbtest2` (
  5. `id` int() NOT NULL AUTO_INCREMENT,
  6. `k` int() NOT NULL DEFAULT '',
  7. `c` char() NOT NULL DEFAULT '', //In the counterpart table,the value is 120.
  8. `pad` char() NOT NULL DEFAULT '', //In the counterpart table,the value is 60.
  9. PRIMARY KEY (`id`),
  10. KEY `k_2` (`k`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8
  12. row in set (0.00 sec)
Change the create statement reference to the value in sbtest2.
  1. when creating table sysbench.sbtest1 beause of the overload value of char.
  2.  
  3. Check the structure of sbtest2 table in sysbench.
  4. root@localhost:mysql3306.sock [(none)]>show create table sysbench.sbtest2\G
  5. *************************** . row ***************************
  6. Table: sbtest2
  7. Create Table: CREATE TABLE `sbtest2` (
  8. `id` int() NOT NULL AUTO_INCREMENT,
  9. `k` int() NO
Add a write lock on these two tables.
  1. root@localhost:mysql3306.sock [(none)]>lock tables sysbench.sbtest1 write;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. root@localhost:mysql3306.sock [(none)]>lock tables zlm.test_flashbk write;
  5. Query OK, rows affected (0.00 sec)
  6.  
  7. root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 discard tablespace;
  8. ERROR (HY000): Table 'sbtest1' was not locked with LOCK TABLES //when locks another table,the lock on previous table will be released.
  9. root@localhost:mysql3306.sock [(none)]>lock tables sysbench.sbtest1 write; //This time,lock one and discard one in order.
  10. Query OK, rows affected (0.00 sec)
  11.  
  12. root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 discard tablespace;
  13. Query OK, rows affected (0.00 sec)
  14.  
  15. root@localhost:mysql3306.sock [(none)]>lock tables zlm.test_flashbk write;
  16. Query OK, rows affected (0.00 sec)
  17.  
  18. root@localhost:mysql3306.sock [(none)]>alter table zlm.test_flashbk discard tablespace;
  19. Query OK, rows affected (0.00 sec)
Copy .ibd files from Xtrabackup and change privilege.
  1. [root@zlm1 :: /data/backup/--06_10--]
  2. #cp sysbench/sbtest1.ibd /data/mysql/mysql3306/data/sysbench
  3.  
  4. [root@zlm1 :: /data/backup/--06_10--]
  5. #cp zlm/test_flashbk.ibd /data/mysql/mysql3306/data/zlm
  6.  
  7. [root@zlm1 :: /data/backup/--06_10--]
  8. #chown -R mysql.mysql /data/mysql/mysql3306/data
  9.  
  10. [root@zlm1 :: /data/backup/--06_10--]
  11. #ls -l /data/mysql/mysql3306/data/sysbench | grep sbtest1.ibd
  12. -rw-r----- mysql mysql Jul : sbtest1.ibd
  13.  
  14. [root@zlm1 :: /data/backup/--06_10--]
  15. #ls -l /data/mysql/mysql3306/data/zlm | grep test_flashbk.ibd
  16. -rw-r----- mysql mysql Jul : test_flashbk.ibd
Import tablespaces and check data of tables.
  1. root@localhost:mysql3306.sock [(none)]>alter table sysbench.sbtest1 import tablespace;
  2. Query OK, rows affected, warning (1.73 sec)
  3.  
  4. root@localhost:mysql3306.sock [(none)]>show warnings;
  5. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  6. | Level | Code | Message |
  7. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  8. | Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './sysbench/sbtest1.cfg', will attempt to import without schema verification |
  9. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  10. row in set (0.00 sec)
  11.  
  12. root@localhost:mysql3306.sock [(none)]>alter table zlm.test_flashbk import tablespace;
  13. Query OK, rows affected, warning (1.01 sec)
  14.  
  15. root@localhost:mysql3306.sock [(none)]>show warnings;
  16. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | Level | Code | Message |
  18. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | Warning | | InnoDB: IO Read error: (, No such file or directory) Error opening './zlm/test_flashbk.cfg', will attempt to import without schema verification |
  20. +---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  21. row in set (0.00 sec)
  22.  
  23. root@localhost:mysql3306.sock [(none)]>select count(*) from sysbench.sbtest1;
  24. +----------+
  25. | count(*) |
  26. +----------+
  27. | |
  28. +----------+
  29. row in set (0.25 sec)
  30.  
  31. root@localhost:mysql3306.sock [(none)]>select count(*) from zlm.test_flashbk;
  32. +----------+
  33. | count(*) |
  34. +----------+
  35. | |
  36. +----------+
  37. row in set (0.10 sec)
  38.  
  39. //The warnings show that tthe message about missing of .cfg file what rally doesn't matter.
  40. //The .cfg file is usually create by executing "flush table ... for export;"
  41. //We can benifit in crash recover scenario with the support of ignoring the missing of .cfg in transportable tablespace feature.
Summary
  • mysqlfrm is a tool of mysql-utilities which is specialized in analyzing .frm files in order to gain the missing structure of tables.
  • mysqlfrm provides two modes of operation:1. connecting to server with "--server"(defaut mode);2. create a new instance with "--basedir".
  • With the help of parameter "--diagnostic",we can even get information from a .frm file without installing a MySQL server on the host.
  • We cannot get character set and collation information on tables if we forget to use "--server" option.
  • Of course,in order to use transportable tablespace properly,the parameter "innodb_file_per_table=1" is necessary.
  • In my case above,the structure of table about char datatype changed accidently which I'm still baffled with.
  • Also,we can restore these table to any other server like transportable tablespace does.
 

基于Xtrabackup恢复单个innodb表的更多相关文章

  1. 从完整备份恢复单个innodb表

    现在大多数同学在线上采取的备份策略都是xtrabackup全备+binlog备份,那么当某天某张表意外的删除那么如何从xtrabackup全备中恢复呢?从mysql 5.6版本开始,支持可移动表空间( ...

  2. MySQL 备份恢复(导入导出)单个 innodb表

    MySQL 备份恢复单个innodb表呢,对于这种恢复我们我们很多朋友都不怎么了解了,下面一起来看一篇关于MySQL 备份恢复单个innodb表的教程 在实际环境中,时不时需要备份恢复单个或多个表(注 ...

  3. xtrabackup 恢复单个表【转】

    一.安装与备份 1. 下载安装XtraBackup$wget http://www.percona.com/redir/downloads/XtraBackup/LATEST/binary/tarba ...

  4. 【理论篇】Percona XtraBackup 恢复单表

    小明在某次操作中,误操作导致误删除了某个表,需要立即进行数据恢复. 如果是数据量较小的实例,并且有备份,即便是全备,做一次全量恢复,然后单表导出导入,虽然麻烦一点,却也花不了多少时间:如果是数据量大的 ...

  5. mysql如何从全备文件中恢复单个库或者单个表

    mysql如何从全备文件中恢复单个库或者单个表 在mysql dba的日常实际工作中,一个实例下有多个库,而我们常见的备份就是全库备份.那么问题就来了,如果需要恢复单个库或者单个表,怎么办了,网上有很 ...

  6. 从xtraback 备份文件中 单独恢复一张 innodb 表

    从xtraback 备份文件中 单独恢复一张 innodb 表 http://blog.sina.com.cn/s/blog_445e807b0101dbgw.html 能够恢复一张表的前提是独立表空 ...

  7. 从MySQL全备文件中恢复单个库或者单个表

    从MySQL全备文件中恢复单个库或者单个表 提取建库语句 sed -n '/^-- Current Database: db_cms/,/^-- Current Database: `/p' back ...

  8. 基于xtrabackup实现mysql备份还原

    简介 Xtrabackup2.2版之前包括4个可执行文件: innobackupex: Perl 脚本 xtrabackup: C/C++ 编译的二进制 xbstream: 支持并发写的流文件格式 x ...

  9. 14.1.2 InnoDB表最佳实践:

    14.1.2 Best Practices for InnoDB Tables InnoDB表最佳实践: 这个章节描述使用InnoDB表最佳实践: 1.指定一个主键用于每个表使用最多查询的列或者多列, ...

随机推荐

  1. Jquery load()加载GB2312页面时出现乱码的解决方法

    问题描述:jquery的字符集是utf-8,load方法加载完GB2312编码静态页面后,出现中文乱码. a.php <script language="javascript" ...

  2. PS基础,数学,语文

    PS基础(修图) 污点修复画笔工具---设置画笔大小---设置类型(内容识别)---修改图片---完成. 修复画笔工具---设置画笔大小---设置源(取样)---修改图片---完成. 修补工具---设 ...

  3. ios 你必须了解的系统定义宏使用

    1. UNAVAILABLE_ATTRIBUTE __attribute__((unavailable)) - (instancetype)init UNAVAILABLE_ATTRIBUTE; 告诉 ...

  4. vue.js ------ 大牛和网站

    hellogirl前端网站 : http://www.jqhtml.com/category/article FungLeo: http://blog.csdn.net/FungLeo/article ...

  5. Windows之CMD查看系统信息

    Windows 系统通过命令行(CMD)查询系统信息有两种方式: 1.图形化界面: 在“运行”中键入CMD,然后输入 dxdiag,回车后弹出图形化界面 ------ DirectX 诊断工具. 2. ...

  6. laravel的nginx配置

    最近阅读laravel官方文档 发现了关于nginx的推荐配置 Nginx 如果你使用 Nginx ,在你的网站配置中加入下述代码将会转发所有的请求到 index.php 前端控制器. locatio ...

  7. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  8. Refactoring in Coding

    Make changes on existing code for subsequent and constant changes of requirement. Reference:http://w ...

  9. 易客CRM-3.0.4 (OpenLogic CentOS 6.5)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache1.3.8 centos6.5 mysql5.1.72 php5.2.17 commercial crm linux 服务优惠价: 按服 ...

  10. SSM事务

    问题描述:查询用户信息时想级联查出用户订单以及订单详情,在查询用户的时候JDBC是will be managed by Spring,但懒加载用户订单以及订单详情时就will not be manag ...