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.

96 lines
2.4 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
  1. #! /bin/sh
  2. set -e
  3. if [ "${S3_ACCESS_KEY_ID}" == "**None**" ]; then
  4. echo "You need to set the S3_ACCESS_KEY_ID environment variable."
  5. exit 1
  6. fi
  7. if [ "${S3_SECRET_ACCESS_KEY}" == "**None**" ]; then
  8. echo "You need to set the S3_SECRET_ACCESS_KEY environment variable."
  9. exit 1
  10. fi
  11. if [ "${S3_BUCKET}" == "**None**" ]; then
  12. echo "You need to set the S3_BUCKET environment variable."
  13. exit 1
  14. fi
  15. if [ "${MYSQL_HOST}" == "**None**" ]; then
  16. echo "You need to set the MYSQL_HOST environment variable."
  17. exit 1
  18. fi
  19. if [ "${MYSQL_USER}" == "**None**" ]; then
  20. echo "You need to set the MYSQL_USER environment variable."
  21. exit 1
  22. fi
  23. if [ "${MYSQL_PASSWORD}" == "**None**" ]; then
  24. echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL."
  25. exit 1
  26. fi
  27. # env vars needed for aws tools
  28. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  29. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  30. export AWS_DEFAULT_REGION=$S3_REGION
  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. echo "Uploading ${DEST_FILE} on S3..."
  37. cat $SRC_FILE | aws s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
  38. if [ $? != 0 ]; then
  39. >&2 echo "Error uploading ${DEST_FILE} on S3"
  40. fi
  41. rm $SRC_FILE
  42. }
  43. # Multi file: yes
  44. if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
  45. if [ "${MYSQLDUMP_DATABASE}" == "--all-databases" ]; then
  46. DATABASES=`mysql $MYSQL_HOST_OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`
  47. else
  48. DATABASES=$MYSQLDUMP_DATABASE
  49. fi
  50. for DB in $DATABASES; do
  51. echo "Creating individual dump of ${DB} from ${MYSQL_HOST}..."
  52. DUMP_FILE="/tmp/${DB}.sql.gz"
  53. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS --databases $DB | gzip > $DUMP_FILE
  54. if [ $? == 0 ]; then
  55. S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
  56. copy_s3 $DUMP_FILE $S3_FILE
  57. else
  58. >&2 echo "Error creating dump of ${DB}"
  59. fi
  60. done
  61. # Multi file: no
  62. else
  63. echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..."
  64. DUMP_FILE="/tmp/dump.sql.gz"
  65. mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > $DUMP_FILE
  66. if [ $? == 0 ]; then
  67. S3_FILE="${DUMP_START_TIME}.dump.sql.gz"
  68. copy_s3 $DUMP_FILE $S3_FILE
  69. else
  70. >&2 echo "Error creating dump of all databases"
  71. fi
  72. fi
  73. echo "SQL backup finished"