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. 在CentOS上搭建Storm集群

    Here's a summary of the steps for setting up a Storm cluster: Set up a Zookeeper clusterInstall depe ...

  2. IntelliJ设置鼠标悬浮提示和修改快捷键

    IntelliJ设置鼠标悬浮提示和修改快捷键 设置鼠标悬浮提示 修改快捷键 进入设置菜单 删除原来的快捷键(注:你可以选择保留原来的快捷键,同时使用两个快捷键) Good Luck

  3. 【活动】监控宝惹火Docker监控,开放试用中

    要说这两年最火爆的技术有哪些,Docker绝对是其中之一. 有人说,Docker缺少必要的运维监控工具,实践起来有难度. 幸福来的太快了. 云智慧旗下产品监控宝又惹火了,推出重量级新功能——Docke ...

  4. python学习之路——基础篇(3)模块(续)

    re正则表达式.shutil.ConfigParser.xml 一.re 正则元字符和语法: 语法 说明 表达式 完全匹配字符 字符 一般字符 匹配自身 abc abc . 匹配除换行符"\ ...

  5. Xilium.CefGlue CEF Chrome 自动上传文件不弹出对话框 CefDialogHandler

    关键代码如下,如何使用Handler,还不清楚的请继续搜索 internal sealed class WyzCefDialogHandler : CefDialogHandler    {      ...

  6. linux 搭建svn

    1          安装SVN 官网下载:http://subversion.apache.org/packages.html SVN客户端:TortoiseSVN,官网下载:http://tort ...

  7. 纯js异步无刷新请求(只支持IE)

    纯js异步无刷新请求 下载地址:http://pan.baidu.com/s/1slakL1F 所以因为非IE浏览器都禁止跨域请求,所以以只支持IE. <HTML> <!-- 乱码( ...

  8. [OC笔记]@property之个人理解,大神轻拍

    /** * 一个简单的对象 * * @author suzhen * */ public class SimpleObjcet { /** * 声明一个age字段 */ private Object ...

  9. DOCTYPE的重要性

    <!DOCTYPE>是文档类型声明: 声明必须是 HTML 文档的第一行,位于 <html> 标签之前.明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTM ...

  10. docker--buildbot安装

    curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname - ...