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.

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