数据恢复实例演示:
xtrabackup全备+binlog备份,恢复单个表单
下面进行从xtrabackup全备恢复单表的测试。
1. 开启了参数innodb_file_per_table
5.7.26 默认是开启的:
mysql> show variables like '%per_table%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
2. 安装工具:mysql-utilities,其中mysqlfrm可以读取表结构。
$ yum install mysql-utilities -y
3、Xtrabackup 安装
官网:
https://www.percona.com/doc/percona-xtrabackup/2.4/installation/yum_repo.html
下载:
$ wget https://repo.percona.com/yum/percona-release-latest.noarch.rpm
$ rpm -ivh percona-release-latest.noarch.rpm
$ yum list | grep percona
$ yum install percona-xtrabackup-24
删除:
yum remove percona-xtrabackup
查看原表中的数据:
mysql> select count(*) from sbtest.sbtest1;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (0.00 sec)
执行备份:
$ innobackupex --defaults-file=/etc/my.cnf --user=root --password=123456 /data/
重放数据:apply-log
$ innobackupex --defaults-file=/etc/my.cnf --apply-log /data/2018-03-21_08-09-43
删除sbtest1表
mysql> drop table sbtest.sbtest1;
利用mysql-utilities工具读取表结构(不支持MariaDB哦)
$ mysqlfrm --diagnostic /data/2018-03-21_08-09-43/sbtest/sbtest1.frm
得到表结构:
CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
加一个写锁,确保安全
mysql> lock tables sbtest1 write;
丢弃表空间
mysql> alter table sbtest1 discard tablespace;
Query OK, 0 rows affected (0.00 sec)
从备份中拷贝ibd文件,并且修改权限
$ cp -a /data/2018-03-21_08-09-43/sbtest/sbtest1.ibd /var/lib/mysql/sbtest/
$ chown -R mysql.mysql /var/lib/mysql/sbtest/sbtest1.ibd
这里有警告,可以忽略。详情可以看:https://yq.aliyun.com/articles/59271
查询数据是否一致:
mysql> alter table sbtest1 import tablespace;
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (0.00 sec)
最后解锁:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
网友评论