在上篇文档http://jishuweiwang.blog.51cto.com/6977090/1419166中,我介绍了Xtrabackup的一些原理类的东西,确实比较生硬乏味。现在开始真正的体验一下XtraBackup带给你的快感。

# 本文错误已经勘正,有发现错误的同学,请留言回复!谢谢

实验前提1:开启数据库分表

mysql> show global variables like '%innodb_file_p%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.00 sec)

如果看到如上“ON”的结果,说明已经开启。如果发现是“OFF”,可怜的孩子,备份数据库,***数据库相关data目录的东西,重新初始化数据库,修改配置文件:innodb_file_per_table=ON.

实验前提2:授权XtraBackup备份MySQL用户,当然了也可以不授权,官网说:备份时会以执行备份的系统用户作为认证用户...If you don’t use the --user option, Percona XtraBackup will assume the database user whose name is the system user executing it.

mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bak'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

实验前提3:授权系统用户READ, WRITE and EXECUTE 的权限

# 官网是这么说的, 但是没创建bak用户也是可以的,此步可以不关注。

[root@slave data]# mkdir -p /data/backup 
[root@slave data]# useradd bak
[root@slave data]# chown -R bak.bak /data/backup/
[root@shell mysql]# ll bin/x* bin/inno*
-rwxr-xr-x 1 root  root    168820 May 27 20:52 bin/innobackupex
-rwxr-xr-x 1 mysql mysql    12838 May 15 21:58 bin/innochecksum
-rwxr-xr-x 1 root  root   2166316 May 27 20:52 bin/xbcrypt
-rwxr-xr-x 1 root  root   2237637 May 27 20:52 bin/xbstream
-rwxr-xr-x 1 root  root  12625572 May 27 20:52 bin/xtrabackup

牛刀小试之innobackupex

http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/how_innobackupex_works.html

常用参数:

--user=USER                     指定备份用户,不指定的话为当前系统用户

--password=PASSWD               指定备份用户密码

--port=PORT                     指定数据库端口

--defaults-group=GROUP-NAME     在多实例的时候使用

--host=HOST                     指定备份的主机,可以为远程数据库服务器

--apply-log                     回滚日志
--database                      指定需要备份的数据库,多个数据库之间以空格分开

--defaults-file                 指定mysql的配置文件

--copy-back                     将备份数据复制回原始位置
--incremental                   增量备份,后面跟要增量备份的路径

--incremental-basedir=DIRECTORY 增量备份时使用指向上一次的增量备份所在的目录

--incremental-dir=DIRECTORY     增量备份还原的时候用来合并增量备份到全量,用来指定全备路径

--redo-only                     对增量备份进行合并

--rsync                         加快本地文件传输,适用于non-InnoDB数据库引擎。不与--stream共用

--safe-slave-backup

--no-timestamp                  生成的备份文件不以时间戳为目录.

  1. 全量操作

1)全量备份


innobackupex --user=bak --password=123456 /data/backup/
[root@db mysql]# ll /data/backup/2014-05-31_14-01-38/
total 131104
-rw-r--r-- 1 root root       189 May 31 14:01 backup-my.cnf
drwx------ 2 root root      4096 May 31 14:01 fight
-rw-r----- 1 root root 134217728 May 31 14:01 ibdata1
drwxr-xr-x 2 root root      4096 May 31 14:01 mysql
drwxr-xr-x 2 root root      4096 May 31 14:01 performance_schema
-rw-r--r-- 1 root root        13 May 31 14:01 xtrabackup_binary
-rw-r--r-- 1 root root        24 May 31 14:01 xtrabackup_binlog_info
-rw-r----- 1 root root        89 May 31 14:01 xtrabackup_checkpoints
-rw-r----- 1 root root      2560 May 31 14:01 xtrabackup_logfile

 #模拟模拟数据丢失 rm -rf /application/mysql/data/*

 2)全量恢复

 It will preserve file’s attributes when copying them, you may have to change the files’ ownership to mysql before starting the database server, as they will be owned by the user who created the backup.

1)关闭数据库
/etc/init.d/mysqld stop
2) 准备完整备份-->日志回滚
#一般情况下,在备份完成后,数据尚且不能用于恢复操作,
#因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。
#因此,此时数据文件仍处理不一致状态。
#“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
#innobakupex命令的--apply-log选项可用于实现上述功能
innobackupex --defaults-file=/etc/my.cnf --user=bak --password=123456 --apply-log /data/backup/2014-05-31_14-01-38/
#如果执行正确,会输出如下信息
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
140531 14:05:01  InnoDB: Starting shutdown...
140531 14:05:02  InnoDB: Shutdown completed; log sequence number 1614348
140531 14:05:02  innobackupex: completed OK!
3)数据恢复
innobackupex --defaults-file=/etc/my.cnf --user=bak --password=123456 --copy-back /data/backup/2014-05-31_14-01-38/
4) 修改恢复文件权限
chown -R mysql.mysql /application/mysql/data
5)启动数据库,查看数据库恢复情况
/etc/init.d/mysqld start
[root@shell data]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.36-log Source distribution

Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fight              |      # 发现数据库已经都回来了,其中fight是我自己创建的.
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from fight.stu;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | lisi     |
|  3 | wangwu   |
|  4 | maliu    |
|  5 | wang     |
+----+----------+
3 rows in set (0.02 sec)

2. 增量操作

增量备份

#增量备份仅能应用于InnoDB或XtraDB表,对于MyISAM表而言,执行增量备份时其实进行的是完全备份

第一次增量(我插入了一条数据--> 6 nimei)

[root@shell backup]# ll
total 8
drwxr-xr-x 6 root root 4096 May 28 22:59 2014-05-28_22-58-57
[root@shell backup]# innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --incremental /data/backup/ --incremental-basedir=/data/backup/2014-05-31_14-01-38/
#其中/data/backup/2014-05-31_14-01-38是首次全备路径
#提示 140528 22:52:13  innobackupex: completed OK! 表示备份完成
[root@db mysql]# ll /data/backup/2014-05-31_14-
2014-05-31_14-01-38/ 2014-05-31_14-20-17/ 
[root@db mysql]# ll /data/backup/2014-05-31_14-20-17/
total 132
-rw-r--r-- 1 root root   189 May 31 14:20 backup-my.cnf
drwx------ 2 root root  4096 May 31 14:20 fight
-rw-r----- 1 root root 98304 May 31 14:20 ibdata1.delta
-rw-r----- 1 root root    44 May 31 14:20 ibdata1.meta
drwxr-xr-x 2 root root  4096 May 31 14:20 mysql
drwxr-xr-x 2 root root  4096 May 31 14:20 performance_schema
-rw-r--r-- 1 root root    13 May 31 14:20 xtrabackup_binary
-rw-r--r-- 1 root root    23 May 31 14:20 xtrabackup_binlog_info
-rw-r----- 1 root root    93 May 31 14:20 xtrabackup_checkpoints
-rw-r----- 1 root root  2560 May 31 14:20 xtrabackup_logfile

第二次增量(我又插入了一条数据--> 7 nidaye)

innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --incremental /data/backup/ --incremental-basedir=/data/backup/2014-05-31_14-20-17/
# /data/backup/2014-05-31_14-20-17/是上一次增量备份目录

此时,目录下存在三个目录

[root@shell backup]#  ll /data/backup/
total 12
#第一次全备
drwxr-xr-x 5 root root 4096 May 31 14:05 2014-05-31_14-01-38
#第一次增量
drwxr-xr-x 5 root root 4096 May 31 14:20 2014-05-31_14-20-17
#第二次增量
drwxr-xr-x 5 root root 4096 May 31 14:24 2014-05-31_14-24-18

增量合并:合并第一次增量备份到全备文件中

#量备份与整理完全备份有着一些不同,尤其要注意的是:

(1)需要在每个备份(包括完全和各个增量备份)上,将已经提交的事务进行“重放”。“重放”之后,所有的备份数据将合并到完全备份上。

(2)基于所有的备份将未提交的事务进行“回滚”。

#1 重放全备
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/
#2 重放第一次增量,并合并
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/ --incremental-dir=/data/backup/2014-05-31_14-20-17/

模拟一:恢复到第一次增量备份的时刻

rm -rf /application/mysql/data/*
#开始恢复
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --copy-back /data/backup/2014-05-31_14-01-38/
chown -R mysql.mysql /application/mysql/data
#查看恢复状态
[root@db mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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> select * from fight.stu;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | lisi     |
|  3 | wangwu   |
|  4 | maliu    |
|  5 | wang     |
|  6 | nimei    |
+----+----------+              # 发现恢复了6行.
6 rows in set (0.00 sec)

上面恢复到了增量第一次增量的时候,现在进行全量恢复,即恢复到7条数据的时候

innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/ --incremental-dir=/data/backup/2014-05-31_14-24-18/
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --copy-back /data/backup/2014-05-31_14-01-38/
chown -R mysql.mysql /application/mysql/data
#查看恢复结果
[root@db mysql]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS! 
[root@db mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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> select * from fight.stu;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | lisi     |
|  3 | wangwu   |
|  4 | maliu    |
|  5 | wang     |
|  6 | nimei    |
|  7 | nidaye   |
+----+----------+              # 发现已经恢复到7条数据的时候了。
7 rows in set (0.00 sec)

………………………………………………………………………………………………

上边我分批次执行了增量恢复,现在一次性执行全量恢复

#对全量进行重放
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/
#对第一次增量进行合并
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/ --incremental-dir=/data/backup/2014-05-31_14-20-17/
#对第二次增量进行合并
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-05-31_14-01-38/ --incremental-dir=/data/backup/2014-05-31_14-24-18/
#全量恢复
innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --copy-back /data/backup/2014-05-31_14-01-38/
#查看恢复结果
chown -R mysql.mysql /application/mysql/data
mysql> select * from fight.stu;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | lisi     |
|  3 | wangwu   |
|  4 | maliu    |
|  5 | wang     |
|  6 | nimei    |
|  7 | nidaye   |
+----+----------+              # 发现已经又恢复到7条数据的时候了。
7 rows in set (0.00 sec)

#注意,此时如果前边进行了增量恢复,那么需要重新从开始进行才能看到效果.否则会提示如下信息

xtrabackup: ########################################################
xtrabackup: # !!WARNING!!                                          #
xtrabackup: # The transaction log file is corrupted.               #
xtrabackup: # The log was not applied to the intended LSN!         #
xtrabackup: ########################################################
xtrabackup: The intended lsn is 1614986
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
#这是在提示你,已经进行了日志回滚...

--------------------xtrabackup流式备份之--stream参数--------------------

  1. 传输到tar进行归档

innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --stream=tar  /data/backup | gzip > /data/backup/`date +%F_%H-%M-%S`.tar.gz
#查看备份结果
[root@db mysql]# ll /data/backup/
total 352
drwxr-xr-x 5 root root   4096 May 31 14:50 2014-05-31_14-01-38
drwxr-xr-x 5 root root   4096 May 31 14:42 2014-05-31_14-20-17
drwxr-xr-x 5 root root   4096 May 31 14:47 2014-05-31_14-24-18
-rw-r--r-- 1 root root 344318 May 31 14:59 2014-05-31_14-59-36.tar.gz
#发现生产了tar.gz文件

2. 传输到其他服务器

innobackupex --user=bak --password=123456 --defaults-file=/etc/my.cnf --stream=tar  /data/ | ssh 192.168.117.134 "cat - > /data/backup/`date +%F.tar`"
#到异地服务器查看,发现多了一个tar打包文件
[root@backup backup]# ll
total 132992
-rw-r--r-- 1 root  root  136165376 May 31 15:13 2014-05-31.tar
drwxr-xr-x 2 rsync rsync      4096 May  6 18:37 db
drwxr-xr-x 3 rsync rsync      4096 May 11 11:16 nfs
drwxr-xr-x 2 rsync rsync      4096 May  6 18:02 web-01
drwxr-xr-x 2 rsync rsync      4096 May  6 11:22 web-02
解包出来,看看数据文件是否完整    
[root@backup backup]# mkdir /backup
[root@backup backup]# tar xvfi 2014-05-31.tar -C /backup/
#这里需要注意的是,stream备份过来的文件需要通过参数 "-i" 来进行解包!否则解包都会失败.
[root@slave opt]# ll /backup/
total 131108
-rw-r--r-- 1 root root       189 May 29 01:34 backup-my.cnf
drwxr-xr-x 2 root root      4096 May 29 01:38 fight
-rw-rw---- 1 root root 134217728 May 28 23:50 ibdata1
drwxr-xr-x 2 root root      4096 May 29 01:38 mysql
drwxr-xr-x 2 root root      4096 May 29 01:38 performance_schema
drwxr-xr-x 2 root root      4096 May 29 01:38 test
-rw-r--r-- 1 root root        13 May 29 01:34 xtrabackup_binary
-rw-r--r-- 1 root root        23 May 29 01:34 xtrabackup_binlog_info
-rw-rw---- 1 root root        89 May 29 01:34 xtrabackup_checkpoints
-rw-rw---- 1 root root      2560 May 29 01:34 xtrabackup_logfile
#发现fight数据库已经过来了. 此时可以复制这里的所有文件到数据库的数据目录,然后chown进行恢复。
#如果在这个时候想做主从的话,是非常容易的了!这里顺便给大家演示下吧。

主从的部署:

  1. 基于上边传输过来的数据进行从库数据恢复操作

[root@slave backup]# /bin/cp -ap /backup/* /application/mysql/data/
[root@slave backup]# chown -R mysql.mysql /application/mysql/data/
[root@slave backup]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS!

2. 主库授权

mysql> grant replication slave on *.* to rep@'192.168.0.103' identified by '123456';
mysql> flush privileges;

3. 从库查看postion信息,并创建从库身份

[root@slave backup]# cd /application/mysql/data/
[root@slave data]# cat xtrabackup_binlog_info 
mysql-bin.000001        107
#登入mysql进行下列操作.
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO   MASTER_HOST='192.168.0.102',      MASTER_PORT=3306,                   MASTER_USER='rep',                  MASTER_PASSWORD='123456',           MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;
Query OK, 0 rows affected (0.01 sec)
#发现这里的 MASTER_LOG_FILE 和 MASTER_LOG_POS都是xtrabackup_binlog_info文件中的内容 
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;                   
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.102
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 343
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 489
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 343
              Relay_Log_Space: 639
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: 
No query specified
#至此,主从同步也做完了。有没有发现特别快?


xtrabackup快速创建从服务器http://www.unixdo.com/DataBase/Percona-XtraBackup-MySQL-Slave.html