说说MySQL自动化备份脚本

数据库 MySQL 自动化
我们用代码说话,告诉您如何完成一个数据库的备份,也希望您给一些改进的意见。

今天完成了一个数据库备份的脚本,主要功能如下,希望大家给一些改进的建议

  1. -a: backup all database #全库备份 
  2. -e: backup each database#分库备份 
  3. -d: backup single/multi database#备份指定的一个库或者多个库 
  4. -t: backup single/multi table of single database#备份一个库下面的一个表或者多个表 
  5. -b: backup binlog#备份binlog日志,备份过程,每次记录***的二进制文件号,将之前范围内的binlog打包(bz2格式),放在$DIR_BACKUP目录下 
  6. -r: recover all database(!require password!)#恢复全库,为保安全,恢复时需要输入密码 
  7. -o: recover single database/talbe,you should be designation database name(!require password!)#恢复单库或单表 
  8. -p: create connect mysql password#创建连接mysql的密码文件,存放位置$DIR_MySQL/etc目录下,权限是600 
  9. -s: configuration rsyncd#创建rsyncd服务(需要时,可修改参数创建,因backup机上有rsyncd服务,故不需要在每台DB server上创建rsyncd服务) 
  10.     If you want ceate a rsyncd, you should enter 'bakrec_mysql.sh -s cet' 
  11.     If you want restart rsyncd, you should enter 'bakrec_mysql.sh -s rst' 
  12. -c: sync to backup center#同步到backup1
  1. #!/bin/bash 
  2. # email: lianjie.ning@qunar.com 
  3. last change time: 2011-08-03 
  4. set -e 
  5. set -u 
  6. TIME=`date +%Y%m%d%H%M%S` 
  7. TIME_7=`date -d '7 days ago' +%Y%m%d%H%M%S` 
  8. TIME_YM=`date +%Y%m` 
  9. DIR_MYSQL='/usr/local/mysql' 
  10. DIR_BACKUP="/tmp/backup" 
  11. DIR_DATA="$DIR_MYSQL/data" 
  12. DIR_PASSWD="$DIR_MYSQL/etc" 
  13. FILE_PASSWD="$DIR_PASSWD/passwordfile" 
  14. BINLOG_NAME='mysql-bin' 
  15. CMD_MYSQLBINLOG="$DIR_MYSQL/bin/mysqlbinlog" 
  16. CMD_MYSQLDUMP="$DIR_MYSQL/bin/mysqldump" 
  17. CMD_MYSQL="$DIR_MYSQL/bin/mysql" 
  18. LIST_EXCLUDE_DB='(test|information_schema|performance_schema)' 
  19. if [ ! -d $DIR_BACKUP/$TIME_YM ]; then 
  20. mkdir -p $DIR_BACKUP/$TIME_YM 
  21. fi 
  22. cd $DIR_BACKUP/$TIME_YM 
  23. function result_status() 
  24. if [ $? -eq 0 ]; then 
  25. echo "[`date +%Y%m%d%H%M%S`] SUCCESS! "|tee -a log.$TIME_YM 
  26. else 
  27. echo "[`date +%Y%m%d%H%M%S`] ERROR! "|mail -s "backup error $HOSTNAME" ning_lianjie@163.com|tee -a log.$TIME_YM 
  28. fi 
  29. function usage_error() 
  30. echo "Usage: $0 RUN ERROR" 
  31. echo " 
  32. -a: backup all database 
  33. -e: backup each database 
  34. -d: backup single/multi database 
  35. -t: backup single/multi table of single database 
  36. -b: backup binlog 
  37. -r: recover all database(!require password!) 
  38. -o: recover single database/talbe,you should be designation database name(!require password!) 
  39. -p: create connect mysql password 
  40. -s: configuration rsyncd 
  41. If you want ceate a rsyncd, you should enter '$0 -s cet' 
  42. If you want restart rsyncd, you should enter '$0 -s rst' 
  43. -c: sync to backup center 
  44. exit 0 
  45. function read_pwd() 
  46. read USER PASSWD < $FILE_PASSWD 
  47. function backup() 
  48. read_pwd 
  49. LOGBIN_STATUS=`$CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW VARIABLES LIKE 'log_bin'" | gawk '{print $2}'
  50. if [ $LOGBIN_STATUS = "ON" ]; then 
  51. MASTER='--master-data=2' 
  52. else 
  53. MASTER=' ' 
  54. fi 
  55. function backup_all() 
  56. backup 
  57. $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R -A --add-drop-database $MASTER |gzip >$HOSTNAME.all.$TIME.sql.gz 
  58. function backup_each() 
  59. backup 
  60. for db in $($CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW DATABASES"|egrep -v $LIST_EXCLUDE_DB) 
  61. do 
  62. $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER $db --databases |gzip >$HOSTNAME.$db.$TIME.sql.gz 
  63. done 
  64. delete 7 days ago 
  65. for db in $($CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW DATABASES"|egrep -v $LIST_EXCLUDE_DB) 
  66. do 
  67. if [ ! -f $HOSTNAME.$db.$TIME_7.sql.gz ]; then 
  68. echo 
  69. else 
  70. rm $HOSTNAME.$db.$TIME_7.sql.gz -f 
  71. fi 
  72. done 
  73. function backup_db() 
  74. shift 
  75. backup 
  76. $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER --databases $@| gzip>$HOSTNAME.$OPTARG.$TIME.sql.gz 
  77. function backup_dt() 
  78. shift 
  79. if [ $# -ge 2 ]; then 
  80. backup 
  81. $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER $@| gzip>$HOSTNAME.$OPTARG.$TIME.sql.gz 
  82. else 
  83. usage_error 
  84. fi 
  85. function backup_binlog() 
  86. if [ -s $DIR_BACKUP/mysql-bin.queue ]; then 
  87. read POS < $DIR_BACKUP/mysql-bin.queue 
  88. cd $DIR_DATA 
  89. tar -jcvf $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME.bz2 `gawk -F'/' '{print $2}' $BINLOG_NAME.index |sed -n "/$POS/,//p"
  90. cd - 
  91. if [ -f $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME_7.bz2 ]; then 
  92. rm $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME_7.bz2 -f 
  93. fi 
  94. fi 
  95. # write last pos 
  96. gawk -F'/' '{print $2}' $DIR_DATA/$BINLOG_NAME.index | tail -n 1 >$DIR_BACKUP/mysql-bin.queue 
  97. function recover_all() 
  98. read_pwd 
  99. shift 
  100. $CMD_MYSQL -u$USER -p -e "source $@" 
  101. function recover_dt() 
  102. read_pwd 
  103. shift 
  104. if [ $# -eq 2 ]; then 
  105. $CMD_MYSQL -u$USER -p -D $1 -e "source $2" 
  106. else 
  107. usage_error 
  108. fi 
  109. function passwd_create() 
  110. if [ ! -e "$DIR_PASSWD" ]; then 
  111. mkdir -p $DIR_PASSWD 
  112. fi 
  113. echo -n "Please enter MySQL(user=root)'s password:" 
  114. read -s MYSQL_FASSWD 
  115. cat >$FILE_PASSWD <<+ 
  116. root $MYSQL_FASSWD 
  117. chmod 600 $FILE_PASSWD 
  118. function rsyncd() 
  119. shift 
  120. if [ $# -eq 0 ]; then 
  121. usage_error 
  122. else 
  123. DIR_RSYNCD='/usr/local/rsync' 
  124. FILE_RSYNCD_PASSWORD="$DIR_RSYNCD/rsyncd.password" 
  125. case "$1" in 
  126. 'cet'
  127. if [ ! -d $DIR_RSYNCD ]; then 
  128. mkdir -p $DIR_RSYNCD 
  129. fi 
  130. if [ ! -e "$DIR_RSYNCD/rsyncd.conf" ]; then 
  131. touch $DIR_RSYNCD/rsyncd.conf 
  132. fi 
  133. mv $DIR_RSYNCD/rsyncd.conf $DIR_RSYNCD/rsyncd.conf.$TIME.bak 
  134. cat >$DIR_RSYNCD/rsyncd.conf <<+ 
  135. uid = root 
  136. gid = root 
  137. use chroot = no 
  138. max connections = 5 
  139. lock file = $DIR_RSYNCD/rsyncd.lock 
  140. log file = $DIR_RSYNCD/rsyncd.log 
  141. pid file = $DIR_RSYNCD/rsyncd.pid 
  142. hosts allow = 192.168.250.251 
  143. hosts deny = * 
  144. ignore errors 
  145. read only = yes 
  146. list = no 
  147. auth users = backupdbuser 
  148. secrets file = $DIR_RSYNCD/rsyncd.password 
  149. [BINLOG] 
  150. path = $DIR_DATA 
  151. include = $BINLOG_NAME.* 
  152. exclude = * 
  153. [DUMPDB] 
  154. path = $DIR_BACKUP 
  155. cat >$FILE_RSYNCD_PASSWORD <<+ 
  156. username:password 
  157. chmod 600 $FILE_RSYNCD_PASSWORD 
  158. exit 0 
  159. ;; 
  160. 'rst'
  161. if [ -s "$DIR_RSYNCD/rsyncd.pid" ]; then 
  162. rsyncd_pid=`cat "$DIR_RSYNCD/rsyncd.pid"
  163. if (kill -0 $rsyncd_pid 2>/dev/null); then 
  164. echo "Shutting down rsyncd" 
  165. kill $rsyncd_pid 
  166. else 
  167. echo "rsyncd #$rsyncd_pid is not running!" 
  168. rm "$DIR_RSYNCD/rsyncd.pid" 
  169. fi 
  170. fi 
  171. sleep 2 
  172. rsync --daemon --config=$DIR_RSYNCD/rsyncd.conf --port=873 
  173. echo "rsync --daemon --config=$DIR_RSYNCD/rsyncd.conf --port=873" 
  174. echo "netstat -tunlp | grep rsync" 
  175. netstat -tunlp | grep rsync 
  176. ;; 
  177. *) 
  178. usage_error 
  179. ;; 
  180. esac 
  181. fi 
  182. #main 
  183. if [ $# -eq 0 ]; then 
  184. usage_error 
  185. else 
  186. while getopts :aed:t:r:o:bpsc varname 
  187. do 
  188. case $varname in 
  189. a) 
  190. backup_all 
  191. ;; 
  192. e) 
  193. backup_each 
  194. ;; 
  195. d) 
  196. backup_db $@ 
  197. ;; 
  198. t) 
  199. backup_dt $@ 
  200. ;; 
  201. b) 
  202. backup_binlog 
  203. ;; 
  204. r) 
  205. recover_all $@ 
  206. ;; 
  207. o) 
  208. recover_dt $@ 
  209. ;; 
  210. p) 
  211. passwd_create 
  212. ;; 
  213. s) 
  214. rsyncd $@ 
  215. ;; 
  216. c) 
  217. # rsync -czrptgoD --password-file=/tmp/.passwd $HOSTNAME.*.$TIME.sql.gz backupdbuser@192.168.250.251::DUMPDB/$HOSTNAME/$TIME_YM 
  218. rsync -czrpt --password-file=/tmp/.passwd $DIR_BACKUP/$TIME_YM backupdbuser@192.168.250.251::DUMPDB/$HOSTNAME 
  219. result_status 
  220. ;; 
  221. :) 
  222. echo "$varname: 缺少参数" 
  223. usage_error 
  224. ;; 
  225. \?) 
  226. echo "$varname: 非法选项" 
  227. usage_error 
  228. ;; 
  229. esac 
  230. done 
  231. fi 

原文链接:http://blog.chinaunix.net/space.php?uid=16844903&do=blog&id=2010853

【编辑推荐】

  1. 教你如何利用MySQL学习MongoDB
  2. Craigslist采用MongoDB替代MySQL
  3. MySQL中的NoSQL插件
  4. SQL与NoSQL——MySQL与NoSQL的融合
  5. 论MySQL何时使用索引,何时不使用索引
责任编辑:艾婧 来源: ning_lianjie的博客
相关推荐

2023-05-11 08:46:28

MySQL

2014-03-11 11:10:10

PowerShell自动化脚本

2015-05-28 10:46:22

shellBackupdatabase

2011-05-31 17:35:45

测试自动化QTP

2022-04-08 09:05:53

Arch LinuxLinux

2023-03-07 08:30:09

MCube模板缓存

2016-12-20 16:47:25

Hadoop部署脚本

2014-03-11 11:03:17

自动化脚本PowerShell

2009-12-15 17:28:11

Ruby自动化脚本框架

2016-09-27 23:31:23

2017-03-03 09:10:09

2017-12-17 21:58:18

2012-03-30 15:52:51

ibmdw

2022-03-10 10:12:04

自动化脚本Bash

2022-02-17 13:03:28

Python脚本代码

2011-08-08 10:10:21

Linux备份

2010-03-30 09:38:58

2010-07-15 13:21:46

Perl多进程

2011-06-08 17:15:46

QTP脚本

2022-08-05 09:06:07

Python脚本代码
点赞
收藏

51CTO技术栈公众号