MySQL的热备份开源工具XtraBackup [2.0.1版](一)


简介
Xtrabackup是percona公司的开源项目,用以实现类似innodb官方的热备份工具InnoDB Hot Backup的功能,能够非常快速地备份与恢复mysql数据库。
Xtrabackup中包含两个工具:
  xtrabackup是用于热备份innodb, xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;
  innobackupex是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。
由于innobackupex的功能更为全面和完善,所以,本文以innobackupex作为基础进行研究描述。

 Xtrabackup有两个主要的工具:xtrabackup、innobackupex 

  1、xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表 
  2、innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackupex是一个perl脚本封装,封装了xtrabackup。
       主要是为了方便的 同时备份InnoDB和MyISAM引擎的表,但在处理myisam时需要加全局读锁。 
       并且加入了一些使用的选项。如slave-info可以记录备份恢 复后,
       作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。 
  3、官方文档:http://www.percona.com/docs/wiki/percona-xtrabackup:start 

测试环境:redhat 5.5 x_86_64,内存8G ,Intel(R) Xeon(R) CPU X3430  @ 2.40GHz
          mysql 5.1.62-compiled-by-lidan-log 
          
1. 下载XtraBackup并安装

当前测试的版本是2.0.1 ,从这里可以选择不同OS的相应版本:
http://www.percona.com/downloads/XtraBackup/XtraBackup-2.0.1/

有各种类型可选这里选择rpm redhat5安装包:
- binary
- deb
- RPM
- source

点击RPM进入下载页面,这里下载x86_64位:
- i386
- x86_64

http://www.percona.com/downloads/XtraBackup/XtraBackup-2.0.1/RPM/rhel5/x86_64/

有正式版本和调试版本,这里选择正式版 1

1), percona-xtrabackup-2.0.1-446.rhel5.x86_64.rpm 2012-06-22 13.9 MB
2), percona-xtrabackup-debuginfo-2.0.1-446.rhel5.x86_64.rpm 2012-06-22 14.6 MB

2,执行安装

rpm -ivh percona-xtrabackup-2.0.1-446.rhel5.x86_64.rpm 
warning: percona-xtrabackup-2.0.1-446.rhel5.x86_64.rpm: Header V4 DSA signature: NOKEY, key ID cd2efd2a
error: Failed dependencies:
        mysql is needed by percona-xtrabackup-2.0.1-446.rhel5.x86_64
        
   报错,提示缺乏依赖包,ya ya 的悲剧!
   
   没办法只能安装依赖包了(前提是配置了yum源[编辑/etc/yum.repos.d/rhel-debuginfo.repo]--送佛送到家):
   
 yum install mysql
Loaded plugins: rhnplugin, security
Repository rhel-debuginfo is listed more than once in the configuration
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.i386 0:5.0.77-4.el5_4.2 set to be updated
---> Package mysql.x86_64 0:5.0.77-4.el5_4.2 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
 Package                   Arch                       Version                              Repository                          Size
====================================================================================================================================
Installing:
 mysql                     i386                       5.0.77-4.el5_4.2                     rhel-debuginfo                     4.8 M
 mysql                     x86_64                     5.0.77-4.el5_4.2                     rhel-debuginfo                     4.8 M
Transaction Summary
====================================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)
Total download size: 9.5 M
Is this ok [y/N]: y
Downloading Packages:
------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                               7.9 GB/s | 9.5 MB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : mysql                                                                                                        1/2 
warning: /etc/my.cnf created as /etc/my.cnf.rpmnew
  Installing     : mysql                                                                                                        2/2 
warning: /etc/my.cnf created as /etc/my.cnf.rpmnew
Installed:
  mysql.i386 0:5.0.77-4.el5_4.2                                   mysql.x86_64 0:5.0.77-4.el5_4.2                                  

Complete!

安装完依赖包,再来一次:

[root@node1 Server]# rpm -ivh percona-xtrabackup-2.0.1-446.rhel5.x86_64.rpm 
warning: percona-xtrabackup-2.0.1-446.rhel5.x86_64.rpm: Header V4 DSA signature: NOKEY, key ID cd2efd2a
Preparing...                ########################################### [100%]
   1:percona-xtrabackup     ########################################### [100%]
   
终于安装成功,万里长征第一步。

3,建立数据库备份账号
  
  GRANT SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backuptest'@'localhost' IDENTIFIED BY 'backuptest';
  flush privileges;
  
  该账户用来完成一些加载和锁表的工作
  想了解详情请查看官方文档:http://www.percona.com/doc/percona-xtrabackup/innobackupex/privileges.html
  
4,全备份测试(某库或全库)

前提:要注意的是my.cnf里datadir这个参数是必须要指定的,xtrabackup_xx 是根据它去定位innodb数据文件的位置。

--include=REGEXP 
对xtrabackup参数--tables的封装,也支持ibbackup。备份包含的库表,例如:--include="test.*",意思是要备份test库中所有的表。如果需要全备份,
则省略这个参数;如果需要备份test库下的2个表:test1和test2,则写成:--include="test.test1|test.test2"。也可以使用通配符,
如:--include="test.test*"。 
关于 innobackupex 参数的详解 请参见官方文档:http://www.percona.com/doc/percona-xtrabackup/innobackupex/innobackupex_option_reference.html

[root@node1 mysql_bak]# innobackupex --defaults-file=/etc/my.cnf --user=backuptest --password=backuptest  --database=rest /mysql_bak

=========--defaults-file 必须是参数的第一项,官方文档这样说,但实际上测试的时候并不需要这样做都可以

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Inc 2009-2012.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

120730 14:54:38  innobackupex: Starting mysql with options:  --defaults-file='/etc/my.cnf' --password=xxxxxxxx --user='backuptest' --unbuffered --
120730 14:54:38  innobackupex: Connected to database with mysql child process (pid=17501)  
120730 14:54:44  innobackupex: Connection to database server closed
IMPORTANT: Please check that the backup run completes successfully.
           At the end of a successful backup run innobackupex
           prints "completed OK!".                                           -------以上是提示信息告诉你如果备份成功将会提示"completed OK!"否则备份失败!

innobackupex: Using mysql  Ver 14.14 Distrib 5.1.62, for unknown-linux-gnu (x86_64) using readline 5.1
innobackupex: Using mysql server version Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.


innobackupex: Created backup directory /mysql_bak/2012-07-30_14-54-44        ------自动以当前时间点命名,创建备份目录
120730 14:54:44  innobackupex: Starting mysql with options:  --defaults-file='/etc/my.cnf' --password=xxxxxxxx --user='backuptest' --unbuffered --
120730 14:54:44  innobackupex: Connected to database with mysql child process (pid=17525)  
120730 14:54:46  innobackupex: Connection to database server closed

120730 14:54:46  innobackupex: Starting ibbackup with command: xtrabackup_51  -------执行innodb表备份时,这里实际上调用的是xtrabackup_51命令
--defaults-file="/etc/my.cnf"  --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/mysql_bak/2012-07-30_14-54-44
innobackupex: Waiting for ibbackup (pid=17531) to suspend            ----专门启一个子进程扫描redo日志里的更新
innobackupex: Suspend file '/mysql_bak/2012-07-30_14-54-44/xtrabackup_suspended'

xtrabackup_51 version 2.0.1 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 446)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql                                     ------进入mysql数据目录
xtrabackup: Target instance is assumed as followings.
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 2
xtrabackup:   innodb_log_file_size = 268435456
>> log scanned up to (0 493729292)        ---记录起始点的lsn(log sequence number)
[01] Copying ./ibdata1 to /mysql_bak/2012-07-30_14-54-44/ibdata1  ------在备份innodb表的时候,备份工具实质上在后台还会不停的扫描日志缓存,
                                                                  ------将新的更改拷贝备份出来
[01]        ...done                                               ------这里在备份的时候数据库没有新事物,故备份前后lsn是一样的
>> log scanned up to (0 493729292)        

120730 14:54:52  innobackupex: Continuing after ibbackup has suspended
120730 14:54:52  innobackupex: Starting mysql with options:  --defaults-file='/etc/my.cnf' --password=xxxxxxxx --user='backuptest' --unbuffered --
120730 14:54:52  innobackupex: Connected to database with mysql child process (pid=17539)  开始备份myisam表(确切的说是非事务性表,本测试用例只有misam表)开启一个子进程并
120730 14:54:54  innobackupex: Starting to lock all tables...   -------为了保证表数据的一致性,这里必须锁定全库(特别注意在表设计的时候,如果用本工具备份,在业务大的时候可能会导致数据库压力暴增,
                                                                -------因此尽量将频繁更新的表设计出innodb类型,因为innodb类型的表在备份的时候不需要锁定全库)
>> log scanned up to (0 493729292)                              
>> log scanned up to (0 493729292)                              
120730 14:55:04  innobackupex: All tables locked and flushed to disk           ------将所有“脏”数据刷新到磁盘

120730 14:55:04  innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,      -----myisam表只是简单的拷贝文件即可
innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in
innobackupex: subdirectories of '/var/lib/mysql'
innobackupex: Backing up files '/var/lib/mysql/rest/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (39 files)
120730 14:55:04  innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files

innobackupex: Resuming ibbackup

xtrabackup: The latest check point (for incremental): '0:493729292'             -----记录最新检查点信息
xtrabackup: Stopping log copying thread.
.>> log scanned up to (0 493729292)    ---记录截止点的lsn ,因为这里在备份的时候没有数据的变化故起始和截止的lsn一样

xtrabackup: Transaction log of lsn (0 493729292) to (0 493729292) was copied.
120730 14:55:06  innobackupex: All tables unlocked                              ------解除全库锁定
120730 14:55:06  innobackupex: Connection to database server closed

innobackupex: Backup created in directory '/mysql_bak/2012-07-30_14-54-44'
innobackupex: MySQL binlog position: filename 'mysql-bin.000002', position 106  ------记录最新的binlog日志名和位置,该位置应该被记录好,位置后崩溃恢复提供恢复的”位置起始点“
120730 14:55:06  innobackupex: completed OK!                                    -------备份成功标志

注意:如果备份完后又有很多binglog生成者到时候恢复的时候'mysql-bin.000002', position 106(恢复起始点)就有用了

5,打包/压缩备份

提示:
在使用参数stream=tar备份的时候,你的xtrabackup_logfile可能会临时放在/tmp目录下,如果你备份的时候并发写入较大的话xtrabackup_logfile可能会很大(5G+),
很可能会撑满你的/tmp目录,可以通过参数--tmpdir指定目录来解决这个问题。 --tmpdir=DIRECTORY 

 1),打包备份

[root@node1 mysql_bak]# innobackupex  --defaults-file=/etc/my.cnf --user=backuptest --password=backuptest --database=rest --stream=tar /mysql_bak 1> /mysql_bak/20120730_mysql_rest_bak.tar 2>xxx.log


InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Inc 2009-2012.  All Rights Reserved.

... ... 偷偷摸摸的省略好多字
120730 16:12:47  innobackupex: Continuing after ibbackup has suspended

120730 16:13:01  innobackupex: All tables unlocked
120730 16:13:01  innobackupex: Connection to database server closed

innobackupex: Backup created in directory '/mysql_bak'
innobackupex: MySQL binlog position: filename 'mysql-bin.000002', position 106
innobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.  -----其他的和普通全库备份一样只有这里提示在恢复的时候需要加 -i选项 忽略文件大小为0的备份
120730 16:13:01  innobackupex: completed OK!

 2),压缩备份

innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --database=rest --stream=tar /mysql_bak  2>xxx.log |gzip > /mysql_bak/20120730_mysql.tar.gz


  5.1 压缩备份的还原
  
  tar -ixvf backup.tar  --这里 i选项很重要,备份的时候innobackupex也会提示在恢复的时候加-i 如: [innobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.]

  解压完后即可俺正常的恢复进行还原操作,具体见后面(7)“全量恢复操作”
  
  5.2 备份到远程主机
  
   [root@node1 mysql_bak]# innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --database=rest --stream=tar /mysql_bak  2>xxx.log |gzip  | ssh 172.17.54.225 cat ">"/root/test_backup_mysql_xtra.tar.gz
   root@172.17.54.225's password: 
   这里需要手动输入密码,如果想不需要输入密码就可登陆可以配置ssh密钥对如下:
   
   [
   
   第一步:生成密匙对,我用的是rsa的密钥。使用命令 "ssh-keygen -t rsa"
   [user1@rh user1]$ ssh-keygen -t rsa
   Generating public/private rsa key pair.
   Enter file in which to save the key (/home/user1/.ssh/id_rsa):
   Created directory '/home/user1/.ssh'.
   Enter passphrase (empty for no passphrase):
   Enter same passphrase again:
   Your identification has been saved in /home/user1/.ssh/id_rsa.
   Your public key has been saved in /home/user1/.ssh/id_rsa.pub.
   The key fingerprint is:
   e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 user1@rh.test.com
   [user1@rh user1]$
   成的过程中提示输入密钥对保存位置,直接回车,接受默认值就行了。接着会提示输入一
  个不同于你的password的密码,直接回车,让它空着。当然,也可以输入一个。(我比较懒,不想每次都要输入密码。) 这样,密钥对就生成完了。
  其中公共密钥保存在 ~/.ssh/id_rsa.pub
  私有密钥保存在 ~/.ssh/id_rsa
  然后改一下 .ssh 目录的权限,使用命令 "chmod 755 ~/.ssh"
   [user1@rh user1]$ chmod 755 ~/.ssh
   [user1@rh user1]$
  之后把这个密钥对中的公共密钥复制到你要访问的机器上去,并保存为 
  ~/.ssh/authorized_keys.
   [user1@rh user1]$ scp ~/.ssh/id_rsa.pub rh1:/home/user1/.ssh/authorized_keys
   user1@rh1's password:
   id_rsa.pub                                    100%  228     3.2MB/s   00:00
   [user1@rh user1]$
   这样就大功告成了。之后你再用ssh scp sftp 之类的访问那台机器时,就不用输入密码
   ]
  
6,关于备份后生成的文件的解释

-rw-r--r-- 1 root root       190 Jul 31 17:56 backup-my.cnf --该文件不是原始my.cnf的备份
-rw-r----- 1 root root 127926272 Jul 31 17:56 ibdata1--innodb数据文件
drwxr-xr-x 2 root root      4096 Jul 31 17:57 mysql  --数据库文件夹
drwxr-xr-x 2 root root      4096 Jul 31 17:57 test   --数据库文件夹
-rw-r--r-- 1 root root        13 Jul 31 17:57 xtrabackup_binary      --见下文的解释
-rw-r--r-- 1 root root        23 Jul 31 17:56 xtrabackup_binlog_info
-rw-r----- 1 root root        87 Jul 31 17:57 xtrabackup_checkpoints
-rw-r----- 1 root root      2560 Jul 31 17:57 xtrabackup_logfile

对上面的文件的相关解释:
Information related to the backup and the server

xtrabackup_checkpoints
The type of the backup (e.g. full or incremental), its state (e.g. prepared) and the LSN range contained in it. 
This information is used for incremental backups.
 该文件包含了备份的类型(全量或增量)状态信息,主要包括备份状态和lsn的范围信息,lsn信息通常用于增量备份

xtrabackup_binlog_info
The binary log file used by the server and its position at the moment of the backup. Result of the SHOW MASTER STATUS.
包含的信息为当前binlog日志名和备份点的位置可是登陆mysql使用SHOW MASTER STATUS查看

xtrabackup_binlog_pos_innodb
The binary log file and its current position for InnoDB or XtraDB tables.
该文件包含binlog日志文件信息和innodb或xtradb表(如果有)当前position

xtrabackup_binary
The xtrabackup binary used in the process.
二进制文件

backup-my.cnf
This file contains information to start the mini instance of InnoDB during the --apply-log. This is NOT a backup of original my.cnf.
该文件包含了使用--apply-log恢复数据文件后启动innodb实例的最小配置信息,切忌该文件不是原始的my.cnf配置文件,所以使用该工具备份数据库
的时候还得记得备份一下原始的my.cnf文件

xtrabackup_logfile
Contains data needed for running the: --apply-log. The bigger this file is the --apply-log process will take longer to finish.
该文件包含使用--apply-log应用日志使用的那些数据,该文件越大执行日志应用时间越长

xtrabackup_slave_info
The CHANGE MASTER statement needed for setting up a slave.
该参数会包含为slave库设置复制点的相关信息,单机环境下使用该参数后change master 之后的内容也为空

The output of mysqld during the backup process:

mysql-stderr
mysql-stdout
指定备份的时候是否输出错误信息

innobackupex --use-memory=1G  --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf  --slave-info /mysql_bak/
干脆这里再多介绍几个参数:
//--no-timestamp 表示备份的时候不生产带有时间戳的文件夹(该文件夹用来存储备份文件)
innobackupex --use-memory=1G --no-timestamp  --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf   /mysql_no_timestamp/ 2> backupout.log
//其中 2> backupout.log 表示记录错误日志 ,--use-memory=1G 该参数在备份和prepare的时候用,控制备份和prepare时innodb实例使用的内存量 默认是100M 视情况进行调整推荐1-2G

具体参数选项的解释请查看官方文档:http://www.percona.com/doc/percona-xtrabackup/innobackupex/innobackupex_option_reference.html

7,全量恢复(神一般的复活开始了)

查看备份信息

drwxr-xr-x 3 root root 4096 Jul 30 20:07 2012-07-30_20-07-13
drwxr-xr-x 3 root root 4096 Jul 30 20:09 2012-07-30_20-08-53
drwxr-xr-x 6 root root 4096 Jul 31 17:15 2012-07-31_17-15-34

关闭mysql (如果已经启动),删除相关的数据文件

service mysqld stop

rm -rf /var/lib/mysql/*  

innobackupex  --defaults-file=/etc/my.cnf --apply-log --use-memory=1G /mysql_bak/2012-07-31_17-15-34  --这里的--apply-log指明是将日志应用到数据文件上
                                                                                 
--日志恢复前备份文件内容:
[root@node1 mysql_bak]# cd 2012-08-03_15-03-32/
[root@node1 2012-08-03_15-03-32]# ll
-rw-r--r-- 1 root root       190 Aug  3 15:03 backup-my.cnf
-rw-r----- 1 root root 127926272 Aug  3 15:03 ibdata1
drwxr-xr-x 2 root root      4096 Aug  3 15:03 mysql
drwxr-xr-x 2 root root      4096 Aug  3 15:03 opensips
drwxr-xr-x 2 root root      4096 Aug  3 15:03 rest
drwx------ 2 root root      4096 Aug  3 15:03 test
-rw-r--r-- 1 root root        13 Aug  3 15:03 xtrabackup_binary
-rw-r--r-- 1 root root        23 Aug  3 15:03 xtrabackup_binlog_info
-rw-r----- 1 root root        87 Aug  3 15:03 xtrabackup_checkpoints
-rw-r----- 1 root root      2560 Aug  3 15:03 xtrabackup_logfile

--日志恢复后备份文件内容:

 -rw-r--r-- 1 root root       190 Aug  3 15:03 backup-my.cnf
-rw-r----- 1 root root 127926272 Aug  3 15:17 ibdata1
-rw-r--r-- 1 root root 268435456 Aug  3 15:17 ib_logfile0  --恢复的日志文件
-rw-r--r-- 1 root root 268435456 Aug  3 15:16 ib_logfile1  --恢复的日志文件
drwxr-xr-x 2 root root      4096 Aug  3 15:03 mysql
drwxr-xr-x 2 root root      4096 Aug  3 15:03 opensips
drwxr-xr-x 2 root root      4096 Aug  3 15:03 rest
drwx------ 2 root root      4096 Aug  3 15:03 test
-rw-r--r-- 1 root root        13 Aug  3 15:03 xtrabackup_binary
-rw-r--r-- 1 root root        23 Aug  3 15:03 xtrabackup_binlog_info
-rw-r--r-- 1 root root        24 Aug  3 15:16 xtrabackup_binlog_pos_innodb --多了一个记录binglog日志名和position的文件内容下文(1)
-rw-r----- 1 root root        87 Aug  3 15:17 xtrabackup_checkpoints
-rw-r----- 1 root root   2097152 Aug  3 15:16 xtrabackup_logfile

(1):
[root@node1 2012-08-03_15-03-32]# cat xtrabackup_binlog_pos_innodb
./mysql-bin.000003      2745

选择最新备份信息执行恢复

 innobackupex   --defaults-file=/etc/my.cnf  --copy-back /mysql_bak/2012-07-31_17-15-34  --执行恢复的时候记得带--copy-back 
 选项将备份文件中的数据恢复到数据库中

对数据文件和目录授权(这步很重要,否则启动mysql将报错)
先将binglog日志拷贝到零时目录(数据库恢复后,还需要根据备份截止位置应用相应的binglog日志,可以期待本篇外传...)
cp /var/lib/myql/mysql-bin* /tmp/
然后删除数据目录的所有文件
chown -R mysql:mysql /var/lib/mysql/*

(&&)以下是没有授权就启动mysql后的报错。

[root@node1 mysql]# service mysqld start
Starting MySQL.Manager of pid-file quit without updating file.[FAILED] --看看是不是启动失败
[root@node1 mysql]# cat /var/log/mysqld.log   
120802 11:42:57 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
/usr/local/mysql/libexec/mysqld: File './mysql-bin.index' not found (Errcode: 13)  --这里其实是告你,哥们该文件不存在或无权限访问
120802 11:42:57 [ERROR] Aborting  
... ... 继续省略
/usr/local/mysql/libexec/mysqld: File './mysql-bin.index' not found (Errcode: 13)
120802 11:44:51 [ERROR] Aborting --再次aborting 彻底死翘翘了!

以下是成功启动mysql后的日志信息,你要是闲的没事可以和上面的错误信息对比一下

cat /var/log/mysql.log 截取部分信息如下:

120731 19:43:37 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120731 19:43:37  InnoDB: Initializing buffer pool, size = 1.0G
... .... 善于省略的孩纸,是个好孩纸
120731 19:43:46 [Note] Event Scheduler: Loaded 0 events
120731 19:43:46 [Note] /usr/local/mysql/libexec/mysqld: ready for connections.
Version: '5.1.62-compiled-by-lidan-log'  

最后登录mysql查询表相关信息检测是否成功!表激动,表激动,这时还需要根据备份时候记录的binglog日志的起始位置,使用mysqlbinglog工具解析之
后的binglog日志并应用到mysql数据库(注:关于mysqlbinglog 工具的使用见本篇外传)

8,增量备份

注意:增量备份特指innodb引擎的表,对于myisam表还是全备份 ,有一种说法:说需要设置默认存储引擎 InnoDB(必须改,否则进行增量备份的时候不成功)
      但实际操作过程中发现不设置好像也没啥关系。 

1),先做一次全量备份
innobackupex   --defaults-file=/etc/my.cnf  --username=backuptest password=backuptest  /mysql_bak/

输出日志信息我就不给出了,你看的累,我粘贴也麻烦。

2),再做一次增量备份

[root@node1 mysql_bak]# innobackupex --user=backuptest --password=backuptest --incremental 
--incremental-basedir=/mysql_bak/2012-08-06_15-42-44/  --指明全备份的目录
 /mysql_bak/incr  --指定增量备份目录incr必须事先存在

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy   -----这些好像是增量备份的日志信息,我靠,刚说不贴日志的
and Percona Inc 2009-2012.  All Rights Reserved.

... ...  此处省略n个信息

innobackupex: Using mysql  Ver 14.14 Distrib 5.1.62, for unknown-linux-gnu (x86_64) using readline 5.1
innobackupex: Using mysql server version Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

... ...  此处省略n个信息

120806 19:31:04  innobackupex: Starting ibbackup with command: xtrabackup_51  --defaults-group="mysqld" --backup --suspend-at-end 
--target-dir=/mysql_bak/incr/2012-08-06_19-31-02 --incremental-basedir='/mysql_bak/2012-08-06_15-42-44/'
innobackupex: Waiting for ibbackup (pid=24728) to suspend
innobackupex: Suspend file '/mysql_bak/incr/2012-08-06_19-31-02/xtrabackup_suspended'

xtrabackup_51 version 2.0.1 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 446)
incremental backup from 0:495693836 is enabled.
xtrabackup: uses posix_fadvise().

... ...  此处省略n个信息

xtrabackup:   innodb_log_file_size = 268435456
>> log scanned up to (0 495693846)
[01] Copying ./ibdata1 to /mysql_bak/incr/2012-08-06_19-31-02/ibdata1.delta
[01]        ...done
[01] Copying ./test/lidant.ibd to /mysql_bak/incr/2012-08-06_19-31-02/./test/lidant.ibd.delta
[01]        ...done
[01] Copying ./test/test.ibd to /mysql_bak/incr/2012-08-06_19-31-02/./test/test.ibd.delta
[01]        ...done

innobackupex: subdirectories of '/var/lib/mysql'
... ...  此处省略n个信息
xtrabackup: The latest check point (for incremental): '0:495693846'
xtrabackup: Stopping log copying thread.
.>> log scanned up to (0 495693846)
xtrabackup: Transaction log of lsn (0 495693846) to (0 495693846) was copied.
120806 19:31:23  innobackupex: All tables unlocked     --备份misam表时要进行全库锁,完了要释放全库锁
innobackupex: MySQL binlog position: filename 'mysql-bin.000006', position 217
120806 19:31:23  innobackupex: completed OK!             --看到 OK 没, 不解释,你懂的

这里做了两次增量备份查看结果如下:
[root@node1 incr]# pwd
/mysql_bak/incr
[root@node1 incr]# ll
total 8
drwxr-xr-x 6 root root 4096 Aug  6 19:31 2012-08-06_19-31-02  --增量备份一
drwxr-xr-x 6 root root 4096 Aug  6 19:36 2012-08-06_19-36-05  --增量备份 2 
 
 9,增量恢复 (增量恢复,啧啧,听上去就像是很高深的东东,各位,大湿,要仔细看)
 
 注意!
 First, only the committed transactions must be replayed on each backup. This will put the base full backup and the 
 incremental ones altogether.Then, the uncommitted transaction must be rolled back in order to have a ready-to-use backup.
 If you replay the commited transactions and rollback the uncommitted ones on the base backup, you will not be able to add the 
 incremental ones. If you do this on an incremental one, you won’t be able to add data from that moment and the remaining increments.
 Having this in mind, the procedure is very straight-forward using the --redo-only option, starting with the base backup:
 上面这段话的意思就是什么呢!就是说要想进行增量恢复,必须先将全备和增量备份合并,并且恢复的必须都是已经提交了的事物,未被提交的事物必须进行回滚,
 如果不这样做的话,那么增量备份备份集是无法和全量备份进行合并,因此在做恢复的时候需要使用--redo-only参数项来干这件事。
 关于--redo-only的官方解释(注意是官方,这就说明是权威,专治各种不服)如下:
 --redo-only
This option is passed directly to xtrabackup’s xtrabackup --apply-log-only option. This forces xtrabackup to skip the “rollback” 
phase and do a “redo” only. This is necessary if the backup will have incremental changes applied to it later.
该参数值直接传递给xtrabackup命令的--apply-log-only选项,它强制xtrabackup 跳过需要回滚的事物而只执行”redo“里提交过的事物

 将redo和log应用到数据文件,这里针对的是全量备份集
innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --apply-log --redo-only   /mysql_bak/2012-08-06_15-42-44/ 

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Inc 2009-2012.  All Rights Reserved.

... ...  此处省略n个信息

120806 20:01:21  innobackupex: Starting ibbackup with command: xtrabackup_51  --defaults-file="/etc/my.cnf"  --defaults-group="mysqld" 
--prepare --target-dir=/mysql_bak/2012-08-06_15-42-44 --apply-log-only

xtrabackup_51 version 2.0.1 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 446)
xtrabackup: cd to /mysql_bak/2012-08-06_15-42-44
xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(0 495693836) --开始从redo日志里恢复对应--redo-only 

... ... 继续省略,偷点懒不好意思 
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Last MySQL binlog file position 0 2745, file name ./mysql-bin.000003 --开始从binlog里恢复数据对应--apply-log

[notice (again)]
  If you use binary log and don't use any hack of group commit,
  the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 2745, file name ./mysql-bin.000003

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
120806 20:01:21  InnoDB: Starting shutdown...
120806 20:01:21  InnoDB: Shutdown completed; log sequence number 0 495693836
120806 20:01:21  innobackupex: completed OK!

恢复第一个增量备份

innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --apply-log [--redo-only 如果这里加了这个会导致后面的的
                                                             备份无法找到正确的lsn,因为该参数会跳过未提交的事物,俺就在这纠结过好久,NND] 
  /mysql_bak/2012-08-06_15-42-44/ --incremental-dir=/mysql_bak/incr/2012-08-06_19-31-02/
  
第二个增量备份恢复

innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --apply-log 
  /mysql_bak/2012-08-06_15-42-44/ --incremental-dir=/mysql_bak/incr/2012-08-06_19-36-08/
 
对(base(full) + incrementals)再进行一次为提交事物的回滚操作(增量里面可能有未提交的操作,官方文档这么建议的,,俺是良民所以照做了)
 innobackupex --user=backuptest --password=backuptest --defaults-file=/etc/my.cnf --apply-log --redo-only   /mysql_bak/2012-08-06_15-42-44/ 
 
 执行数据库修复
 
innobackupex --defaults-file=/etc/my.cnf --copy-back /mysql_bak/2012-08-06_15-42-44/ --这语句是不是似曾相识,对,这步和普通的全量恢复本来就没啥区别

innobackupex: back to original InnoDB data directory '/var/lib/mysql'
innobackupex: Copying file '/mysql_bak/2012-08-06_20-55-57/ibdata1'

innobackupex: Starting to copy InnoDB log files
innobackupex: in '/mysql_bak/2012-08-06_20-55-57'
innobackupex: back to original InnoDB log directory '/var/lib/mysql'
innobackupex: Finished copying back files.

120806 21:11:09  innobackupex: completed OK!  --不管什么时候,只要看到OK,说明你可以开啤酒庆祝了

进入数据目录并授权
[root@node1 mysql_bak]# cd /var/lib/mysql/
[root@node1 mysql]# ll         --属组和owner都是root,这个时候可别冲动,数据库是起不来的
total 125076
-rw-r----- 1 root root 127926272 Aug  6 21:09 ibdata1
drwxr-xr-x 2 root root      4096 Aug  6 21:11 mysql
drwxr-xr-x 2 root root      4096 Aug  6 21:11 opensips
drwxr-xr-x 2 root root      4096 Aug  6 21:11 rest
drwxr-xr-x 2 root root      4096 Aug  6 21:11 test
-rw-r--r-- 1 root root        24 Aug  6 21:11 xtrabackup_binlog_pos_innodb

[root@node1 mysql]# chown -R mysql:mysql *   --授授权限
[root@node1 mysql]# ll                       --看看成功没,看来还行
total 125076
-rw-r----- 1 mysql mysql 127926272 Aug  6 21:09 ibdata1
drwxr-xr-x 2 mysql mysql      4096 Aug  6 21:11 mysql
drwxr-xr-x 2 mysql mysql      4096 Aug  6 21:11 opensips
drwxr-xr-x 2 mysql mysql      4096 Aug  6 21:11 rest
drwxr-xr-x 2 mysql mysql      4096 Aug  6 21:11 test
-rw-r--r-- 1 mysql mysql        24 Aug  6 21:11 xtrabackup_binlog_pos_innodb
启动mysql
[root@node1 mysql]# service mysqld start
Starting MySQL..........[  OK  ]
查看日志确认成功

[root@node1 mysql]# service mysqld start
Starting MySQL..........[  OK  ]
[root@node1 mysql]# cat /var/log/mysqld.log 

InnoDB: Database physically writes the file full: wait...
InnoDB: Progress in MB: 100 200
120806 21:12:48  InnoDB: Log file ./ib_logfile1 did not exist: new to be created  --注意这里使用innobackupex进行恢复的时候
                                                                              --不会在恢复的操作中生成iblog*文件如果不想再启动的时候生成
                                                                              (所以启动相对慢点)则恢复的时候可以使用 xtrabackup –prepare 进行恢复
InnoDB: Setting log file ./ib_logfile1 size to 256 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Progress in MB: 100 200

----人世间最大的痛苦莫过于,一直告诉自己不要粘贴输出日志,可为了让大家明白,却不得不粘贴到腰酸背疼,手抽筋!

120806 21:12:53  InnoDB: Started; log sequence number 0 495701004
120806 21:12:53 [Note] Event Scheduler: Loaded 0 events
120806 21:12:53 [Note] /usr/local/mysql/libexec/mysqld: ready for connections.
Version: '5.1.62-compiled-by-lidan-log'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution


ps: 其实这篇文章1年多前就写完了,一直忘了贴上来,现在版本已经到了2.1.7版本了,后续补充一篇最新版的使用说明。



开篇简介部分参考了部分网络文章,具体链接已经忘了,对不住。

 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值