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.

119 lines
3.2 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
10 years ago
  1. #!/bin/sh
  2. set -eo pipefail
  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. if [ "${S3_ENSURE_BUCKET_EXISTS}" != "no" ]; then
  42. echo "Ensuring S3 bucket $S3_BUCKET exists"
  43. EXISTS_ERR=`aws $AWS_ARGS s3api head-bucket --bucket "$S3_BUCKET" 2>&1 || true`
  44. if [[ ! -z "$EXISTS_ERR" ]]; then
  45. echo "Bucket $S3_BUCKET not found (or owned by someone else), attempting to create"
  46. aws $AWS_ARGS s3api create-bucket --bucket $S3_BUCKET
  47. fi
  48. fi
  49. echo "Uploading ${DEST_FILE} on S3..."
  50. cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
  51. if [ $? != 0 ]; then
  52. >&2 echo "Error uploading ${DEST_FILE} on S3"
  53. fi
  54. rm $SRC_FILE
  55. }
  56. # Multi file: yes
  57. if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
  58. if [ "${MYSQLDUMP_DATABASE}" == "--all-databases" ]; then
  59. DATABASES=`mysql $MYSQL_HOST_OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys|innodb)"`
  60. else
  61. DATABASES=$MYSQLDUMP_DATABASE
  62. fi
  63. for DB in $DATABASES; do
  64. echo "Creating individual dump of ${DB} from ${MYSQL_HOST}..."
  65. DUMP_FILE="/tmp/${DB}.sql.gz"
  66. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS --databases $DB | gzip > $DUMP_FILE
  67. if [ $? == 0 ]; then
  68. if [ "${S3_FILENAME}" == "**None**" ]; then
  69. S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
  70. else
  71. S3_FILE="${S3_FILENAME}.${DB}.sql.gz"
  72. fi
  73. copy_s3 $DUMP_FILE $S3_FILE
  74. else
  75. >&2 echo "Error creating dump of ${DB}"
  76. fi
  77. done
  78. # Multi file: no
  79. else
  80. echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..."
  81. DUMP_FILE="/tmp/dump.sql.gz"
  82. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > $DUMP_FILE
  83. if [ $? == 0 ]; then
  84. if [ "${S3_FILENAME}" == "**None**" ]; then
  85. S3_FILE="${DUMP_START_TIME}.dump.sql.gz"
  86. else
  87. S3_FILE="${S3_FILENAME}.sql.gz"
  88. fi
  89. copy_s3 $DUMP_FILE $S3_FILE
  90. else
  91. >&2 echo "Error creating dump of all databases"
  92. fi
  93. fi
  94. echo "SQL backup finished"