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.

118 lines
2.8 KiB

  1. #! /bin/sh
  2. set -e
  3. set -o pipefail
  4. if [ -n "${POSTGRES_PASSWORD_FILE}" ]; then
  5. POSTGRES_PASSWORD=$(cat "$POSTGRES_PASSWORD_FILE")
  6. export POSTGRES_PASSWORD
  7. fi
  8. if [ -n "${S3_ACCESS_KEY_ID_FILE}" ]; then
  9. S3_ACCESS_KEY_ID=$(cat "$S3_ACCESS_KEY_ID_FILE")
  10. export S3_ACCESS_KEY_ID
  11. fi
  12. if [ -n "${S3_SECRET_ACCESS_KEY_FILE}" ]; then
  13. S3_SECRET_ACCESS_KEY=$(cat "$S3_SECRET_ACCESS_KEY_FILE")
  14. export S3_SECRET_ACCESS_KEY
  15. fi
  16. if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then
  17. echo "You need to set the S3_ACCESS_KEY_ID environment variable."
  18. exit 1
  19. fi
  20. if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then
  21. echo "You need to set the S3_SECRET_ACCESS_KEY environment variable."
  22. exit 1
  23. fi
  24. if [ "${S3_BUCKET}" = "**None**" ]; then
  25. echo "You need to set the S3_BUCKET environment variable."
  26. exit 1
  27. fi
  28. if [ "${POSTGRES_DATABASES}" = "**None**" ]; then
  29. echo "You need to set the POSTGRES_DATABASES environment variable."
  30. exit 1
  31. fi
  32. if [ "${POSTGRES_HOST}" = "**None**" ]; then
  33. if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
  34. POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
  35. POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
  36. else
  37. echo "You need to set the POSTGRES_HOST environment variable."
  38. exit 1
  39. fi
  40. fi
  41. if [ "${POSTGRES_USER}" = "**None**" ]; then
  42. echo "You need to set the POSTGRES_USER environment variable."
  43. exit 1
  44. fi
  45. if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
  46. echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES."
  47. exit 1
  48. fi
  49. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  50. AWS_ARGS=""
  51. else
  52. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  53. fi
  54. # env vars needed for aws tools
  55. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  56. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  57. export AWS_DEFAULT_REGION=$S3_REGION
  58. export PGPASSWORD=$POSTGRES_PASSWORD
  59. copy_s3 () {
  60. SRC_FILE=$1
  61. DEST_FILE=$2
  62. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  63. AWS_ARGS=""
  64. else
  65. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  66. fi
  67. echo "Uploading ${DEST_FILE} to S3..."
  68. cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
  69. if [ $? != 0 ]; then
  70. >&2 echo "Error uploading ${DEST_FILE} on S3"
  71. fi
  72. rm $SRC_FILE
  73. }
  74. POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
  75. DUMP_START_TIME=$(date +"%Y-%m-%dT%H%M%SZ")
  76. for DB in "$POSTGRES_DATABASES"; do
  77. echo "Creating dump of ${DB} from ${POSTGRES_HOST}..."
  78. DUMP_FILE="/tmp/${DB}.sql.gz"
  79. pg_dump $POSTGRES_HOST_OPTS $DB | gzip > $DUMP_FILE
  80. if [ $? == 0 ]; then
  81. if [ "${S3_FILENAME}" == "**None**" ]; then
  82. S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
  83. else
  84. S3_FILE="${S3_FILENAME}.${DB}.sql.gz"
  85. fi
  86. copy_s3 $DUMP_FILE $S3_FILE
  87. else
  88. >&2 echo "Error creating dump of ${DB}"
  89. fi
  90. done
  91. echo "SQL backup uploaded successfully"