You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /bin/sh
  2. set -e
  3. if [ "${S3_ACCESS_KEY_ID}" == "**None**" ]; then
  4. echo "Warning: You did not set the S3_ACCESS_KEY_ID environment variable."
  5. fi
  6. if [ "${S3_SECRET_ACCESS_KEY}" == "**None**" ]; then
  7. echo "Warning: You did not set the S3_SECRET_ACCESS_KEY environment variable."
  8. fi
  9. if [ "${S3_BUCKET}" == "**None**" ]; then
  10. echo "You need to set the S3_BUCKET environment variable."
  11. exit 1
  12. fi
  13. if [ "${MYSQL_HOST}" == "**None**" ]; then
  14. echo "You need to set the MYSQL_HOST environment variable."
  15. exit 1
  16. fi
  17. if [ "${MYSQL_USER}" == "**None**" ]; then
  18. echo "You need to set the MYSQL_USER environment variable."
  19. exit 1
  20. fi
  21. if [ "${MYSQL_PASSWORD}" == "**None**" ]; then
  22. echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL."
  23. exit 1
  24. fi
  25. if [ "${S3_IAMROLE}" != "true" ]; then
  26. # env vars needed for aws tools - only if an IAM role is not used
  27. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  28. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  29. export AWS_DEFAULT_REGION=$S3_REGION
  30. fi
  31. MYSQL_HOST_OPTS="-h $MYSQL_HOST -P $MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD"
  32. DUMP_START_TIME=$(date +"%Y-%m-%dT%H%M%SZ")
  33. copy_s3 () {
  34. SRC_FILE=$1
  35. DEST_FILE=$2
  36. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  37. AWS_ARGS=""
  38. else
  39. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  40. fi
  41. echo "Uploading ${DEST_FILE} on S3..."
  42. cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
  43. if [ $? != 0 ]; then
  44. >&2 echo "Error uploading ${DEST_FILE} on S3"
  45. fi
  46. rm $SRC_FILE
  47. }
  48. deleteOld_s3 () {
  49. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  50. AWS_ARGS=""
  51. else
  52. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  53. fi
  54. aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | while read -r line;
  55. do
  56. createDate=`echo $line|awk {'print $1" "$2'}`
  57. createDate=`date -d"$createDate" +%s`
  58. olderThan=`date -d"-${DELETE_OLDER_THAN}" +%s`
  59. if [[ $createDate -lt $olderThan ]]
  60. then
  61. fileName=`echo $line|awk {'print $4'}`
  62. echo $fileName
  63. if [[ $fileName != "" ]]
  64. then
  65. aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
  66. echo "Deleted ${fileName} on S3"
  67. fi
  68. fi
  69. done;
  70. }
  71. shouldDeleteOld_s3() {
  72. if [ $? == 0 ]; then
  73. if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
  74. deleteOld_s3
  75. fi
  76. else
  77. >&2 echo "Error deleting s3 files older than ${DELETE_OLDER_THAN}"
  78. fi
  79. }
  80. # Multi file: yes
  81. if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
  82. if [ "${MYSQLDUMP_DATABASE}" == "--all-databases" ]; then
  83. DATABASES=`mysql $MYSQL_HOST_OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys|innodb)"`
  84. else
  85. DATABASES=$MYSQLDUMP_DATABASE
  86. fi
  87. for DB in $DATABASES; do
  88. echo "Creating individual dump of ${DB} from ${MYSQL_HOST}..."
  89. DUMP_FILE="/tmp/${DB}.sql.gz"
  90. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS --databases $DB | gzip > $DUMP_FILE
  91. if [ $? == 0 ]; then
  92. S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
  93. copy_s3 $DUMP_FILE $S3_FILE
  94. else
  95. >&2 echo "Error creating dump of ${DB}"
  96. fi
  97. shouldDeleteOld_s3
  98. done
  99. # Multi file: no
  100. else
  101. echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..."
  102. DUMP_FILE="/tmp/dump.sql.gz"
  103. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > $DUMP_FILE
  104. if [ $? == 0 ]; then
  105. S3_FILE="${DUMP_START_TIME}.dump.sql.gz"
  106. copy_s3 $DUMP_FILE $S3_FILE
  107. else
  108. >&2 echo "Error creating dump of all databases"
  109. fi
  110. shouldDeleteOld_s3
  111. fi
  112. echo "SQL backup finished"