xtrabackup在线备份及恢复-全备及增备
时间:2014-01-23 11:01:15 作者:beebol 标签: 分类: Mysql
Xtrabackup是由percona开发的一个开源软件,它是innodb热备工具ibbackup(收费的商业软件)的一个开源替代品。 Xtrabackup由个部分组成:xtrabackup和innobackupex,其中xtrabackup工具用于备份innodb和 xtraDB引擎的表;而innobackupex工具用于备份myisam和innodb引擎的表,本文将介绍如何用innobackupex工具做全 量和增量备份。
The innobackupex tool is a Perl script that acts as a wrapper for the xtrabackup C program. It is a patched version of the innobackup Perl script that Oracle distributes with the InnoDB Hot Backup tool. It enables more functionality by integrating xtrabackup and other functions such as file copying and streaming, and adds some convenience. It lets you perform point-in-time backups of InnoDB / XtraDB tables together with the schema definitions, MyISAM tables, and other portions of the server.
1、安装:
为了方便,直接到官网下载个rpm包安装后就行
[root@Server3 packages]#yum install perl-DBD-MySQL -y[root@Server3 packages]# rpm -ivh percona-xtrabackup-2.1.6-702.rhel5.i386.rpm [root@Server3 packages]# rpm -ivh percona-xtrabackup-2.1.6-702.rhel5.i386.rpm
2、创建一个专门的备份用户,并授权。
[root@localhost:Tue Jan 21 19:49:04 2014 {test2}]>create user 'bkuser'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.01 sec) [root@localhost:Tue Jan 21 20:13:17 2014 {test2}]>grant reload,lock tables,replication client on *.* to 'bkuser'@'localhost'; Query OK, 0 rows affected (0.00 sec) [root@localhost:Tue Jan 21 20:14:19 2014 {test2}]>flush privileges;Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec)
注意备份用户需要的权限:reload,lock tables,replication client
3、做一次全备
innobackupex --defaults-file=/home/mysql/my.cnf --user=bkuser --password=123456 /home/root/xtrabackup_backupfiles/test/
innobackupex到--defaults-file指定的配置文件/home/mysql/my.cnf读取配置项,如datadir数据目录拷贝数据到/home/root/xtrabackup_backupfiles/test/中,注意:如果不用--defaults-file参数,一定要指定--datadir参数,不然会找不到mysql数据目录,无法进行备份。指定--defaults-file参数后,my.cnf需要有datadir的参数配置,不然也不行。最终备份的目录为:/home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18,就是会在指定的目录添加时间,如果不需要这个时间目录,可以加上--no-timestamp参数
4、恢复全备
先关闭数据库,将数据库目录全部删除,包括日志、数据文件
innobackupex --apply-log --defaults-file=/home/mysql/my.cnf /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/innobackupex --copy-back --defaults-file=/home/mysql/my.cnf /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ innobackupex --copy-back --defaults-file=/home/mysql/my.cnf /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/
恢复时需要执行两步操作,第一步apply-log,为了加快速度,一般建议设置--use-memory,这个步骤完成之后,/home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/这个备份已经准备就绪。
为什么要apply-log这一步原因官方是这么说的:
After creating a backup, the data is not ready to be restored. There might be uncommitted transactions to be undone or transactions in the logs to be replayed. Doing those pending operations will make the data files consistent and it is the purpose of the prepare stage . Once this has been done, the data is ready to be used.
第二步,执行copy-back,恢复备份数据到mysql数据目录。
一定不要忘记检查数据目录的权限。
5、进行增量备份和恢复
增量备份是基于全备的基础上进行备份的,全备数据是/home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/
innobackupex --incremental /home/root/xtrabackup_backupfiles/increment/ --defaults-file=/home/mysql/my.cnf --user=bkuser --password=123456 --incremental-basedir=/home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/
--incremental 表示是增量备份,/home/root/xtrabackup_backupfiles/increment/是增量备份的路径;--incremental-basedir=/home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/这是全备路径。
最终本次增量备份的路径是/home/root/xtrabackup_backupfiles/increment/2014-01-22_02-21-11
然后在2014-01-22_02-21-11这次增量备份的基础上再次进行增量备份:
innobackupex --incremental /home/root/xtrabackup_backupfiles/increment/ --defaults-file=/home/mysql/my.cnf --user=bkuser --password=123456 --incremental-basedir=/home/root/xtrabackup_backupfiles/increment/2014-01-22_02-21-11/
如下是两次增量备份的checkpoint:
第一次增备 [root@Server3 2014-01-22_02-21-11]# cat xtrabackup_checkpoints backup_type = incremental from_lsn = 1685370 to_lsn = 1807193 last_lsn = 1807193 compact = 0 在第一次增备的基础上的增备 [root@Server3 2014-01-22_02-21-11]# cat ../2014-01-22_22-00-09/xtrabackup_checkpoints backup_type = incremental from_lsn = 1807193 to_lsn = 1809978 last_lsn = 1809978compact = 0 compact = 0
从上面可以看出,第一次增量备份的to_lsn就是第二次的from_lsn,--incremental-basedir指定第一次增量备份的路径即可
恢复:
在所在备份中进行重做日志操作
innobackupex --apply-log --redo-only /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ innobackupex --apply-log --redo-only /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ --incremental-dir=/home/root/xtrabackup_backupfiles/increment/2014-01-22_02-21-11/ innobackupex --apply-log /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ --defaults-file=/home/mysql/my.cnfinnobackupex --copy-back /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ --defaults-file=/home/mysql/my.cnf innobackupex --copy-back /home/root/xtrabackup_backupfiles/test/2014-01-21_20-16-18/ --defaults-file=/home/mysql/my.cnf
首先在全备上使用--redo-only进行重做已经提交的日志。然后再把第一次增量备份重做到全备当中;第三步回滚没有提交的日志,这时已经有一个准备就绪的备份了,那就这全备;第四步将全备copy到数据目录即可恢复;第五步注意目录权限。
这里需要注意的是:最后一次增量备份不需要加--redo-only选项,即使你用了对数据是没有影响的。
如下是官方提供的说明:
Note:--redo-only should be used when merging all incrementals except the last one. That’s why the previous line doesn’t contain the --redo-only option. Even if the --redo-only was used on the last step, backup would still be consistent but in that case server would perform the rollback phase.