时间:2021-07-01 10:21:17 帮助过:29人阅读
理解部分(及原理部分): XtraBackup是percona公司提供的开源工具,以热备Innodb表著称而被广泛采用。 XtraBackup对Innodb的备份之所以是热备,无需锁表,是基于Innodb自身的崩溃恢复机制,它首先复制所有的Innodb数据文件,这样复制出来的文件肯定是不一致
理解部分(及原理部分):
XtraBackup是percona公司提供的开源工具,以热备Innodb表著称而被广泛采用。
XtraBackup对Innodb的备份之所以是热备,无需锁表,是基于Innodb自身的崩溃恢复机制,它首先复制所有的Innodb数据文件,这样复制出来的文件肯定是不一致的,然后对每个文件进行崩溃恢复处理,最终达到一致。就和MySQL在启动Innodb的时候一样,会通过比较数据文件头和redo log文件头信息来检查数据是否是一致的,如果不一致就尝试通过前滚(把redo log中所有提交的事务写入数据文件)和回滚(从数据文件中撤销所有redo log中未提交的事务引起的修改)来使数据达到最终一致。
XtraBackup在启动的时候会记录一个LSN(log sequence number),然后就把所有的Innodb数据文件复制出来,这样复制出来的数据文件是不一致的,但是XtraBackup会在后台运行一个进程把所有对redo log file的修改记录下来,只要有了这个数据,就能进行崩溃恢复。只所以要额外记录下来,是因为MySQL自身的redo log file是可重用的。
以上的操作是由xtrabackup二进制程序(比如xtrabackup_55)完成的,如果使用innobackupex 脚本,刚才的步骤完成以后,innobackupex就会去备份MyISAM表和.frm文件,这时要保证数据的一致性就会先锁表了,通过FLUSH TABLES WITH READ LOCK命令锁表然后把文件复制出来,再释放掉这个锁。
在恢复数据的时候,要经过prepare(recovery)和restore两个步骤。在prepare结束以后,Innodb的表恢复到了复制Innodb文件结束的时间点,这个时间点也就是锁表复制MyISAM表的起点,所以最终数据是一致的。一般我们在恢复的时候执行两次prepare,是因为第二次prepare会帮助我们生成redo log文件,从而加快MySQL数据库启动的速度。
简单总结Xtrabackup:
Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点: (1)备份过程快速、可靠; (2)备份过程不会打断正在执行的事务; (3)能够基于压缩等功能节约磁盘空间和流量; (4)自动实现备份检验; (5)还原速度快; |
环境介绍:
平台:CentOS-6.5-x86_64(单台主机实现)
percona-XtraBackup版本:percona-xtrabackup-2.1.8-733.rhel6.x86_64.rpm
下面是 应用案列 的部署:
其最新版的软件可从 http://www.percona.com/software/percona-xtrabackup/ 安装包: percona-toolkit-2.2.4-1.noarch.rpm percona-xtrabackup-2.1.8-733.rhel6.x86_64.rpm yum安装即可: [root@localhost ~]# yum -y install ./*.rpm |
二、备份的实现
(一)完全备份
1.1 修改配置文件:
[root@localhost ~]# vim /etc/my.cnf 定位且添加: thread_concurrency = 4 datadir = /data/mydata innodb_file_per_table = ON 指定自定义路径: log-bin=/data/binlogs/master-bin |
1.2 停止MySQL服务:
[root@localhost ~]# service mysqld stopxtrabackup_checkpoints Shutting down MySQL.. [ OK ] [root@localhost ~]# |
1.3 创建binlogs目录:
[root@localhost ~]# mkdir /data/binlogs/ [root@localhost ~]# chown -R mysql:mysql /data/binlogs/ [root@localhost ~]# |
1.4 启动MySQL服务:
[root@localhost ~]# service mysqld start Starting MySQL.. [ OK ] [root@localhost ~]# |
1.5 连入MySQL查看日志:
[root@localhost ~]# mysql MariaDB [(none)]> SHOW BINARY LOGS; +-------------------+-----------+ | Log_name | File_size | +-------------------+-----------+ | master-bin.000001 | 313 | +-------------------+-----------+ 1 row in set (0.01 sec) MariaDB [(none)]> |
1.6 删除/data/mydata/目录下的mysql-bin.*日志文件(这个是为了后面不影响我们备份):
[root@localhost ~]# cd /data/mydata/ [root@localhost mydata]# ls aria_log.00000001 ib_logfile1 mysql performance_schema aria_log_control localhost.localdomain.err mysql-bin.000001 test ibdata1 localhost.localdomain.pid mysql-bin.index ib_logfile0 multi-master.info mysql-bin.state [root@localhost mydata]# rm -rf mysql-bin.* |
1.7 创建备份所需目录:
[root@localhost mydata]# mkdir /mybackups |
1.8 为了演示热备效果吧hellodb中的内容改下hellodb坐下修改:
è修改引擎: [root@localhost ~]# vim hellodb.sql %s/ENGINE=MyISAM/ENGINE=InnoDB/g [root@localhost ~]# |
1.9 查看都是InnoDB的引擎:
MariaDB [hellodb]> SHOW TABLE STATUS FROM hellodb; |
1.10 关闭二进制日志,且导入测试库hellodb:
[root@localhost ~]# mysql MariaDB [(none)]> set sql_log_bin=0; MariaDB [(none)]> source /root/hellodb.sql MariaDB [(none)]> set sql_log_bin=1; |
1.11 完全备份(注意:备份时候MySQL必须是运行状态,而在做数据恢复时候服务必须是处于停滞状态。)
[root@localhost ~]# innobackupex --user=root /mybackups/ 省略...出现此行可以了。 140307 09:43:00 innobackupex: completed OK! [root@localhost ~]# |
1.12 查看备份后的数据:
[root@localhost ~]# ls /mybackups/2014-03-07_09-42-56/ backup-my.cnf ibdata1 performance_schema xtrabackup_binary xtrabackup_checkpoints hellodb mysql test xtrabackup_binlog_info xtrabackup_logfile [root@localhost ~]# |
2.1 做一个完全备份还原恢复:
2.2 模拟停止服务及删除/data/mydata目录下的所有数据:(假设在备份前备份后没有做任何操作,怎么办呢)
[root@localhost ~]# service mysqld stop Shutting down MySQL.. [ OK ] [root@localhost ~]# cd /data/mydata/ [root@localhost mydata]# ls aria_log.00000001 hellodb ib_logfile0 localhost.localdomain.err mysql test aria_log_control ibdata1 ib_logfile1 multi-master.info performance_schema [root@localhost mydata]# rm -rf ./* |
2.3 先准备(prepare)一个完全备份:
[root@localhost mydata]# innobackupex --apply-log /mybackups/2014-03-07_09-42-56/ 看到如下几项就ok,一般在最后: xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1712150 140307 09:45:06 innobackupex: completed OK! [root@localhost mydata]# |
2.4 再从一个完全备份中恢复数据:
[root@localhost mydata]# innobackupex --copy-back /mybackups/2014-03-07_09-42-56/ 看到如下几项就ok,一般在最后: 140307 09:49:06 innobackupex: completed OK! [root@localhost mydata]# |
2.5 修改其属组属主:
[root@localhost mydata]# chown -R mysql:mysql ./* |
2.6 启动MySQL服务:
[root@localhost mydata]# service mysqld start Starting MySQL.. [ OK ] [root@localhost mydata]# |
2.7 查看是否已成功恢复:
MariaDB [(none)]> use hellodb Database changed MariaDB [hellodb]> SHOW TABLES; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ |
(二)使用innobackupex进行增量备份
1、如何做基于时间点(增量)恢复的数据呢?????
1.1 首先做一个完全备份:
[root@localhost ~]# innobackupex --user=root /mybackups/ 省略...出现此行可以了。 140307 09:51:33 innobackupex: completed OK! |
1.2 查看备份的数据(注意:每一次备份都会产生新的备份数据目录的):
[root@localhost ~]# ls /mybackups/2014-03-07_09-51-29/ backup-my.cnf ibdata1 performance_schema xtrabackup_binary xtrabackup_checkpoints hellodb mysql test xtrabackup_binlog_info xtrabackup_logfile [root@localhost ~]# |
1.3 为了做第一次增量备份,插入数据:
[root@localhost ~]# mysql MariaDB [(none)]> use hellodb MariaDB [hellodb]> CREATE TABLE tb1 (id int); MariaDB [hellodb]> INSERT INTO tb1 VALUES (1),(2); |
1.4 其次,在做第一次增量备份:
[root@localhost ~]# innobackupex --incremental /mybackups/ --incremental-basedir=/mybackups/2014-03-07_09-51-29/ 省略...出现此行可以了。 140307 09:56:31 innobackupex: completed OK! [root@localhost ~]# |
1.5 xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
1)查看完全与增量1的对比: [root@localhost ~]# cat /mybackups/2014-03-07_09-51-29/xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 1712180 last_lsn = 1712180 compact = 0 [root@localhost ~]# ==对比== [root@localhost ~]# cat /mybackups/2014-03-07_09-56-26/xtrabackup_checkpoints backup_type = incremental from_lsn = 1712180 to_lsn = 1721035 last_lsn = 1721035 compact = 0 [root@localhost ~]# |
2.1 为了做第二次增量备份,再次插入数据:
[root@localhost 2014-03-07_13-16-05]# mysql MariaDB [(none)]> CREATE DATABASE xiaoma; MariaDB [(none)]> use xiaoma MariaDB [xiaoma]> CREATE TABLE hehe1 (Name CHAR(30)); MariaDB [xiaoma]> \q |
2.2 其次,在做第二次增量备份:
[root@localhost ~]# innobackupex --incremental /mybackups/ --incremental-basedir=/mybackups/2014-03-07_09-56-26/ 省略...出现此行可以了。 140307 10:05:25 innobackupex: completed OK! [root@localhost ~]# |
2.3 查看xtrabackup_checkpoints文件信息:
[root@localhost mybackups]# cat /mybackups/2014-03-07_10-05-20/xtrabackup_checkpoints backup_type = incremental from_lsn = 1721035 to_lsn = 1728639 last_lsn = 1728639 compact = 0 [root@localhost mybackups]# |
2.4 假如说这时候我们又做了修改,而没有做增量备份,那么就从要最后一次增量备份中(如下信息的日志及位置)做相关数据恢复:
[root@localhost mybackups]# cat /mybackups/2014-03-07_10-05-20/xtrabackup_binlog_info master-bin.000002 874 [root@localhost mybackups]# |
2.5 那么就又再一次的插入数据(注意此时没有做任何备份):
[root@localhost 2014-03-07_13-24-26]# mysql MariaDB [(none)]> use hellodb MariaDB [hellodb]> INSERT INTO tb1 VALUES (3),(4); |
3、模拟数据损坏或操作失误:
[root@localhost ~]# service mysqld stop Shutting down MySQL.. [ OK ] [root@localhost ~]# rm -rf /data/mydata/* |
4、模拟将数据恢复:
4.1 先执行恢复完全备份:
[root@localhost ~]# innobackupex --apply-log --redo-only /mybackups/2014-03-07_09-51-29/ ... ... 140307 10:17:01 innobackupex: completed OK! [root@localhost ~]# |
4.2 接着执行恢复第一次增量备份:
[root@localhost ~]# innobackupex --apply-log --redo-only /mybackups/2014-03-07_09-51-29/ --incremental-dir=/mybackups/2014-03-07_09-56-26/ ... ... 140307 10:21:37 innobackupex: completed OK! [root@localhost ~]# |
4.3 最后执行第二次增量备份:
[root@localhost ~]# innobackupex --apply-log --redo-only /mybackups/2014-03-07_09-51-29/ --incremental-dir=/mybackups/2014-03-07_10-05-20/ ... ... 40307 10:22:31 innobackupex: completed OK! [root@localhost ~]# |
4.4 查看几次恢复合并后的文件(可以和之前做对比):
[root@localhost ~]# cd /mybackups/2014-03-07_09-51-29/ [root@localhost 2014-03-07_09-51-29]# ls backup-my.cnf ibdata1 performance_schema xiaoma2 xtrabackup_binlog_info xtrabackup_logfile hellodb mysql test xtrabackup_binary xtrabackup_checkpoints [root@localhost 2014-03-07_09-51-29]# |
4.5 查看xtrabackup_checkpoints文件信息:
[root@localhost ~]# cat /mybackups/2014-03-07_09-51-29/xtrabackup_checkpoints backup_type = full-prepared from_lsn = 0 to_lsn = 1728639 last_lsn = 1728639 compact = 0 [root@localhost ~]# |
4.6 查看完全备份与第二次备份的时间点:
[root@localhost 2014-03-07_09-51-29]# cat /mybackups/2014-03-07_09-51-29/xtrabackup_binlog_info master-bin.000002 874 [root@localhost 2014-03-07_09-51-29]# cat /mybackups/2014-03-07_10-05-20/xtrabackup_binlog_info master-bin.000002 874 [root@localhost 2014-03-07_09-51-29]# |
4.7 恢复数据(完全备份):
[root@localhost 2014-03-07_09-51-29]# innobackupex --copy-back /mybackups/2014-03-07_09-51-29/ ... ... 140307 10:33:42 innobackupex: completed OK! [root@localhost 2014-03-07_09-51-29]# |
4.8 修改器属主属组:
[root@localhost ~]# cd /data/mydata/ [root@localhost mydata]# chown -R mysql:mysql ./* |
4.9 查看且导出数据:
[root@localhost mydata]# mysqlbinlog --start-position=874 /data/binlogs/master-bin.000002 ... ... INSERT INTO tb1 VALUES (3),(4) ... ... [root@localhost mydata]# mysqlbinlog --start-position=874 /data/binlogs/master-bin.000002 > /tmp/inc.sql |
4.10 启动服务:
[root@localhost mydata]# service mysqld start Starting MySQL. [ OK ] [root@localhost mydata]# |
4.11 关闭二进制日志: MariaDB [hellodb]> set session sql_log_bin=0; 4.12 恢复刚刚保存在tmp下文件: MariaDB [hellodb]> source /tmp/inc.sql 4.13 开启二进制日志: MariaDB [hellodb]> set session sql_log_bin=1; |
4.14 查看hellodb中的表是否还原:
MariaDB [hellodb]> SHOW TABLES; +--------------------------+ | Tables_in_hellodb | +--------------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | | xioam1 | +--------------------------+ |
4.16 查看表中的数据是否还原:
MariaDB [hellodb]> SELECT * FROM xioam1; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ |
4.17 查看xiaoma2库:
MariaDB [hellodb]> MariaDB [hellodb]> use xiaoma2 Database changed MariaDB [xiaoma2]> SHOW TABLES; +-------------------+ | Tables_in_xiaoma2 | +-------------------+ | hehe1 | +-------------------+ |
(三)导入或导出单张表的实现???
假如说我想实现期望能够把hellodb中students表中的数据导出来,每一张表都给他单独进行导出。
1.1 将之前的所有备份删除,以免出问题,后备份:
[root@localhost ~]# rm -rf /mybackups/* [root@localhost ~]# innobackupex --user=root /mybackups/ 一般出现此行就可以了。 140307 14:16:20 innobackupex: completed OK! [root@localhost ~]# |
1.2 导出表:
[root@localhost ~]# innobackupex --apply-log --export /mybackups/2014-03-07_14-16-07/ 一般出现此行就可以了。 xtrabackup: starting shutdown with innodb_fast_shutdown = 0 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1745942 140307 14:18:14 innobackupex: completed OK! [root@localhost ~]# |
1.3 查看导出的表(看.exp的):
[root@localhost ~]# cd /mybackups/2014-03-07_14-16-07/hellodb/ [root@localhost hellodb]# ls classes.cfg coc.exp courses.frm scores.frm students.ibd testtb.cfg toc.exp xioam1.frm classes.exp coc.frm courses.ibd scores.ibd teachers.cfg testtb.exp toc.frm xioam1.ibd classes.frm coc.ibd db.opt students.cfg teachers.exp testtb.frm toc.ibd classes.ibd courses.cfg scores.cfg students.exp teachers.frm testtb.ibd xioam1.cfg coc.cfg courses.exp scores.exp students.frm teachers.ibd toc.cfg xioam1.exp [root@localhost hellodb]# |
1.4 导入表(要在mysql服务器上导入来自于其它服务器的某innodb表,需要先在当前服务器上创建一个跟原表表结构一致的表,而后才能实现将表导入):
MariaDB [(none)]> use hellodb MariaDB [hellodb]> SHOW CREATE TABLE students; 将如下命令复制后并且执行: CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Age` tinyint(3) unsigned NOT NULL, `Gender` enum('F','M') NOT NULL, `ClassID` tinyint(3) unsigned DEFAULT NULL, `TeacherID` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`StuID`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 MariaDB [hellodb]> |
2.1 创建一个mydb库:
MariaDB [hellodb]> CREATE DATABASE mydb; MariaDB [hellodb]> use mydb; |
2.2 在mydb中创建表students:
MariaDB [mydb]> CREATE TABLE `students` ( -> `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, -> `Name` varchar(50) NOT NULL, -> `Age` tinyint(3) unsigned NOT NULL, -> `Gender` enum('F','M') NOT NULL, -> `ClassID` tinyint(3) unsigned DEFAULT NULL, -> `TeacherID` int(10) unsigned DEFAULT NULL, -> PRIMARY KEY (`StuID`) -> ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 -> ; MariaDB [mydb]> |
2.3 查看表结构:
MariaDB [mydb]> DESC students; +-----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------------+------+-----+---------+----------------+ | StuID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | varchar(50) | NO | | NULL | | | Age | tinyint(3) unsigned | NO | | NULL | | | Gender | enum('F','M') | NO | | NULL | | | ClassID | tinyint(3) unsigned | YES | | NULL | | | TeacherID | int(10) unsigned | YES | | NULL | | +-----------+---------------------+------+-----+---------+----------------+ |
2.4 然后将此表的表空间删除:
MariaDB [mydb]> ALTER TABLE students DISCARD TABLESPACE; |
2.5 接下来,将来自于“导出”表的服务器的mytable表的mytable.ibd和mytable.exp文件复制到当前服务器的数据目录:
[root@localhost ~]# cd /mybackups/2014-03-07_14-16-07/hellodb/ [root@localhost hellodb]# cp students.exp students.ibd /data/mydata/mydb/ [root@localhost hellodb]# ls /data/mydata/mydb/ db.opt students.exp students.frm students.ibd [root@localhost hellodb]# |
2.6 修改其属组属主:
[root@localhost hellodb]# cd /data/mydata/mydb/ [root@localhost mydb]# chown -R mysql:mysql students.* |
3.1 然后使用如下命令将其导入:
MariaDB [mydb]> ALTER TABLE students IMPORT TABLESPACE; |
3.2 查看是否导入成功:
MariaDB [mydb]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ |
OK!!!