利用xtrabackup8.0完全、增量备份及还原MySQL8.0
作者:互联网
实验环境:
主机1:CentOS 8.3(IP地址:10.0.0.8/24),作为MySQL的源服务器,MySQL版本:8.0
主机2:CentOS 8.3(IP地址:10.0.0.18/24),作为MySQL的目标服务器,MySQL版本:8.0
两台主机都需要安装MySQL8.0以及xtrabackup8.0,需要配置EPEL源,这里我的YUM源指向我本地搭建的YUM源仓库
[root@CentOS8 ~]# cat /etc/yum.repos.d/CentOS-8.repo [AppStream] name=CentOS 8 AppStream baseurl=http://172.18.61.80/centos/$releasever/AppStream/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [BaseOS] name=CentOS 8 BaseOS baseurl=http://172.18.61.80/centos/$releasever/BaseOS/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [extras] name=CentOS 8 extras baseurl=http://172.18.61.80/centos/extras-$releasever/ gpgcheck=0 [epel] name=CentOS 8 EPEL baseurl=http://172.18.61.80/epel-$releasever/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 enabled=1
xtrabackup下载地址:https://www.percona.com/downloads/
主机1安装MySQL8.0并设为开机自启:
[root@CentOS8 ~]# yum install -y mysql-server [root@CentOS8 ~]# yum info mysql-server Last metadata expiration check: 0:04:20 ago on Tue 18 May 2021 07:03:34 PM CST. Installed Packages Name : mysql-server Version : 8.0.21 Release : 1.module_el8.2.0+493+63b41e36 Architecture : x86_64 Size : 108 M Source : mysql-8.0.21-1.module_el8.2.0+493+63b41e36.src.rpm Repository : @System From repo : AppStream Summary : The MySQL server and related files URL : http://www.mysql.com License : GPLv2 with exceptions and LGPLv2 and BSD Description : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a : client/server implementation consisting of a server daemon (mysqld) : and many different client programs and libraries. This package contains : the MySQL server and some accompanying files and directories. [root@CentOS8 ~]# systemctl enable --now mysqld.service
一、备份过程(在主机1上进行操作)
1.安装下载好的xtrabackup RPM包
[root@CentOS8 ~]# yum install -y percona-xtrabackup-80-8.0.23-16.1.el8.x86_64.rpm
2.创建备份目录,并进行完全备份
[root@CentOS8 ~]# mkdir /backup [root@CentOS8 ~]# xtrabackup -uroot --backup --target-dir=/backup/base xtrabackup: recognized server arguments: --datadir=/var/lib/mysql xtrabackup: recognized client arguments: --user=root --backup=1 --target-dir=/backup/base xtrabackup version 8.0.23-16 based on MySQL server 8.0.23 Linux (x86_64) (revision id: 934bc8f) 210518 19:19:23 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root' (using password: NO). 210518 19:19:23 version_check Connected to MySQL server 210518 19:19:23 version_check Executing a version check against the server... 中间略。。。。 210518 19:19:34 Executing UNLOCK INSTANCE 210518 19:19:34 All tables unlocked 210518 19:19:34 [00] Copying ib_buffer_pool to /backup/base/ib_buffer_pool 210518 19:19:34 [00] ...done 210518 19:19:34 Backup created in directory '/backup/base/' MySQL binlog position: filename 'binlog.000002', position '156' 210518 19:19:34 [00] Writing /backup/base/backup-my.cnf 210518 19:19:34 [00] ...done 210518 19:19:34 [00] Writing /backup/base/xtrabackup_info 210518 19:19:34 [00] ...done xtrabackup: Transaction log of lsn (17720213) to (17720223) was copied. 210518 19:19:35 completed OK! [root@CentOS8 ~]#
3.第一次修改数据,导入一个数据库
[root@CentOS8 ~]# mysql < hellodb_innodb.sql [root@CentOS8 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.21 Source distribution Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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 | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> quit Bye [root@CentOS8 ~]#
4.第一次进行增量备份
[root@CentOS8 ~]# xtrabackup -uroot --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base xtrabackup: recognized server arguments: --datadir=/var/lib/mysql xtrabackup: recognized client arguments: --user=root --backup=1 --target-dir=/backup/inc1 --incremental-basedir=/backup/base xtrabackup version 8.0.23-16 based on MySQL server 8.0.23 Linux (x86_64) (revision id: 934bc8f) 210518 19:24:53 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root' (using password: NO). 210518 19:24:53 version_check Connected to MySQL server 210518 19:24:53 version_check Executing a version check against the server... 210518 19:24:53 version_check Done. 210518 19:24:53 Connecting to MySQL server host: localhost, user: root, password: not set, port: not set, socket: not set Using server version 8.0.21 中间略。。。。 210518 19:24:55 Executing UNLOCK INSTANCE 210518 19:24:55 All tables unlocked 210518 19:24:55 [00] Copying ib_buffer_pool to /backup/inc1/ib_buffer_pool 210518 19:24:55 [00] ...done 210518 19:24:55 Backup created in directory '/backup/inc1/' MySQL binlog position: filename 'binlog.000003', position '156' 210518 19:24:55 [00] Writing /backup/inc1/backup-my.cnf 210518 19:24:55 [00] ...done 210518 19:24:55 [00] Writing /backup/inc1/xtrabackup_info 210518 19:24:55 [00] ...done xtrabackup: Transaction log of lsn (17857378) to (17857388) was copied. 210518 19:24:56 completed OK! [root@CentOS8 ~]#
5.第二次修改数据,插入两条表记录
mysql> use hellodb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.00 sec) mysql> insert students(name,age,gender)values('zhangsan',20,'M'); Query OK, 1 row affected (0.00 sec) mysql> insert students(name,age,gender)values('lisi',22,'M'); Query OK, 1 row affected (0.00 sec) mysql> select * from hellodb.students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | | 26 | zhangsan | 20 | M | NULL | NULL | | 27 | lisi | 22 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 27 rows in set (0.00 sec) mysql> quit Bye [root@CentOS8 ~]#
6.进行第二次增量备份
[root@CentOS8 ~]# xtrabackup -uroot --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1 中间略。。。。 210518 19:33:25 completed OK! [root@CentOS8 ~]#
备份过程生成三个备份目录,/backup/{base,inc1,inc2}
[root@CentOS8 ~]# ll /backup/ total 12 drwxr-x--- 5 root root 4096 May 18 19:19 base drwxr-x--- 6 root root 4096 May 18 19:24 inc1 drwxr-x--- 6 root root 4096 May 18 19:33 inc2 [root@CentOS8 ~]#
7.将备份的数据复制到目标主机2,复制之前需要在主机2上创建/backup/目录
[root@CentOS8 ~]# scp -r /backup/* 10.0.0.18:/backup/
二、还原过程(在主机2上进行操作)
1.安装MySQL8.0,安装完成之后不要立即启动服务
[root@CentOS8 ~]# yum install -y mysql-server [root@CentOS8 ~]# yum info mysql-server Last metadata expiration check: 0:31:07 ago on Tue 18 May 2021 07:12:06 PM CST. Installed Packages Name : mysql-server Version : 8.0.21 Release : 1.module_el8.2.0+493+63b41e36 Architecture : x86_64 Size : 108 M Source : mysql-8.0.21-1.module_el8.2.0+493+63b41e36.src.rpm Repository : @System From repo : AppStream Summary : The MySQL server and related files URL : http://www.mysql.com License : GPLv2 with exceptions and LGPLv2 and BSD Description : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a : client/server implementation consisting of a server daemon (mysqld) : and many different client programs and libraries. This package contains : the MySQL server and some accompanying files and directories. [root@CentOS8 ~]#
2.安装xtrabackup8.0
[root@CentOS8 ~]# yum install -y percona-xtrabackup-80-8.0.23-16.1.el8.x86_64.rpm
3.预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务
[root@CentOS8 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base 中间略。。。。 210518 19:49:00 completed OK! [root@CentOS8 ~]#
4.合并第1次增量备份到完全备份
[root@CentOS8 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1 中间略。。。。 210518 19:51:29 completed OK! [root@CentOS8 ~]#
5.合并第2次增量备份到完全备份:最后一次还原不需要加选项--apply-log-only
[root@CentOS8 ~]# xtrabackup --prepare --target-dir=/backup/base --incremental-dir=/backup/inc2 中间略。。。。 210518 19:53:29 completed OK! [root@CentOS8 ~]#
6.复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动
[root@CentOS8 ~]# systemctl status mysqld.service ● mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: inactive (dead) [root@CentOS8 ~]# ll /var/lib/mysql total 0 [root@CentOS8 ~]# xtrabackup --copy-back --target-dir=/backup/base 210518 19:55:52 [01] ...done. 210518 19:55:52 completed OK! [root@CentOS8 ~]#
7.还原属性
[root@CentOS8 ~]# chown -R mysql:mysql /var/lib/mysql [root@CentOS8 ~]# ll -d /var/lib/mysql drwxr-xr-x 6 mysql mysql 305 May 18 19:55 /var/lib/mysql [root@CentOS8 ~]#
8.启动MySQL服务
[root@CentOS8 ~]# systemctl start mysqld.service
9.验证数据库:
[root@CentOS8 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 Source distribution Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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 | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> select * from hellodb.students where name='zhangsan' or name='lisi'; +-------+----------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+----------+-----+--------+---------+-----------+ | 26 | zhangsan | 20 | M | NULL | NULL | | 27 | lisi | 22 | M | NULL | NULL | +-------+----------+-----+--------+---------+-----------+ 2 rows in set (0.04 sec) mysql> quit Bye [root@CentOS8 ~]#
标签:210518,19,backup,备份,MySQL8.0,mysql,xtrabackup8.0,root,CentOS8 来源: https://blog.51cto.com/u_13589013/2784604