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.

61 lines
1.7 KiB

  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 [ "${POSTGRES_DATABASE}" = "**None**" ]; then
  16. echo "You need to set the POSTGRES_DATABASE environment variable."
  17. exit 1
  18. fi
  19. if [ "${POSTGRES_HOST}" = "**None**" ]; then
  20. if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
  21. POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
  22. POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
  23. else
  24. echo "You need to set the POSTGRES_HOST environment variable."
  25. exit 1
  26. fi
  27. fi
  28. if [ "${POSTGRES_USER}" = "**None**" ]; then
  29. echo "You need to set the POSTGRES_USER environment variable."
  30. exit 1
  31. fi
  32. if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
  33. echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES."
  34. exit 1
  35. fi
  36. # env vars needed for aws tools
  37. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  38. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  39. export AWS_DEFAULT_REGION=$S3_REGION
  40. export PGPASSWORD=$POSTGRES_PASSWORD
  41. POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER"
  42. echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
  43. pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | gzip > dump.sql.gz
  44. echo "Uploading dump to $S3_BUCKET"
  45. cat dump.sql.gz | aws s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz || exit 2
  46. echo "SQL backup uploaded successfully"