赞
踩
(1)innobackupex启动后,会先fork一个进程,用于启动xtrabackup,然后等待xtrabackup备份ibd数据文件;
(2)xtrabackup在备份innoDB数据是,有2种线程:redo拷贝线程和ibd数据拷贝线程。xtrabackup进程开始执行后,会启动一个redo拷贝的线程,用于从最新的checkpoint点开始顺序拷贝redo.log;再启动ibd数据拷贝线程,进行拷贝ibd数据。这里是先启动redo拷贝线程的。在此阶段,innobackupex进行处于等待状态(等待文件被创建)
(4)xtrabackup拷贝完成ibd数据文件后,会通知innobackupex(通过创建文件),同时xtrabackup进入等待状态(redo线程依旧在拷贝redo.log)
(5)innobackupex收到xtrabackup通知后哦,执行FLUSH TABLES WITH READ LOCK(FTWRL),取得一致性位点,然后开始备份非InnoDB文件(如frm、MYD、MYI、CSV、opt、par等格式的文件),在拷贝非InnoDB文件的过程当中,数据库处于全局只读状态。
(6)当innobackup拷贝完所有的非InnoDB文件后,会通知xtrabackup,通知完成后,进入等待状态;
(7)xtrabackup收到innobackupex备份完成的通知后,会停止redo拷贝线程,然后通知innobackupex,redo.log文件拷贝完成;
(8)innobackupex收到redo.log备份完成后,就进行解锁操作,执行:UNLOCK TABLES;
(9)最后innbackupex和xtrabackup进程各自释放资源,写备份元数据信息等,innobackupex等xtrabackup子进程结束后退出。
Xtrabackup是一个开源的免费的热备份工具,在Xtrabackup包中主要有Xtrabackup和innobakcupex两个工具。其中Xtrabackup只能备份InnoDB和XtraDB两种引擎;Innobackup则是封装了Xtrabackup,同时增加了备份MylSAM引擎功能。
Xtrabackup备份时不能备份表结构、触发器等等,也不能智能区分.idb数据库文件。另外innobakcup还不能完全支持增量备份,需要和Xtrabackup结合起来实现全备的功能
1)下载安装包
2)解压安装
[root@k8s-master01 ~]# ls
libev-4.04-2.el6.x86_64.rpm Percona-XtraBackup-2.4.21-r5988af5-el7-x86_64-bundle.tar
[root@k8s-master01 ~]# yum localinstall libev-4.04-2.el6.x86_64.rpm -y
[root@k8s-master01 ~]# tar xf Percona-XtraBackup-2.4.21-r5988af5-el7-x86_64-bundle.tar # 解压完之后出出现三个rpm
[root@k8s-master01 ~]# yum install percona-xtrabackup-24-2.4.21-1.el7.x86_64.rpm -y
3)配置文件
4)重启mysqld
一般使用的是innobackupex脚本,因为innobackupex是perl脚本对xtrbackup的封装和功能扩展
备份数据库时会涉及到两个用户:系统用户与数据库内部的用户
innobackupex [参数] [目的地址] [源地址]
--user # 以什么用于身份进行操作
--password # 数据库用户的密码
--port # 数据库的端口号,默认3306
--stream # 打包(数据流)
--defaults-file # 指定默认配置文件,默认读取/etc/my.cnf
--no-timestamp # 不创建时间戳文件,而改用目的地址(可以自动创建)
--copy-back # 备份还原的主要选项
--incremental # 使用增量备份,默认使用的完整备份
--incremental-basedir # 与--incremental选项联合使用,该参数指定上一级备份的地址来做增量备份
[root@k8s-master01 ~]# innobackupex --user=root --password=chinaedu /home/mysqlbackup/ # 备份的时候有大量的日志信息
[root@k8s-master01 ~]# ls /home/mysqlbackup/
2021-01-29_11-17-03
[root@k8s-master01 ~]# innobackupex --user=root --password=chinaedu /home/mysqlbackup/ 2>> /home/mysqlbackup/bakcup.log # 将备份输出信息保存到文件
[root@k8s-master01 ~]# innobackupex --user=root --password=chinaedu --no-timestam /home/mysqlbackup/test 2>> /home/mysqlbackup/bakcup-test.log # 不使用时间戳创建目录,可自动创建目的地址
[root@k8s-master01 ~]# ls /home/mysqlbackup/ # 可以看到有自己创建的test目录
2021-01-29_11-17-03 2021-01-29_11-21-52 bakcup.log test bakcup-test.log
[root@k8s-master01 ~]# systemctl stop mysqld
[root@k8s-master01 ~]# rm -rf /var/lib/mysql/* # 危险操作,请在测试环境测试
[root@k8s-master01 ~]# innobackupex --copy-back /home/mysqlbackup/2021-01-29_11-25-59/
[root@k8s-master01 ~]# ll /var/lib/mysql/
总用量 12324
-rw-r----- 1 root root 436 1月 29 11:27 ib_buffer_pool
-rw-r----- 1 root root 12582912 1月 29 11:27 ibdata1
drwxr-x--- 2 root root 4096 1月 29 11:27 mysql
drwxr-x--- 2 root root 8192 1月 29 11:27 performance_schema
drwxr-x--- 2 root root 8192 1月 29 11:27 sys
drwxr-x--- 2 root root 20 1月 29 11:27 test1
drwxr-x--- 2 root root 48 1月 29 11:27 test2
-rw-r----- 1 root root 475 1月 29 11:27 xtrabackup_info
[root@k8s-master01 ~]# chown -R mysql.mysql /var/lib/mysql/
[root@k8s-master01 ~]# systemctl start mysqld
[root@k8s-master01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.01 sec)
[root@k8s-master01 ~]# innobackupex --user=root --password=chinaedu /home/mysqlbackup/
[root@k8s-master01 ~]# cd /home/mysqlbackup/2021-01-29_11-31-32
[root@k8s-master01 2021-01-29_11-31-32]# cat xtrabackup_checkpoints
backup_type = full-backuped # 代表完整备份
from_lsn = 0
to_lsn = 2747432
last_lsn = 2747441
compact = 0
recover_binlog_info = 0
flushed_lsn = 2747441
[root@k8s-master01 2021-01-29_11-31-32]# mysql -uroot -p
mysql> create database test3;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test3 |
+--------------------+
7 rows in set (0.14 sec)
mysql> create table test3.user(id int,name varchar(20));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into test3.user values(1,'test');
Query OK, 1 row affected (0.16 sec)
mysql> select * from test3.user;
+------+------+
| id | name |
+------+------+
| 1 | test |
+------+------+
1 row in set (0.00 sec)
[root@k8s-master01 2021-01-29_11-31-32]# innobackupex --user=root --password=chinaedu --incremental /home/mysqlbackup/ --incremental-basedir=/home/mysqlbackup/2021-01-29_11-31-32 # 创建增量备份
[root@k8s-master01 2021-01-29_11-31-32]# cat 2021-01-29_13-52-49/xtrabackup_checkpoints
backup_type = incremental # 表示增量备份
from_lsn = 2747432
to_lsn = 2752047
last_lsn = 2752056
compact = 0
recover_binlog_info = 0
flushed_lsn = 2752056
1)把全量备份进行一次apply
[root@k8s-master01 mysqlbackup]# innobackupex --apply-log --redo-only /home/mysqlbackup/2021-01-29_11-31-32/
2)应用第一次增量备份到全量备份(把全量备份跟增量备份进行一个结合)
[root@k8s-master01 mysqlbackup]# innobackupex --apply-log --redo-only /home/mysqlbackup/2021-01-29_11-31-32/ --incremental-dir=/home/mysqlbackup/2021-01-29_13-52-49
3)查看全量备份xtrabackup_checkpoints
[root@k8s-master01 mysqlbackup]# cat 2021-01-29_11-31-32/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 2752047
last_lsn = 2752056
compact = 0
recover_binlog_info = 0
flushed_lsn = 2752056
4)停止mysqld服务并移除数据目录(生产环境建议用mv)
[root@k8s-master01 mysqlbackup]# systemctl stop mysqld
[root@k8s-master01 mysqlbackup]# rm -rf /var/lib/mysql/*
5)使用–copy-back参数恢复拷贝到data目录
[root@k8s-master01 mysqlbackup]# innobackupex --copy-back /home/mysqlbackup/2021-01-29_11-31-32/
6)验证数据还原
[root@k8s-master01 mysqlbackup]# ls /var/lib/mysql
ib_buffer_pool mysql sys test2 xtrabackup_binlog_pos_innodb xtrabackup_master_key_id
ibdata1 performance_schema test1 test3 xtrabackup_info
7)授权并启用MySQL
[root@k8s-master01 mysqlbackup]# chown -R mysql.mysql /var/lib/mysql/
[root@k8s-master01 mysqlbackup]# systemctl start mysqld
[root@k8s-master01 mysqlbackup]# mysql -uroot -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test3 |
+--------------------+
7 rows in set (0.00 sec)
mysql> select * from test3.user;
+------+------+
| id | name |
+------+------+
| 1 | test |
+------+------+
1 row in set (0.01 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。