Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. export AWS_ACCESS_KEY_ID=MASKED
  4. export AWS_SECRET_ACCESS_KEY=MASKED
  5. TARGET_DIRECTORY='/mnt/backups/'
  6. S3_PATH='s3://aam-backups/mysql/backups'
  7. SNAPSHOT_MOUNT='/mnt/snapshot/'
  8. SNAPSHOT_VOLUME='vol-4e0484ea'
  9. WEEK=week`date +%V-%Y`
  10. MYSQL_USER=dbadmin
  11. MYSQL_PASS='MASKED'
  12. INNOBACKUP_COMMAND="innobackupex --user=$MYSQL_USER --pass=$MYSQL_PASS"
  13. LOG="echo \`date '+[%m/%d/%Y %H:%M:%S]'\`"
  14. LOG_FILE=$TARGET_DIRECTORY/simple_mysql_backup.log
  15. COUNT=0
  16. CURRENT_BACKUP_DIR=$(find $TARGET_DIRECTORY |grep week |head -1)
  17. FULL_BACKUP_DIR=$(grep -l "incremental = N" $TARGET_DIRECTORY*/*/xtrabackup_info |awk -F'xtrabackup_info' {'print $1'})
  18.  
  19. exec 2>> $TARGET_DIRECTORY/backup.log >> $TARGET_DIRECTORY/backup_error.log
  20.  
  21. s3_sync()
  22. {
  23. if [ -d $CURRENT_BACKUP_DIR ]; then
  24. eval $LOG "Syncing last week to s3" >> $LOG_FILE
  25. aws s3 sync $CURRENT_BACKUP_DIR $S3_PATH/`basename $CURRENT_BACKUP_DIR`
  26. S3_STATUS=$?
  27. eval $LOG "s3sync exited with status $S3_STATUS" >> $LOG_FILE
  28. else
  29. S3_STATUS=0
  30. fi
  31. if [ $S3_STATUS -eq 0 ]; then
  32. create_snapshot
  33. eval $LOG "Deleting last week\'s backup" >> $LOG_FILE
  34. rm -rf $TARGET_DIRECTORY/week*
  35. else
  36. eval $LOG "s3sync failed, trying again after $COUNT tries" >> $LOG_FILE
  37. if [ $COUNT -gt 2 ]; then
  38. eval $LOG "s3sync tries exhausted, giving up" >> $LOG_FILE
  39. mail -s 'mysql backup failed!!!!' busnatu@adobe.com
  40. fi
  41. COUNT=$((COUNT+1))
  42. s3_sync
  43. fi
  44. }
  45.  
  46. create_snapshot()
  47. {
  48. rm -rf $SNAPSHOT_MOUNT*
  49. cd $FULL_BACKUP_DIR
  50. eval $LOG `pwd` >> $LOG_FILE
  51. eval $LOG "copying mysql data from `pwd` to snapshot mount" >> $LOG_FILE
  52. cp -R ./* $SNAPSHOT_MOUNT
  53. $INNOBACKUP_COMMAND --apply-log --use-memory=2g $SNAPSHOT_MOUNT
  54. eval $LOG "copy finished, creating ebs snapshot" >> $LOG_FILE
  55. RESPONSE=`aws ec2 create-snapshot --volume-id $SNAPSHOT_VOLUME --description "MySQL production snapshot for $WEEK" --region=us-east-1`
  56. eval $LOG $RESPONSE >> $LOG_FILE
  57. SNAPSHOT_ID=`$RESPONSE |grep -o snap-........`
  58. aws ec2 create-tags --resources $SNAPSHOT_ID --tags Key=type,Value=mysqlvolume Key=date,Value=`basename $CURRENT_BACKUP_DIR` --region=us-east-1
  59. aws ec2 modify-snapshot-attribute --snapshot-id $SNAPSHOT_ID --attribute createVolumePermission --operation-type add --user-ids 317085423413 --region=us-east-1
  60. }
  61.  
  62.  
  63. backup()
  64. {
  65. FULL_BACKUP_COUNT=$(grep "incremental = N" $TARGET_DIRECTORY*/*/xtrabackup_info |wc -l)
  66. INCREMENTAL_COUNT=$(grep "incremental = Y" $TARGET_DIRECTORY*/*/xtrabackup_info |wc -l)
  67. eval $LOG "Starting mysql backup" >> $LOG_FILE
  68. if [ $INCREMENTAL_COUNT -gt 5 ]; then
  69. eval $LOG "Incremental count exceeded, moving to s3, snapshot, and deleting" >> $LOG_FILE
  70. s3_sync
  71. backup
  72. elif [ $FULL_BACKUP_COUNT -gt 1 ]; then
  73. eval $LOG "More than one full backup found, starting over" >> $LOG_FILE
  74. rm -rf $TARGET_DIRECTORY/week*
  75. backup
  76. elif [ $FULL_BACKUP_COUNT -eq 1 ]; then
  77. eval $LOG "Full backup exists, performing incremental backup" >> $LOG_FILE
  78. $INNOBACKUP_COMMAND --slave-info --incremental $FULL_BACKUP_DIR/..
  79. if [ $? -eq 0 ]; then
  80. eval $LOG "Backup completed successfully" >> $LOG_FILE
  81. else
  82. eval $LOG "Backup failed, check logs" >> $LOG_FILE
  83. fi
  84. else
  85. eval $LOG "No backups found, performing full backup" >> $LOG_FILE
  86. mkdir -p $TARGET_DIRECTORY$WEEK
  87. $INNOBACKUP_COMMAND --slave-info $TARGET_DIRECTORY$WEEK
  88. if [ $? -eq 0 ]; then
  89. eval $LOG "Backup completed successfully" >> $LOG_FILE
  90. else
  91. mail -s 'mysql backup failed!!!!' honig@adobe.com
  92. eval $LOG "Backup failed, check logs" >> $LOG_FILE
  93. fi
  94. fi
  95. }
  96.  
  97. backup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement