Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1859209
  • 博文数量: 117
  • 博客积分: 2559
  • 博客等级: 少校
  • 技术积分: 4385
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-13 20:08
个人简介

作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.

文章分类

全部博文(117)

文章存档

2014年(1)

2013年(25)

2012年(13)

2011年(77)

2010年(1)

分类: 系统运维

2011-06-28 09:06:13

脚本功能:
1·指定不同的参数,实现分库、分表、rsync创建
2·打印执行结果,同时记录到日志
脚本内容如下:
  1. #!/bin/bash
  2. STARTTIME=`/bin/date +%Y%m%d%H%M%S`
  3. STARTTIME7=`date -d '7 days ago' +%Y%m%d%H%M%S`
  4. YEARMONTH=`date +%Y%m`
  5. MKDIR='/bin/mkdir'
  6. MYSQLDUMP='/usr/bin/mysqldump'
  7. GZIP='/bin/gzip'
  8. RM='/bin/rm'
  9. MV='/bin/mv'
  10. CHMOD='/bin/chmod'
  11. TOUCH='/bin/touch'
  12. BACKUP_DIR='/server/backup_mysql'
  13. BINLOG_DIR='/var/lib/mysql'
  14. BINLOG_NAME='mysql-bin'
  15. #define function
  16. function log_success_msg()
  17. {
  18. echo " SUCCESS! $@"
  19. }
  20. function log_failure_msg()
  21. {
  22. echo " ERROR! $@"
  23. }
  24. function ResultStat() {
  25. if [ $RESULT -eq 0 ]; then
  26. log_success_msg " [`/bin/date +%Y%m%d%H%M%S`] $STATS_NAME
  27. -------------------------------------------------------------------------------"|tee -a backup_mysql.$YEARMONTH.log
  28. else
  29. log_failure_msg " [`/bin/date +%Y%m%d%H%M%S`] $STATS_NAME
  30. -------------------------------------------------------------------------------"|tee -a backup_mysql.$YEARMONTH.log
  31. fi
  32. }
  33. function UsageError()
  34. {
  35. echo
  36. echo "$0 : Run Error"
  37. echo "Usage : Try '$0 -d or -a'
  38. -d : designation database or table
  39. -a : detach of all databases
  40. -rsyncd : configuration rsync server"
  41. }
  42. function CreateDir()
  43. {
  44. if [ ! -d $BACKUP_DIR ]; then
  45. $MKDIR -p $BACKUP_DIR
  46. RESULT=$?
  47. STATS_NAME="Check OR Create dir...$BACKUP_DIR"
  48. ResultStat
  49. sleep 2
  50. fi
  51. cd $BACKUP_DIR
  52. }
  53. # create .my.cnf file
  54. if [ ! -e '/root/.my.cnf' ]; then
  55. $TOUCH /root/.my.cnf
  56. $MV /root/.my.cnf /root/.my.cnf.$STARTTIME.bak
  57. cat >/root/.my.cnf <<+
  58. [client]
  59. user = root
  60. password = 123456
  61. +
  62. RESULT=$?
  63. STATS_NAME="The /root/.my.cnf is created."
  64. ResultStat
  65. fi
  66. # chose backup type
  67. if [ $# -eq 0 ]; then
  68. UsageError
  69. exit 1
  70. elif [ $1 = '-d' ]; then
  71. CreateDir
  72. if [[ -n $2 && -z $3 ]]; then
  73. $MYSQLDUMP -x -R --triggers --master-data=2 $2 |$GZIP >$2.$STARTTIME.sql.gz
  74. RESULT=$?
  75. STATS_NAME="# $MYSQLDUMP -x -R --triggers --master-data=2 $2 |$GZIP >$2.$STARTTIME.sql.gz"
  76. ResultStat
  77. elif [[ -n $2 && -n $3 ]]; then
  78. $MYSQLDUMP -x -R --triggers --master-data=2 $2 $3 |$GZIP >$2.$3.$STARTTIME.sql.gz
  79. RESULT=$?
  80. STATS_NAME="# $MYSQLDUMP -x -R --triggers --master-data=2 $2 $3 |$GZIP >$2.$3.$STARTTIME.sql.gz"
  81. ResultStat
  82. else
  83. UsageError
  84. exit 1
  85. fi
  86. elif [ $1 = '-a' ]; then
  87. CreateDir
  88. for db in $(mysql -e "show databases" | egrep -v "(Database|test|information_schema|performance_schema)")
  89. do
  90. $MYSQLDUMP -x -R --triggers --master-data=2 $db |$GZIP >$db.$STARTTIME.sql.gz
  91. RESULT=$?
  92. STATS_NAME="# $MYSQLDUMP -x -R --triggers --master-data=2 $db |$GZIP >$db.$STARTTIME.sql.gz"
  93. ResultStat
  94. done
  95. elif [ $1 = '-rsyncd' ]; then
  96. PASSWORD_FILE='/usr/local/rsync/rsyncd.password'
  97. RSYNCD_DIR='/usr/local/rsync'
  98. if [ ! -d $RSYNCD_DIR ]; then
  99. $MKDIR -p $RSYNCD_DIR
  100. RESULT=$?
  101. STATS_NAME="# $MKDIR -p $RSYNCD_DIR"
  102. ResultStat
  103. fi
  104. if [ ! -e '/etc/xinetd.d/rsync' ]; then
  105. $TOUCH /etc/xinetd/d/rsync
  106. fi
  107. $MV /etc/xinetd.d/rsync /etc/xinetd.d/rsync.$STARTTIME.bak
  108. cat >/etc/xinetd.d/rsync <<+
  109. service rsync
  110. {
  111. disable = no
  112. socket_type = stream
  113. wait = no
  114. user = root
  115. server = /usr/bin/rsync
  116. server_args = --daemon --config=/etc/rsyncd.conf
  117. log_on_failure += USERID
  118. }
  119. +
  120. RESULT=$?
  121. STATS_NAME="The /etc/xinetd.d/rsync is created."
  122. ResultStat
  123. if [ ! -e '/etc/rsyncd.conf' ]; then
  124. $TOUCH /etc/rsyncd.conf
  125. fi
  126. $MV /etc/rsyncd.conf /etc/rsyncd.conf.$STARTTIME.bak
  127. cat >/etc/rsyncd.conf <<+
  128. uid = root
  129. gid = root
  130. use chroot = no
  131. max connections = 5
  132. lock file = /usr/local/rsync/rsyncd.lock
  133. log file = /usr/local/rsync/rsyncd.log
  134. pid file = /usr/local/rsync/rsyncd.pid
  135. hosts allow = 192.168.250.250
  136. hosts deny = *
  137. ignore errors
  138. read only = yes
  139. list = no
  140. auth users = backupdbuser
  141. secrets file = /usr/local/rsync/rsyncd.password
  142. [BINLOG]
  143. path = $BINLOG_DIR
  144. include = $BINLOG_NAME.*
  145. exclude = *
  146. [DUMPDB]
  147. path = $BACKUP_DIR
  148. +
  149. RESULT=$?
  150. STATS_NAME="The /etc/rsyncd.conf is created."
  151. ResultStat
  152. cat >$PASSWORD_FILE <<+
  153. backupdbuser:vn55JU;bp
  154. +
  155. $CHMOD 600 $PASSWORD_FILE
  156. RESULT=$?
  157. STATS_NAME="The $PASSWORD_FILE is created.
  158. # $CHMOD 600 $PASSWORD_FILE
  159. please start RsyncServer. Refer: /etc/init.d/xinetd reload | restart"
  160. ResultStat
  161. exit 0
  162. else
  163. UsageError
  164. exit 1
  165. fi
  166. #rm 7 days ago
  167. for db in $(mysql -e "show databases" | egrep -v "(Database|test|information_schema|performance_schema)")
  168. do
  169. if [ ! -f $db.$STARTTIME7.sql.gz ]; then
  170. STATS_NAME="$db.$STARTTIME7.sql.gz is not exist..."
  171. ResultStat
  172. else
  173. $RM $db.$STARTTIME7.sql.gz -rf
  174. RESULT=$?
  175. STATS_NAME="# $RM $db.$STARTTIME7.sql.gz -rf..."
  176. ResultStat
  177. fi
  178. done
阅读(3742) | 评论(0) | 转发(1) |
0

上一篇:mysqlslap

下一篇:MySQL升级+MMM部署文档

给主人留下些什么吧!~~