MySQL 备份恢复案例之 Xtrabackup

Xtrabackup是一个第三方的备份工具

Xtrabackup是一个开源、免费的支持对innodb存储引擎进行热备份的软件。由percona公司发布并支持。

Xtrabackup可以办到:

不暂停MySQL服务创建innodb热备份;

为MySQL做增量备份;

在MySQL服务器之间做在线表迁移;

使得创建MySQL replication更加容易;

备份MySQL但不增加服务器的负载

Xtrabackup介绍:

说明:MySQL-5.7适用于Xtrabackup-2.4

xtrabackup是Percona公司CTO Vadim参与开发的一款基于InnoDB的在线热备工具,具有开源,免费,支持在线热备,备份恢复速度快,占用磁盘空间小等特点,并且支持不同情况下的多种备份形式。xtrabackup的官方下载地址为:http://www.percona.com/software/percona-xtrabackup。

xtrabackup包含两个主要的工具,即xtrabackup和innobackupex 。二者区别如下:

(1)xtrabackup只能备份innodb和xtradb两种引擎的表,而不能备份myisam引擎的表;

(2)innobackupex是一个封装了xtrabackup的Perl脚本,支持同时备份innodb和myisam,但在对myisam备份时需要加一个全局的读锁。还有就是myisam不支持增量备份。

Xtrabackup2.2版之前包括4个可执行文件:

innobackupex: Perl 脚本

xtrabackup: C/C++ 编译的二进制

xbcrypt: 加解密

xbstream: 支持并发写的流文件格式

xtrabackup 是用来备份 InnoDB 表的,不能备份非 InnoDB 表,和 MySQL Server 没有交互

innobackupex 脚本用来备份非 InnoDB 表,同时会调用 xtrabackup 命令来备份 InnoDB 表,还会和 MySQL Server 发送命令进行交互,如加全局读锁(FTWRL)、获取位点(SHOW SLAVE STATUS)等。即innobackupex是在 xtrabackup 之上做了一层封装实现的

虽然目前一般不用 MyISAM 表,只是 MySQL 库下的系统表是 MyISAM 的,因此备份基本都通过 innobackupex 命令进行

Xtrabackup的新版变化:

xtrabackup版本升级到2.4后,相比之前的2.1有了比较大的变化:innobackupex 功能全部集成到 xtrabackup 里面,只有一个 binary程序,另外为了兼容考虑,innobackupex作为 xtrabackup 的软链接,即xtrabackup现在支持非Innodb表备份,并且Innobackupex将在下一版本中移除,建议通过xtrabackup替换innobackupex

xtrabackup安装:

最新版本下载安装:

https://www.percona.com/downloads/XtraBackup/LATEST/

[root@CentOS ~]#

[root@CentOS ~]# yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm #安装yum源

[root@CentOS ~]#

[root@CentOS ~]# yum install percona-xtrabackup-24.x86_64 #安装XtraBackup 2.4

[root@CentOS ~]#

[root@CentOS ~]# rpm -qa |grep xtrabackup #查看xtrabackup是否被安装

percona-xtrabackup-24-2.4.19-1.el7.x86_64

[root@CentOS ~]#

[root@CentOS ~]# xtrabackup –v #查看xtrabackup版本

xtrabackup: recognized server arguments: --datadir=/var/lib/mysql

xtrabackup version 2.4.19 based on MySQL server 5.7.26 Linux (x86_64) (revision id: c2d69da)

[root@CentOS ~]#

mysql>

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| caacsc |

| mysql |

| performance_schema |

| sys |

+--------------------+

5 rows in set (0.00 sec)

mysql>

xtrabackup第1次全备:

创建用于备份恢复的用户 backup 并赋予权限:

mysql>

mysql> create user backup@'localhost' identified by 'Bak.2020';

Query OK, 0 rows affected (0.10 sec)

mysql>

mysql> grant reload,process,lock tables,replication client on . to backup@localhost;

Query OK, 0 rows affected (0.01 sec)

mysql>

创建备份文件的存放目录:

[root@CentOS ~]#

[root@CentOS ~]# ll /opt/backup/

总用量 0

drwxr-xr-x 2 root root 6 4月 2 11:44 full_bak

drwxr-xr-x 2 root root 6 4月 2 11:44 increment_bak

[root@CentOS ~]#

1)数据库第一次全备

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --defaults-file=/etc/my.cnf --user=backup --password='Bak.2020' --socket=/var/lib/mysql/mysql.sock /opt/backup/full_bak/

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第1张图片

可以看到整个备份过程:连接数据库,开始拷贝redo log,拷贝innodb表文件,锁表、拷贝非innodb表文件,停止拷贝redo log,解锁

[root@CentOS ~]#

[root@CentOS ~]# ll /opt/backup/full_bak/2020-04-02_11-50-00/

MySQL 备份恢复案例之 Xtrabackup_第2张图片

[root@CentOS ~]#

2)数据库第一次全备恢复

关闭数据库并删除数据文件

[root@CentOS ~]#

[root@CentOS ~]# systemctl stop mysqld #停止数据库服务

[root@CentOS ~]#

[root@CentOS ~]# mv /var/lib/mysql /root/mysql_bak #删除原来的数据文件

[root@CentOS ~]# ls

anaconda-ks.cfg mysql_bak

[root@CentOS ~]#

[root@CentOS ~]# ls /var/lib/mysql #原数据文件已被删除

ls: 无法访问/var/lib/mysql: 没有那个文件或目录

[root@CentOS ~]#

[root@CentOS ~]# mkdir /var/lib/mysql #建立新的数据文件目录

[root@CentOS ~]# ls /var/lib/mysql

[root@CentOS ~]#

3)准备一个完全备份

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --apply-log /opt/backup/full_bak/2020-04-02_11-50-00/ #prepare(准备)

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第3张图片

4)执行第一次全备恢复

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /opt/backup/full_bak/2020-04-02_11-50-00/ #恢复操作

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第4张图片

更改 data/ 目录权限并启动mysql:

[root@CentOS ~]#

[root@CentOS ~]# chown -R mysql:mysql /var/lib/mysql #更改权限

[root@CentOS ~]# systemctl start mysqld #启动mysql服务

[root@CentOS ~]#

[root@CentOS ~]# ll /var/lib/mysql

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第5张图片

可以看到数据库和表均已恢复:

mysql>

mysql> show databases;

MySQL 备份恢复案例之 Xtrabackup_第6张图片

xtrabackup增备

我们以前面所做的全备为基准,在其基础上做增量备份:

mysql>

mysql> create database test;

Query OK, 1 row affected (0.00 sec)

mysql>

mysql> use test;

Database changed

mysql>

mysql> create table tb2 (id int,name varchar(40));

Query OK, 0 rows affected (0.01 sec)

mysql>

mysql> insert into tb2 values (1,'aaa'),(2,'bbb'),(3,'ccc'),(26,'zzz');

Query OK, 4 rows affected (0.19 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql>

mysql> select * from tb2;

+------+------+

| id | name |

+------+------+

| 1 | aaa |

| 2 | bbb |

| 3 | ccc |

| 26 | zzz |

+------+------+

4 rows in set (0.00 sec)

mysql>

1)增量备份1

以全备为基准:/opt/backup/full_bak/2020-04-02_11-50-00/

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --defaults-file=/etc/my.cnf --user=backup --password='Bak.2020' --socket=/var/lib/mysql/mysql.sock --incremental /opt/backup/increment_bak/ --incremental-basedir=/opt/backup/full_bak/2020-04-02_11-50-00/ #第一次增量备份

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第7张图片

再往 tb2 里插入数据:

mysql>

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql>

mysql>

mysql> insert into tb2 values (201,'aaa'),(202,'bbb'),(203,'ccc'),(326,'zzz');

Query OK, 4 rows affected (0.00 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql>

mysql> select * from tb2;

+------+------+

| id | name |

+------+------+

| 1 | aaa |

| 2 | bbb |

| 3 | ccc |

| 26 | zzz |

| 201 | aaa |

| 202 | bbb |

| 203 | ccc |

| 326 | zzz |

+------+------+

8 rows in set (0.00 sec)

mysql>

2)增量备份2

以增备1为基准:/opt/backup/increment_bak/2020-04-02_12-39-35/

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --defaults-file=/etc/my.cnf --user=backup --password='Bak.2020' --socket=/var/lib/mysql/mysql.sock --incremental /opt/backup/increment_bak/ --incremental-basedir=/opt/backup/increment_bak/2020-04-02_12-39-35/ #第二次增备

[root@CentOS ~]#

200402 12:47:15 Finished backing up non-InnoDB tables and files

200402 12:47:15 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...

xtrabackup: The latest check point (for incremental): '2668063'

xtrabackup: Stopping log copying thread.

.200402 12:47:15 >> log scanned up to (2668072)

200402 12:47:15 Executing UNLOCK TABLES

200402 12:47:15 All tables unlocked

200402 12:47:15 [00] Copying ib_buffer_pool to /opt/backup/increment_bak/2020-04-02_12-47-13/ib_buffer_pool

200402 12:47:15 [00] ...done

200402 12:47:15 Backup created in directory '/opt/backup/increment_bak/2020-04-02_12-47-13/'

200402 12:47:15 [00] Writing /opt/backup/increment_bak/2020-04-02_12-47-13/backup-my.cnf

200402 12:47:15 [00] ...done

200402 12:47:15 [00] Writing /opt/backup/increment_bak/2020-04-02_12-47-13/xtrabackup_info

200402 12:47:15 [00] ...done

xtrabackup: Transaction log of lsn (2668063) to (2668072) was copied.

200402 12:47:15 completed OK!

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第8张图片

[root@CentOS ~]# ls /opt/backup/increment_bak/ #查看两个增量备份文件

2020-04-02_12-39-35 2020-04-02_12-47-13

[root@CentOS ~]#

[root@CentOS ~]#

[root@CentOS ~]# cat //opt/backup/increment_bak/2020-04-02_12-39-35/xtrabackup_checkpoints

backup_type = incremental

from_lsn = 2656696

to_lsn = 2665404

last_lsn = 2665413

compact = 0

recover_binlog_info = 0

flushed_lsn = 2665413

[root@CentOS ~]#

[root@CentOS ~]#

[root@CentOS ~]# cat //opt/backup/increment_bak/2020-04-02_12-47-13/xtrabackup_checkpoints

backup_type = incremental

from_lsn = 2665404

to_lsn = 2668063

last_lsn = 2668072

compact = 0

recover_binlog_info = 0

flushed_lsn = 2668072

[root@CentOS ~]#

3)Xtrabackup增备的恢复

增量备份的恢复需要有3个步骤:

准备一个完全备份(开始准备的全量备份要添加--redo-only参数);

准备增量备份到完全备份(开始准备的增量备份要添加--redo-only参数,到最后一次增量备份要去掉--redo-only) ;

对整体的完全备份进行恢复,回滚未提交的数据

准备一个全备:

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --apply-log --redo-only /opt/backup/full_bak/2020-04-02_11-50-00/

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第9张图片

将增备1应用到完全备份:(准备第一个增备)

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --apply-log --redo-only /opt/backup/full_bak/2020-04-02_11-50-00/ --incremental-dir=/opt/backup/increment_bak/2020-04-02_12-39-35/

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第10张图片

将增量2应用到完全备份,不加 --redo-only 参数 (准备第二个增备)

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --apply-log /opt/backup/full_bak/2020-04-02_11-50-00/ --incremental-dir=/opt/backup/increment_bak/2020-04-02_12-47-13/

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第11张图片

把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --apply-log /opt/backup/full_bak/2020-04-02_11-50-00/

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第12张图片

模拟删除测试:

mysql>

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql>

mysql> drop table tb2;

Query OK, 0 rows affected (0.00 sec)

mysql>

mysql> show tables;

Empty set (0.00 sec)

mysql>

[root@CentOS ~]#

[root@CentOS ~]# systemctl stop mysqld #停止服务

[root@CentOS ~]#

[root@CentOS ~]# mv /var/lib/mysql /root/mysql_bak_1 #删除数据文件

[root@CentOS ~]# ls

anaconda-ks.cfg mysql_bak mysql_bak_1

[root@CentOS ~]#

[root@CentOS ~]# mkdir /var/lib/mysql #创建新的数据文件目录

[root@CentOS ~]# ls /var/lib/mysql

[root@CentOS ~]#

Xtrabackup增备恢复

[root@CentOS ~]#

[root@CentOS ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /opt/backup/full_bak/2020-04-02_11-50-00/ #增量备份恢复

[root@CentOS ~]#

MySQL 备份恢复案例之 Xtrabackup_第13张图片

[root@CentOS ~]#

[root@CentOS ~]# chown -R mysql:mysql /var/lib/mysql #修改权限

[root@CentOS ~]#

[root@CentOS ~]# systemctl start mysqld #启动mysql服务

[root@CentOS ~]#

mysql>

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql>

mysql>

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| tb2 |

+----------------+

1 row in set (0.00 sec)

mysql>

mysql> desc tb2;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(40) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql>

mysql> select * from tb2;

+------+------+

| id | name |

+------+------+

| 1 | aaa |

| 2 | bbb |

| 3 | ccc |

| 26 | zzz |

| 201 | aaa |

| 202 | bbb |

| 203 | ccc |

| 326 | zzz |

+------+------+

8 rows in set (0.00 sec)

mysql>

你可能感兴趣的:(MySQL 备份恢复案例之 Xtrabackup)