Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Configuration
  4. . /etc/mysql/mysql_backup.conf
  5.  
  6. # Dump the mysql databases to a daily dump file.
  7. function log()
  8. {
  9. logger -i -p daemon.info -t mysqldump "$1"
  10. echo $1 >> $LOGFILE
  11.  
  12. if [ -n "$2" ] ;then
  13. echo $1 | $HIPCHATALERT
  14. fi
  15. }
  16.  
  17. log "starting backup on `hostname -f`" hipchat
  18.  
  19. # Test for lock file.
  20. if [ -e ${LOCKFILE} ] ;then
  21. log "Lockfile ${LOCKFILE} exists, skipping backup on `hostname -f`" hipchat
  22. # Mail reports
  23. mail -s "hetzner backup02 skipped for $(date): lockfile ${LOCKFILE} found" $ALERT_EMAILS < $LOGFILE
  24. exit 1
  25. fi
  26.  
  27. # Write pid to lockfile.
  28. echo $$ > ${LOCKFILE}
  29.  
  30. ## BINARY BACKUPS FIRST
  31.  
  32. log "starting binary backups for `hostname -f`"
  33.  
  34. # Check if the highest numbered backup exist and delete it
  35. if [ -e "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${KEEP_BINARY_BACKUPS}.tar.gz" ]; then
  36. log "$(date): Expire oldest daily backup: ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${KEEP_BINARY_BACKUPS}.tar.gz"
  37. /bin/rm -rf ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${KEEP_BINARY_BACKUPS}.tar.gz >> $LOGFILE
  38. fi
  39.  
  40. # Rename all backups to N+1
  41. for i in $(seq ${KEEP_BINARY_BACKUPS} -1 0); do
  42. if [ -e "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${i}.tar.gz" ]; then
  43. new=$((${i} + 1))
  44. log "$(date): Rename backup ${i} to ${new}"
  45. /bin/mv -f "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${i}.tar.gz" "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.${new}.tar.gz" >> $LOGFILE
  46. fi
  47. done
  48.  
  49. # Create new full binary backup
  50. log "$(date): Creating full backup in ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0"
  51. if [ -d "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0" ]; then
  52. /bin/rm -rf ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0
  53. fi
  54. mkdir -p ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0
  55. $BACKUP_BIN "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0" --user=${MYSQL_USER} --password=${MYSQL_PASS} --use-memory=12GB --slave-info --safe-slave-backup >> $LOGFILE
  56. #touch ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0
  57.  
  58. # # Tar it up
  59. log "$(date): Creating archive of daily backup in ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0.tar.gz"
  60. /bin/tar -zcf "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0.tar.gz" "${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0" >> $LOGFILE
  61.  
  62. ## MYSQLDUMP BACKUPS NEXT
  63.  
  64. log "Dump to .sql files"
  65.  
  66. # Check if the highest numbered backup exist and delete it
  67. if [ -d "${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${KEEP_DUMP_BACKUPS}" ]; then
  68. log "$(date): Expire oldest daily backup: ${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${KEEP_DUMP_BACKUPS}"
  69. /bin/rm -rf ${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${KEEP_DUMP_BACKUPS} >> $LOGFILE
  70. fi
  71.  
  72. # Rename all backups to N+1
  73. for i in $(seq ${KEEP_DUMP_BACKUPS} -1 0); do
  74. if [ -d "${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${i}" ]; then
  75. new=$((${i} + 1))
  76. log "$(date): Rename backup ${i} to ${new}"
  77. /bin/mv -f "${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${i}" "${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.${new}" >> $LOGFILE
  78. fi
  79. done
  80.  
  81. # Create new full dump backup
  82. log "$(date): Creating full dump backup in ${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.0"
  83.  
  84. DUMP_DIR="${BACKUP_DIR}/${DUMP_BACKUP_BASE_NAME}.0"
  85.  
  86. if [ -d "${DUMP_DIR}" ]; then
  87. /bin/rm -rf ${DUMP_DIR}
  88. fi
  89. mkdir -p ${DUMP_DIR}
  90.  
  91. # Get a re-useable list of databases.
  92. DATABASES="$(mysql -u ${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} -B -s -e 'show databases')"
  93.  
  94. # Dump mysql tables.
  95. #
  96. for db in ${DATABASES}; do
  97. if [ "z${db}" != "zinformation_schema" ]; then
  98. log "Dumping database '${db}'...";
  99. /usr/bin/ionice -c3 /usr/bin/mysqldump -u ${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} --single-transaction --triggers --quick --routines --master-data=2 --result-file "${DUMP_DIR}/${db}.sql" --databases "${db}";
  100. log " ok."
  101.  
  102. log "Dumping database definitions '${db}'...";
  103. /usr/bin/ionice -c3 /usr/bin/mysqldump -u ${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} --no-data --single-transaction --triggers --quick --routines --master-data=2 --result-file "${DUMP_DIR}/${db}.frm.sql" --databases "${db}";
  104. log " ok."
  105. fi
  106. done
  107.  
  108.  
  109. # # Tar it up
  110. log "$(date): Creating archive of daily backup in ${DUMP_DIR}"
  111.  
  112. # Do a compression run.
  113. # Note: do not compress the archive as a whole so it is easier to retrieve individual backups
  114. for db in ${DATABASES}; do
  115. # If the database dump exists, compress it.
  116. #
  117. log "Compressing ${db}.sql";
  118. if [ -f "${DUMP_DIR}/${db}.sql" ]; then
  119. /usr/bin/ionice -c3 /bin/tar -zcf "${DUMP_DIR}/${db}.sql.tar.gz" "${DUMP_DIR}/${db}.sql";
  120. /bin/rm -rf "${DUMP_DIR}/${db}.sql";
  121. fi
  122. log " ${db}.frm.sql";
  123. if [ -f "${DUMP_DIR}/${db}.frm.sql" ]; then
  124. /usr/bin/ionice -c3 /bin/tar -zcf "${DUMP_DIR}/${db}.frm.sql.tar.gz" "${DUMP_DIR}/${db}.frm.sql";
  125. /bin/rm -rf "${DUMP_DIR}/${db}.frm.sql";
  126. fi
  127. log " ok."
  128. done
  129.  
  130.  
  131.  
  132. # reset variables
  133. log "$(date): Cleaning up"
  134. log "$(date): Backup size: `du -sh ${BACKUP_DIR}/${BINARY_BACKUP_BASE_NAME}.0`" hipchat
  135. log "$(date): Backup size: `du -sh ${DUMP_DIR}`" hipchat
  136. log "$(date): Free disk space: `df -h`" hipchat
  137. log "$(date): MySQL Slave Status: `/usr/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} -e 'show slave status\G'`" hipchat
  138.  
  139. # Remove lockfile.
  140. /bin/rm -f ${LOCKFILE}
  141.  
  142. # Mail reports
  143. mail -s "hetzner backup02 report for $(date)" $ALERT_EMAILS < $LOGFILE
  144. log "backup complete for $(date) on `hostname -f`. Log: $LOGFILE" hipchat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement