MysqlMysql

七,MySQL备份恢复

2021-05-29  本文已影响0人  会倒立的香飘飘

1,备份策略设计

备份周期:
根据数据量
备份工具:
mysqldump (MDP) , XBK (PBK) percona Xtrabackup  ,  MEB(MySQL Enterprise BACKUP  MEB) ,mysqlbinlog
备份方式:

逻辑:

全备   mysqldump 
增量   binlog (flush logs ,cp)

物理备份:

全备 : XBK(percona Xtrabackup) 
增量 : XBK(percona Xtrabackup)

备份的策略

备份方式: 全备:全库备份,备份所有数据 增量:备份变化的数据 
逻辑备份=mysqldump+mysqlbinlog 
物理备份=xtrabackup_full+xtrabackup_incr+binlog或者xtrabackup_full+binlog 备份周期: 根据数据量设计备份周期 
比如:周日全备,周1-周6增量

数据恢复

只要备份和日志是完整的,恢复到故障之前的时间点(快速)

数据迁移

操作系统不同的迁移
mysql   ->  mysql 
其他    ->  mysql 
mysql   ->   其他

2,备份的介绍

备份的分类

热备:在数据库正常业务时,备份数据,并且能够一致性恢复(只能是innodb)对业务影响非常小
温备:锁表备份,只能查询不能修改(myisam) 影响到写入操作
冷备:关闭数据库业务,数据库没有任何变更的情况下,进行备份数据. 业务停止

备份工具

物理备份&逻辑备份

·····基于磁盘数据文件备份·····
xtrabackup(XBK) :percona 第三方 
MySQL Enterprise Backup(MEB)
-----------
·····基于SQL语句进行备份·····
mysqldump      
mysqlbinlog

物理备份&逻辑备份的比较

mysqldump

优点:
1.不需要下载安装
2.备份出来的是SQL,文本格式,可读性高,便于备份处理
3.压缩比较高,节省备份的磁盘空间

缺点:
4.依赖于数据库引擎,需要从磁盘把数据读出
然后转换成SQL进行转储,比较耗费资源,数据量大的话效率较低
建议:
100G以内的数据量级,可以使用mysqldump
超过TB以上,我们也可能选择的是mysqldump,配合分布式的系统
1EB  =1024 PB =1000000 TB

xtrabackup(XBK)

优点:
1.类似于直接cp数据文件,不需要管逻辑结构,相对来说性能较高
缺点:
2.可读性差
3.压缩比低,需要更多磁盘空间
建议:>100G<TB

3,mysqldump客户端逻辑备份工具

参数介绍

-A(全备参数)

mysqldump -uroot -p -A >/data/backup/full.sql

 补充:
1.常规备份是要加 --set-gtid-purged=OFF,解决备份时的警告

 [root@db01 ~]# mysqldump -uroot -p123 -A  --set-gtid-purged=OFF  >/backup/full.sql

 2.构建主从时,做的备份,不需要加这个参数

 [root@db01 ~]# mysqldump -uroot -p123 -A    --set-gtid-purged=ON >/backup/full.sql

-B (单库备份)

mysqldump -uroot -p1 -B world  test  >/backup/bak.sql

ps: -B后面跟的全都是库的名字,不能跟表的名字,

单表或者多表备份

mysqldump -uroot -p1 world city country >/backup/bak1.sql

ps: 库名后面只能是表名不需要加参数,以上备份恢复时:必须库事先存在,并且ues到库才能source恢复
特殊备份参数
-R                 备份存储过程及函数
--triggers         备份触发器
-E                 备份事件

--master-data=2

--master-data=2 :以注释的形式,保存备份开始时间点的binlog的状态信息

功能:
(1)在备份时,会自动记录,二进制日志文件名和位置号  0 默认值
       1  以change master to命令形式,可以用作主从复制
       2  以注释的形式记录,备份时刻的文件名+postion号
(2) 自动锁表
       如果配合--single-transaction,只对非InnoDB表进行锁表备份,InnoDB表进行“热“”备,实际上是实现快照备份。
master-data可以自动加锁
(1)在不加--single-transaction ,启动所有表的温备份,所有表都锁定
(2)加上--single-transaction ,对innodb进行快照备份,对非innodb表可以实现自动锁表功能

--single-transaction

`--single-transaction :` innodb 存储引擎开启热备(快照备份)功能

4,模拟数据恢复(使用position号&GTID)

position号恢复

准备数据

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

周二晚上十一点全备

mysqldump -uroot -p123 -A  -R  --triggers --set-gtid-purged=OFF --master-data=2  --single-transaction|gzip > /backup/full_$(date +%F).sql.gz

模拟周二 23:00到周三 10点之间数据变化

use backup
insert into t1 values(11),(22),(33);
commit;
create table t2 (id int);
insert into t2 values(11),(22),(33);
commit;

模拟故障,删除库表(不代表生产操作)

drop  database backup;

恢复过程(使用)

准备一个临时数据库
1,准备全备

cd /backup
[root@mister_f backup]# ll
total 196
-rw-r--r-- 1 root root 197054 May 10 10:50 full_2021-05-10.sql.gz
[root@mister_f backup]# gunzip full_2021-05-10.sql.gz
[root@mister_f backup]# ll
total 776
-rw-r--r-- 1 root root 793656 May 10 10:50 full_2021-05-10.sql

2,截取二进制日志
找到日志中的

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=154;

查找position号码的起点和重点

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     1018 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> show binlog events in 'mysql-bin.000003';
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                   |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| mysql-bin.000003 |   4 | Format_desc    |         6 |         123 | Server ver: 5.7.26-log, Binlog ver: 4  |
| mysql-bin.000003 | 123 | Previous_gtids |         6 |         154 |                                        |
| mysql-bin.000003 | 154 | Anonymous_Gtid |         6 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 219 | Query          |         6 |         293 | BEGIN                                  |
| mysql-bin.000003 | 293 | Table_map      |         6 |         340 | table_id: 110 (backup.t1)              |
| mysql-bin.000003 | 340 | Write_rows     |         6 |         390 | table_id: 110 flags: STMT_END_F        |
| mysql-bin.000003 | 390 | Xid            |         6 |         421 | COMMIT /* xid=478 */                   |
| mysql-bin.000003 | 421 | Anonymous_Gtid |         6 |         486 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 486 | Query          |         6 |         588 | use `backup`; create table t2 (id int) |
| mysql-bin.000003 | 588 | Anonymous_Gtid |         6 |         653 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 653 | Query          |         6 |         727 | BEGIN                                  |
| mysql-bin.000003 | 727 | Table_map      |         6 |         774 | table_id: 142 (backup.t2)              |
| mysql-bin.000003 | 774 | Write_rows     |         6 |         824 | table_id: 142 flags: STMT_END_F        |
| mysql-bin.000003 | 824 | Xid            |         6 |         855 | COMMIT /* xid=481 */                   |
| mysql-bin.000003 | 855 | Anonymous_Gtid |         6 |         920 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 920 | Query          |         6 |        1018 | drop database backup                   |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
16 rows in set (0.00 sec)

找到起点和终点过后进行二进制日志截取

mysqlbinlog  --skip-gtids --start-position=154 --stop-position=920  /data/binlog/mysql-bin.000003 >/backup/bin.sql

3,恢复到临时库

[root@mister_f ~]# mysql -S /data/3307/mysql.sock
mysql -S /data/3307/mysql.sock
set sql_log_bin=0;
source /backup/full_2021-05-10.sql
source /backup/bin.sql

4,将故障的表导出恢复到生产库

mysqldump -S /data/3307/mysql.sock -B backup >/backup/backup.sql
mysql -uroot -p123 
set sql_log_bin=0;
source /backup/backup.sql;
set_sql_log_bin=1;
mysql> use backup;

mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|   11 |
|   22 |
|   33 |
+------+
6 rows in set (0.00 sec)

mysql> select * from t2;
+------+
| id   |
+------+
|   11 |
|   22 |
|   33 |
+------+
3 rows in set (0.00 sec)

mysql> 

使用GTID

准备数据

create database boy charset utf8mb4;
use boy
create table t1 (id int);
insert into t1 values(1),(2),(3),(4),(5);
commit;

进行全备

[root@mister_f backup]# mysqldump -uroot -p123 -A --master-data=2  --single-transaction -R -E --triggers >/backup/all.sql

从日志中找出GTID号码

SET @@GLOBAL.GTID_PURGED='f4926326-afda-11eb-aca3-525400956ca6:1-31';

模拟数据变化

mysql> insert into t1 values(6),(7),(8);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> commit;

mysql> update t1 set id=10 where id>5;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> commit;
mysql> delete from t1 where id=5;
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.01 sec)

mysql> delete from t1;
Query OK, 7 rows affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values(1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

mysql> update t1 set id=10 where id>2;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|   10 |
|   10 |
|   10 |
+------+
5 rows in set (0.00 sec)

进行二进制日志的截取

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000002 |     2249 |              |                  | f4926326-afda-11eb-aca3-525400956ca6:1-36 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> show binlog events in 'mysql-bin.000002';
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                               |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000002 |    4 | Format_desc    |         6 |         123 | Server ver: 5.7.26-log, Binlog ver: 4                              |
| mysql-bin.000002 |  123 | Previous_gtids |         6 |         194 | f4926326-afda-11eb-aca3-525400956ca6:1-28                          |
| mysql-bin.000002 |  194 | Gtid           |         6 |         259 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:29' |
| mysql-bin.000002 |  259 | Query          |         6 |         366 | create database boy charset utf8mb4                                |
| mysql-bin.000002 |  366 | Gtid           |         6 |         431 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:30' |
| mysql-bin.000002 |  431 | Query          |         6 |         527 | use `boy`; create table t1 (id int)                                |
| mysql-bin.000002 |  527 | Gtid           |         6 |         592 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:31' |
| mysql-bin.000002 |  592 | Query          |         6 |         663 | BEGIN                                                              |
| mysql-bin.000002 |  663 | Table_map      |         6 |         707 | table_id: 245 (boy.t1)                                             |
| mysql-bin.000002 |  707 | Write_rows     |         6 |         767 | table_id: 245 flags: STMT_END_F                                    |
| mysql-bin.000002 |  767 | Xid            |         6 |         798 | COMMIT /* xid=2176 */                                              |
| mysql-bin.000002 |  798 | Gtid           |         6 |         863 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:32' |
| mysql-bin.000002 |  863 | Query          |         6 |         934 | BEGIN                                                              |
| mysql-bin.000002 |  934 | Table_map      |         6 |         978 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 |  978 | Write_rows     |         6 |        1028 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 1028 | Table_map      |         6 |        1072 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 | 1072 | Update_rows    |         6 |        1138 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 1138 | Xid            |         6 |        1169 | COMMIT /* xid=2858 */                                              |
| mysql-bin.000002 | 1169 | Gtid           |         6 |        1234 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:33' |
| mysql-bin.000002 | 1234 | Query          |         6 |        1305 | BEGIN                                                              |
| mysql-bin.000002 | 1305 | Table_map      |         6 |        1349 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 | 1349 | Delete_rows    |         6 |        1389 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 1389 | Xid            |         6 |        1420 | COMMIT /* xid=2862 */                                              |
| mysql-bin.000002 | 1420 | Gtid           |         6 |        1485 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:34' |
| mysql-bin.000002 | 1485 | Query          |         6 |        1556 | BEGIN                                                              |
| mysql-bin.000002 | 1556 | Table_map      |         6 |        1600 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 | 1600 | Delete_rows    |         6 |        1670 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 1670 | Xid            |         6 |        1701 | COMMIT /* xid=2864 */                                              |
| mysql-bin.000002 | 1701 | Gtid           |         6 |        1766 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:35' |
| mysql-bin.000002 | 1766 | Query          |         6 |        1837 | BEGIN                                                              |
| mysql-bin.000002 | 1837 | Table_map      |         6 |        1881 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 | 1881 | Write_rows     |         6 |        1941 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 1941 | Xid            |         6 |        1972 | COMMIT /* xid=2866 */                                              |
| mysql-bin.000002 | 1972 | Gtid           |         6 |        2037 | SET @@SESSION.GTID_NEXT= 'f4926326-afda-11eb-aca3-525400956ca6:36' |
| mysql-bin.000002 | 2037 | Query          |         6 |        2108 | BEGIN                                                              |
| mysql-bin.000002 | 2108 | Table_map      |         6 |        2152 | table_id: 253 (boy.t1)                                             |
| mysql-bin.000002 | 2152 | Update_rows    |         6 |        2218 | table_id: 253 flags: STMT_END_F                                    |
| mysql-bin.000002 | 2218 | Xid            |         6 |        2249 | COMMIT /* xid=2868 */                                              |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
38 rows in set (0.00 sec)


[root@mister_f backup]# mysqlbinlog --skip-gtids --include-gtids='f4926326-afda-11eb-aca3-525400956ca6:32-36' --exclude-gtids='f4926326-afda-11eb-aca3-525400956ca6:33'  /data/binlog/mysql-bin.000001 >/backup/gtid.sql

因为全备日志中有1-31号事物所以我们只需要截取出32-36号事物然后排除掉删除表的那个事物

数据恢复

mysql> set sql_log_bin=0;
mysql> source /backup/all.sql;
mysql> source /backup/gtid.sql;
mysql> set sql_log_bin=1;

5,扩展参数

--set-gtid-purged

参数:auto , on,off
默认是auto
使用场景:
1. --set-gtid-purged=OFF,可以使用在日常备份参数中.
mysqldump -uroot -p -A -R -E --triggers --master-data=2  --single-transaction --set-gtid-purged=OFF >/data/backup/full.sql
2. auto , on:在构建主从复制环境时需要的参数配置
mysqldump -uroot -p -A -R -E --triggers --master-data=2  --single-transaction --set-gtid-purged=ON >/data/backup/full.sql

--max-allowed-packet=128M(控制的是备份时传输数据包的大小)

mysqldump -uroot -p -A -R -E --triggers --master-data=2  --single-transaction --set-gtid-purged=OFF --max-allowed-packet=256M >/data/backup/full.sql

6,xtrabackup(XBK)备份使用

下载并安装依赖包

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.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm

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 -y install percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm

innobackupex 使用


备份核心理念
1. 针对非InnoDB,进行锁表备份,copy所有的非innoDB表文件
2. 针对InnoDB表,立即触发CKPT,copy所有InnoDB表相关的文件(ibdata1,ibd,frm).
并且将备份过程中产生,新的数据变化的部分redo一起备份走
3. 在恢复时,xbk会调用InnoDB引擎的CSR过程,将数据和redo的LSN追平,然后进行一致性恢复.
备份恢复
6,1全备
在配置文件中mysql模块配置下添加sockte文件位置
vim /etc/my.cnf
[mysql]
[xtrabackup]
socket=/tmp/mysql.sock
 全备:
[root@mister_f ~]#  innobackupex  --user=root --password=123 --no-timestamp /backup/full
[root@mister_f ~]# ll /backup/full/
total 77900
drwxr-x--- 2 root root     4096 May 21 16:01 backup
-rw-r----- 1 root root      487 May 21 16:01 backup-my.cnf
drwxr-x--- 2 root root     4096 May 21 16:01 bin
drwxr-x--- 2 root root     4096 May 21 16:01 binlog
drwxr-x--- 2 root root     4096 May 21 16:01 boy
drwxr-x--- 2 root root     4096 May 21 16:01 gtid
-rw-r----- 1 root root     1251 May 21 16:01 ib_buffer_pool
-rw-r----- 1 root root 79691776 May 21 16:01 ibdata1
drwxr-x--- 2 root root     4096 May 21 16:01 mysql
drwxr-x--- 2 root root     4096 May 21 16:01 performance_schema
drwxr-x--- 2 root root    12288 May 21 16:01 sys
drwxr-x--- 2 root root     4096 May 21 16:01 test
drwxr-x--- 2 root root     4096 May 21 16:01 wordpress
drwxr-x--- 2 root root     4096 May 21 16:01 world
-rw-r----- 1 root root       63 May 21 16:01 xtrabackup_binlog_info
-rw-r----- 1 root root      117 May 21 16:01 xtrabackup_checkpoints
-rw-r----- 1 root root      556 May 21 16:01 xtrabackup_info
-rw-r----- 1 root root     2560 May 21 16:01 xtrabackup_logfile


利用全备进行恢复

1. 停止数据库
[root@db01 ~]# pkill mysqld

2. 删除数据库
[root@mister_f ~]# rm -rf /data/mysql/*

3 准备备份
将redo进行重做,已提交的写到数据文件,未提交的使用undo回滚掉。模拟了CSR的过程
[root@db01 ~]# innobackupex --apply-log /backup/full/

4. 恢复备份
[root@mister_f ~]# cp -a /backup/full/* /data/mysql/

5. 数据授权
[root@mister_f ~]#  chown -R mysql.mysql /data/mysql/*

6. 启动数据库
systemctl start mysqld

备份多出来的文件

备份集中多出来的文件
-rw-r----- 1 root root       24 Jun 29 09:59 xtrabackup_binlog_info
-rw-r----- 1 root root      119 Jun 29 09:59 xtrabackup_checkpoints
-rw-r----- 1 root root      489 Jun 29 09:59 xtrabackup_info
-rw-r----- 1 root root     2560 Jun 29 09:59 xtrabackup_logfile

xtrabackup_binlog_info :(备份时刻的binlog位置)
[root@mister_f full]# cat xtrabackup_binlog_info 
mysql-bin.000005    194 f4926326-afda-11eb-aca3-525400956ca6:1-36
记录的是备份时刻,binlog的文件名字和当时的结束的position,可以用来作为截取binlog时的起点。


[root@mister_f full]# cat xtrabackup_checkpoints 
backup_type = full-prepared
from_lsn = 0                  上次所到达的LSN号(对于全备就是从0开始,对于增量有别的显示方法)
to_lsn = 254842339            备份开始时间(ckpt)点数据页的LSN    
last_lsn = 254842348          备份结束后,redo日志最终的LSN
compact = 0
recover_binlog_info = 0

(1)备份时刻,立即将已经commit过的,内存中的数据页刷新到磁盘(CKPT).开始备份数据,数据文件的LSN会停留在to_lsn位置。
(2)备份时刻有可能会有其他的数据写入,已备走的数据文件就不会再发生变化了。
(3)在备份过程中,备份软件会一直监控着redo的undo,如果一旦有变化会将日志也一并备走,并记录LSN到last_lsn。
从to_lsn  --last_lsn 就是,备份过程中产生的数据变化.

#主要关注以上两个文件
6.2增量

模拟数据

create database full charset utf8mb4;
use full;
create table t1 (id int);
insert into t1 values(1),(2),(3);
commit;

进行周日的全备

[root@mister_f ~]# innobackupex --user=root --password=123 --no-timestamp /backup/full

模拟周一数据变化

create database inc1 charset utf8mb4;
use inc1;
create table t1 (id int);
insert into t1 values(1),(2),(3);
commit;

周二的增量:

[root@mister_f ~]# innobackupex  --user=root --password=123 --no-timestamp --incremental --incremental-basedir=/backup/full  /backup/inc1

--incremental                          开关
--incremental-basedir=/backup/full     基于那个备份进行增量  
/backup/inc1                           增量备份的位置点

检查备份:

[root@mister_f ~]# ll /backup/
total 8
drwxr-x--- 14 root root 4096 May 21 16:33 full
drwxr-x--- 15 root root 4096 May 21 17:00 inc1
[root@mister_f ~]# cat /backup/full/xtrabackup_checkpoints 
backup_type = full-backuped
from_lsn = 0
to_lsn = 254849041
last_lsn = 254849050
compact = 0
recover_binlog_info = 0
[root@mister_f ~]# cat /backup/inc1/xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 254849041
to_lsn = 254854952
last_lsn = 254854961
compact = 0
recover_binlog_info = 0

上一个last号码减9就是下一个from号码的开始

模拟周二的数据变化:

create database inc2 charset utf8mb4;
use inc2;
create table t1 (id int);
insert into t1 values(1),(2),(3);
commit;

模拟周二的增量备份:

[root@mister_f ~]# innobackupex   --user=root --password=123 --no-timestamp --incremental --incremental-basedir=/backup/inc1  /backup/inc2

模拟周三数据变化:

create database inc3 charset utf8mb4;
use inc3;
create table t1 (id int);
insert into t1 values(1),(2),(3);
commit;

模拟上午十点数据库崩溃

pkill mysqld 
\rm -rf /data/mysql/*
image.png
恢复数据前准备

恢复思路:

1. 数据库挂了首先挂维护页,停业务
2. 查找可用备份并处理备份:full+inc1+inc2 
3. binlog: inc2 到 故障时间点的binlog
4. 恢复全备+增量+binlog
5. 验证数据
6. 起业务,撤维护页

恢复过程:

整理备份:
1,整理全备
[root@mister_f ~]# innobackupex --apply-log --redo-only /backup/full/

--redo-only:这个选项在整理全备和所有的增量合并时除了最后一次增量时不用
功能:不做回滚

2,合并inc1到full

[root@mister_f ~]# innobackupex  --apply-log --redo-only --incremental-dir=/backup/inc1 /backup/full

--incremental-dir:将哪一个增量合并到哪里
合并后检查:
[root@mister_f ~]# cat /backup/full/xtrabackup_checkpoints 
backup_type = full-prepared
from_lsn = 0
to_lsn = 254854952
last_lsn = 254854961
compact = 0
recover_binlog_info = 0
[root@mister_f ~]# cat /backup/inc1/xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 254849041
to_lsn = 254854952
last_lsn = 254854961
compact = 0
recover_binlog_info = 0
#检查合并后的full中的last和inc1中的last号码一直说明full中已经有了inc1的增量

3,合并inc2到full
[root@mister_f ~]# innobackupex  --apply-log --incremental-dir=/backup/inc2 /backup/full

4,最后一次整理全备
[root@mister_f ~]# innobackupex --apply-log  /backup/full

截取二进制日志:
查看GTID
起点:
[root@mister_f ~]# cat /backup/inc2/xtrabackup_binlog_info 
mysql-bin.000007    2351    2d95ab58-ba1a-11eb-942a-525400956ca6:1-11,
38cf6993-ba0c-11eb-8fd4-525400956ca6:1-12,
f4926326-afda-11eb-aca3-525400956ca6:1-36
查看一下mysqlbinlog文件中的号码:
终点:
[root@mister_f ~]# mysqlbinlog /data/binlog/mysql-bin.000007 |grep 'SET'
[root@mister_f ~]# mysqlbinlog --skip-gtids --include-gtids='2d95ab58-ba1a-11eb-942a-525400956ca6:12-14' /data/binlog/mysql-bin.000007 >/backup/binlog.sql

恢复数据:
[root@mister_f ~]# cp -a /backup/full/* /data/mysql/
[root@mister_f ~]# chown -R mysql. /data/
[root@mister_f ~]# systemctl start mysqld
mysql> set sql_log_bin=0;
mysql> source /backup/binlog.sql;
mysql> set sql_log_bin=1;
验证数据:
mysql> select * from full.t1;
mysql> select * from inc1.t1;
mysql> select * from inc2.t1;
mysql> select * from inc3.t1;

7,迁移(5.6.44 - 5.7.26)

搭建5.6环境

创建目录
mkdir /data/mysql/data  -p 
mkdir /application/ -p
mkdir /data/binlog -p
上传软件至 /application 下并解压

创建用户修改权限
useradd mysql
chown -R mysql. /data /application/

修改环境变量
vim /etc/profile
export PATH=/application/mysql/bin:$PATH
source /etc/profile

初始化数据
yum remove mariadb-libs
yum install -y libaio-devel
rm -rf /data/mysql/data/*
/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data

准备配置文件和启动脚本

cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/application/mysql 
datadir=/data/mysql/data 
server_id=99
user=mysql
log_bin=/data/binlog/mysql-bin
binlog_format=row
socket=/tmp/mysql.sock 
[mysql]
socket=/tmp/mysql.sock
EOF
cp  /application/mysql/support-files/mysql.server /etc/init.d/mysqld

启动数据库

/etc/init.d/mysqld start
mysqladmin -uroot -p password 123

5.6数据迁移到5.7

5.6数据备份

mysqldump -uroot -p123 -A --master-data=2 --single-transaction -R -E --triggers >/tmp/full.sql
scp /tmp/full.sql 10.0.0.51:/data/3308 

准备5.7数据库
systemctl start mysqld3308
mysql -S /data/3308/mysql.sock
set sql_log_bin=0;
source /data/3308/full.sql
set sql_log_bin=1;
flush privileges;
mysql_upgrade  -uroot -p123 -S /data/3308/mysql.sock
由于旧版本哟一些系统库与新版本不同所以mysql使用mysql_upgrade工具进行更新升级到新版本兼容的库表及字段

上一篇下一篇

猜你喜欢

热点阅读