Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上唯一一款开源的可以对innodb和xtradb数据库进行热备的工具。特色:
(1)备份过程快速、可靠;
(2)备份过程不会打断正在执行的事务;
(3)可以基于压缩等功能节约磁盘空间和流量;
(4)自动实现备份检验;
(5)还原速度快;mysql
获取: http://www.percona.com/software/percona-xtrabackup/sql
直接安装 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
以后则能够正常安装app
Xtabackup 的命令行工具: innobackupexide
innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/
进行备份的用户最小权限为: RELOAD, LOCK TABLES, REPLICATION CLIENT , 可单首创建一个最小权限用户工具
使用innobakupex备份时,其会调用xtrabackup备份全部的InnoDB表,复制全部关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件。这些文件会被保存至一个以时间命令的目录中。ui
在备份的同时,innobackupex还会在备份目录中建立以下文件:
(1)xtrabackup_checkpoints —— 备份类型(如彻底或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;命令行
每一个InnoDB页(一般为16k大小)都会包含一个日志序列号,即LSN。LSN是整个数据库系统的系统版本号,每一个页面相关的LSN可以代表此页面最近是如何发生改变的。rest
(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
通常状况下,在备份完成后,数据尚且不能用于恢复操做,由于备份的数据中可能会包含还没有提交的事务或已经提交但还没有同步至数据文件中的事务。所以,此时数据文件仍处理不一致状态。“准备”的主要做用正是经过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
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!
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)
完成