热备份

  • ibbackup
  • XtraBackup
  • XtraBackup实现增量备份


ibbackup

ibbackup是InnoDB存储引擎官方提供的热备工具,可以同时备份MyISAM存储引擎和InnoDB存储引擎表
对于InnoDB存储引擎表其备份工作原理如下:

  1. 记录备份开始时,InnoDB存储引擎重做日志文件检查点的LSN
  2. 复制共享表空间文件以及独立表空间文件
  3. 记录复制完表空间文件后,InnoDB存储引擎重做日志文件检查点的LSN
  4. 复制在备份时产生的重做日志

对于事务的数据库,如Microsoft SQL Server数据库和Oracle数据库,热备的原理大致和上述相同
可以发现,在备份期间不会对数据库本身有任何影响,所做的操作只是复制数据库文件,因此任何对数据库的操作都是允许的,不会阻塞任何操作

ibbackup的优点如下:

  • 在线备份,不阻塞任何的SQL语句
  • 备份性能好,备份的实质是复制数据库文件和重做日志文件
  • 支持压缩备份,通过选项,可以支持不同级别的压缩
  • 跨平台支持,ibbackup可以运行在Linux、Windows以及主流的UNIX系统平台上

ibbackup对InnoDB存储引擎表的恢复步骤为:

  1. 恢复表空间文件
  2. 应用重做日志文件

ibbackup提供了一种高性能的热备方式,是InnoDB存储引擎备份的首选方式

XtraBackup

XtraBackup备份工具是由Percona公司开发的开源热备工具
支持MySQL5.0以上的版本。XtraBackup在GPL v2开源下发布,官网地址是:https://launchpad.net/percona-xtrabackup。
xtrabackup命令的使用方法如下:

xtrabackup--backup|--prepare[OPTIONS]

xtrabackup命令的可选参数如下:

(The defaults options should be given as the first argument)
--print-defaults Prints the program's argument list and exit.
--no-defaults Don't read the default options from any file.
--defaults-file=Read the default options from this file.
--defaults-extra-file=Read this file after the global options files have been read.
--target-dir=The destination directory for backups.
--backup Make a backup of a mysql instance.
--stats Calculate the statistic of the datadir(it is recommended you take mysqld offline).
--prepare Prepare a backup so you can start mysql server with your restore.
--export Create files to import to another database after it has been prepared.
--print-param Print the parameters of mysqld that you will need for a forcopyback.
--use-memory=This value is used instead of buffer_pool_size.
--suspend-at-end Creates a file called xtrabackup_suspended and waits until the user deletes that file at the end of the backup.
--throttle=(use with--backup)Limits the IO operations(pairs of reads and writes)per second to the values set here.
--log-stream outputs the contents of the xtrabackup_logfile to stdout.
--incremental-lsn=(use with--backup)Copy only.ibd pages newer than the specified LSN high:low.##ATTENTION##:checkpoint lsn*must*be used.Be Careful!
--incremental-basedir=(use with--backup)Copy only.ibd pages newer than the existing backup at the specified directory.
--incremental-dir=(use with--prepare)Apply.delta files and logfiles located in the specified directory.
--tables=name Regular Expression list of table names to be backed up.
--create-ib-logfile(NOT CURRENTLY IMPLEMENTED)will create ib_logfile*after a--prepare.
###If you want to create ib_logfile*only re-execute this
command using the same options.###
--datadir=name Path to the database root.
--tmpdir=name Path for temporary files.Several paths may be specified as a colon(:)separated string.
If you specify multiple paths they are used round-robin.

在开始备份时,xtrabackup首先记录了重做日志的位置
然后对备份的InnoDB存储引擎表的物理文件,即共享表空间和独立表空间进行copy操作
最后记录备份完成后的重做日志位置

XtraBackup实现增量备份

MySQL数据库本身提供的工具并不支持真正的增量备份
更准确地说,二进制日志的恢复应该是point-in-time的恢复而不是增量备份

XtraBackup工具支持对于InnoDB存储引擎的增量备份,其工作原理如下:

  1. 首选完成一个全备,并记录下此时检查点的LSN
  2. 在进行增量备份时,比较表空间中每个页的LSN是否大于上次备份时的LSN,如果是,则备份该页,同时记录当前检查点的LSN

XtraBackup的备份和恢复的过程大致如下:

(full backup)
#./xtrabackup--backup--target-dir=/backup/base
...
(incremental backup)
#./xtrabackup--backup--target-dir=/backup/delta--incremental-basedir=/backup/base
...
(prepare)
#./xtrabackup--prepare--target-dir=/backup/base
...
(apply incremental backup)
#./xtrabackup--prepare--target-dir=/backup/base--incremental-dir=/backup/delta
...

在上述过程中,首先将全部文件备份到/backup/base目录下,增量备份产生的文件备份到/backup/delta
在恢复过程中,首先指定全备的路径,然后将增量的备份应用于该完全备份