【Mysql】xbackup全量与增量备份恢复


XtraBackup提供了增量备份和恢复的功能
背景:
实验是在原博客的基础上做的!http://blog.itpub.net/7607759/viewspace-700064/

tom库下面有张t1表
  1. mysql> create table t1(id int(10));
    Query OK, 0 rows affected (0.03 sec)
    mysql> 
    mysql> insert into t1 values(1);
    Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)


做个全备

  1. [root@node2 tom]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 /xback/full/
  2. [root@node2 xback]# ls full/
    2015-10-20_21-04-43

-----用来备份的用户只需要这些权限即可,可不用root:grant reload,lock tables,replication client on *.* 


tom下新建表t2

  1. mysql>  create table t2(id int(10));
    Query OK, 0 rows affected (0.06 sec)


    mysql> insert into t1 values(1);
    Query OK, 1 row affected (0.00 sec)


    mysql> insert into t2 values(1);
    Query OK, 1 row affected (0.00 sec)


    mysql> select * from t1;
    +------+
    | id   |
    +------+
    |    1 |
    |    1 |
    +------+
    2 rows in set (0.00 sec)


    mysql> select * from t2;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)
做个增量备份
  1. [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --incremental --incremental-basedir=/xback/full/2015-10-20_21-04-43/ /xback/inc/
  2. [root@node2 xback]# ls inc
    2015-10-20_21-06-04
    --以全备为基础做一次增量备份





  1. 关库,创造恢复环境
  1. [root@node2 mysql]# /etc/init.d/mysqld stop
  2. [root@node2 lib]# mv mysql mysql_bak4
  3. [root@node2 lib]# mkdir mysql



两个实验都是在以上背景的基础上做的!
实验一:全量恢复(此时只是恢复到了t,而t2表示还没有恢复的)
Prepare完整备份集:
  1. [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --apply-log /xback/full/2015-10-09_01-09-54/

恢复
  1. [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --copy-back /xback/full/2015-10-09_01-09-54/
  2. [root@node2 xback]chown mysql:mysql mysql -R
 
启动查看数据库
  1. mysql> show tables
        -> ;
    +---------------+
    | Tables_in_tom |
    +---------------+
    | t             |
    +---------------+
    1 row in set (0.00 sec)


实验二:增量恢复(恢复到t2)

  1. 直接恢复增量集
  2. [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --apply-log /xback/full/2015-10-09_01-36-06/ --incremental-dir=/xback/inc/2015-10-09_01-37-32/

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

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

  7. IMPORTANT: Please check that the apply-log run completes successfully.
  8.            At the end of a successful apply-log run innobackupex
  9.            prints "completed OK!".



  10. 151009 01:39:57 innobackupex: Starting ibbackup with command: xtrabackup_55 --defaults-file="/etc/my.cnf" --prepare --target-dir=/xback/full/2015-10-09_01-36-06 --incremental-dir=/xback/inc/2015-10-09_01-37-32/

  11. xtrabackup_55 Ver 1.6 Rev undefined for 5.5.9 Linux (i686)
  12. incremental backup from 0 is enabled.
  13. xtrabackup: cd to /xback/full/2015-10-09_01-36-06
  14. xtrabackup: This target seems to be not prepared yet.
  15. xtrabackup: error: applying incremental backup needs target prepared. --报错 需要target
  16. innobackupex: Error:
  17. innobackupex: ibbackup failed at /usr/bin/innobackupex line 336.
 
那好,那先恢复全量再恢复增量
  1. [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --apply-log  --redo-only
  2.  /xback/full/2015-10-09_01-36-06/       ---Prepare全量备份集:
[root@jonn xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --apply-log  --redo-only /xback/full/2015-10-20_21-04-43/ --incremental-dir=/xback/inc/2015-10-20_21-06-04/ Prepare增量备份(其实到这后面的都可以不用做了,直接用mv的方式即可) [root@node2 xback]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=ESBecs00 --apply-log  /xback/full/2015-10-09_01-36-06/    ---回滚那些未提交的事务,这一步中innobackupex也会自动创建innodb日志文件: [ root@node2 xback ] #  innobackupex  - - defaults - file = / etc/my . cnf  - - user = root  - - password = ESBecs00  - - copy -back   /xback/full/2015 - 10 - 09_01 - 36 - 06/    ---恢复完成



开启mysql查看
  1. chown mysql:mysql mysql -R
  2. mysql> show tables;
    +---------------+
    | Tables_in_hhh |
    +---------------+
    | t1            |
    +---------------+
    1 row in set (0.00 sec)

  3. mysql> select * from t1;
    +------+
    | id   |
    +------+
    |    1 |
    |    1 |
    +------+
    2 rows in set (0.00 sec)


    mysql> select * from t2;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)
    恢复成功


--提示:一定要使用高版本的 percona-xtrabackup-2.2.12-1.el6.x86_64  最好是2.2版本或以上啊。。我用的2.2以下的TM的t2怎么都恢复不出来~~~~一个bug....我做了好多遍实验


完整备份并打包:

[plain]  view plain  copy
  1. innobackupex --user=root --password=MySQLPASSWORD --defaults-file=/etc/my.cnf --database=test --stream=tar /mysqlbackup > /mysqlbackup/dbbackup20110809.tar  

其中,--stream指定流的格式,目前只支持tar。

完整备份并打包压缩:

[plain]  view plain  copy
  1. innobackupex --user=root --password=MySQLPASSWORD --defaults-file=/etc/my.cnf --database=test --stream=tar /mysqlbackup/ | gzip /mysqlbackup/dbbackup20110809.tar.gz  

完整备份到远程主机: 

[plain]  view plain  copy
  1. innobackupex --user=root --password= MySQLPASSWORD --defaults-file=/etc/my.cnf --database=test --stream=tar /mysqlbackup | ssh root@remote-host cat ">"   /mysqlbackup/dbbackup20110809.tar  

更快的方式:可参考:http://mp.weixin.qq.com/s/TxveGZK9o2QBEJbx1Vd9MA
安装lz4这个压缩工具,这个工具号称压缩能达到300M+/s,解压能达到1G/s。配合xtrabackup自带的--stream=[tar|xbstream]参数,该参数可以把备份的数据输出到标准输出,而lz4支持标准输出,这样也可以实现和测试(3)一样的效果。由于lz4的压缩性能比诸如gzip、bzip2之流快太多,这里就不对比了,留给读者做gzip和bzip2的测试,会发现慢太多
[root@hostnfsd :/root]$ innobackupex --defaults-file=/data/mydata/my_3307.cnf --user=root --password=dkhfdt#@Ladl123 --parallel=4 --stream=xbstream . | lz4 -B4 > bak
  解压:
 [root@hostnfsd :/root]$ cat bak | lz4 -d -B7 | xbstream -x  -C ./bbk/


在上面的基础上通过ssh传输
[root@hostnfsd :/tmp]$ innobackupex --defaults-file=/data/mydata/my_3307.cnf  --user=root --password=dkhfdt#@Ladl123  --parallel=4 --stream=xbstream . | lz4 -B4 |ssh 218.78.186.162 "cat - > /soft/back.0110"



补充,
概念/多个增量备份等可以看下面的这篇博客~~~
http://blog.csdn.net/heizistudio/article/details/23937935

一篇比较好的博客
http://www.itpub.net/thread-1612540-1-1.html


FAQ: 启动过程中报告InnoDB数据页发生损
http://imysql.com/2015/04/23/mysql-faq-mysqld-start-fail-case1.shtml





来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-1813711/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-1813711/

你可能感兴趣的:(【Mysql】xbackup全量与增量备份恢复)