Partial Backups

XtraBackup支持partial backups,这意味着你可以只备份部分表或库.要备份的表必须是独立表空间,即innodb_file_per_table=1
有一点需要注意的是,部分备份不要copy back prepared backup.restore partial backups应该用导入,而不是--copy-back

创建partial backups

有三种方式可以创建partial backups:
1.--include 支持正则
2.--tables-file 读取一个文件,备份其中写明的所有表
3.--databases 列出库名

--include方式

innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --include='^test[.]t.*[0-9]$' /data/mysqldata/backup/partial/

此方法发现一个问题

[mysql@master backup]$ ls partial/2016-08-19_18-42-28/test/
t1.frm  t2.MYD              test_null#P#p1.ibd  trb3.frm       trb3#P#p1.ibd  trb4.frm       trb4#P#p1.ibd
t1.ibd  t2.MYI              test_null#P#p2.ibd  trb3.par       trb3#P#p2.ibd  trb4.par       trb4#P#p2.ibd
t2.frm  test_null#P#p0.ibd  test_null#P#p3.ibd  trb3#P#p0.ibd  trb3#P#p3.ibd  trb4#P#p0.ibd  trb4#P#p3.ibd
备份中包含了test_null表,而test_null表并不匹配我的正则表达式,但是为什么备份到了?(test_null为分区表)

创建分区表thehe,分区名称p0 p1 p2 p3 p4
CREATE TABLE thehe (id INT, name VARCHAR(50), purchased DATE)
    PARTITION BY RANGE( YEAR(purchased) ) (
        PARTITION p0 VALUES LESS THAN (1990),
        PARTITION p1 VALUES LESS THAN (1995),
        PARTITION p2 VALUES LESS THAN (2000),
        PARTITION p3 VALUES LESS THAN (2005)
    );

创建分区表tpapa,分区名称 pa pb pc pd
CREATE TABLE tpapa (id INT, name VARCHAR(50), purchased DATE)
    PARTITION BY RANGE( YEAR(purchased) ) (
        PARTITION pa VALUES LESS THAN (1990),
        PARTITION pb VALUES LESS THAN (1995),
        PARTITION pc VALUES LESS THAN (2000),
        PARTITION pd VALUES LESS THAN (2005)
    );

再次备份
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --include='^test[.]t.*[0-9]$' /data/mysqldata/backup/partial3/

[mysql@master backup]$ ls partial3/2016-08-19_18-49-31/test/
t1.frm  t2.MYD          thehe#P#p1.ibd  trb3.frm       trb3#P#p1.ibd  trb4.frm       trb4#P#p1.ibd
t1.ibd  t2.MYI          thehe#P#p2.ibd  trb3.par       trb3#P#p2.ibd  trb4.par       trb4#P#p2.ibd
t2.frm  thehe#P#p0.ibd  thehe#P#p3.ibd  trb3#P#p0.ibd  trb3#P#p3.ibd  trb4#P#p0.ibd  trb4#P#p3.ibd
只有thehe被备份了
这说明,对于分区表,每个分区在这里被当做一个表,表名是 原table_name+分区名

创建分区表tpapa,分区名称 p1 pb pc p4
CREATE TABLE txxoo (id INT, name VARCHAR(50), purchased DATE)
    PARTITION BY RANGE( YEAR(purchased) ) (
        PARTITION pa VALUES LESS THAN (1990),
        PARTITION pb VALUES LESS THAN (1995),
        PARTITION pc VALUES LESS THAN (2000),
        PARTITION pd VALUES LESS THAN (2005)
    );

再次备份
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --include='^test[.]t.*[0-9]$' /data/mysqldata/backup/partial4/

[mysql@master backup]$ ls partial4/2016-08-19_18-58-39/test/
t1.frm  t2.MYD          thehe#P#p1.ibd  trb3.frm       trb3#P#p1.ibd  trb4.frm       trb4#P#p1.ibd
t1.ibd  t2.MYI          thehe#P#p2.ibd  trb3.par       trb3#P#p2.ibd  trb4.par       trb4#P#p2.ibd
t2.frm  thehe#P#p0.ibd  thehe#P#p3.ibd  trb3#P#p0.ibd  trb3#P#p3.ibd  trb4#P#p0.ibd  trb4#P#p3.ibd
没有备份txxoo,因为若备份p1 p4分区,整个表就是不完整的备份

--tables-file

--tables-file选项后面跟的是一个包含需要备份的表的列表文件.格式为每行一个表,表名以databasename.tablename为格式

cat >> backup_list.txt <<!
fandb.dept
test.app_phone_download_speed_detail
test.quarterly_report_status
!

innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --no-timestamp --tables-file=/data/mysqldata/backup/backup_list.txt /data/mysqldata/backup/list

在备份过程中,未包含在备份列表中的表会提示skipping
160822 14:33:16 [01] Skipping ./mysql/innodb_table_stats.ibd.

备份的结构,可见只包含了在备份列表中的表

[mysql@master backup]$ tree list/
list/
|-- backup-my.cnf
|-- fandb
|   |-- dept.frm
|   `-- dept.ibd
|-- ibdata1
|-- test
|   |-- app_phone_download_speed_detail.frm
|   |-- app_phone_download_speed_detail.ibd
|   |-- quarterly_report_status.frm
|   |-- quarterly_report_status.par
|   |-- quarterly_report_status#P#p0.ibd
|   |-- quarterly_report_status#P#p1.ibd
|   |-- quarterly_report_status#P#p2.ibd
|   |-- quarterly_report_status#P#p3.ibd
|   |-- quarterly_report_status#P#p4.ibd
|   |-- quarterly_report_status#P#p5.ibd
|   |-- quarterly_report_status#P#p6.ibd
|   |-- quarterly_report_status#P#p7.ibd
|   |-- quarterly_report_status#P#p8.ibd
|   `-- quarterly_report_status#P#p9.ibd
|-- xtrabackup_binlog_info
|-- xtrabackup_checkpoints
|-- xtrabackup_info
`-- xtrabackup_logfile

--databases

--database选项可以直接接收需要备份的"表"和"库"名,或接收一个包含所需备份表名的列表文件

innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --no-timestamp --databases="test.app_phone_download_speed_detail fandb" /data/mysqldata/backup/databases

上面的命令备份了单个test.app_phone_download_speed_detail和全部fandb库下的表
[mysql@master backup]$ tree databases/
databases/
|-- backup-my.cnf
|-- fandb
|   |-- db.opt
|   |-- dept.frm
|   |-- dept.ibd
|   |-- emp.frm
|   |-- emp.ibd
|   |-- incr.frm
|   |-- incr.ibd
|   |-- l_csv.CSM
|   |-- l_csv.CSV
|   |-- l_csv.frm
|   |-- master2.frm
|   |-- master2.ibd
|   |-- salary.frm
|   `-- salary.ibd
|-- ibdata1
|-- test
|   |-- app_phone_download_speed_detail.frm
|   `-- app_phone_download_speed_detail.ibd
|-- xtrabackup_binlog_info
|-- xtrabackup_checkpoints
|-- xtrabackup_info
`-- xtrabackup_logfile

文件列表
cat >> db_list.txt <<!
fandb
test.quarterly_report_status
!

innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup' --no-timestamp --databases=/data/mysqldata/backup/db_list.txt /data/mysqldata/backup/db_list

[mysql@master backup]$ tree db_list
db_list
|-- backup-my.cnf
|-- fandb
|   |-- db.opt
|   |-- dept.frm
|   |-- dept.ibd
|   |-- emp.frm
|   |-- emp.ibd
|   |-- incr.frm
|   |-- incr.ibd
|   |-- l_csv.CSM
|   |-- l_csv.CSV
|   |-- l_csv.frm
|   |-- master2.frm
|   |-- master2.ibd
|   |-- salary.frm
|   `-- salary.ibd
|-- ibdata1
|-- test
|   |-- quarterly_report_status.frm
|   |-- quarterly_report_status.par
|   |-- quarterly_report_status#P#p0.ibd
|   |-- quarterly_report_status#P#p1.ibd
|   |-- quarterly_report_status#P#p2.ibd
|   |-- quarterly_report_status#P#p3.ibd
|   |-- quarterly_report_status#P#p4.ibd
|   |-- quarterly_report_status#P#p5.ibd
|   |-- quarterly_report_status#P#p6.ibd
|   |-- quarterly_report_status#P#p7.ibd
|   |-- quarterly_report_status#P#p8.ibd
|   `-- quarterly_report_status#P#p9.ibd
|-- xtrabackup_binlog_info
|-- xtrabackup_checkpoints
|-- xtrabackup_info
`-- xtrabackup_logfile

Preparing Partial Backups

innobackupex --apply-log --export /path/to/partial/backup

在prepare过程中你会看到一些错误

[mysql@master backup]$ innobackupex --apply-log --export /data/mysqldata/backup/db_list

InnoDB: xtrabackup: Last MySQL binlog file position 2519, file name mysql-bin.000035
InnoDB: Failed to find tablespace for table `sakila`.`FTS_0000000000000203_00000000000001c5_INDEX_1` in the cache. Attempting to load the tablespace with space id 507
InnoDB: Operating system error number 2 in a file operation.
InnoDB: The error means the system cannot find the path specified.
InnoDB: If you are installing InnoDB, remember that you must create directories yourself, InnoDB does not create them.
InnoDB: Cannot open datafile for read-only: './sakila/FTS_0000000000000203_00000000000001c5_INDEX_1.ibd' OS error: 71
InnoDB: Operating system error number 2 in a file operation.
InnoDB: The error means the system cannot find the path specified.

只要最后出现 160822 21:52:11 completed OK! 就是成功的

官方文档中的解释

You may see warnings in the output about tables that don’t exist. This is because InnoDB -based engines stores its data dictionary inside the tablespace files besides the .frm files. innobackupex will use xtrabackup to remove the missing tables (those who weren’t selected in the partial backup) from the data dictionary in order to avoid future warnings or errors:

111225 0:54:06 InnoDB: Error: table 'mydatabase/mytablenotincludedinpartialb'
InnoDB: in InnoDB data dictionary has tablespace id 6,
InnoDB: but tablespace with that id or name does not exist. It will be removed from data dictionary.

You should also see the notification of the creation of a file needed for importing (.exp file) for each table included in the partial backup:

xtrabackup: export option is specified.
xtrabackup: export metadata of table 'employees/departments' to file .//departments.exp (2 indexes
xtrabackup: name=PRIMARY, id.low=80, page=3
xtrabackup: name=dept_name, id.low=81, page=4

需要注意的是,你可以对一个已经prepare的备份再一次执行 --apply-log --export来生产.exp文件

Restoring Partial Backups

在5.6版本前,即便你的innodb表开启了innodb_file_per_table 独立表空间,你也无法通过直接cp数据文件 来在server间复制表
但是有了PerconaXtrabackup,你懂的

首先export表
innobackupex --apply-log --export /path/to/backup
和之前的prepare是一样的,这个过程中会为每个innodb表创建.exp文件

[mysql@master backup]$ find /data/mysqldata/backup/db_list/ -name dept.*
/data/mysqldata/backup/db_list/fandb/dept.ibd
/data/mysqldata/backup/db_list/fandb/dept.frm
/data/mysqldata/backup/db_list/fandb/dept.cfg
/data/mysqldata/backup/db_list/fandb/dept.exp

其中除了.frm文件,剩下的三个文件是import过程中需要的

导入表:
1.创建表create table ..
2.discard tablespace
3.cp .ibd .cfg .exp文件到相应的目录中
4.import tablespace

(mysql@localhost) [(none)]> use fandb;
Database changed
(mysql@localhost) [fandb]> select * from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.01 sec)

(mysql@localhost) [fandb]> drop table dept;
Query OK, 0 rows affected (0.01 sec)

CREATE TABLE `dept` (
  `deptno` int(11) NOT NULL,
  `dname` varchar(14) DEFAULT NULL,
  `loc` varchar(13) DEFAULT NULL,
  PRIMARY KEY (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE dept DISCARD TABLESPACE;

cp /data/mysqldata/backup/db_list/fandb/dept.ibd /data/mysqldata/3306/data/fandb/
cp /data/mysqldata/backup/db_list/fandb/dept.cfg /data/mysqldata/3306/data/fandb/
cp /data/mysqldata/backup/db_list/fandb/dept.exp /data/mysqldata/3306/data/fandb/

ALTER TABLE dept IMPORT TABLESPACE;

(mysql@localhost) [fandb]> select * from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

导入与备份时不同的库也是可以的

(mysql@localhost) [test]> drop table dept;
Query OK, 0 rows affected (0.01 sec)

(mysql@localhost) [test]> CREATE TABLE `dept` (
    ->   `deptno` int(11) NOT NULL,
    ->   `dname` varchar(14) DEFAULT NULL,
    ->   `loc` varchar(13) DEFAULT NULL,
    ->   PRIMARY KEY (`deptno`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    ->
    -> ;
Query OK, 0 rows affected (0.00 sec)

(mysql@localhost) [test]> ALTER TABLE dept DISCARD TABLESPACE;
Query OK, 0 rows affected (0.01 sec)

cp /data/mysqldata/backup/db_list/fandb/dept.ibd /data/mysqldata/3306/data/test/
cp /data/mysqldata/backup/db_list/fandb/dept.cfg /data/mysqldata/3306/data/test/
cp /data/mysqldata/backup/db_list/fandb/dept.exp /data/mysqldata/3306/data/test/

(mysql@localhost) [test]> ALTER TABLE dept IMPORT TABLESPACE;
Query OK, 0 rows affected (0.01 sec)

(mysql@localhost) [test]>  select * from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

Partial backup 备份指定表/库的更多相关文章

  1. Percona XtraBackup的部分备份与恢复/单库备份/单表备份/指定库备份/指定表备份

    本文是翻译的Percona XtraBackup 2.2版的官方文档,原文地址在此:https://www.percona.com/doc/percona-xtrabackup/2.2/innobac ...

  2. Percona备份mysql全库及指定数据库(完整备份与增量备份)

    Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份) Xtrabackup简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对I ...

  3. Bat脚本备份sqlserver 表结构、存储过程、函数、指定表数据

    Bat脚本备份sqlserver 表结构.存储过程.指定表数据: @echo off cd /d %~dp0 ::备份表结构.存储过程和部分配置表的数据 set LogFile=report.log ...

  4. 【Oracle】整库导出后怎么恢复到指定用户的指定表

    在导出的时候,整库导出 这里使用的是dba权限 $exp "'/ as sysdba'" file=full20180227.dmp log=exp_full20180227.lo ...

  5. oracle imp导入库到指定表空间

    1.创建表空间 create tablespace example_tablespace datafile 'e:\****.dbf' size 10m reuse autoextend on nex ...

  6. Oracle 删除用户和表空间////Oracle创建删除用户、角色、表空间、导入导出、...命令总结/////Oracle数据库创建表空间及为用户指定表空间

    Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用如下命令来完成. 步骤一:  删除user drop ...

  7. 工作随笔——使用svnsync实时备份SVN版本库

    前段时间把SVN版本库从win迁移到了Linux上,没隔几天那台win的磁盘就严重坏道了....这TMD什么运气! 花费了点时间研究了下svn自己的同步工具.写个日志记录下. 注意:svnsync要求 ...

  8. Windows Server Backup 备份Hypver-V VM

    在Windows Server 2012中,可以通过Windows Server Backup备份Hypver-V VM.在还原时,将会还原到Hypver-V管理器中. 设置只保存2个备份副本,命令如 ...

  9. 利用Azure backup备份和恢复Azure虚拟机(1)

    中国区Azure最近发布了关于使用Azure Backup来备份VM服务,于3月1日正式上线,该功能对于需要对关键工作负载进行备份的用户来讲,极大的降低了操作复杂度.以前我们所使用Powershell ...

随机推荐

  1. BZOJ1093 [ZJOI2007]最大半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

  2. Redis(三)节省内部空间优化

    总体原则:key的名称不易过长,剩下的所有 能用纯数字表示的尽量用 Redis的每一个键值都是用一个redisObject结构体表示的结构体中有:    键值的类型(string/list/hash/ ...

  3. 使用Goertzel算法识别DTMF信号

    Goertzel算法 Goertzel算法由Gerald Goertzel在1958年提出,用于数字信号处理,是属于离散傅里叶变换的范畴,目的是从给定的采样中求出某一特定频率信号的能量,用于有效性的评 ...

  4. auth用户认证库

    关于auth库,建议如下:1. ion_auth,基于Redux重写而成,非常不错的认证库,国外用的很多,几个最新的ci2.0.2基础上的开源系统(如doveforum)都用它,支持ci 2.0和以上 ...

  5. About Sustainability

    我不喜欢人们常说"紧张感", "危机感".这只会让人觉得疲惫,无非是自己拿鞭子抽着自己,最终会觉得难以继续.那么快乐到底从哪里来?成就感到底从哪里来呢? 我更喜 ...

  6. android之handle

    Android中异步消息处理主要由四个部分组成,Message.handler.messageQueue和looper. 1.message message是线程之间传递的消息,他可以在内部携带少量的 ...

  7. c#中如何获取listbox中选中值的问题

    StringBuilder ConponPerson = new StringBuilder();            for (int i = 0; i < lb_tzs.Items.Cou ...

  8. Linux用户和组的管理操作

    用户和组的管理操作 命令中涉及的单词和简写: 用户:user:组:group : 增加:add :  修改:modify  mod ;  删除 : delete  del 增加用户     /etc/ ...

  9. PP常用bapi

    PPCO0012 co01/CO02/CO03屏幕bapi  生产订单:显示/更改订单抬头数据 PPCO0001 开发应用程序: PP订单 PPCO0007 保存生产订单时退出 PPCO0001  A ...

  10. textArea 高度自适应

    <textarea name="apparatus" class="dhxTextArea" style="width:100%;height: ...