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.

62 lines
1.6 KiB

  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. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  32. AWS_ARGS=""
  33. else
  34. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  35. fi
  36. if [ "${S3_FILENAME}" == "**None**" ]; then
  37. echo "Finding latest backup"
  38. S3_FILENAME=$(aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | sort | tail -n 1 | awk '{ print $4 }')
  39. fi
  40. MYSQL_HOST_OPTS="${MYSQL_OPTS} -h $MYSQL_HOST -P $MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD"
  41. echo "Fetching ${S3_FILENAME} from S3"
  42. aws $AWS_ARGS s3 cp s3://$S3_BUCKET/$S3_PREFIX/${S3_FILENAME} dump.sql.gz
  43. gzip -d dump.sql.gz
  44. echo "Restoring ${S3_FILENAME}"
  45. mysql $MYSQL_HOST_OPTS < dump.sql
  46. echo "MySQL Restore complete"