阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Percona Xtrabackup 安装

192次阅读
没有评论

共计 9385 个字符,预计需要花费 24 分钟才能阅读完成。

Percona XtraBackup 简介

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

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

RedHat/CentOS 安装:

直接安装 rpm 包:

[root@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# service mysqld start
Starting MySQL (Percona Server) SUCCESS!
[root@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# rm -rf /data/mysql/*
[root@linuxidc ~]# innobackupex --copy-back /backup/2016-05-30_05-39-26/
... # 你懂
...
160530 05:49:54 completed OK!
[root@linuxidc ~]# chown -R mysql:mysql /data/mysql/*
[root@linuxidc ~]# 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@linuxidc ~]# 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@linuxidc ~]# 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/linuxidc.pid).
[root@linuxidc ~]# kill 1382
[root@linuxidc ~]# service mysqld start
Starting MySQL (Percona Server). SUCCESS!
[root@linuxidc ~]# mysql -h127.0.0.1 -p
...
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| week1              |
+--------------------+
5 rows in set (0.00 sec)

更多 XtraBackup 相关教程见以下内容

MySQL 管理之使用 XtraBackup 进行热备 http://www.linuxidc.com/Linux/2014-04/99671.htm

使用 Xtrabackup 进行 MySQL 备份  http://www.linuxidc.com/Linux/2016-11/137734.htm

MySQL 开源备份工具 Xtrabackup 备份部署 http://www.linuxidc.com/Linux/2013-06/85627.htm

MySQL Xtrabackup 备份和恢复 http://www.linuxidc.com/Linux/2011-12/50275.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137735.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计9385字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中