天天看点

运维实战:Xtrabackup备份与还原运维实战:percona-xtrabackup备份与还原一、工作原理二、版本区别三、Xtrabckup特点及限制四、xtrabckup安装(mariadb5.5  xtrabckup 2.4)五、Xtrabackup增量备份及还原实战 

目录

运维实战:percona-xtrabackup备份与还原

一、工作原理

二、版本区别

三、Xtrabckup特点及限制

3.1 特点

3.2 限制

四、xtrabckup安装(mariadb5.5  xtrabckup 2.4)

4.1 rpm安装xtrabackup

4.2 xtrabackup的rpm包含哪些内容

4.2.1 xtrabackup主要的两个工具

4.3 查看MariaDB的存储版本及存储引擎

五、Xtrabackup增量备份及还原实战

5.1 测试环境介绍

5.2 Xtrabackup 使用

5.2.1 Xtrabackup 增量备份

5.3 Xtrabackup恢复演练

5.3.1 模拟故障

5.3.2 数据恢复

5.3.3 备份生成的文件

5.3.4 Innobackup常用参数

运维实战:percona-xtrabackup备份与还原

      Xtrabackup是由percona开源的免费数据库热备份软件,它能对InnoDB数据库和XtraDB存储引擎进行热备,对Myisam引擎实现几乎热备。

一、工作原理

       在InnoDB内部会维护一个redo日志文件,叫做事务日志文件。Xtrabackup所有的备份操作是基于日志序列号(LSN)实现的。通过判断LSN的变化从而做增量备份。

LSN:可以判断当前所处的位置点,及LSN变化表示数据发生修改。

二、版本区别

Xtrabckup版本 MySQL版本支持
8.0 8.0
2.4 5.7,5.6,5.5
2.3 5.6,5.5
2.2 5.5

                                                                                             图1. 版本支持

三、Xtrabckup特点及限制

3.1 特点

  1. 备份过程快速、可靠;
  2. 备份过程不会打断正在执行的事务;
  3. 能够支持压缩功能,解压磁盘空间和流量;
  4. 自动失效备份检验;
  5. 还原速度快;

3.2 限制

  1. xtrabckup的高级功能使用,需要开启独立表空间(innodb_file_per_table=ON);
  2. xtrabckup对于Myisam不能实现实际意义上的热备,只能支持温备;

四、xtrabckup安装(mariadb5.5  xtrabckup 2.4)

4.1 rpm安装xtrabackup

 官方下载地址:https://www.percona.com/downloads/Percona-XtraBackup-2.4/

运维实战:Xtrabackup备份与还原运维实战:percona-xtrabackup备份与还原一、工作原理二、版本区别三、Xtrabckup特点及限制四、xtrabckup安装(mariadb5.5  xtrabckup 2.4)五、Xtrabackup增量备份及还原实战 

                                                                                 图4.1  xtrabckup的版本

4.2 xtrabackup的rpm包含哪些内容

/usr/bin/innobackupex
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/lib64/xtrabackup/plugin/keyring_file.so
/usr/lib64/xtrabackup/plugin/keyring_vault.so
/usr/share/doc/percona-xtrabackup-24-2.4.16
/usr/share/doc/percona-xtrabackup-24-2.4.16/COPYING
/usr/share/man/man1/innobackupex.1.gz
/usr/share/man/man1/xbcrypt.1.gz
/usr/share/man/man1/xbstream.1.gz
/usr/share/man/man1/xtrabackup.1.gz
           

4.2.1 xtrabackup主要的两个工具

/usr/bin/innobackupex # 封装了xtrabackup,同时可以备份MyISAM数据表;

/usr/bin/xtrabackup   # 只能备份InnoDB和XtraDB两种数据表;
           

4.3 查看MariaDB的存储版本及存储引擎

MariaDB [(none)]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                    | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                  | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                                      | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                                         | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                                      | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                                     | NO           | NO   | NO         |
| FEDERATED          | YES     | FederatedX pluggable storage engine                                        | YES          | NO   | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                         | NO           | NO   | NO         |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                     | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)

MariaDB [(none)]> SYSTEM mysql --version
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
           

五、Xtrabackup增量备份及还原实战

5.1 测试环境介绍

[[email protected] ~]# grep ^innodb /etc/my.cnf
innodb_file_per_table = ON

# 进入数据库
MariaDB [(none)]> CREATE DATABASE db1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW CREATE DATABASE db1;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| db1      | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> USE db1;
Database changed

MariaDB [db1]> CREATE TABLE tb1(id int(10))ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

MariaDB [db1]> DESC tb1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(10) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec) 

MariaDB [db1]> SHOW TABLE STATUS\G
*************************** 1. row ***************************
           Name: tb1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 10485760
 Auto_increment: NULL
    Create_time: 2019-11-11 11:58:56
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

MariaDB [db1]> INSERT INTO tb1 VALUES(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [db1]> SELECT * FROM tb1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)
           

5.2 Xtrabackup 使用

[[email protected] ~]# mkdir /backup
# 创建备份目录;

[[email protected] ~]# innobackupex --uesr=root /backup/
-----
-----
191111 15:05:19 completed OK!
# 全备,尾行为成功提示;

[[email protected] 2019-11-11_15-23-19]# cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 1604444
last_lsn = 1604444
compact = 0
recover_binlog_info = 0
flushed_lsn = 1604444
# 查看备份状态;
           

5.2.1 Xtrabackup 增量备份

MariaDB [db1]> INSERT INTO tb1 VALUES(4),(5),(6);
MariaDB [db1]> SELECT * FROM tb1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
+------+
6 rows in set (0.00 sec)
# 有意增加几行数据;

[[email protected] ~]# innobackupex --uesr=root --incremental /backup/ --incremental-basedir=/backup/2019-11-11_15-23-19/
--------------
--------------
--------------
xtrabackup: Transaction log of lsn (1605583) to (1605583) was copied.
191111 15:24:51 completed OK!
# 基于之前一次全量做增量;

[[email protected] 2019-11-11_15-24-49]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 1604444
to_lsn = 1605583
last_lsn = 1605583
compact = 0
recover_binlog_info = 0
flushed_lsn = 1605583
# 查看增量的备份状态;
           

5.3 Xtrabackup恢复演练

5.3.1 模拟故障

[[email protected] ~]# rm -fr /var/lib/mysql/*
# 删除所有数据文件;
           

5.3.2 数据恢复

恢复过程

1. 准备全量备份
innobackup --apply-log --redo-only 全备数据

2.合并增量备份
innobackup --apply-log --redo-only 全备数据 --incremental-dir=增量1

3.如果有多个增量则依次合并
innobackup --apply-log --redo-only 全备数据 --incremental-dir=增量{N}

4.还原主分支全备
innobackupex --copy-back 全备数据
           

开始恢复

注意:MySQL在做增量恢复操作时应该注意提交事务继续提交,未提交事务不能回滚!!!
[[email protected] ~]# innobackupex --user=root --apply-log --redo-only /backup/2019-11-11_15-23-19/
# 准备全备;

[[email protected] ~]# innobackupex --user=root --apply-log --redo-only /backup/2019-11-11_15-23-19/ --incremental-dir=/backup/2019-11-11_15-24-49/
----------
----------
----------
191111 15:41:47 completed OK!
# 将增量合并到主干上;

[[email protected] ~]# cat /backup/2019-11-11_15-23-19/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 1605583
last_lsn = 1605583
compact = 0
recover_binlog_info = 0
flushed_lsn = 1605583
# 检查主干数据的LSN;

[[email protected] ~]# innobackupex --user=root --copy-back /backup/2019-11-11_15-23-19/
---------
---------
---------
191111 15:45:34 completed OK!

[[email protected] mysql]# ll
总用量 18452
drwxr-x--- 2 root root       35 11月 11 15:45 db1
-rw-r----- 1 root root 18874368 11月 11 15:45 ibdata1
drwxr-x--- 2 root root     4096 11月 11 15:45 mysql
drwxr-x--- 2 root root     4096 11月 11 15:45 performance_schema
drwxr-x--- 2 root root       20 11月 11 15:45 test
-rw-r----- 1 root root       26 11月 11 15:45 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root      529 11月 11 15:45 xtrabackup_info
-rw-r----- 1 root root        1 11月 11 15:45 xtrabackup_master_key_id
[[email protected] mysql]#  chown -R mysql. mysql

MariaDB [db1]> SELECT * FROM tb1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
+------+
# 检测恢复后的数据;
           

5.3.3 备份生成的文件

xtrabackup_checkpoints 
# 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;

xtrabackup_binlog_info
# mysql服务器当前正在使用的二进制日志文件及备份这一刻为止二进制日志事件位置;
xtrabackup_binlog_pos_innodb

# 二进制日志文件用于InnoDB或者XtraDB表的二进制日志文件的当前位置点;

xtrabackup_binary
# 备份中用到的xtrabackup的可执行文件;

backup-my.cnf
# 备份命令用到的配置选项信息;
           

5.3.4 Innobackup常用参数

--host
# 指定主机

--user
# 指定用户名

--password
# 指定密码

--port
# 指定端口

--databases
# 指定数据库

--incremental
# 创建增量备份

--incremental-basedir
# 指定包含完全备份的目录

--incremental-dir
# 指定包含增量备份的目录   

--redo-only
# 不回滚未提交事务

--copy-back
# 恢复备份目录

--stream=tar
# 输出格式以tar方式输出数据流

--target-dir=/backups
# 指明备份后的文件存放路径

--apply-log
# 对备份进行预处理操作
           

继续阅读