MySQL自动化备份脚本_升级版


经过一段时间的测试,在上次脚本的基础上做了大量的修改优化,使之更为实用,欢迎大家测试,提出宝贵意见.

脚本仅供参考,不可直接粘贴使用,重要的还是分享思路.
脚本说明请看上一篇
主要说明一下变更内容:
1,增加年月目录下面的日期目录,比如在201110目录下,会按日期生成22日的目录,该目录下,为当天的备份
2,增加f参数,可以自己定保存的天数,保留7天,则设置-f 7
3,删除了恢复数据的函数,恢复数据比较重要,需要单独编写脚本.
4,脚本文件与变量文件分开,便于脚本更新.
5,脚本名改为backup_mysql.sh
使用说明:
-a: backup all database 备份全库
-e: backup each database 备份所有分库
-d: backup single/multi database eg. -d ‘mysql test‘ 备份单库,或者多个库
-t: backup single/multi table of single database eg. -t ‘mysql user‘ 备份单库下面的表
-b: backup binlog 备份binlog
-f: delete backup 7 days ago! eg. -f 7 删除7天之前的备份,天数可自定义
-p: create connect mysql password 创建密码文件
-c: sync to backup center 传送到异机
脚本文件组成:
backup_mysql.pass传送用的密码文件
backup_mysql.sh主脚本
backup_mysql.var定义的变量
脚本内容:
  1. #!/bin/bash


  2. # script name: backup_mysql.sh

  3. # version: 2011XX


  4. set -e

  5. set -u


  6. #导入backup_mysql.var中的变量.

  7. function prepare()

  8. {

  9. if [ -r ‘backup_mysql.var‘ ]; then

  10. . backup_mysql.var

  11. echo ". backup_mysql.var is success!"

  12. else

  13. echo ". backup_mysql.var is fail!"

  14. exit 0

  15. fi


  16. #检查and创建目录

  17. if [ ! -d $DIR_BACKUP/$TIME_YM/$TIME_DAY ]; then

  18. mkdir -p $DIR_BACKUP/$TIME_YM/$TIME_DAY

  19. fi

  20. cd $DIR_BACKUP/$TIME_YM/$TIME_DAY

  21. }


  22. #结果状态函数

  23. function result_status()

  24. {

  25. echo "[`date +%Y%m%d%H%M%S`] SUCCESS!

  26. $COMMAND"|tee -a log.$TIME_YM

  27. }


  28. #使用帮助and错误提示

  29. function usage_error()

  30. {

  31. echo "Usage: $0 RUN ERROR"

  32. echo "

  33. -a: backup all database

  34. -e: backup each database

  35. -d: backup single/multi database eg. -d ‘mysql test‘

  36. -t: backup single/multi table of single database eg. -t ‘mysql user‘

  37. -b: backup binlog

  38. -f: delete backup 7 days ago! eg. -f 7

  39. -p: create connect mysql password

  40. -c: sync to backup center

  41. "

  42. exit 0

  43. }


  44. #读取密码文件

  45. function read_pwd()

  46. {

  47. read USER PASSWD < $FILE_PASSWD

  48. }


  49. #同步

  50. #设置局部变量 FILE_NAME

  51. function rsync_cmd()

  52. {

  53. if [ -z "$POS" ]; then

  54. typeset FILE_NAME="$HOSTNAME.*.$TIME.sql.gz"

  55. else

  56. typeset FILE_NAME="$HOSTNAME.$POS.$TIME.tgz"

  57. fi

  58. cd $DIR_BACKUP

  59. rsync -crptR --password-file=$RSYNC_PASSWD $TIME_YM/$TIME_DAY/$FILE_NAME backupdbuser@$BACKUPHOST/$HOSTNAME/

  60. typeset COMMAND="rsync -crpt --password-file=$RSYNC_PASSWD $TIME_YM/$TIME_DAY/$FILE_NAME backupdbuser@$BACKUPHOST/$HOSTNAME/"

  61. result_status

  62. cd -

  63. }


  64. #基础备份函数

  65. function backup()

  66. {

  67. read_pwd

  68. LOGBIN_STATUS=`$CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW VARIABLES LIKE ‘log_bin‘" | gawk ‘{print $2}‘`

  69. if [ $LOGBIN_STATUS = "ON" ]; then

  70. MASTER=‘--master-data=2‘

  71. else

  72. MASTER=‘ ‘

  73. fi


  74. #mysqldump部分命令+参数

  75. CMD_PART="$CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER --socket=$SOCKET --default-character-set=utf8"

  76. }



  77. #调用backup函数,备份

  78. function backup_all()

  79. {

  80. backup

  81. $CMD_PART -A --add-drop-database |gzip >$HOSTNAME.all.$TIME.sql.gz

  82. }


  83. function backup_each()

  84. {

  85. backup

  86. for db in $($CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW DATABASES"|egrep -v $LIST_EXCLUDE_DB)

  87. do

  88. $CMD_PART $db --databases |gzip >$HOSTNAME.$db.$TIME.sql.gz

  89. # sleep 20

  90. done

  91. }


  92. function backup_db()

  93. {

  94. backup

  95. $CMD_PART --databases $OPTARG | gzip > "$HOSTNAME.`echo "$OPTARG"|gawk ‘{print $1$2}‘`.$TIME.sql.gz"

  96. }


  97. function backup_dt()

  98. {

  99. backup

  100. $CMD_PART $OPTARG | gzip > "$HOSTNAME.`echo "$OPTARG"|gawk ‘{print $1$2}‘`.$TIME.sql.gz"

  101. }


  102. function backup_binlog()

  103. {

  104. if [ -s $DIR_BACKUP/mysql-bin.queue ]; then

  105. read POS < $DIR_BACKUP/mysql-bin.queue

  106. cd $DIR_DATA

  107. tar -zcvf $DIR_BACKUP/$TIME_YM/$TIME_DAY/$HOSTNAME.$POS.$TIME.tgz `gawk -F‘/‘ ‘{print $NF}‘ $BINLOG_NAME.index |sed -n "/$POS/,//p"`

  108. cd -

  109. fi

  110. # write last pos

  111. gawk -F‘/‘ ‘{print $NF}‘ $DIR_DATA/$BINLOG_NAME.index | tail -n 1 >$DIR_BACKUP/mysql-bin.queue

  112. }


  113. #删除7天之前的备份,如果目录为空,则会删除目录

  114. function backup_delete()

  115. {

  116. cd $DIR_BACKUP

  117. find -mtime +$OPTARG -name "$HOSTNAME.*gz"|xargs -i rm {} -f

  118. typeset TIME_YM=`date -d ‘2 month ago‘ +%Y%m`

  119. if [ -d $TIME_YM ]; then

  120. rm $TIME_YM -rf

  121. fi

  122. cd -

  123. }


  124. function passwd_create()

  125. {

  126. if [ ! -e "$DIR_PASSWD" ]; then

  127. mkdir -p $DIR_PASSWD

  128. fi

  129. echo -n "Please enter MySQL(user=root)‘s password:"

  130. read -s MYSQL_FASSWD

  131. cat >$FILE_PASSWD <<+

  132. root $MYSQL_FASSWD

  133. +

  134. chmod 600 $FILE_PASSWD

  135. }


  136. #main

  137. if [ $# -eq 0 ]; then

  138. usage_error

  139. else

  140. prepare

  141. while getopts :aed:t:bf:pc varname

  142. do

  143. case $varname in

  144. a)

  145. backup_all

  146. ;;

  147. e)

  148. backup_each

  149. ;;

  150. d)

  151. backup_db

  152. ;;

  153. t)

  154. backup_dt

  155. ;;

  156. b)

  157. backup_binlog

  158. ;;

  159. f)

  160. backup_delete

  161. ;;

  162. p)

  163. passwd_create

  164. ;;

  165. c)

  166. rsync_cmd

  167. ;;

  168. :)

  169. echo "$varname: 缺少参数"

  170. usage_error

  171. ;;

  172. \?)

  173. echo "$varname: 非法选项"

  174. usage_error

  175. ;;

  176. esac

  177. done

  178. fi

变量(backup_mysql.var)格是如下:
  1. TIME=`date +%Y%m%d%H%M%S`

  2. TIME_7=`date -d ‘7 days ago‘ +%Y%m%d%H%M%S`

  3. TIME_YM=`date +%Y%m`

  4. TIME_DAY=`date +%d`

  5. DIR_MYSQL=‘/usr/local/mysql‘

  6. DIR_BACKUP="/backupdb"

  7. DIR_DATA="$DIR_MYSQL/data"

  8. DIR_PASSWD="$DIR_MYSQL/etc"

  9. FILE_PASSWD="$DIR_PASSWD/passwordfile"

  10. BINLOG_NAME=‘mysql-bin‘

  11. CMD_MYSQLDUMP="$DIR_MYSQL/bin/mysqldump"

  12. CMD_MYSQL="$DIR_MYSQL/bin/mysql"

  13. LIST_EXCLUDE_DB=‘(test|information_schema|performance_schema)‘

  14. SOCKET=‘/usr/local/mysql/data/mysql.sock‘

  15. BACKUPHOST=‘192.168.250.251::DUMPDB‘

  16. RSYNC_PASSWD=‘backup_mysql.pass‘


本文出自 “Linux运维” 博客,请务必保留此出处http://2853725.blog.51cto.com/2843725/1394432

MySQL自动化备份脚本_升级版,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。