innobackup数据备份使用
MySQL Backup--Xtrabackup远程备份和限速备份
使用xbstream 备份到远程服务器
##xbstream 备份到远程服务器 innobackupex \ --defaults-file="/export/servers/mysql/etc/my.cnf" \ --host="localhost" \ --port=3358 \ --user="backuper" \ --password="backup@123" \ --stream=xbstream "/export/mysql_backup/" \ | ssh root@10.0.0.2 \ "gzip ->/export/mysql_backup/mysql_backup.gz" ## 由于备份文件使用xbstream和gzip进行两次压缩,因此需要进行两次解压 ## 第一次使用gzip解压备份 gzip -d mysql_backup.gz ##第二次使用xbstream解压gzip xbstream -x < mysql_backup
xtrabackup --host=127.0.0.1 --user=root --password='Admin1234%^&*' --port=3306 --socket=/var/lib/mysql/mysql.sock --backup --target-dir=/opt/0722/ --incremental-basedir=/opt/0721 --no-server-version-check --no-lock
使用tar备份到远程服务器
##tar备份到远程服务器 innobackupex \ --defaults-file="/export/servers/mysql/etc/my.cnf" \ --host="localhost" \ --port=3358 \ --user="backuper" \ --password="backup@123" \ --stream=tar "/export/mysql_backup/" \ | ssh root@10.0.0.2 \ "gzip ->/export/mysql_backup/mysql_backup.tar.gz" ##使用tar解压 tar -ixzvf mysql_backup.tar.gz
本地限速备份(使用PV限速)
## 备份到/export/mysql_backup/full ## 使用tar进行流备份,限速后再使用tar -x 解压 cd /export/mysql_backup/full innobackupex \ --defaults-file="/export/servers/mysql/etc/my.cnf" \ --host="localhost" \ --port=3358 \ --user="backuper" \ --password="backup@123" \ --stream=tar \ "/export/mysql_backup/tmp/" |pv -q -L50m | tar -x
##############################################################全量增量恢复备份########################################
实验环境:
主机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源仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[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 [BaseOS] name=CentOS 8 BaseOS baseurl=http://172.18.61.80/centos/$releasever/BaseOS/ gpgcheck=1 enabled=1 [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/ gpgcheck=1 enabled=1 |
xtrabackup下载地址:https://www.percona.com/downloads/
主机1安装MySQL8.0并设为开机自启:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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包
1
|
[root@CentOS8 ~]# yum install -y percona-xtrabackup-80-8.0.23-16.1.el8.x86_64.rpm |
2.创建备份目录,并进行完全备份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[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.第一次修改数据,导入一个数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[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.第一次进行增量备份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[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.第二次修改数据,插入两条表记录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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.进行第二次增量备份
1
2
3
4
|
[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}
1
2
3
4
5
6
|
[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/目录
1
|
[root@CentOS8 ~]# scp -r /backup/* 10.0.0.18:/backup/ |
二、还原过程(在主机2上进行操作)
1.安装MySQL8.0,安装完成之后不要立即启动服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[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
1
|
[root@CentOS8 ~]# yum install -y percona-xtrabackup-80-8.0.23-16.1.el8.x86_64.rpm |
3.预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务
1
2
3
4
|
[root@CentOS8 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base 中间略。。。。 210518 19:49:00 completed OK! [root@CentOS8 ~]# |
4.合并第1次增量备份到完全备份
1
2
3
4
|
[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
1
2
3
4
|
[root@CentOS8 ~]# xtrabackup --prepare --target-dir=/backup/base --incremental-dir=/backup/inc2 中间略。。。。 210518 19:53:29 completed OK! [root@CentOS8 ~]# |
6.复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动
1
2
3
4
5
6
7
8
9
10
|
[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.还原属性
1
2
3
4
|
[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服务
1
|
[root@CentOS8 ~]# systemctl start mysqld.service |
9.验证数据库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[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 ~]# |