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.

148 lines
4.0 KiB

  1. #! /bin/sh
  2. set -eo pipefail
  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**" -a "${POSTGRES_BACKUP_ALL}" != "true" ]; 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. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  37. AWS_ARGS=""
  38. else
  39. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  40. fi
  41. # env vars needed for aws tools
  42. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  43. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  44. export AWS_DEFAULT_REGION=$S3_REGION
  45. export PGPASSWORD=$POSTGRES_PASSWORD
  46. POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
  47. if [ -z ${S3_PREFIX+x} ]; then
  48. S3_PREFIX="/"
  49. else
  50. S3_PREFIX="/${S3_PREFIX}/"
  51. fi
  52. if [ "${POSTGRES_BACKUP_ALL}" == "true" ]; then
  53. SRC_FILE=dump.sql.gz
  54. DEST_FILE=all_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
  55. if [ "${S3_FILE_NAME}" != "**None**" ]; then
  56. DEST_FILE=${S3_FILE_NAME}.sql.gz
  57. fi
  58. echo "Creating dump of all databases from ${POSTGRES_HOST}..."
  59. pg_dumpall $POSTGRES_HOST_OPTS | gzip > $SRC_FILE
  60. if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
  61. echo "Encrypting ${SRC_FILE}"
  62. openssl enc -aes-256-cbc -in $SRC_FILE -out ${SRC_FILE}.enc -k $ENCRYPTION_PASSWORD
  63. if [ $? != 0 ]; then
  64. >&2 echo "Error encrypting ${SRC_FILE}"
  65. fi
  66. rm $SRC_FILE
  67. SRC_FILE="${SRC_FILE}.enc"
  68. DEST_FILE="${DEST_FILE}.enc"
  69. fi
  70. echo "Uploading dump to $S3_BUCKET"
  71. cat $SRC_FILE | aws $AWS_ARGS s3 cp - "s3://${S3_BUCKET}${S3_PREFIX}${DEST_FILE}" || exit 2
  72. echo "SQL backup uploaded successfully"
  73. rm -rf $SRC_FILE
  74. else
  75. if [ "${POSTGRES_FORMAT}" = "**None**"]; then
  76. POSTGRES_FORMAT="p"
  77. fi
  78. case "$POSTGRES_FORMAT" in
  79. (p|c|t) ;;
  80. (d) echo "Directory format not supported, only single-file formats are supported: p, c, t" >&2; exit 1 ;;
  81. (*) echo "Unknown pg_dump format '$POSTGRES_FORMAT'. Please use one within: p, c, d, t" >&2; exit 1 ;;
  82. esac
  83. POSTGRES_HOST_OPTS+=" -F${POSTGRES_FORMAT}"
  84. FILE_EXT="sql.gz"
  85. case "$POSTGRES_FORMAT" in
  86. (c) FILE_EXT="dump";;
  87. (t) FILE_EXT="tar";;
  88. esac
  89. OIFS="$IFS"
  90. IFS=','
  91. for DB in $POSTGRES_DATABASE
  92. do
  93. IFS="$OIFS"
  94. SRC_FILE="dump.${FILE_EXT}"
  95. DEST_FILE="${DB}_$(date +"%Y-%m-%dT%H:%M:%SZ").${FILE_EXT}"
  96. if [ "${S3_FILE_NAME}" != "**None**" ]; then
  97. DEST_FILE="${S3_FILE_NAME}_${DB}.${FILE_EXT}"
  98. fi
  99. echo "Creating dump of ${DB} database from ${POSTGRES_HOST}..."
  100. if [ "$POSTGRES_FORMAT" = "p" ]; then
  101. pg_dump $POSTGRES_HOST_OPTS $DB | gzip > ${SRC_FILE}
  102. else
  103. pg_dump $POSTGRES_HOST_OPTS $DB > ${SRC_FILE}
  104. fi
  105. if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
  106. echo "Encrypting ${SRC_FILE}"
  107. openssl enc -aes-256-cbc -in $SRC_FILE -out ${SRC_FILE}.enc -k $ENCRYPTION_PASSWORD
  108. if [ $? != 0 ]; then
  109. >&2 echo "Error encrypting ${SRC_FILE}"
  110. fi
  111. rm $SRC_FILE
  112. SRC_FILE="${SRC_FILE}.enc"
  113. DEST_FILE="${DEST_FILE}.enc"
  114. fi
  115. echo "Uploading dump to $S3_BUCKET"
  116. cat $SRC_FILE | aws $AWS_ARGS s3 cp - "s3://${S3_BUCKET}${S3_PREFIX}${DEST_FILE}" || exit 2
  117. echo "SQL backup uploaded successfully"
  118. rm -rf $SRC_FILE
  119. done
  120. fi