目录
一、备份恢复的工作职责
1.备份的时间周期
2.备份的方式
3.恢复方案
4.检查备份
5.定期恢复演练
6.故障恢复策略
7.迁移升级
二、逻辑备份工具--mysqldump
1.介绍
2.使用场景
3.mysqldump命令的参数介绍
1)全备:
2)单库或多库备份:--常用
3)备份某个库下的单表或多表:--不常用
4)--master-data=2:备份时自动生成当前的binlog位置信息
5)--single-transaction:对于InnoDB表开启一致性快照备份-->不用锁表就可以备份
6)-R -E --triggers:特殊对象备份
7)--max-allowed-packet=128M:服务端允许最大传输的数据包
总结全备命令:
全备命令带压缩
三、mysqldump+binlog 生产故障恢复模拟
1.模拟环境
2.全备
3.全备后产生了新的数据变化
4.搞破坏,删库
5.恢复数据
四、物理备份工具--Percona-Xtrabackup
1.使用场景
2.下载安装
3.修改配置文件
4.XBK备份原理解释
5.XBK恢复原理解释
6.XBK全备实际应用模拟
7.XBK增量备份
五、Xtrabackup全备+增量备份生产故障恢复模拟
1.全备:
2.增量数据备份
3.数据损坏
4.数据恢复(全备+周一增量+周二增量+周三binlog日志)
一、备份恢复的工作职责
1.备份的时间周期
凌晨、每天一备、每周一备
2.备份的方式
全备、增量、逻辑、物理
3.恢复方案
制定备份方案,注意备份方案准确性、备份用时
4.检查备份
检查备份存在性、大小、日志
5.定期恢复演练
6.故障恢复策略
7.迁移升级
二、逻辑备份工具--mysqldump
1.介绍
基于sql语句(create database,create table,insert into)的备份
针对InnoDB表可以实现非锁定备份,原理是通过MVCC中的快照技术进行备份
针对非InnoDB表启用了锁表备份,FTWRL(global read lock,全局锁表禁止写入)
无需安装,是mysql自带的工具,数据恢复方式和binlog很像
2.使用场景
100G以内的数据,比较常用逻辑备份
优点:文本形式存储,便于查看处理,自带工具,不需要单独安装,压缩比高,节省空间
缺点:备份时间较长,恢复时间更长(是备份时间的4-6倍)
3.mysqldump命令的参数介绍
连接参数:-u -p -s -h -P
备份方式:
1)全备:
mysqldump -uyizuo -pok -A >/tmp/full.sql
2)单库或多库备份:--常用
mysqldump -uyizuo -pok -B world >/tmp/mdp2.sql
3)备份某个库下的单表或多表:--不常用
mysqldump -uyizuo -pok world city >/tmp/mdp3.sql
注:单表或多表备份,在恢复时需要提前创建库,use到库中再恢复
4)--master-data=2:备份时自动生成当前的binlog位置信息
5)--single-transaction:对于InnoDB表开启一致性快照备份-->不用锁表就可以备份
6)-R -E --triggers:特殊对象备份
7)--max-allowed-packet=128M:服务端允许最大传输的数据包
总结全备命令:
mysqldump -uroot -A --master-data=2 --single-transaction -R -E --triggers --max-allowed-packet=128M > /opt/full_`date +%F`.sql
全备命令带压缩
mysqldump -uroot -A --master-data=2 --single-transaction -R -E --triggers --max-allowed-packet=128M|gzip > /opt/full_`date +%F`.sql.gz
三、mysqldump+binlog 生产故障恢复模拟
1.模拟环境
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world |
| yizuo |
+--------------------+
6 rows in set (0.00 sec)mysql> use yizuo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+-----------------+
| Tables_in_yizuo |
+-----------------+
| city |
+-----------------+
1 row in set (0.01 sec)mysql> select * from city;
+----+--------+----------+------------+--------------+
| id | name | province | population | district |
+----+--------+----------+------------+--------------+
| 2 | 盘锦 | 辽宁 | 109万 | 兴隆台区 |
| 4 | 盘锦 | 辽宁 | 109万 | 兴隆台区 |
+----+--------+----------+------------+--------------+
2 rows in set (0.00 sec)mysql>
2.全备
mysqldump -uroot -A --master-data=2 --single-transaction -R -E --triggers --max-allowed-packet=128M|gzip > /opt/full_$`date +%F`.sql.gz
3.全备后产生了新的数据变化
mysql> use yizuo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> CREATE TABLE `city_bak` (-> `id` int NOT NULL AUTO_INCREMENT COMMENT '编号',-> `name` varchar(100) DEFAULT NULL COMMENT '城市名',-> `province` varchar(100) DEFAULT NULL,-> `population` varchar(100) DEFAULT NULL COMMENT '人口',-> `district` varchar(100) DEFAULT NULL COMMENT '街区',-> PRIMARY KEY (`id`),-> KEY `idx_name` (`name`),-> KEY `idx_nn` (`name`(5)) /*!80000 INVISIBLE */-> ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Query OK, 0 rows affected (0.02 sec)mysql> show tables;
+-----------------+
| Tables_in_yizuo |
+-----------------+
| city |
| city_bak |
+-----------------+
2 rows in set (0.00 sec)mysql> insert into city_bak(id,name,province,population,district) values(2,'盘锦','辽宁','109万','兴隆台区');
Query OK, 1 row affected (0.01 sec)
4.搞破坏,删库
drop database yizuo;
5.恢复数据
1)全备文件解压缩:gunzip full_\$2024-05-25.sql.gz
2)检查全备文件:
3)截取binlog日志
mysqlbinlog --skip-gtids --include-gtids='518de310-1806-11ef-b5d7-000c2912a662:18-19' binlog.000010 >/opt/bin18-19.sql
4)恢复全备数据和二进制日志数据
set sql_log_bin=0;
source /opt/full_$2024-05-25.sql;
source /opt/bin18-19.sql
set sql_log_bin=1;
四、物理备份工具--Percona-Xtrabackup
1.使用场景
100G-TB级别数据量,一般采用xbk
优点:备份、恢复速度更快
缺点:二进制文件方式,不便于查看,可读性差,压缩比低,浪费空间
2.下载安装
1)安装依赖包
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
2)下载软件并安装
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpmhttps://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.rpmyum -y install percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
注:安装错版本了,参考官网文件,不同的mysql版本对应不同的xtrabackup版本:
卸载旧版本:rpm安装方式的卸载
[root@DB-Server ~]# rpm -qa | grep percona
percona-xtrabackup-24-2.4.5-1.el7.x86_64
[root@DB-Server ~]# rpm -ev percona-xtrabackup-24-2.4.5-1.el7.x86_64
安装正确版本:Use RPM repositories - Percona XtraBackup
3.修改配置文件
4.XBK备份原理解释
InnoDB表 | 1.XBK备份执行的瞬间,立即触发ckpt检查点,把已经提交的数据脏页,从内存刷写到磁盘,并记录此时的LSN号 |
2.备份时,拷贝ibd,ibdata,undo,ibtmp1,将备份期间产生的redolog截取拷贝,记录LSN |
非InnoDB表 | 1.触发FTWRL全局锁,关闭binlog记录 |
2.拷贝非InnoDB表数据 | |
3.解锁 |
备份数据完成后:记录binlog位置,停止redo拷贝,记录last LSN,记录所有备份日志到指定日志文件中,解锁
5.XBK恢复原理解释
1)prepare:
InnoDB表利用ACSR自动故障恢复的功能,使用redo进行前滚,利用undo进行回滚,保证数据一致
非InnoDB表由于是锁表备份,不会产生新的数据,所以不需要prepare
2)恢复:
cp文件至原路径
6.XBK全备实际应用模拟
官方文档:Restore full, incremental, compressed backups - Percona XtraBackup
1)全备命令:xtrabackup --backup --target-dir=/data/3306/backups/
文件解释xtrabackup_binlog_info:binlog文件的position和gtid位置点
[root@localhost backups]# cat xtrabackup_binlog_info
binlog.000014 196 518de310-1806-11ef-b5d7-000c2912a662:1-20
文件解释xtrabackup_checkpoints:
[root@localhost backups]# cat xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 21000068
last_lsn = 21000068
flushed_lsn = 21000068
redo_memory = 0
redo_frames = 0
文件解释xtrabackup_info:备份信息总览
[root@localhost backups]# cat xtrabackup_info
uuid = 42b032af-1b33-11ef-aad9-000c2912a662
name =
tool_name = xtrabackup
tool_command = --backup --target-dir=/data/3306/backups/
tool_version = 8.0.35-30
ibbackup_version = 8.0.35-30
server_version = 8.0.20
start_time = 2024-05-26 15:40:19
end_time = 2024-05-26 15:40:44
lock_time = 3
binlog_pos = filename 'binlog.000014', position '196', GTID of the last change '518de310-1806-11ef-b5d7-000c2912a662:1-20'
innodb_from_lsn = 0
innodb_to_lsn = 21000068
partial = N
incremental = N
format = file
compressed = N
encrypted = N
文件解释xtrabackup_logfile:备份期间的redolog文件
2)搞破坏
pkill mysqld
rm -rf /data/3306/data/*
3)全备数据恢复
1)prepare:
xtrabackup --prepare --target-dir=/data/3306/backups/
2)恢复:
xtrabackup --copy-back --target-dir=/data/3306/backups/ --datadir=/data/3306/data
chown -R mysql.mysql data/*
重启数据库
7.XBK增量备份
1)增量备份原理:
XBK自动检查LSN号码的变化,将每天LSN发生变化的数据页备份
2)增量备份必须依赖全备,恢复时必须合并到全备,再由全备文件做数据恢复
五、Xtrabackup全备+增量备份生产故障恢复模拟(mysql8.0.20,Xtrabackup8.0.35)
1.全备:
xtrabackup --backup --target-dir=/data/3306/backups/
chown -R mysql.mysql backups/*
2.增量数据备份
第一天数据改动:
create table city_bak......
insert into city_bak......
第一天增量备份:
xtrabackup --backup --target-dir=/data/3306/backups/inc1 \
--incremental-basedir=/data/3306/backups/base
第二天数据改动:
create table city_bak2......
insert into city_bak2......
第二天增量备份:
xtrabackup --backup --target-dir=/data/3306/backups/inc2 \
--incremental-basedir=/data/3306/backups/inc1
增量数据给予权限,增量数据检查:(to_lsn,from_lsn)
[root@localhost backups]# ll
总用量 12
drwxr-xr-x 8 root root 4096 5月 26 19:09 base
drwxr-x--- 8 root root 4096 5月 26 19:10 inc1
drwxr-x--- 8 root root 4096 5月 26 19:22 inc2
[root@localhost backups]# cat base/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 21026872 --对应着下一个备份文件的from_lsn
last_lsn = 21026872
flushed_lsn = 21026872
redo_memory = 0
redo_frames = 0
[root@localhost backups]# cat inc1/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 21026872 --对应着上一个备份文件的to_lsn
to_lsn = 21051876 --对应着下一个备份文件的from_lsn
last_lsn = 21052354
flushed_lsn = 21051876
redo_memory = 0
redo_frames = 0
[root@localhost backups]# cat inc2/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 21051876 --对应着上一个备份文件的to_lsn
to_lsn = 21079740
last_lsn = 21083679
flushed_lsn = 21079740
redo_memory = 0
redo_frames = 0
3.数据损坏
第三天:
create table city_bak3......
insert into city_bak3......
让数据损坏:rm -rf data/*
4.数据恢复(全备+周一增量+周二增量+周三binlog日志)
1)prepare:
全备prepare:
xtrabackup --prepare --apply-log-only --target-dir=/data/3306/backups/base
合并第一天增量到全备文件:
xtrabackup --prepare --apply-log-only --target-dir=/data/3306/backups/base \
--incremental-dir=/data/3306/backups/inc1
合并第二天增量到全备文件:(最后一次合并不需要加--apply-log-only参数)
xtrabackup --prepare --target-dir=/data/3306/backups/base \
--incremental-dir=/data/3306/backups/inc2
2)备份数据恢复
xtrabackup --copy-back --target-dir=/data/3306/backups/base/ --datadir=/data/3306/data
chown -R mysql.mysql data/*
重启数据库
3)截取binlog日志
查看最后一次备份文件的binlog gtid位置点:
与当前日志位置点进行核对一致
[root@localhost inc2]# cat xtrabackup_binlog_info
binlog.000018 236 518de310-1806-11ef-b5d7-000c2912a662:1-20,583319b5-1b3f-11ef-8916-000c2912a662:1-4
mysql> show binlog events in 'binlog.000018';
+---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| binlog.000018 | 4 | Format_desc | 51 | 125 | Server ver: 8.0.20, Binlog ver: 4 |
| binlog.000018 | 125 | Previous_gtids | 51 | 236 | 518de310-1806-11ef-b5d7-000c2912a662:1-20,
583319b5-1b3f-11ef-8916-000c2912a662:1-4 |
| binlog.000018 | 236 | Gtid | 51 | 315 | SET @@SESSION.GTID_NEXT= '583319b5-1b3f-11ef-8916-000c2912a662:5' |
| binlog.000018 | 315 | Query | 51 | 878 | use `world`; CREATE TABLE `city_bak3` (`id` int NOT NULL AUTO_INCREMENT COMMENT '编号',`name` varchar(100) DEFAULT NULL COMMENT '城市名',`province` varchar(100) DEFAULT NULL,`population` varchar(100) DEFAULT NULL COMMENT '人口',`district` varchar(100) DEFAULT NULL COMMENT '街区',PRIMARY KEY (`id`),KEY `idx_name` (`name`),KEY `idx_nn` (`name`(5)) /*!80000 INVISIBLE */
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /* xid=180 */ |
| binlog.000018 | 878 | Gtid | 51 | 957 | SET @@SESSION.GTID_NEXT= '583319b5-1b3f-11ef-8916-000c2912a662:6' |
| binlog.000018 | 957 | Query | 51 | 1033 | BEGIN |
| binlog.000018 | 1033 | Table_map | 51 | 1106 | table_id: 96 (world.city_bak3) |
| binlog.000018 | 1106 | Write_rows | 51 | 1184 | table_id: 96 flags: STMT_END_F |
| binlog.000018 | 1184 | Xid | 51 | 1215 | COMMIT /* xid=181 */ |
| binlog.000018 | 1215 | Gtid | 51 | 1294 | SET @@SESSION.GTID_NEXT= '583319b5-1b3f-11ef-8916-000c2912a662:7' |
| binlog.000018 | 1294 | Query | 51 | 1857 | use `world`; CREATE TABLE `city_bak4` (`id` int NOT NULL AUTO_INCREMENT COMMENT '编号',`name` varchar(100) DEFAULT NULL COMMENT '城市名',`province` varchar(100) DEFAULT NULL,`population` varchar(100) DEFAULT NULL COMMENT '人口',`district` varchar(100) DEFAULT NULL COMMENT '街区',PRIMARY KEY (`id`),KEY `idx_name` (`name`),KEY `idx_nn` (`name`(5)) /*!80000 INVISIBLE */
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /* xid=198 */ |
| binlog.000018 | 1857 | Gtid | 51 | 1936 | SET @@SESSION.GTID_NEXT= '583319b5-1b3f-11ef-8916-000c2912a662:8' |
| binlog.000018 | 1936 | Query | 51 | 2012 | BEGIN |
| binlog.000018 | 2012 | Table_map | 51 | 2085 | table_id: 97 (world.city_bak4) |
| binlog.000018 | 2085 | Write_rows | 51 | 2163 | table_id: 97 flags: STMT_END_F |
| binlog.000018 | 2163 | Xid | 51 | 2194 | COMMIT /* xid=199 */ |
| binlog.000018 | 2194 | Stop | 51 | 2217 |
截取binlog日志:
mysqlbinlog --skip-gtids --include-gtids='583319b5-1b3f-11ef-8916-000c2912a662:5-8' binlog.000018 >/tmp/gtid18.sql
4)binlog数据恢复
set sql_log_bin=0;
source /tmp/gtid18.sql;
set sql_log_bin=1;
5.清空binlog日志,从新开始记
reset master;
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 156 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@localhost binlog]# ll
总用量 8
-rw-r----- 1 mysql mysql 156 5月 26 20:32 binlog.000001
-rw-r----- 1 mysql mysql 32 5月 26 20:32 binlog.index
[root@localhost binlog]#
6.恢复成功后再重新发起全备
xtrabackup --backup --target-dir=/data/3306/backups/base