Percona Xtrabackup 安装

1501 查看

Percona XtraBackup 简介

Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点:
(1)备份过程快速、可靠;
(2)备份过程不会打断正在执行的事务;
(3)能够基于压缩等功能节约磁盘空间和流量;
(4)自动实现备份检验;
(5)还原速度快;

获取: http://www.percona.com/software/percona-xtrabackup/

RedHat/CentOS 安装:

直接安装 rpm 包:

[root@bogon ~]# yum localinstall percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm --skip-broken
Loaded plugins: fastestmirror
Setting up Local Package Process
Examining percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm: percona-xtrabackup-24-2.4.3-1.el6.x86_64
Marking percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm to be installed
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: perl(DBD::mysql) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
--> Processing Dependency: perl(DBI::Const::GetInfoType) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: perl(DBI) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
 
Packages skipped because of dependency problems:
    percona-xtrabackup-24-2.4.3-1.el6.x86_64 from /percona-xtrabackup-24-2.4.3-1.el6.x86_64
    perl-DBD-MySQL-4.013-3.el6.x86_64 from srr
    perl-DBI-1.609-4.el6.x86_64 from srr
[root@bogon ~]# yum localinstall percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
Loaded plugins: fastestmirror
Setting up Local Package Process
Examining percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm: percona-xtrabackup-24-2.4.3-1.el6.x86_64
Marking percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm to be installed
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: perl(DBD::mysql) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
--> Processing Dependency: perl(DBI::Const::GetInfoType) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: perl(DBI) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed
--> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
--> Finished Dependency Resolution
Error: Package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 (/percona-xtrabackup-24-2.4.3-1.el6.x86_64)
           Requires: libev.so.4()(64bit)
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest

报这个错是因为没有安装epel-release

yum install epel-release

之后则可以正常安装

XtraBackup 备份:

1.完全备份:

Xtabackup 的命令行工具: innobackupex

innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/

进行备份的用户最小权限为: RELOAD, LOCK TABLES, REPLICATION CLIENT , 可单独创建一个最小权限用户

使用innobakupex备份时,其会调用xtrabackup备份所有的InnoDB表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件。这些文件会被保存至一个以时间命令的目录中。

在备份的同时,innobackupex还会在备份目录中创建如下文件:
(1)xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;

每个InnoDB页(通常为16k大小)都会包含一个日志序列号,即LSN。LSN是整个数据库系统的系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的。

(2)xtrabackup_binlog_info —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置。

(3)xtrabackup_binlog_pos_innodb —— 二进制日志文件及用于InnoDB或XtraDB表的二进制日志文件的当前position。

(4)xtrabackup_binary —— 备份中用到的xtrabackup的可执行文件;

(5)backup-my.cnf —— 备份命令用到的配置选项信息;

在使用innobackupex进行备份时,还可以使用--no-timestamp选项来阻止命令自动创建一个以时间命名的目录;如此一来,innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据。

[root@bogon ~]# service mysqld start
Starting MySQL (Percona Server) SUCCESS!
[root@bogon ~]# mysql -h127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11-4 Percona Server (GPL), Release 4, Revision 5c940e1
 
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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> create database week1;
Query OK, 1 row affected (0.00 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| week1              |
+--------------------+
5 rows in set (0.00 sec)
 
mysql> grant all privileges on *.* to 'shiina'@'%' identified by 'shiina';
Query OK, 0 rows affected, 1 warning (0.01 sec)
 
mysql> \q
Bye
[root@bogon ~]# innobackupex --user=shiina --password=shiina /backup/
... #省略很多很多
...
160530 05:39:31 Finished backing up non-InnoDB tables and files
160530 05:39:31 Executing LOCK BINLOG FOR BACKUP...
160530 05:39:31 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '2491433'
xtrabackup: Stopping log copying thread.
.160530 05:39:31 >> log scanned up to (2491442)
 
160530 05:39:31 Executing UNLOCK BINLOG
160530 05:39:31 Executing UNLOCK TABLES
160530 05:39:31 All tables unlocked
160530 05:39:31 [00] Copying ib_buffer_pool to /backup/2016-05-30_05-39-26/ib_buffer_pool
160530 05:39:31 [00]        ...done
160530 05:39:31 Backup created in directory '/backup/2016-05-30_05-39-26'
160530 05:39:31 [00] Writing backup-my.cnf
160530 05:39:31 [00]        ...done
160530 05:39:31 [00] Writing xtrabackup_info
160530 05:39:31 [00]        ...done
xtrabackup: Transaction log of lsn (2491433) to (2491442) was copied.
160530 05:39:31 completed OK!
[root@bogon ~]# ls /backup/2016-05-30_05-39-26/
backup-my.cnf   ibdata1  performance_schema  week1                   xtrabackup_info
ib_buffer_pool  mysql    sys                 xtrabackup_checkpoints  xtrabackup_logfile

2、准备(prepare)一个完全备份

一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。

innobakupex命令的--apply-log选项可用于实现上述功能。如下面的命令:

innobackupex --apply-log /path/to/BACKUP-DIR

如果执行正确,其最后输出的几行信息通常如下:

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
120407 9:01:36 InnoDB: Starting shutdown...
120407 9:01:40 InnoDB: Shutdown completed; log sequence number 92036620
120407 09:01:40 innobackupex: completed OK!

在实现“准备”的过程中,innobackupex通常还可以使用--use-memory选项来指定其可以使用的内存的大小,默认通常为100M。如果有足够的内存可用,可以多划分一些内存给prepare的过程,以提高其完成速度。

[root@bogon ~]# innobackupex --apply-log /backup/2016-05-30_05-39-26/
... #又省略了很多
...
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2491944
160530 05:45:33 completed OK!

3、从一个完全备份中恢复数据

innobackupex命令的--copy-back选项用于执行恢复操作,其通过复制所有数据相关的文件至mysql服务器DATADIR目录中来执行恢复过程。innobackupex通过backup-my.cnf来获取DATADIR目录的相关信息。

innobackupex --copy-back /path/to/BACKUP-DIR

如果执行正确,其输出信息的最后几行通常如下:

innobackupex: Starting to copy InnoDB log files
innobackupex: in '/backup/2012-04-07_08-17-03'
innobackupex: back to original InnoDB log directory '/mydata/data'
innobackupex: Finished copying back files.
 
120407 09:36:10 innobackupex: completed OK!

请确保如上信息的最行一行出现“innobackupex: completed OK!”。

当数据恢复至DATADIR目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要事先修改数据文件的属主和属组。如:

chown -R mysql:mysql /mydata/data/
 
[root@bogon ~]# rm -rf /data/mysql/*
[root@bogon ~]# innobackupex --copy-back /backup/2016-05-30_05-39-26/
... # 你懂
...
160530 05:49:54 completed OK!
[root@bogon ~]# chown -R mysql:mysql /data/mysql/*
[root@bogon ~]# ll /data/mysql/
total 122912
-rw-r----- 1 mysql mysql      290 May 30 05:49 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 30 05:49 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 30 05:49 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 30 05:49 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 30 05:49 ibtmp1
drwxr-x--- 2 mysql mysql     4096 May 30 05:49 mysql
drwxr-x--- 2 mysql mysql     4096 May 30 05:49 performance_schema
drwxr-x--- 2 mysql mysql    12288 May 30 05:49 sys
drwxr-x--- 2 mysql mysql     4096 May 30 05:49 week1
-rw-r----- 1 mysql mysql      421 May 30 05:49 xtrabackup_info
[root@bogon ~]# mysql -h127.0.0.1 -p
...
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| week1              |
+--------------------+
5 rows in set (0.01 sec)

然而此时 mysql 服务却出了问题 --> 服务无法停止, 因为pid文件被删除了, 找不到pid文件不知道mysql服务的进程号, 所以无法结束mysql进程
杀死进程, 重启服务, 一切正常

[root@bogon ~]# service mysqld restart
ERROR! MySQL (Percona Server) PID file could not be found!
Starting MySQL (Percona Server)... ERROR! The server quit without updating PID file (/data/mysql/bogon.pid).
[root@bogon ~]# kill 1382
[root@bogon ~]# service mysqld start
Starting MySQL (Percona Server). SUCCESS!
[root@bogon ~]# mysql -h127.0.0.1 -p
...
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| week1              |
+--------------------+
5 rows in set (0.00 sec)

完成