Xtrabackup实现数据备份与恢复

一、Xtrabackup工具的介绍
  Xtrabackup工具是由percona开源的免费的数据库热备份软件,它能对InnoDB数据库存储引擎和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加上表级锁);mysqldump工具的备份方式是采用的数据库逻辑备份,其最大的缺陷是备份和恢复速度较慢,如果数据库大于50G,那么使用mysqldump工具来进行备份就不太合适了。
  Xtrabackup安装完成后有4个可执行文件,其中2个比较重要的备份工具是innobackupex、xtrabackup。
  1)xtrabackup是专门用来备份InnoDB表的,和mysql server没有交互;
  2)innobackupex是一个封装xtrabackup的Perl脚本,支持同时备份Innodb存储引擎的数据库和Myisam存储引擎的数据库,但在对Myisam存储引擎数据库备份时需要加上一个全局的读锁;
  3)xbcryt加密解密备份工具;
  4)xbstream流传打包传输工具,类似于tar;

二、Xtrabackup的优点
  1)备份速度快。物理备份更可靠;
  2)备份过程不会打断正在执行的事务(无需锁表)
  3)能够基于压缩等功能节约磁盘空间和流量
  4)自动备份校验
  5)还原速度快
  6)可以流传将备份文件传输到另一台机器上实现冗余
  7)在不增加服务器负载的情况备份数据

三、Xtrabackup的备份概念
  备份原理:备份开始的时候首先会开启一个后台检测进程,实时检测mysql redo的变化,一旦发现有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log中,之后复制innodb的数据文件–系统表空间文件ibdatax,复制结束后,将执行flush tables with readlock,然后复制.frm MYD MYI等文件,最后执行unlock tables,最终停止xtrabackup_log。

Xtrabackup增量备份原理:
  1)首先我们需要完成一个完全备份,并记录下此时检查点LSN;
  2)然后增量备份时,比较表空间中每个页的LSN是否大于上次的LSN,若是则备份该页并记录当前点的LSN;

增量备份的优点:
  1)数据库的数据量太大没有足够的空间来进行全量备份,增量备份能有效的节省空间,并且效率高;
  2)支持热备份,备份过程中不用锁表(主要是针对InnoDB存储引擎而言),不阻塞数据库的读写;
  3)每日备份只产生少量的数据,也可采用远程备份,节省本地空间;
  4)备份恢复基于文件操作,降低直接对数据库操作风险;
  5)备份的效率更高,恢复效率更高;

四、Xtrabackup的使用
1)下载安装xtrabackup

[root@www application]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/6/x86_64/Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
--2019-08-25 04:02:17--  https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/6/x86_64/Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
Resolving www.percona.com (www.percona.com)... 74.121.199.234
Connecting to www.percona.com (www.percona.com)|74.121.199.234|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 65689600 (63M) [application/x-tar]
Saving to: ‘Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar.1’

 8% [====>                                                       ] 5,816,887   1.88MB/s  
   
[root@www application]# tar -xf Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@www application]# yum install percona-xtrabackup-24-2.4.9-1.el6.x86_64.rpm -y
[root@www application]# which xtrabackup
[root@www application]# innobackupex -v
innobackupex version 2.4.9 Linux (x86_64) (revision id: a467167cdd4)
[root@www application]# echo "export PATH=/usr/local/xtrabackup:$PATH"  > /etc/profile.d/xtrabackup.sh

2)Xtrabackup全量备份数据与恢复

[root@www ~]# mysql -uroot -p -e"show databases;"
Enter password: 
+------------------------------+
| Database                     |
+------------------------------+
| information_schema           |
| #mysql50#2019-08-24_23-54-29 |
| #mysql50#2019-08-24_23-55-01 |
| #mysql50#2019-08-24_23-58-24 |
| myemployees                  |
| mysql                        |
| oldboydb                     |
| performance_schema           |
| test                         |
| xixi                         |
| zabbix                       |
+------------------------------+
[root@www ~]# 
[root@www ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password="redhat" --backup /backup
#指明数据库的用户名、密码、配置文件等信息
#--backup后的路径时备份的物理文件的存放位置

xtrabackup: Transaction log of lsn (826740596) to (826742360) was copied.
190825 04:08:58 completed OK!
#出现此语句代表备份成功

[root@www ~]# ll /backup/2019-08-25_04-08-53/
total 92204
drwxr-x--- 2 root root       19 Aug 25 04:08 2019-08-24_23-54-29
drwxr-x--- 2 root root       19 Aug 25 04:08 2019-08-24_23-55-01
drwxr-x--- 2 root root       19 Aug 25 04:08 2019-08-24_23-58-24
-rw-r----- 1 root root      417 Aug 25 04:08 backup-my.cnf
-rw-r----- 1 root root 94371840 Aug 25 04:08 ibdata1
drwxr-x--- 2 root root       96 Aug 25 04:08 myemployees
drwxr-x--- 2 root root     4096 Aug 25 04:08 mysql
drwxr-x--- 2 root root       37 Aug 25 04:08 oldboydb
drwxr-x--- 2 root root     4096 Aug 25 04:08 performance_schema
drwxr-x--- 2 root root     4096 Aug 25 04:08 test
drwxr-x--- 2 root root       57 Aug 25 04:08 xixi
-rw-r----- 1 root root      117 Aug 25 04:08 xtrabackup_checkpoints
-rw-r----- 1 root root      463 Aug 25 04:08 xtrabackup_info
-rw-r----- 1 root root     4608 Aug 25 04:08 xtrabackup_logfile
drwxr-x--- 2 root root     8192 Aug 25 04:08 zabbix
#这个备份文件目录里面就是相关的备份文件,同样也可以看到我们创建的库的名称
[root@www ~]# innobackupex --apply-log /backup/2019-08-25_04-08-53/
#使用此参数使相关的数据性文件保持一致性状态

3)模拟删除数据库并恢复

[root@www ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
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)]> drop database zabbix;
Query OK, 146 rows affected (0.50 sec)

MariaDB [(none)]> 
MariaDB [(none)]> exit
Bye
[root@www ~]# 

[root@www ~]# rm -rf /var/lib/mysql/* 
#恢复数据库之前我们需要把mysql数据目录下的文件都删除
[root@www ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/2019-08-25_04-08-53/
................................................
190825 04:19:42 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1
190825 04:19:42 [01]        ...done
190825 04:19:42 [01] Creating directory /var/lib/mysql/2019-08-25_04-17-15
190825 04:19:42 [01] ...done.190825 04:19:42 [01] Creating directory /var/lib/mysql/2019-08-25_04-18-55
190825 04:19:42 [01] ...done.190825 04:19:42 completed OK!

[root@www ~]# chown mysql:mysql   /var/lib/mysql/* -R 
[root@www ~]# 
[root@www ~]# systemctl restart mariadb 
[root@www ~]# mysql -uroot -predhat   -e"show databases;"
+------------------------------+
| Database                     |
+------------------------------+
| information_schema           |
| #mysql50#2019-08-24_23-54-29 |
| #mysql50#2019-08-24_23-55-01 |
| #mysql50#2019-08-24_23-58-24 |
| #mysql50#2019-08-25_04-17-15 |
| #mysql50#2019-08-25_04-18-55 |
| myemployees                  |
| mysql                        |
| oldboydb                     |
| performance_schema           |
| test                         |
| xixi                         |
| zabbix                       |
+------------------------------+
#zabbix数据库已然恢复成功!

五、Xtrabackup实现增量备份与恢复
  注意:增量备份仅仅能应用于InnoDB存储引擎的数据库或者是XtraDB存储引擎的数据库,对于MyISAM存储引擎的数据库,增量备份与完全备份相同。
1)在数据库中创建新的数据

[root@www ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
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)]> 
MariaDB [(none)]> create database   test01  charset=utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> 
MariaDB [(none)]> use test01;
Database changed
MariaDB [test01]>  create table   student(id int(10) not null primary key,name varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test01]> select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | xiaoming |
|  2 | xiaohong |
+----+----------+
2 rows in set (0.00 sec)

2)创建用于增量备份的数据

[root@www ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=redhat --incremental /backup/ --incremental-basedir=/backup/2019-08-25_04-08-53 
#--incremental 后面接增量备份文件的存放路径
#--incremental-basedir 指明上一次全备或增量备份的目录,基于哪个目录文件做增量备份

[root@www ~]# ls -l /backup/
total 22740
drwxr-x--- 14 root root     4096 Aug 25 04:18 2019-08-25_04-08-53
drwxr-x--- 15 root root     4096 Aug 25 04:30 2019-08-25_04-30-14
-rw-r--r--  1 root root 23274785 Aug 24 23:08 all.sql
[root@www ~]# 
[root@www ~]# ls -l /backup/2019-08-25_04-30-14/
total 8220
drwxr-x--- 2 root root      19 Aug 25 04:30 2019-08-24_23-54-29
drwxr-x--- 2 root root      19 Aug 25 04:30 2019-08-24_23-55-01
drwxr-x--- 2 root root      19 Aug 25 04:30 2019-08-24_23-58-24
drwxr-x--- 2 root root      19 Aug 25 04:30 2019-08-25_04-17-15
drwxr-x--- 2 root root      19 Aug 25 04:30 2019-08-25_04-18-55
-rw-r----- 1 root root     417 Aug 25 04:30 backup-my.cnf
-rw-r----- 1 root root 8372224 Aug 25 04:30 ibdata1.delta
-rw-r----- 1 root root      44 Aug 25 04:30 ibdata1.meta
drwxr-x--- 2 root root      96 Aug 25 04:30 myemployees
drwxr-x--- 2 root root    4096 Aug 25 04:30 mysql
drwxr-x--- 2 root root      37 Aug 25 04:30 oldboydb
drwxr-x--- 2 root root    4096 Aug 25 04:30 performance_schema
drwxr-x--- 2 root root    4096 Aug 25 04:30 test
drwxr-x--- 2 root root      37 Aug 25 04:30 test01
drwxr-x--- 2 root root      57 Aug 25 04:30 xixi
-rw-r----- 1 root root     123 Aug 25 04:30 xtrabackup_checkpoints
-rw-r----- 1 root root     527 Aug 25 04:30 xtrabackup_info
-rw-r----- 1 root root    4096 Aug 25 04:30 xtrabackup_logfile
drwxr-x--- 2 root root    8192 Aug 25 04:30 zabbix

3)模拟删除一条数据并恢复

[root@www ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
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)]> 
MariaDB [(none)]> 
MariaDB [(none)]> 
MariaDB [(none)]> 
MariaDB [(none)]> use test01;
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
MariaDB [test01]> delete  from student  where id=2;
Query OK, 1 row affected (0.00 sec)

MariaDB [test01]> select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | xiaoming |
+----+----------+
1 row in set (0.00 sec)
#此时student表只剩下一条数据集

4)恢复数据

[root@www ~]# innobackupex --apply-only /backup/2019-08-25_04-08-53/
[root@www ~]# innobackupex --apply-log --redo-only /backup/2019-08-25_04-08-53/ --incremental-dir=/backup/2019-08-25_04-30-14/
#--incremental-dir是指明把哪一个增量备份的数据文件整合到哪个全备文件中去,
#然后我们只需要基于全备的文件即可完成恢复操作


[root@www ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/2019-08-25_04-08-53/
...........................................
190825 04:41:23 [01]        ...done
190825 04:41:23 [01] Copying ./test01/db.opt to /var/lib/mysql/test01/db.opt
190825 04:41:23 [01]        ...done
190825 04:41:23 [01] Copying ./test01/student.frm to /var/lib/mysql/test01/student.frm
190825 04:41:23 [01]        ...done
190825 04:41:23 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
190825 04:41:23 [01]        ...done
190825 04:41:23 completed OK!



[root@www ~]# chown -R mysql:mysql   /var/lib/mysql/* 
[root@www ~]# 
[root@www ~]# systemctl restart   mariadb 
[root@www ~]# 
[root@www ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
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)]> select * from test01.student;
+----+----------+
| id | name     |
+----+----------+
|  1 | xiaoming |
|  2 | xiaohong |
+----+----------+
2 rows in set (0.00 sec)
#数据增量恢复成功!

发布了83 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Micky_Yang/article/details/100063660