Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # @auther <james@lightbeijing.com>
  3. set -x
  4. shopt -s extglob
  5.  
  6. BACKUP_HOST=''
  7. BACKUP_USER=''
  8. BACKUP_PASSWORD=''
  9. BACKUP_PORT=3306
  10. MAIL_ADDR=('james@lightbeijing.com')
  11. # Back up the root directory
  12. BACKUP_DIR='/data/mysql_data/master_mysql-3306/data/db'
  13. # Backup the database list, all for the entire library export
  14. BACKUP_DATABASES=('all' 'dev')
  15. # Sql backup level d(every day) w(every week) m(every month)
  16. BACKUP_LEVEL='d'
  17. # Daily days of sql retention days
  18. BACKUP_RESERVED_D=7
  19. # Monthly backup of sql retention days
  20. BACKUP_RESERVED_W=30
  21. # The number of days to keep the sql
  22. BACKUP_RESERVED_M=360
  23. # Backup file prefix
  24. BACKUP_FILE_PREFIX='backupsql_'
  25. # he current year and the next day
  26. CURRENT_TIME=$(date +%Y%m%d-%H%M%S)
  27. # The timestamp of the current date
  28. TIMESTAMP=$(date -d $(date +"%Y-%m-%d") +%s)
  29.  
  30. # One day timestamp
  31. let DAY_TIMESTAMP=60*60*24
  32. # One week timestamp
  33. let WEEK_TIMESTAMP=${DAY_TIMESTAMP}*7
  34. # One month timestamp
  35. let MONTH_TIMESTAMP=${WEEK_TIMESTAMP}*4
  36. # Delete the timestamp of the daily backup
  37. let RESERVED_D_TIMESTAMP=${TIMESTAMP}-${BACKUP_RESERVED_D}*${DAY_TIMESTAMP}
  38. # Delete the timestamp of the monthly backup
  39. let RESERVED_W_TIMESTAMP=${TIMESTAMP}-${BACKUP_RESERVED_W}*${DAY_TIMESTAMP}
  40. # Delete the timestamp of the annual backup
  41. let RESERVED_M_TIMESTAMP=${TIMESTAMP}-${BACKUP_RESERVED_M}*${DAY_TIMESTAMP}
  42.  
  43. # Create the root directory
  44. if [ ! -d $BACKUP_DIR ]
  45. then
  46. echo "info: mkdir -p $BACKUP_DIR"
  47. mkdir -p $BACKUP_DIR
  48. fi
  49.  
  50. # Create a directory based on the root directory based on the parameters
  51. _mkBackupDir ()
  52. {
  53. BACKUP_MKDIR="${BACKUP_DIR}/$1"
  54. if [ ! -d $BACKUP_MKDIR ]
  55. then
  56. echo "info: mkdir -p ${BACKUP_MKDIR}"
  57. mkdir -p $BACKUP_MKDIR
  58. fi
  59. return 0
  60. }
  61.  
  62. # The initial time of the embedded operation
  63. _get_TIMESTAMP_EMBEDDING ()
  64. {
  65. if [ ! -e ${BACKUP_TMP_DIR}/timestampembedding ]
  66. then
  67. echo "info: Timestamp embedding"
  68. echo $TIMESTAMP > ${BACKUP_TMP_DIR}/timestampembedding
  69. fi
  70. TIMESTAMP_EMBEDDING=$(cat ${BACKUP_TMP_DIR}/timestampembedding)
  71. }
  72.  
  73. # Use mysqldump and gzip to package backup sql files
  74. _dumpsql ()
  75. {
  76. _mkBackupDir $1/$2 && BACKUP_DATABASE_DIR=$BACKUP_MKDIR
  77.  
  78. BACKUP_FILE_SQL=${BACKUP_FILE_PREFIX}$1_$2_${CURRENT_TIME}_${TIMESTAMP}.sql.tar.gz
  79.  
  80. if [[ $2 == 'all' ]]
  81. then
  82. local DATABASE_NAME='--all-databases'
  83. else
  84. local DATABASE_NAME=$2
  85. fi
  86. echo "Package export sql file : ${BACKUP_DATABASE_DIR}/${BACKUP_FILE_SQL}" >> $LOG_FILE
  87. mysqldump -u${BACKUP_USER} -h${BACKUP_HOST} -P${BACKUP_PORT} -p${BACKUP_PASSWORD} ${DATABASE_NAME} | gzip > ${BACKUP_DATABASE_DIR}/${BACKUP_FILE_SQL}
  88.  
  89. return 0
  90. }
  91.  
  92. # Get the timestamp of the file
  93. _getFileTimestamp ()
  94. {
  95. local filefead=${1##*_}
  96. filetimestamp=${filefead%%.*}
  97. }
  98.  
  99. # Get the maximum timestamp in the file in the directory, and clear the expired file
  100. _getMaxTimestampAndClearOldFile ()
  101. {
  102. local files=$(ls $1)
  103. _getFileTimestamp ${files['0']} && maxtimestamp=${filetimestamp}
  104. case $2 in
  105. 'd')
  106. local RESERVED_TIMESTAMP=$RESERVED_D_TIMESTAMP
  107. ;;
  108. 'w')
  109. local RESERVED_TIMESTAMP=$RESERVED_W_TIMESTAMP
  110. ;;
  111. 'm')
  112. local RESERVED_TIMESTAMP=$RESERVED_M_TIMESTAMP
  113. ;;
  114. esac
  115.  
  116. for file in $files
  117. do
  118. _getFileTimestamp $file
  119. if [[ $filetimestamp > $maxtimestamp ]]
  120. then
  121. maxtimestamp=$filetimestamp
  122. fi
  123. if [[ $filetimestamp < $RESERVED_TIMESTAMP ]]
  124. then
  125. echo "info: rm -rf ${file}"
  126. rm -rf ${file}
  127. fi
  128. done
  129.  
  130. return 0
  131. }
  132.  
  133. _init ()
  134. {
  135. for database in ${BACKUP_DATABASES[@]}
  136. do
  137. echo " " >> $LOG_FILE
  138. echo "${database}:" >> $LOG_FILE
  139. echo "--------------------------------------------------------------" >> $LOG_FILE
  140.  
  141. if [[ $BACKUP_LEVEL == 'd' ]]
  142. then
  143. echo "Every Day :" >> $LOG_FILE
  144. _getMaxTimestampAndClearOldFile ${BACKUP_DAY_DIR}/$database d
  145. _dumpsql day $database
  146. fi
  147.  
  148. let DIFF_TIMESTAMP=${TIMESTAMP}-${TIMESTAMP_EMBEDDING}
  149. let DIFF_WEEK=${DIFF_TIMESTAMP}/${WEEK_TIMESTAMP}
  150. let DIFF_MONTH=${DIFF_TIMESTAMP}/${MONTH_TIMESTAMP}
  151.  
  152. if [[ $DIFF_TIMESTAMP == 0 ]]
  153. then
  154. if [[ ! -d ${BACKUP_WEEK_DIR}/$database ]]
  155. then
  156. echo "Every Week :" >> $LOG_FILE
  157. _dumpsql week $database
  158. fi
  159.  
  160. if [[ ! -d ${BACKUP_MONTH_DIR}/$database ]]
  161. then
  162. echo "Every Month :" >> $LOG_FILE
  163. _dumpsql month $database
  164. fi
  165. fi
  166.  
  167. if [[ $DIFF_WEEK != 0 && $BACKUP_LEVEL != 'm' ]]
  168. then
  169. _getMaxTimestampAndClearOldFile ${BACKUP_WEEK_DIR}/$database w
  170. let MAX_DIFF_TIMESTAMP=${maxtimestamp}-${TIMESTAMP_EMBEDDING}
  171. let MAX_DIFF_WEEK=${MAX_DIFF_TIMESTAMP}/${WEEK_TIMESTAMP}
  172. if [[ $DIFF_WEEK > $MAX_DIFF_WEEK ]]
  173. then
  174. echo "Every Week :" >> $LOG_FILE
  175. _dumpsql week $database
  176. fi
  177. fi
  178.  
  179. if [[ $DIFF_MONTH != 0 ]]
  180. then
  181. _getMaxTimestampAndClearOldFile ${BACKUP_MONTH_DIR}/$database m
  182. let MAX_DIFF_TIMESTAMP=${maxtimestamp}-${TIMESTAMP_EMBEDDING}
  183. let MAX_DIFF_MONTH=${MAX_DIFF_TIMESTAMP}/${MONTH_TIMESTAMP}
  184. if [[ $DIFF_MONTH > $MAX_DIFF_MONTH ]]
  185. then
  186. echo "Every Month :" >> $LOG_FILE
  187. _dumpsql month $database
  188. fi
  189. fi
  190. done
  191.  
  192. return 0
  193. }
  194.  
  195. # Get the tmp path
  196. _mkBackupDir tmp && BACKUP_TMP_DIR=${BACKUP_MKDIR}
  197. # Get the initial buried time point
  198. _get_TIMESTAMP_EMBEDDING
  199.  
  200. LOG_FILE=${BACKUP_TMP_DIR}/${BACKUP_FILE_PREFIX}_${TIMESTAMP}.log
  201. echo "为IP是${BACKUP_HOST}端口为${BACKUP_PORT}的mysql服务器进行备份" >> $LOG_FILE
  202.  
  203. _mkBackupDir day && BACKUP_DAY_DIR=${BACKUP_MKDIR}
  204. _mkBackupDir week && BACKUP_WEEK_DIR=${BACKUP_MKDIR}
  205. _mkBackupDir month && BACKUP_MONTH_DIR=${BACKUP_MKDIR}
  206.  
  207. # Start the backup
  208. _init
  209.  
  210. for email in $MAIL_ADDR
  211. do
  212. cat $LOG_FILE | mutt -s 'MySQL Backup Log and SQL Files for '`hostname`" - `date`" $email
  213. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement