码迷,mamicode.com
首页 > 数据库 > 详细

mysql增量备份 percona-xtrabackup

时间:2016-03-21 10:31:16      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

  1. 先说下实际环境
  2. 阿里云买的ESC跑的mysql服务,目前由于数据量过大,数据库目前有800GB,每次全备需要等20多个小时才能够完成,然后就想到做增量备份,写下此文档

一、测试环境

  1. [root@localhost ~]# cat /etc/redhat-release
  2. CentOS release 6.2 (Final)
  3. [root@localhost ~]# mysql --version
  4. mysql Ver 14.14 Distrib 5.5.20, for Linux (x86_64) using readline 5.1

二、安装软件及其作用

  1. 软件名:percona-xtrabackup
  2. 作用:针对Mysql数据库进行热备份,及增量备份,主要应用于innodb引擎

三、安装步骤

  1. yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses5-devel #安装依赖包
  2. yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm #安装percona YUM库
  3. yum -y install percona-xtrabackup.x86_64 #安装percona-xtrabackup

四、备份及恢复命令

1)完整备份
  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=PASSWORD --database=DATE BACK_DIR_PATH
  2. # PASSWORD 数据库密码
  3. # DATE 数据库名
  4. # --database=DATE 针对所有库做备份去掉该选项即可
  5. # BACK_DIR_PATH 完全备份目录位置
  6. 注:该命令执行后会在备份目录位置以当前时间为命名生成一个目录,备份的文件会备份到该目录中。例如2014-12-06_16-00-15
2)完整备份的恢复
  1. 1. 首先需要备份数据库
  2. 2. 停止数据库运行
  3. 3. 删除数据库目录内所有文件
  4. 4. 然后执行下列命令进行恢复
  5. innobackupex --defaults-file=/etc/my.cnf --user=root --password=PASSWORD --database=DATE --apply-log BACK_DIR_PATH/2014-12-06_16-00-15
  6. # PASSWORD 数据库密码
  7. # DATE 数据库名
  8. # BACK_DIR_PATH 完全备份目录位置
3)增量备份
  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=PASSWORD --database=DATE --incremental-basedir=BACK_DIR_PATH/2014-12-06_16-00-15 --incremental BACK_DIR_ZENG_1
  2. # PASSWORD 数据库密码
  3. # DATE 数据库名
  4. # BACK_DIR_PATH 完全备份目录位置
  5. # BACK_DIR_ZENG_1 第一次增量备份目录位置
  6. # --incremental-basedir 该选项后面写的为第一次完整备份的路径
  7. 注:此次增量备份是在第一次完整备份的基础之上进行的增量备份
4)第二次增量备份
  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=PASSWORD --database=DATE --incremental-basedir=BACK_DIR_ZENG_1 --incremental BACK_DIR_ZENG_2
  2. # PASSWORD 数据库密码
  3. # DATE 数据库名
  4. # BACK_DIR_PATH 完全备份目录位置
  5. # BACK_DIR_ZENG_1 第一次增量备份目录位置
  6. # BACK_DIR_ZENG_2 第二次增量备份目录位置
  7. # --incremental-basedir 该选项后面写的为第一次增量备份的路径
  8. 注:此次增量备份是在第一次增量备份的基础之上进行的第二次增量备份
5)第三次增量备份
  1. 同第二次增量备份
6)多次增量备份之后的恢复
  1. 1、首先需要备份数据库
  2. 2、停止数据库运行
  3. 3、删除数据库目录内所有文件
  4. 4、然后执行下列命令进行恢复
  5. innobackupex --apply-log --redo-only BACK_DIR_PATH
  6. innobackupex --apply-log --redo-only BACK_DIR_PATH --incremental-dir=BACK_DIR_ZENG_1
  7. innobackupex --apply-log BACK_DIR_PATH --incremental-dir=BACK_DIR_ZENG_2
  8. # BACK_DIR_PATH 完全备份目录位置
  9. # BACK_DIR_ZENG_1 第一次增量备份目录位置
  10. # BACK_DIR_ZENG_2 第二次增量备份目录位置
  11. 注: 如果只有2次增量备份则执行此条命令,如果有多次增量命令,则参照上一条命令

五、脚本

  1. 此脚本为一次全备+6次增备轮询
  2. #!/bin/sh
  3. DATE=`date +%Y-%m-%d`
  4. BACKUP_DIR_PATH=/backup/data
  5. FILE_CNF=/etc/my.cnf
  6. USER_NAME=root
  7. PASSWORD=root
  8. DATEBASE=test
  9. BACK_LOG=$BACKUP_DIR_PATH/backup_cost_time
  10. OUT_LOG=Xtrabackup_$DATE.log
  11. if [ -d "$BACKUP_DIR_PATH/rec6" ];
  12. then
  13. mv $BACKUP_DIR_PATH $BACKUP_DIR_PATH-bak
  14. mkdir $BACKUP_DIR_PATH
  15. fi
  16. if [ ! -d "$BACKUP_DIR_PATH/full" ];
  17. then
  18. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  19. mkdir $BACKUP_DIR_PATH/full
  20. echo "###start full backup at $START_DATE to directory full" >> $BACK_LOG
  21. echo "###start full backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  22. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE $BACKUP_DIR_PATH/full 2>>$BACKUP_DIR_PATH/$OUT_LOG
  23. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  24. echo "###stop full backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  25. echo "###stop full backup at $STOP_DATE to directory full" >> $BACK_LOG
  26. echo "" >> $BACK_LOG
  27. echo "" >> $BACK_LOG
  28. echo "" >> $BACK_LOG
  29. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  30. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  31. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  32. break;
  33. elif [ ! -d "$BACKUP_DIR_PATH/rec1" ];
  34. then
  35. BACKUP_FULL_PATH=`ls -l $BACKUP_DIR_PATH/full |awk ‘/^d/ {print $NF}‘`
  36. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  37. mkdir $BACKUP_DIR_PATH/rec1
  38. echo "###start 1 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  39. echo "###start 1 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  40. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/full/$BACKUP_FULL_PATH --incremental $BACKUP_DIR_PATH/rec1 2>>$BACKUP_DIR_PATH/$OUT_LOG
  41. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  42. echo "###stop 1 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  43. echo "###stop 1 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  44. echo "" >> $BACK_LOG
  45. echo "" >> $BACK_LOG
  46. echo "" >> $BACK_LOG
  47. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  48. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  49. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  50. break;
  51. elif [ ! -d "$BACKUP_DIR_PATH/rec2" ];
  52. then
  53. BACKUP_REC1_PATH=`ls -l $BACKUP_DIR_PATH/rec1 |awk ‘/^d/ {print $NF}‘`
  54. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  55. mkdir $BACKUP_DIR_PATH/rec2
  56. echo "###start 2 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  57. echo "###start 2 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  58. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/rec1/$BACKUP_REC1_PATH --incremental $BACKUP_DIR_PATH/rec2 2>>$BACKUP_DIR_PATH/$OUT_LOG
  59. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  60. echo "###stop 2 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  61. echo "###stop 2 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  62. echo "" >> $BACK_LOG
  63. echo "" >> $BACK_LOG
  64. echo "" >> $BACK_LOG
  65. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  66. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  67. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  68. break;
  69. elif [ ! -d "$BACKUP_DIR_PATH/rec3" ];
  70. then
  71. BACKUP_REC2_PATH=`ls -l $BACKUP_DIR_PATH/rec2 |awk ‘/^d/ {print $NF}‘`
  72. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  73. mkdir $BACKUP_DIR_PATH/rec3
  74. echo "###start 3 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  75. echo "###start 3 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  76. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/rec2/$BACKUP_REC2_PATH --incremental $BACKUP_DIR_PATH/rec3 2>>$BACKUP_DIR_PATH/$OUT_LOG
  77. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  78. echo "###stop 3 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  79. echo "###stop 3 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  80. echo "" >> $BACK_LOG
  81. echo "" >> $BACK_LOG
  82. echo "" >> $BACK_LOG
  83. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  84. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  85. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  86. break;
  87. elif [ ! -d "$BACKUP_DIR_PATH/rec4" ];
  88. then
  89. BACKUP_REC3_PATH=`ls -l $BACKUP_DIR_PATH/rec3 |awk ‘/^d/ {print $NF}‘`
  90. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  91. mkdir $BACKUP_DIR_PATH/rec4
  92. echo "###start 4 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  93. echo "###start 4 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  94. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/rec3/$BACKUP_REC3_PATH --incremental $BACKUP_DIR_PATH/rec4 2>>$BACKUP_DIR_PATH/$OUT_LOG
  95. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  96. echo "###stop 4 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  97. echo "###stop 4 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  98. echo "" >> $BACK_LOG
  99. echo "" >> $BACK_LOG
  100. echo "" >> $BACK_LOG
  101. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  102. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  103. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  104. break;
  105. elif [ ! -d "$BACKUP_DIR_PATH/rec5" ];
  106. then
  107. BACKUP_REC4_PATH=`ls -l $BACKUP_DIR_PATH/rec4 |awk ‘/^d/ {print $NF}‘`
  108. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  109. mkdir $BACKUP_DIR_PATH/rec5
  110. echo "###start 5 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  111. echo "###start 5 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  112. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/rec4/$BACKUP_REC4_PATH --incremental $BACKUP_DIR_PATH/rec5 2>>$BACKUP_DIR_PATH/$OUT_LOG
  113. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  114. echo "###stop 5 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  115. echo "###stop 5 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  116. echo "" >> $BACK_LOG
  117. echo "" >> $BACK_LOG
  118. echo "" >> $BACK_LOG
  119. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  120. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  121. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  122. break;
  123. elif [ ! -d "$BACKUP_DIR_PATH/rec6" ];
  124. then
  125. BACKUP_REC5_PATH=`ls -l $BACKUP_DIR_PATH/rec5 |awk ‘/^d/ {print $NF}‘`
  126. START_DATE=`date +%Y-%m-%d_%H:%M:%S`
  127. mkdir $BACKUP_DIR_PATH/rec6
  128. echo "###start 6 incremental backup at $START_DATE to directory full" >> $BACK_LOG
  129. echo "###start 6 incremental backup at $START_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  130. innobackupex --defaults-file=$FILE_CNF --user=$USER_NAME --password=$PASSWORD --database=$DATEBASE --incremental-basedir=$BACKUP_DIR_PATH/rec5/$BACKUP_REC5_PATH --incremental $BACKUP_DIR_PATH/rec6 2>>$BACKUP_DIR_PATH/$OUT_LOG
  131. STOP_DATE=`date +%Y-%m-%d_%H:%M:%S`
  132. echo "###stop 6 incremental backup at $STOP_DATE to directory full" >> $BACKUP_DIR_PATH/$OUT_LOG
  133. echo "###stop 6 incremental backup at $STOP_DATE to directory full" >> $BACK_LOG
  134. echo "" >> $BACK_LOG
  135. echo "" >> $BACK_LOG
  136. echo "" >> $BACK_LOG
  137. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  138. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  139. echo "" >> $BACKUP_DIR_PATH/$OUT_LOG
  140. break;
  141. fi




mysql增量备份 percona-xtrabackup

标签:

原文地址:http://www.cnblogs.com/CongZhang/p/5300699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!