物理备份-XBK
- 安装依赖包
yum -y install perl perl-devel perl-Digest libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev
- 安装percona-toolkit和percona-xtrabackup
包下载位置:Software Downloads - Percona
将percona-toolkit-3.3.0-1.el7.x86_64、percona-xtrabackup-24-2.4.12-1.el7.x86_64包拖入
rpm -ivh percona-toolkit-3.3.0-1.el7.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
- innobackupex 使用
备份核心理念:
- 针对非InnoDB,进行锁表备份,copy所有的非innoDB表文件
- 针对InnoDB表,立即触发CKPT,copy所有InnoDB表相关的文件(ibdata1,ibd,frm)
- 并且将备份过程中产生新的数据变化的部分redo一起备份走
- 在恢复时,xbk会调用InnoDB引擎的CSR过程,将数据和redo的LSN追平,然后进行一致性恢复
- 备份过程
前提:开启二进制日志和GITD
开启二进制日志
vim /etc/my.cnf
添加:
log_bin=/data/binlog/mysql-bin
创建目录并授权
mkdir -p /data/binlog
chown -R mysql.mysql /data
开启GTID
vim /etc/my.cnf
添加:
gtid-mode=on
enforce-gtid-consistency=true
创建备份目录
mkdir /backup
重启MySQL
systemctl restart mysqld
1)全备
innobackupex --user=root --password=123.com -S /tmp/mysql.sock --no-timestamp /backup/full
查看备份文件
cd /backup/full/
2)关闭MySQL删除数据库文件
systemctl stop mysqld
rm -rf /usr/local/mysql/data/*
3)1
innobackupex --apply-log /backup/full/
4)将备份数据拷贝的MySQL的数据目录
cp -a /backup/full/* /usr/local/mysql/data/
5)更改数据文件文件属主和属组
chown -R mysql.mysql /usr/local/mysql/data/*
6)重启MySQL
systemctl restart mysqld
netstat -anpt | grep mysqld
- 备份产生的文件介绍
1)xtrabackup_binlog_info
cat /backup/full/xtrabackup_binlog_info
记录备份时刻的二进制日志信息. 可以作为binlog截取的起点.
2)xtrabackup_checkpoints
cat /backup/full/xtrabackup_checkpoints
from:备份中包含的LSN号的起点,全备:0, 增量:上次备份的结束位置
to:ckpt时的LSN
last-9:备份结束时的LSN,下次增量备份的起始位置
增量备份
1)清空备份文件和日志
rm -rf /backup/*
reset master;
show master status;
rm -rf /usr/local/mysql/data/xtrabackup_*
2)创建数据
create database full charset utf8mb4;
use full;
create table t1 (id int);
insert into t1 values(1),(2),(3);
3)进行周日的全备
innobackupex --user=root --password=123.com --socket=/tmp/mysql.sock --no-timestamp /backup/full
cat /backup/full/xtrabackup_binlog_info
4)模拟周一的数据变化
create database inc1 charset utf8mb4;
use inc1;
create table t1 (id int);
insert into t1 values(1),(2),(3);
5)进行周一的增量备份
innobackupex --user=root --password=123.com --no-timestamp --incremental --socket=/tmp/mysql.sock --incremental-basedir=/backup/full /backup/inc1
说明:
--incremental 开启增量备份
--incremental-basedir=/backup/full 基于哪个备份进行增量
/backup/inc1 增量备份的目录
6)检查备份的LSN
全备文件:
cat /backup/full/xtrabackup_checkpoints
增备文件:
cat /backup/inc1/xtrabackup_checkpoints
7)模拟周二数据变化
create database inc2 charset utf8mb4;
use inc2;
create table t1 (id int);
insert into t1 values(1),(2),(3);
8)进行周二的增量备份(对比周一的做增量)
innobackupex --user=root --password=123.com --no-timestamp --incremental --socket=/tmp/mysql.sock --incremental-basedir=/backup/inc1 /backup/inc2
查看备份的LSN
cat /backup/inc2/xtrabackup_binlog_info
cat /backup/inc2/xtrabackup_checkpoints
9)模拟周三的数据变化
create database inc3 charset utf8mb4;
use inc3;
create table t1 (id int);
insert into t1 values(1),(2),(3);
10)模拟没有备份,数据库发生崩溃
systemctl stop mysqld
rm -rf /usr/local/mysql/data/*
11)恢复思路
停业务,挂维护页
查找可用备份并处理备份:full+inc1+inc2
binlog:inc2到故障时间点的binlog
恢复全备+增量+binlog
验证数据
开启业务,撤维护页
12)恢复前的准备
整理full
innobackupex --apply-log --redo-only /backup/full
合并inc1到full,并整理备份
innobackupex --apply-log --redo-only --incremental-dir=/backup/inc1 /backup/full
合并inc2到full,并整理备份
innobackupex --apply-log --incremental-dir=/backup/inc2 /backup/full
最后一次整理full
innobackupex --apply-log /backup/full
13)截取二进制日志
起点:
cat /backup/inc2/xtrabackup_binlog_info
终点:
mysqlbinlog /data/binlog/mysql-bin.000001 | grep 'SET'
截取:
mysqlbinlog --skip-gtids --include-gtids='0862fe11-b6d1-11ef-865d-000c2900ee94:10-12' /data/binlog/mysql-bin.000001 > /backup/binlog.sql
14)恢复备份数据
cp -a /backup/full/* /usr/local/mysql/data/
chown -R mysql.mysql /usr/local/mysql/
systemctl restart mysqld
set sql_log_bin=0;
source /backup/binlog.sql
set sql_log_bin=1;
15)验证数据
show databases;
select * from full.t1;
select * from inc1.t1;
select * from inc2.t1;
select * from inc3.t1;
标签:binlog,full,--,备份,还原,mysql,backup From: https://blog.csdn.net/2402_88627342/article/details/145080411