从MySQL全备中恢复Innodb单表

来源:互联网 发布:我要开淘宝店怎么开 编辑:程序博客网 时间:2024/04/27 20:02

First, you must meet certain prerequisites to be able to restore a ibd tablespace:

1.The ibd file must be from a consistent backup with all insert buffer entries merged  and have no uncommitted transactions in order to not be dependent of the shared 2.tablespace ibdata. That is, shutting down with innodb_fast_shutdown=0. We’ll use XtraBackup to avoid the server shutdown.
3.You must not drop, truncate or alter the schema of the table after the backup has been taken.
The variable innodb_file_per_table must be enabled.

[root@test bin]# ./innobackupex --defaults-file=/service/mysql5.5/my.cnf  --export /backup/5.5/InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oyand Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.This software is published underthe GNU GENERAL PUBLIC LICENSE Version 2, June 1991.131125 19:50:23  innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/service/mysql5.5/my.cnf;mysql_read_default_group=xtrabackup' (using password: NO).131125 19:50:23  innobackupex: Connected to MySQL serverIMPORTANT: Please check that the backup run completes successfully.           At the end of a successful backup run innobackupex           prints "completed OK!".innobackupex: Using mysql server version 5.5.25-loginnobackupex: Created backup directory /backup/5.5/2013-11-25_19-50-23131125 19:50:23  innobackupex: Starting ibbackup with command: xtrabackup_55  --defaults-file="/service/mysql5.5/my.cnf"  --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/backup/5.5/2013-11-25_19-50-23 --tmpdir=/tmpinnobackupex: Waiting for ibbackup (pid=13361) to suspendinnobackupex: Suspend file '/backup/5.5/2013-11-25_19-50-23/xtrabackup_suspended_2'xtrabackup_55 version 2.1.5 for Percona Server 5.5.31 Linux (x86_64) (revision id: 680)xtrabackup: uses posix_fadvise().xtrabackup: cd to /data/mysql5.5xtrabackup: using the following InnoDB configuration:xtrabackup:   innodb_data_home_dir = ./xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextendxtrabackup:   innodb_log_group_home_dir = ./xtrabackup:   innodb_log_files_in_group = 3xtrabackup:   innodb_log_file_size = 5242880>> log scanned up to (1600610)[01] Copying ./ibdata1 to /backup/5.5/2013-11-25_19-50-23/ibdata1[01]        ...done[01] Copying ./test/t2.ibd to /backup/5.5/2013-11-25_19-50-23/test/t2.ibd[01]        ...done>> log scanned up to (1600610)xtrabackup: Creating suspend file '/backup/5.5/2013-11-25_19-50-23/xtrabackup_suspended_2' with pid '13361'131125 19:50:25  innobackupex: Continuing after ibbackup has suspended131125 19:50:25  innobackupex: Starting to lock all tables...131125 19:50:25  innobackupex: All tables locked and flushed to disk131125 19:50:25  innobackupex: Starting to backup non-InnoDB tables and filesinnobackupex: in subdirectories of '/data/mysql5.5'innobackupex: Backing up file '/data/mysql5.5/test/t2.frm'innobackupex: Backing up files '/data/mysql5.5/mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (72 files)>> log scanned up to (1600610)innobackupex: Backing up files '/data/mysql5.5/performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)131125 19:50:26  innobackupex: Finished backing up non-InnoDB tables and files131125 19:50:26  innobackupex: Waiting for log copying to finishxtrabackup: The latest check point (for incremental): '1600610'xtrabackup: Stopping log copying thread..>> log scanned up to (1600610)xtrabackup: Creating suspend file '/backup/5.5/2013-11-25_19-50-23/xtrabackup_log_copied' with pid '13361'xtrabackup: Transaction log of lsn (1600610) to (1600610) was copied.131125 19:50:27  innobackupex: All tables unlockedinnobackupex: Backup created in directory '/backup/5.5/2013-11-25_19-50-23'innobackupex: MySQL binlog position: filename 'ZWC-TEST.000003', position 420131125 19:50:27  innobackupex: Connection to database server closed131125 19:50:27  innobackupex: completed OK!


Truncate t2 table

root@test 07:52:19>show create table t2\G*************************** 1. row ***************************       Table: t2Create Table: CREATE TABLE `t2` (  `id` int(11) NOT NULL,  `name` varchar(10) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec)root@test 07:52:28>select count(*) from t2;+----------+| count(*) |+----------+|        2 |+----------+1 row in set (0.00 sec)root@test 07:52:36>alter table t2 discard tablespace;Query OK, 0 rows affected (0.00 sec)root@test 07:52:46>select count(*) from t2;ERROR 1030 (HY000): Got error -1 from storage engine

Begin recovery t2

Then apply the logs to get a consistent backup.

[root@test bin]# ./innobackupex --defaults-file=/service/mysql5.5/my.cnf --apply-log  --export /backup/5.5/2013-11-25_19-50-23/InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oyand Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.This software is published underthe GNU GENERAL PUBLIC LICENSE Version 2, June 1991.IMPORTANT: Please check that the apply-log run completes successfully.           At the end of a successful apply-log run innobackupex           prints "completed OK!".131125 19:53:30  innobackupex: Starting ibbackup with command: xtrabackup_55  --defaults-file="/service/mysql5.5/my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/backup/5.5/2013-11-25_19-50-23 --export --tmpdir=/tmpxtrabackup_55 version 2.1.5 for Percona Server 5.5.31 Linux (x86_64) (revision id: 680)xtrabackup: cd to /backup/5.5/2013-11-25_19-50-23xtrabackup: This target seems to be not prepared yet.xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1600610)xtrabackup: using the following InnoDB configuration for recovery:xtrabackup:   innodb_data_home_dir = ./xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextendxtrabackup:   innodb_log_group_home_dir = ./xtrabackup:   innodb_log_files_in_group = 1xtrabackup:   innodb_log_file_size = 2097152xtrabackup: using the following InnoDB configuration for recovery:xtrabackup:   innodb_data_home_dir = ./xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextendxtrabackup:   innodb_log_group_home_dir = ./xtrabackup:   innodb_log_files_in_group = 1xtrabackup:   innodb_log_file_size = 2097152xtrabackup: Starting InnoDB instance for recovery.xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)131125 19:53:30 InnoDB: The InnoDB memory heap is disabled131125 19:53:30 InnoDB: Mutexes and rw_locks use GCC atomic builtins131125 19:53:30 InnoDB: Compressed tables use zlib 1.2.3131125 19:53:30 InnoDB: Initializing buffer pool, size = 100.0M131125 19:53:30 InnoDB: Completed initialization of buffer pool131125 19:53:30 InnoDB: highest supported file format is Barracuda.InnoDB: The log sequence number in ibdata files does not matchInnoDB: the log sequence number in the ib_logfiles!131125 19:53:30  InnoDB: Database was not shut down normally!InnoDB: Starting crash recovery.InnoDB: Reading tablespace information from the .ibd files...InnoDB: Last MySQL binlog file position 0 420, file name ./ZWC-TEST.000003131125 19:53:30  InnoDB: Waiting for the background threads to start131125 19:53:31 Percona XtraDB (http://www.percona.com) 5.5.31-29.3 started; log sequence number 1600610xtrabackup: export option is specified.xtrabackup: export metadata of table 'test/t2' to file `./test/t2.exp` (1 indexes)xtrabackup:     name=PRIMARY, id.low=15, page=3[notice (again)]  If you use binary log and don't use any hack of group commit,  the binary log position seems to be:InnoDB: Last MySQL binlog file position 0 420, file name ./ZWC-TEST.000003xtrabackup: starting shutdown with innodb_fast_shutdown = 0131125 19:53:31  InnoDB: Starting shutdown...131125 19:53:35  InnoDB: Shutdown completed; log sequence number 1601824131125 19:53:35  innobackupex: Restarting xtrabackup with command: xtrabackup_55  --defaults-file="/service/mysql5.5/my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/backup/5.5/2013-11-25_19-50-23 --export --tmpdir=/tmpfor creating ib_logfile*xtrabackup_55 version 2.1.5 for Percona Server 5.5.31 Linux (x86_64) (revision id: 680)xtrabackup: cd to /backup/5.5/2013-11-25_19-50-23xtrabackup: This target seems to be already prepared.xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.xtrabackup: using the following InnoDB configuration for recovery:xtrabackup:   innodb_data_home_dir = ./xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextendxtrabackup:   innodb_log_group_home_dir = ./xtrabackup:   innodb_log_files_in_group = 3xtrabackup:   innodb_log_file_size = 5242880xtrabackup: using the following InnoDB configuration for recovery:xtrabackup:   innodb_data_home_dir = ./xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextendxtrabackup:   innodb_log_group_home_dir = ./xtrabackup:   innodb_log_files_in_group = 3xtrabackup:   innodb_log_file_size = 5242880xtrabackup: Starting InnoDB instance for recovery.xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)131125 19:53:35 InnoDB: The InnoDB memory heap is disabled131125 19:53:35 InnoDB: Mutexes and rw_locks use GCC atomic builtins131125 19:53:35 InnoDB: Compressed tables use zlib 1.2.3131125 19:53:35 InnoDB: Initializing buffer pool, size = 100.0M131125 19:53:35 InnoDB: Completed initialization of buffer pool131125 19:53:35  InnoDB: Log file ./ib_logfile0 did not exist: new to be createdInnoDB: Setting log file ./ib_logfile0 size to 5 MBInnoDB: Database physically writes the file full: wait...131125 19:53:35  InnoDB: Log file ./ib_logfile1 did not exist: new to be createdInnoDB: Setting log file ./ib_logfile1 size to 5 MBInnoDB: Database physically writes the file full: wait...131125 19:53:35  InnoDB: Log file ./ib_logfile2 did not exist: new to be createdInnoDB: Setting log file ./ib_logfile2 size to 5 MBInnoDB: Database physically writes the file full: wait...131125 19:53:35 InnoDB: highest supported file format is Barracuda.InnoDB: The log sequence number in ibdata files does not matchInnoDB: the log sequence number in the ib_logfiles!131125 19:53:35  InnoDB: Database was not shut down normally!InnoDB: Starting crash recovery.InnoDB: Reading tablespace information from the .ibd files...InnoDB: Last MySQL binlog file position 0 420, file name ./ZWC-TEST.000003131125 19:53:35  InnoDB: Waiting for the background threads to start131125 19:53:36 Percona XtraDB (http://www.percona.com) 5.5.31-29.3 started; log sequence number 1602060xtrabackup: export option is specified.xtrabackup: export metadata of table 'test/t2' to file `./test/t2.exp` (1 indexes)xtrabackup:     name=PRIMARY, id.low=15, page=3[notice (again)]  If you use binary log and don't use any hack of group commit,  the binary log position seems to be:InnoDB: Last MySQL binlog file position 0 420, file name ./ZWC-TEST.000003xtrabackup: starting shutdown with innodb_fast_shutdown = 0131125 19:53:36  InnoDB: Starting shutdown...131125 19:53:40  InnoDB: Shutdown completed; log sequence number 1602060131125 19:53:40  innobackupex: completed OK!

Copy the t2.ibd files from the backup to the database data directory

[root@test bin]# cp /backup/5.5/2013-11-25_19-50-23/test/t2.ibd /data/mysql5.5/test/[root@test bin]# chown mysql.mysql /data/mysql5.5/test/t2.ibd 

Import tablespace.

root@test 07:55:55>select count(*) from t2;ERROR 1030 (HY000): Got error -1 from storage engineroot@test 07:55:56>root@test 07:55:56>root@test 07:55:57>alter table t2 import tablespace;Query OK, 0 rows affected (0.00 sec)root@test 07:56:04>select count(*) from t2;+----------+| count(*) |+----------+|        2 |+----------+1 row in set (0.00 sec)root@test 07:56:08>select * from t2;+----+------+| id | name |+----+------+|  1 | aaa  ||  2 | bbb  |+----+------+2 rows in set (0.00 sec)root@test 07:56:13>