您的位置:首页 > 数据库 > MySQL

xtrabackup 备份mysql数据库三:partial backup 部分备份及恢复

2014-06-22 08:47 489 查看
innobackup 部分备份恢复实验

三种方式:
## Using the --include option
The command above will create a timestamped directory with the usual files that innobackupex creates,
but only the data files related to the tables matched.
Note that this option is passed to xtrabackup --tables and is matched against each table of each database,
the directories of each database will be created even if they are empty.
该方式会将没有备份表的数据库目录也生成,支持模式匹配

--备份test库t_innodb开头的表
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --include='^test[.]t_innodb'
--备份test库下所有的表
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --include='^test[.]'

## Using the --tables-file option
Note that this option is passed to xtrabackup --tables and is matched against each table of each database,
the directories of each database will be created even if they are empty.
该方式不能实现模式匹配,只生产需要备份表的数据库目录

--备份test.t_innodb,
$ echo "test.t_innodb" > /tmp/tables.txt
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --tables-file=/tmp/tables.txt /backup

## Using the --databases option
This option is specific to innobackupex and accepts either a space-separated list of the databases and
tables to backup - in the databasename[.tablename] form - or a file containing the list at one element per line
Currently, only .frm and non-InnoDB tables are limited by that option
当前版本,只对non-innodb 有限制作用

$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --databases="test.innodb" /backup

## 测试表 t_innodb ,并插入10000条记录
(root@localhost) [test]>desc t_innodb;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
| password | varchar(150) | YES | | NULL | |
| userstatus | int(2) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

(root@localhost) [test]>call addTest(10000,0);
Query OK, 1 row affected (4.12 sec)

(root@localhost) [test]>select count(*) from t_innodb;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (0.01 sec)

## 执行一次表t_innodb的备份,作为全备
$ echo "test.t_innodb" > /tmp/tables.txt
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --tables-file=/tmp/tables.txt /backup/full_innodb --no-timestamp

## 全备后,再次插入10000条记录
(root@localhost) [test]>call addTest(10000,0);
Query OK, 1 row affected (4.12 sec)

(root@localhost) [test]>select count(*) from t_innodb;
+----------+
| count(*) |
+----------+
| 20000 |
+----------+
1 row in set (0.01 sec)

## 执行一次增量备份
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --incremental --incremental-basedir=/backup/full_innodb --tables-file=/tmp/tables.txt /backup/inc_innodb --no-timestamp

## 删除原库中的标
(root@localhost) [test]>drop table t_innodb;
Query OK, 0 rows affected (0.01 sec)

## 针对全备做一次apply-log,需要使用redo-only 选项
innobackupex --apply-log --redo-only --export /backup/full_innodb

## 增量备份,做一次apply-log,作为最后一个增量,不要使用redo-only
innobackupex --defaults-file=/etc/my.cnf --apply-log --incremental-dir=/backup/full_innodb /backup/full_innodb

## 再次执行apply-log, 是用export 生成exp、cfg 文件
innobackupex --apply-log --export /backup/full_innodb

## 在数据库中新建表t_innodb
(root@localhost) [test]>drop table t_innodb;

## 可以是用mysqlfrm 从t_innodb 中获取建表语句
# 显示frm的建表语句
mysqlfrm --server=root:mysql@localhost:3306 /backup/full_innodb/test/t_innodb.frm --port=3333

(root@localhost) [test]>CREATE TABLE `t_innodb` (
-> `id` bigint(20) NOT NULL AUTO_INCREMENT,
-> `name` varchar(50) DEFAULT NULL,
-> `password` varchar(150) DEFAULT NULL,
-> `userstatus` int(2) DEFAULT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=20001 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

## 删除当前的.ibd文件
(root@localhost) [test]>ALTER TABLE test.t_innodb DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)

## 把cfg、ibd、exp文件放到test数据库目录中
[mysql@rhel5 test]$ cp t_innodb.cfg /usr/local/mysql/data/test/
[mysql@rhel5 test]$ cp t_innodb.ibd /usr/local/mysql/data/test/
[mysql@rhel5 test]$ cp t_innodb.exp /usr/local/mysql/data/test/

## 把备份的.ibd文件还原到表中
(root@localhost) [test]>ALTER TABLE test.t_innodb IMPORT TABLESPACE;

## 查询恢复的数据
(root@localhost) [test]>select count(*) from t_innodb;
+----------+
| count(*) |
+----------+
| 20000 |
+----------+
1 row in set (0.01 sec)本文出自 “天边云” 博客,请务必保留此出处http://cloudhorizon.blog.51cto.com/3296809/1535468
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: