因为percona打算放弃使用innobackupex备份工具,因此我们这里也说明一下innobackupex的兄弟工具xtraceback工具的使用

这个工具的安装可以参考上面的一些博文,上面详细说明了innobackupex的备份与使用:https://www.cnblogs.com/wxzhe/p/10033983.html

首先我们来看一下xtracbackup的帮助文档:

[root@test3 ~]# xtrabackup --help
xtrabackup: recognized server arguments:
xtrabackup: recognized client arguments: --user=root --password=* --user=root --password=* --host=localhost
xtrabackup version 2.4. based on MySQL server 5.7. Linux (x86_64) (revision id: 170eb8c)
Open source backup tool for InnoDB and XtraDB
......
Usage: [xtrabackup [--defaults-file=#] --backup | xtrabackup [--defaults-file=#] --prepare] [OPTIONS] Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
The following groups are read: xtrabackup mysqld
The following options may be given as the first argument:
.......
后面是一些参数信息

xtracebackup是一款物理的在线备份工具,那么其备份的原理是什么?我们应该知道在备份过程数据时可以写入的。

xtracebackup的备份过程详解:http://www.cnblogs.com/zhoujinyi/p/5888271.html

下面通过实例来说明使用xtracebackup的备份与恢复

[root@test3 backup]# xtrabackup --user=root --password= --backup --target-dir=backup/
xtrabackup: recognized server arguments:
xtrabackup: recognized client arguments: --user=root --password=* --user=root --password=* --host=localhost --user=root --password=* --backup= --target-dir=backup/
:: version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;host=localhost' as 'root' (using password: YES).
Failed to connect to MySQL server: DBI connect(';mysql_read_default_group=xtrabackup;host=localhost','root',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at - line 1314
:: Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
Failed to connect to MySQL server: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2). #这个报错是因为MySQL5.7是源码安装,sock文件不在默认的/var/lib/mysql下面,可以做个软连接指定到/var/lib/mysql/mysql.sock这里,
#或者使用“-S”参数,指定mysql.sock的文件位置

xtracebackup2.3版本之前不能备份innodb的表,而innobackupex可以备份innodb的表,因此在2.3之前的版本通常使用innobackupex,innobackupex对xtracebackup做了封装,在备份innodb的表时使用innobackupex,而在备份非innodb表时内部调用接口使用了xtracebackup工具。因为这两个工具中一个是使用perl写的,另一个是使用c写的,通信相对困难。在xtracebackup2.4之后,重写了这两个,都使用c写的,把innobackupex的功能附加到了xtracebackup中,也就是xtracebackup既可以备份非innodb表也可以备份innodb表。

下面通过一次完全备份,来理解xtracebackup备份的过程。

在备份过程中,您应该看到很多输出显示正在复制的数据文件,以及日志文件线程反复扫描日志文件并从中复制。

[root@test3 ~]# xtrabackup --user=root --password=  --backup --target-dir=backup/   #target-dir参数指定备份文件存在的目录
xtrabackup: recognized server arguments:
xtrabackup: recognized client arguments: --user=root --password=* --backup= --target-dir=backup/
:: version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root' (using password: YES).
:: version_check Connected to MySQL server
:: version_check Executing a version check against the server...
:: version_check Done.
:: Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set #连接mysql server
Using server version 5.7.
xtrabackup version 2.4. based on MySQL server 5.7. Linux (x86_64) (revision id: 170eb8c)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/mysql/ #首先备份innodb表
xtrabackup: open files limit requested , set to
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group =
xtrabackup: innodb_log_file_size =
InnoDB: Number of pools:
:: >> log scanned up to ()
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID for mysql/slave_worker_info, old maximum was
:: [] Copying ./ibdata1 to /root/backup/ibdata1
:: [] ...done
:: [] Copying ./mysql/slave_worker_info.ibd to /root/backup/mysql/slave_worker_info.ibd
:: [] ...done
:: [] Copying ./mysql/server_cost.ibd to /root/backup/mysql/server_cost.ibd ......... :: Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
:: Executing FLUSH TABLES WITH READ LOCK... #加上读锁,开始备份non-innodb表
:: Starting to backup non-InnoDB tables and files
:: [] Copying ./performance_schema/socket_summary_by_instance.frm to /root/backup/performance_schema/socket_summary_by_instance.frm
:: [] ...done
:: [] Copying ./performance_schema/socket_summary_by_event_name.frm to /root/backup/performance_schema/socket_summary_by_event_name.frm
:: [] ...done
:: [] Copying ./performance_schema/users.frm to /root/backup/performance_schema/users.frm
:: [] ...done
:: [] Copying ./performance_schema/hosts.frm to /root/backup/performance_schema/hosts.frm ............ :: Finished backing up non-InnoDB tables and files
:: Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS... #把日志刷新到磁盘
xtrabackup: The latest check point (for incremental): ''
xtrabackup: Stopping log copying thread.
. :: >> log scanned up to () :: Executing UNLOCK TABLES #完成non-innodb表的备份
:: All tables unlocked #所有的表解锁
:: [] Copying ib_buffer_pool to /root/backup/ib_buffer_pool
:: [] ...done
:: Backup created in directory '/root/backup/'
:: [] Writing /root/backup/backup-my.cnf
:: [] ...done
:: [] Writing /root/backup/xtrabackup_info
:: [] ...done
xtrabackup: Transaction log of lsn () to () was copied.
:: completed OK!
:: [] ...done

查看一下备份出来的文件:

[root@test3 backup]# ls           #这里的信息和innobackupex备份出来的信息差不多,因为没有开启二进制日志,因此,少了二进制日志的相关信息。
backup-my.cnf employees ib_buffer_pool ibdata1 mysql performance_schema sbtest sys xtrabackup_checkpoints xtrabackup_info xtrabackup_logfile
[root@test3 backup]# cat xtrabackup_checkpoints
backup_type = full-backuped #指明了这是一个完全备份
from_lsn =
to_lsn =
last_lsn =
compact =
recover_binlog_info =
[root@test3 backup]# cat xtrabackup_info
uuid = ba508f21-0be5-11e9-8fc2-fa336351fc00
name =
tool_name = xtrabackup
tool_command = --user=root --password=... --backup --target-dir=backup/
tool_version = 2.4.
ibbackup_version = 2.4.
server_version = 5.7.
start_time = -- ::
end_time = -- ::
lock_time =
binlog_pos =
innodb_from_lsn =
innodb_to_lsn =
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N

xtracebackup备份时在线备份,也就是再备份的过程中,数据库是可以进行操作的,备份的数据时间点自然就是不一致的,时间范围是从备份开始到备份结束这段时间,因此在使用备份出来的数据进行恢复之前,首先要做的就是准备数据,利用重做日志把数据恢复到同一个时间点。

您可以在任何机器上运行准备操作; 它不需要位于原始服务器或您要还原的服务器上。您可以将备份复制到实用程序服务器并在那里进行准备。

准备过程如下:

[root@test3 ~]# xtrabackup --prepare --target-dir=backup/
......
......
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 293839318
181230 14:22:32 completed OK!

恢复过程

恢复过程和之前使用innobackupex工具备份恢复的过程是一样的,需要注意的是,xtracebackup和innobackupex备份的数据恢复需要停掉原来的数据库。

[root@test3 ~]# service mysqld stop              #停掉数据库
[root@test3 ~]# rm -fr /data/mysql/* #清空数据库目录
[root@test3 ~]# xtrabackup --copy-back --target-dir=/root/backup/ #恢复数据,其实就是一个copy过程
。。。。。。
:: [] ...done
:: [] Copying ./xtrabackup_info to /data/mysql/xtrabackup_info
:: [] ...done
:: completed OK!
[root@test3 ~]# chown -R mysql:mysql /data/mysql/ #修改数据库目录的属主和属组
[root@test3 ~]# service mysqld start #启动数据库
Starting MySQL. SUCCESS!
[root@test3 ~]#

上面就是一个完整的数据库备份与恢复过程,当然xtracebackup还是有很多参数的,可以在使用的时候查看!

说两个参数吧!
--galera-info This options creates the xtrabackup_galera_info file
which contains the local node state at the time of the
backup. Option should be used when performing the backup
of Percona-XtraDB-Cluster. Has no effect when backup
locks are used to create the backup.
#我们公司使用的mariadb集群,因此备份的时候会加上这个选项,备份一些集群信息。
--slave-info This option is useful when backing up a replication slave
server. It prints the binary log position and name of the
master server. It also writes this information to the
"xtrabackup_slave_info" file as a "CHANGE MASTER"
command. A new slave for this master can be set up by
starting a slave server on this backup and issuing a
"CHANGE MASTER" command with the binary log position
saved in the "xtrabackup_slave_info" file.
#这个就不用说了,是一些slave的信息。

上面是一个完整实例说明全备的备份与恢复,下面来说明一个增量备份的用法!

增量备份

摘自官方文档: 增量备份

增量备份有效,因为每个InnoDB页面都包含一个日志序列号或LSN。该LSN是整个数据库系统的版本号。每个页面的LSN显示它最近的更改。

增量备份会复制其LSN比先前的增量备份或完整备份的LSN更新的每个页面。有两种算法用于查找要复制的此类页面集。第一个可用于所有服务器类型和版本的是通过读取所有数据页来直接检查页面LSN。Percona Server提供的第二个 功能是在服务器上启用更改的页面跟踪功能,该功能将在更改页面时记录这些功能。然后,该信息将在紧凑的单独的所谓位图文件中写出。该xtrabackupbinary将使用该文件只读取增量备份所需的数据页,从而可能节省许多读取请求。如果xtrabackup二进制文件找到位图文件,则默认启用后一种算法。即使位图数据可用,也可以指定xtrabackup --incremental-force-scan 读取所有页面。

增量备份实际上并不将数据文件与先前备份的数据文件进行比较。实际上,如果您知道其LSN,则可以使用它 来执行增量备份,甚至不需要先前的备份。增量备份只是读取页面并将其LSN与最后一个备份的LSN进行比较。但是,您仍需要完整备份来恢复增量更改; 如果没有完整备份作为基础,增量备份将毫无用处。

第一步:完全备份:

[root@test3 ~]# xtrabackup --user=root --password= --backup --target-dir=/root/backup/ -S /data/mysql/mysql.sock

[root@test3 ~]# cd backup/                    #这个文件记录了最后的lsn信息
[root@test3 backup]# cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn =
to_lsn =
last_lsn =
compact =
recover_binlog_info =
[root@test3 backup]#

然后我们对数据库做一些更改!

[root@test3 backup]# mysql -uroot -p123456 -S /data/mysql/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use sbtest;
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> create table test(dt datetime);
Query OK, 0 rows affected (0.03 sec) mysql> insert into test select now();
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test;
+---------------------+
| dt |
+---------------------+
| 2018-12-30 14:50:07 |
+---------------------+
1 row in set (0.00 sec) mysql> #如上,仅创建了一张表,插入了一行记录!

开始做增量备份

[root@test3 ~]# xtrabackup --user=root --password=123456 -S /data/mysql/mysql.sock --backup --incremental-basedir=/root/backup/  --target-dir=/root/inc1

#--backup:指定操作是备份
#--incremental-basedir:此次增量备份时基于哪一个备份文件的。
#--target-dir:       指定增量备份的文件地址 #查看增量备份的文件
[root@test3 ~]# cd backup/inc1/
[root@test3 inc1]# ls
backup-my.cnf  ib_buffer_pool  ibdata1.meta  performance_schema  sys                     xtrabackup_checkpoints  xtrabackup_logfile
employees      ibdata1.delta   mysql         sbtest              xtrabackup_binlog_info  xtrabackup_info
[root@test3 inc1]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 293839337
to_lsn = 293846059
last_lsn = 293846068
compact = 0
recover_binlog_info = 0
[root@test3 inc1]#

思考?这里的增量备份时基于全备开始,如果我们还想继续下一次的增量备份,该基于全备呢?还是基于第一个增量备份呢?回想一下增量备份时根据lsn来备份的,如果想要下一个增量备份,肯定是基于前面的一个增量备份开始的!

增量备份恢复操作

和上面的一样,也需要先准备阶段!

增量备份的步骤与完全备份的步骤不同。在完全备份中,执行两种类型的操作以使数据库保持一致:从日志文件中对数据文件重放已提交的事务,并回滚未提交的事务。在准备增量备份时,必须跳过未提交事务的回滚,因为备份时未提交的事务可能正在进行中,并且很可能它们将在下一次增量备份中提交。您应该使用--apply-log-only该 选项来阻止回滚阶段

准备的时候先prepare全备数据,如果有多个增量备份,则按照to_lsn的顺序来执行prepare操作!

执行全备的prepare阶段
[root@test3 ~]# xtrabackup --prepare --apply-log-only --target-dir=/root/backup/
#执行增量备份的prepare阶段
[root@test3 ~]# xtrabackup --prepare --target-dir=/root/backup/ --incremental-dir=/root/inc1/
#将增量文件和全备文件附件在一起

特别需要注意一点:

xtrabackup --apply-log-only should be used when merging all incrementals except the last one. That’s why the previous line doesn’t contain the 
xtrabackup --apply-log-only option. Even if the xtrabackup --apply-log-only was used on the last step, backup would still be consistent but in
that case server would perform the rollback phase. #--apply-log-only参数应该被用于除最后一个增量备份之外的所有的增量备份(最后一个增量备份不使用),原因有点不太理解,先这样用。

数据准备完成之后,就会恢复了,和之前的过程是一样的!

停掉MySQL服务,清空datadir目录
[root@test3 ~]# xtrabackup --copy-back --target-dir=/root/backup/
[root@test3 ~]# chown -R mysql:mysql /data/mysql
[root@test3 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@test3 ~]# mysql -uroot -p123456 -S /data/mysql/mysql.sock #验证增量备份
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use sbtest;
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> select * from test;
+---------------------+
| dt |
+---------------------+
| -- :: |
+---------------------+

可以看到增量备份已经恢复成功!

后续:

在实际中可能会更经常遇到基于时间点的恢复,譬如我经常遇到的是恢复到某天的23:59:59的数据,如果这样的话,这个增量可能无法满足,因此建议使用二进制日志来做增量备份。

关于二进制日志的增量备份,在上一篇博文中innobackupex备份中已经说明了,不再赘述!

mysql备份与恢复-xtracebackup的更多相关文章

  1. ch5 MySQL 备份与恢复

    第 5 章 MySQL 备份与恢复 前言 数据库的备份与恢复一直都是 DBA 工作中最为重要的部分之一,也是基本工作之一.任何正式环境的数据库都必须有完整的备份计划和恢复测试,本章内容将主要介绍 My ...

  2. mysql常用操作 mysql备份与恢复

    先登录mysql  ==>mysql -uroot -p  查看数据库的版本 select version(); 查看有哪些库 show datases; 查看当前处于哪个库 select da ...

  3. 第 5 章 MySQL 备份与恢复

    第 5 章 MySQL 备份与恢复 前言 数据库的备份与恢复一直都是 DBA 工作中最为重要的部分之一,也是基本工作之一.任何正式环境的数据库都必须有完整的备份计划和恢复测试,本章内容将主要介绍 My ...

  4. centos LAMP第四部分mysql操作 忘记root密码 skip-innodb 配置慢查询日志 mysql常用操作 mysql常用操作 mysql备份与恢复 第二十二节课

    centos  LAMP第四部分mysql操作  忘记root密码  skip-innodb 配置慢查询日志 mysql常用操作  mysql常用操作 mysql备份与恢复   第二十二节课 mysq ...

  5. Data Base mysql备份与恢复

    mysql  备份与恢复 为什么要备份: 由于系统使用到了MySQL 数 据库,所以每天的工作,就设计到了MySQL数据库的备份问题.但如果每天手工来做MySQL数据库的定时备份,工作量不说,时间还不 ...

  6. Mysql备份与恢复(2)---逻辑备份

    数据库及时备份可以帮助我们在数据库出现异常宕机时及时的使用备份数据进行恢复工作,将因为数据库宕机产生的影响降低到最小.上一篇针对使用xtrabackup工具进行物理备份和数据恢复做了一个详细讲解,本篇 ...

  7. MySQL备份与恢复-innobackupex

    :上一片myloder搞崩溃,为什么百度的博文都是抄袭一模一样的,哎烦! 这一片文章我们来介绍物理备份工具xtracebackup! 首先是安装可以percona官网下载安装,下载rpm包直接yum安 ...

  8. MySQL备份与恢复-mysqldump备份与恢复

    这片博文主要用来介绍MySQL的备份与恢复: MySQL的备份形式可以分为如下几种: 热备----即不停机备份 冷备----需要关闭MySQL,然后备份其数据文件.(停机备份一般是直接拷贝其datad ...

  9. MySQL备份与恢复-mydumper

    上一片博文中,我们说明了mysqldump的备份与恢复.因为mysqldump是单线程导出,单线程恢复的,因此备份与恢复的时间比较长! 首先来安装mydumper: 下载源码:https://gith ...

随机推荐

  1. one order 理解

    1: one order core

  2. 外部盒模型大小固定 内部有边框div设置浮动时 缩放窗口内部div溢出的解决办法

    原因分析: chorme和firefox浏览器下当缩放窗口大小时,边框的计算宽度变大造成内部div宽度的计算宽度变大,外部div放不下内部div而溢出. 解决办法: 给内部div设置 box-sizi ...

  3. 339A

    #include <iostream> #include <string> #include <algorithm> using namespace std; #d ...

  4. Visual Studio Code的常用快捷键

    一.Visual Studio Code简介 Visual Studio Code是个牛逼的编辑器,启动非常快,完全可以用来代替其他文本文件编辑工具.又可以用来做开发,支持各种语言,相比其他IDE,轻 ...

  5. Let Encrypt延期(转自虞大胆的叽叽喳喳)

    前几天发现我的 letsencrypt 通配符证书快过期了,想为这两张证书续期(renew). 首先运行命令查看我的所有证书: $ certbot-auto certificates 其中证书名 si ...

  6. vue 使用高德地图vue-amap组件

    首先    npm install -S vue-amap 然后在 main.js import VueAMap from 'vue-amap'; //注意不要和 AMap原始名称覆盖 Vue.use ...

  7. Sqlserver2008R2配置数据库镜像之我的经验总结

    一. 相关环境介结. 数据库:Sqlserver2008R2    网络环境:主机.镜像机(阿里云,青岛节点同域),见证机(本公司自己托管在上海) 二. 服务器相关配置. 1. 分别开启三台服务器50 ...

  8. 【漏洞公告】高危:Windows系统 SMB/RDP远程命令执行漏洞

    2017年4月14日,国外黑客组织Shadow Brokers发出了NSA方程式组织的机密文档,包含了多个Windows 远程漏洞利用工具,该工具包可以可以覆盖全球70%的Windows服务器,为了确 ...

  9. pandas.query()

    1. 查询 已知data: 查询概率等于0.4的所有行 问题所在:query后面只支持string形式的值,而"probability"==0.4返回的是一个bool类型,结果不是 ...

  10. 17.在自适应屏幕里通过JQ来获取宽高并赋给需要的

    在自适应屏幕里通过JQ来获取宽高并赋给需要的div. var height = document.documentElement.clientHeight; $(window).height();(同 ...