MySQL physical backup tools -xtrabackup

installation

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
yum install -y percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm

Backup - physical backup

(1) For non-Innodb table (such as MyISAM), the lock table cp data file, belongs to a warm standby.

(2) The table Innodb (transactional), the lock table is not, copy the data page, the data eventually stored in file manner down, together with the portion of the redo and undo prepared walking, hot standby mode belongs.

xtrabackup innodb table in the backup and recovery process

(1) xtrabackup backup performed instantly, immediately triggering ckpt, dirty pages submitted data from memory flushed to disk, and record numbers at this time of LSN

(2) a backup copy disk data page, and records generated during the backup redo and undo copy go together, i.e. log after checkpoint LSN

(3) before resuming simulate "Auto Recovery" process Innodb, the redo (roll forward) were applied and undo (rollback)

(4) recovery process is back up to the original data directory cp

innobackupex use

All equipment

/Etc/my.cnf need to specify the location of the socket

[client]
socket=/var/lib/mysql/mysql.sock
innobackupex --user=root --password=Klvchen_123  /data/backup

Completely customizable backup path name

innobackupex --user=root --password=Klvchen_123 --no-timestamp /data/backup/full

Multi-file backup set out

cd /data/backup/full

-rw-r----- 1 root root      104 Jan 11 23:23 xtrabackup_binlog_info
-rw-r----- 1 root root      113 Jan 11 23:23 xtrabackup_checkpoints
-rw-r----- 1 root root      598 Jan 11 23:23 xtrabackup_info
-rw-r----- 1 root root     2560 Jan 11 23:23 xtrabackup_logfile

cat xtrabackup_binlog_info 
mysql-bin.000005        234     e4b4a809-3445-11ea-abe9-00155d1f4002:1-7,f10baaa9-3446-11ea-ba25-00155d1f4002:1-2
# 记录的是备份时刻,binlog的文件名字和当时的结束的position,可以用来作为截取binlog时的起点。

cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0                           # 上次所到达的LSN号(对于全备就是从0开始,对于增量有别的显示方法)
to_lsn = 4072216                       # 备份开始时间(ckpt)点数据页的LSN 
last_lsn = 4072225                     # 备份开始时间(ckpt)点数据页的LSN 
compact = 0
recover_binlog_info = 0
(1) 备份时刻,立即将已经commit过的,内存中的数据页刷新到磁盘(CKPT).开始备份数据,数据文件的LSN会停留在to_lsn位置。
(2) 备份时刻有可能会有其他的数据写入,已备走的数据文件就不会再发生变化了。
(3) 在备份过程中,备份软件会一直监控着redo的undo,如果一旦有变化会将日志也一并备走,并记录LSN到last_lsn。
从to_lsn  ----》last_lsn 就是,备份过程中产生的数据变化。


cat xtrabackup_info
uuid = 55504fc5-3486-11ea-a184-00155d1f4002
name = 
tool_name = innobackupex
tool_command = --user=root --password=... --no-timestamp /data/backup/full
tool_version = 2.4.4
ibbackup_version = 2.4.4
server_version = 5.7.28-log
start_time = 2020-01-11 23:23:20
end_time = 2020-01-11 23:23:34
lock_time = 0
binlog_pos = filename 'mysql-bin.000005', position '234', GTID of the last change 'e4b4a809-3445-11ea-abe9-00155d1f4002:1-7,
f10baaa9-3446-11ea-ba25-00155d1f4002:1-2'
innodb_from_lsn = 0
innodb_to_lsn = 4072216
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N

# 备份的重做日志文件
file xtrabackup_logfile 
xtrabackup_logfile: data

Restore bountiful

# 将redo进行重做,已提交的写到数据文件,未提交的使用undo回滚掉。模拟了CSR的过程
innobackupex --apply-log  /data/backup/full

# 停止 mysql ,删除 data目录
pkill mysql
rm -rf /var/lib/mysql

# 拷贝目录
cp -rf /data/backup/full /var/lib/mysql
chown -R mysql.mysql /var/lib/mysql

# 启动
systemctl start mysqld

innobackupex incremental backups (incremental)

(1) an incremental backup mode, based on the last incremental backup.

(2) incremental backups can not be restored individually. It must be based on the full recovery equipment.

(3) all increments according to the order must be incorporated in all equipment.

Analog data changes (all equipment before)

create database klvchen;
use klvchen;
create table t1 (id int);
insert into t1 values(1),(2),(3);

All equipment

innobackupex --user=root --password=Klvchen_123 --no-timestamp /data/backup/full

A first analog-day data

use klvchen;
create table t2 (id int);
insert into t2 values(1),(2),(3);

The first day of incremental backups

innobackupex --user=root --password=Klvchen_123 --no-timestamp --incremental --incremental-basedir=/data/backup/full  /data/backup/inc1

The next day simulation data

use klvchen;
create table t3 (id int);
insert into t3 values(1),(2),(3);

The next incremental backup

innobackupex --user=root --password=Klvchen_123 --no-timestamp --incremental --incremental-basedir=/data/backup/inc1  /data/backup/inc2

On the third day simulation data

use klvchen;
create table t4 (id int);
insert into t4 values(1),(2),(3);

# 误删数据库 klvchen
drop database klvchen;

Restore data to the state before the error drop Wednesday

恢复思路:
1. 增量不能单独恢复
2. 增量必须按照顺序合并到全备(LSN号码)
3. 所有备份都必须要 --apply-log 进行整理备份
4. 部分备份需要只 redo 不 undo (--redo-only)

No full finishing equipment prior to the merger (--redo-only)

innobackupex --apply-log --redo-only /data/backup/full/

Combined with the incremental backup to full consolidation inc1

innobackupex --apply-log --redo-only --incremental-dir=/data/backup/inc1 /data/backup/full/

Combined with the incremental backup to full consolidation inc2

innobackupex --apply-log --incremental-dir=/data/backup/inc2 /data/backup/full/

Finishing the last full backup

innobackupex --apply-log  /data/backup/full

Binary log interception

# 判断起点
cat /data/backup/inc2/xtrabackup_binlog_info 
mysql-bin.000002        2455    512c7182-354a-11ea-9aa0-00155d1f4002:1-9

# 判断终点
show binlog events in 'mysql-bin.000002';

# 截取 drop 之前的 binlog
mysqlbinlog --skip-gtids --include-gtids='512c7182-354a-11ea-9aa0-00155d1f4002:10-11' /data/mysql-bin/mysql-bin.000002 >/data/backup/bin.sql

Recovery

mkdir /data/3307/data -p
cp -a /data/backup/full/* /data/3307/data/
chown -R mysql.mysql /data/3307/

# 把 /etc/my.cnf 下的 datadir 路径改为新的目录
datadir=/data/3307/data

# 重启 mysql
systemctl restart mysqld

# 恢复二进制日志
set sql_log_bin=0;
source /data/backup/bin.sql;
set sql_log_bin=1;

# 检查
mysql -uroot -pKlvchen_123 -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| klvchen            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
# klvchen 数据库已经成功恢复

Guess you like

Origin www.cnblogs.com/klvchen/p/12181036.html