mysql数据库使用xtrabackup工具 实现备份和还原

下载xtrabackup工具包

percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm 工具包可以从官网下载
https://www.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.4/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.4-1.el7.x86_64.rpm

安装xtrabackup工具包(需要启用epel源)

[root@CentOS7 ~]# yum install -y percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm

一、实验:xtrabackup 实现完全备份和还原

备份过程

1)在原主机做完全备份到/backups

原主机上有mysql的hellodb表

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

使用xtrabackup 工具备份数据库

[root@CentOS7 ~]# xtrabackup --backup --target-dir=/data/backup/ 

查看backup下已经生成了备份的文件

[root@CentOS7 ~]#ls /data/backup/ -l 
total 18460
-rw-r----- 1 root root      431 May  6 22:32 backup-my.cnf
drwxr-x--- 2 root root      272 May  6 22:32 hellodb
-rw-r----- 1 root root 18874368 May  6 22:32 ibdata1
drwxr-x--- 2 root root     4096 May  6 22:32 mysql
drwxr-x--- 2 root root     4096 May  6 22:32 performance_schema
drwxr-x--- 2 root root       20 May  6 22:32 test
-rw-r----- 1 root root       21 May  6 22:32 xtrabackup_binlog_info
-rw-r----- 1 root root      113 May  6 22:32 xtrabackup_checkpoints
-rw-r----- 1 root root      468 May  6 22:32 xtrabackup_info
-rw-r----- 1 root root     2560 May  6 22:32 xtrabackup_logfile

xtrabackup_binlog_info、 xtrabackup_checkpoints、 xtrabackup_info文件里存放了备份时的信息

[root@CentOS7 backup]#cat xtrabackup_binlog_info 
mysql-bin.000011    245
[root@CentOS7 backup]#cat xtrabackup_checkpoints 
backup_type = full-backuped
from_lsn = 0
to_lsn = 1638757
last_lsn = 1638757
compact = 0
recover_binlog_info = 0
[root@CentOS7 backup]#cat xtrabackup_info 
uuid = c65e06f6-700b-11e9-9ac5-000c2926023f
name = 
tool_name = xtrabackup
tool_command = --backup --target-dir=/data/backup/
tool_version = 2.4.13
ibbackup_version = 2.4.13
server_version = 5.5.60-MariaDB
start_time = 2019-05-06 22:32:26
end_time = 2019-05-06 22:32:28
lock_time = 0
binlog_pos = filename 'mysql-bin.000011', position '245'
innodb_from_lsn = 0
innodb_to_lsn = 1638757
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
2)将备份的文件夹全部拷贝至目标主机
[root@CentOS7 ~]# scp -r /data/backup/* 192.168.93.102:/data/backup

还原过程(在目标主机上 )

1)预准备:确保数据一致,提交完成的事务,回滚未完成的事务
[root@CentOS7 ~]# xtrabackup --prepare --target-dir=/data/backup/ 
2)复制到数据库目录

注意:数据库目录必须为空,MySQL服务不能启动

[root@CentOS7 ~]# systemctl stop mariadb 
[root@CentOS7 ~]# ls  /var/lib/mysql/ -l 
total 0
[root@CentOS7 ~]#
[root@CentOS7 ~]# xtrabackup --copy-back --target-dir=/data/backup/ 
3)还原属性
[root@CentOS7 ~]# chown -R mysql:mysql /var/lib/mysql 
4)启动服务
[root@CentOS7 ~]# systemctl start mariadb 
5)测试
[root@CentOS7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

二、实验:xtrabackup 实现增量备份和还原

备份过程

1)完全备份
[root@CentOS7 ~]# xtrabackup --backup --target-dir=/data/backup/base
2)第一次修改数据
MariaDB [hellodb]> insert teachers (name,age)value('xiaoai',33);
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)

MariaDB [hellodb]> drop table toc;
Query OK, 0 rows affected (0.01 sec)
3)第一次增量备份
[root@CentOS7 ~]# xtrabackup --backup --target-dir=/data/backup/inc1 --incremental-basedir=/data/backup/base
4)第二次修改数据
MariaDB [hellodb]> update teachers set gender='F' where tid=5;`
5)第二次增量
[root@CentOS7 ~]#xtrabackup --backup --target-dir=/data/backup/inc2 --incremental-basedir=/data/backup/inc1
6)将备份的数据库文件拷贝至远程主机

备份过程生成三个备份目录

[root@CentOS7 ~]# ll /data/backup/
total 0
drwxr-x--- 6 root root 217 May  6 23:11 base
drwxr-x--- 6 root root 243 May  6 23:13 inc1
drwxr-x--- 6 root root 243 May  6 23:17 inc2

[root@CentOS7 ~]# scp -r /data/backup/* 192.168.93.102:/data/backup

还原过程

在远程主机上确认是否复制成功

[root@CentOS7 ~]# ll /data/backup/
total 0
drwxr-x---. 6 root root 217 May  6 15:19 base
drwxr-x---. 6 root root 243 May  6 15:19 inc1
drwxr-x---. 6 root root 243 May  6 15:19 inc2
1)预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务
[root@CentOS7 ~]# xtrabackup --prepare --apply-log-only --target-dir=/data/backup/base
2)合并第1次增量备份到完全备份
[root@CentOS7 ~]# xtrabackup --prepare --apply-log-only --target-dir=/data/backup/base --incremental-dir=/data/backup/inc1
3)合并第2次增量备份到完全备份

如果是最后一次增量备份,还原时不需要加选项--apply-log-only

[root@CentOS7 ~]# xtrabackup --prepare  --target-dir=/data/backup/base --incremental-dir=/data/backup/inc2
4)复制到数据库目录

注意数据库目录必须为空,MySQL服务不能启动

[root@CentOS7 ~]# xtrabackup --copy-back --target-dir=/data/backup/base
5)还原属性
[root@CentOS7 ~]# chown -R mysql:mysql /var/lib/mysql
6)启动服务
[root@CentOS7 ~]# systemctl start mariadb
7)测试是否还原
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | xiaoai        |  33 | F      |
+-----+---------------+-----+--------+
5 rows in set (0.00 sec)

转载于:https://blog.51cto.com/14233815/2390053

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值