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.

87 lines
2.7 KiB

5 years ago
5 years ago
  1. #! /bin/sh
  2. set -e
  3. set -x
  4. set -o pipefail
  5. if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then
  6. echo "You need to set the S3_ACCESS_KEY_ID environment variable."
  7. exit 1
  8. fi
  9. if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then
  10. echo "You need to set the S3_SECRET_ACCESS_KEY environment variable."
  11. exit 1
  12. fi
  13. if [ "${S3_BUCKET}" = "**None**" ]; then
  14. echo "You need to set the S3_BUCKET environment variable."
  15. exit 1
  16. fi
  17. if [ "${POSTGRES_DATABASE}" = "**None**" ]; then
  18. echo "You need to set the POSTGRES_DATABASE environment variable."
  19. exit 1
  20. fi
  21. if [ "${POSTGRES_HOST}" = "**None**" ]; then
  22. if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
  23. POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
  24. POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
  25. else
  26. echo "You need to set the POSTGRES_HOST environment variable."
  27. exit 1
  28. fi
  29. fi
  30. if [ "${POSTGRES_USER}" = "**None**" ]; then
  31. echo "You need to set the POSTGRES_USER environment variable."
  32. exit 1
  33. fi
  34. if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
  35. echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES."
  36. exit 1
  37. fi
  38. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  39. AWS_ARGS=""
  40. else
  41. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  42. fi
  43. # env vars needed for aws tools
  44. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  45. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  46. export AWS_DEFAULT_REGION=$S3_REGION
  47. export PGPASSWORD=$POSTGRES_PASSWORD
  48. POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER"
  49. echo "Finding latest backup"
  50. LATEST_BACKUP=$(aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | sort | tail -n 1 | awk '{ print $4 }')
  51. echo "Fetching ${LATEST_BACKUP} from S3"
  52. aws $AWS_ARGS s3 cp s3://$S3_BUCKET/$S3_PREFIX/${LATEST_BACKUP} dump.sql.gz
  53. gzip -d dump.sql.gz
  54. if [ "${DROP_PUBLIC}" == "yes" ]; then
  55. echo "Recreating the public schema"
  56. psql $POSTGRES_HOST_OPTS -d "$POSTGRES_DATABASE" -c "drop schema public cascade; create schema public;"
  57. fi
  58. echo "Restoring ${LATEST_BACKUP} to ${POSTGRES_DATABASE} on ${POSTGRES_HOST}:${POSTGRES_PORT} with user ${POSTGRES_USER}"
  59. set +e
  60. psql $POSTGRES_HOST_OPTS -d postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$POSTGRES_DATABASE' AND pid <> pg_backend_pid();"
  61. psql $POSTGRES_HOST_OPTS -d postgres -c "DROP DATABASE IF EXISTS \"$POSTGRES_DATABASE\";"
  62. psql $POSTGRES_HOST_OPTS -d postgres -c "CREATE DATABASE \"$POSTGRES_DATABASE\";"
  63. psql $POSTGRES_HOST_OPTS -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE \"$POSTGRES_DATABASE\" TO $POSTGRES_USER;"
  64. set -e
  65. pg_restore $POSTGRES_HOST_OPTS --no-owner --single-transaction -n public --no-privileges -v -d "$POSTGRES_DATABASE" dump.sql
  66. echo "Restore complete"