Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom options to pg_dumpall and custom formats #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions postgres-backup-s3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ENV POSTGRES_PORT 5432
ENV POSTGRES_USER **None**
ENV POSTGRES_PASSWORD **None**
ENV POSTGRES_EXTRA_OPTS ''
ENV POSTGRES_FORMAT **None**
ENV S3_ACCESS_KEY_ID **None**
ENV S3_SECRET_ACCESS_KEY **None**
ENV S3_BUCKET **None**
Expand Down
7 changes: 4 additions & 3 deletions postgres-backup-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pgbackups3:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_EXTRA_OPTS: '--schema=public --blobs'
POSTGRES_FORMAT: 'c'
```

### Automatic Periodic Backups
Expand All @@ -45,9 +46,9 @@ You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="
More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).

### Backup File Name / Path
By default, if `POSTGRES_BACKUP_ALL` is true, the dump file will be put at `<S3_PREFIX=''>/all_<timestamp>.sql.gz`. When using `POSTGRES_DATABASE`, each database listed will be backed up to the object path `<S3_PREFIX=''>/<database>_<timestamp>.sql.gz`.
By default, if `POSTGRES_BACKUP_ALL` is true, the dump file will be put at `<S3_PREFIX=''>/all_<timestamp>.sql.gz`. When using `POSTGRES_DATABASE`, each database listed will be backed up to the object path `<S3_PREFIX=''>/<database>_<timestamp>.<extension>`.

If you wish to make these filenames static, you can use the `S3_FILE_NAME` variable, which will change these formats to `<S3_PREFIX=''>/<S3_FILE_NAME>.sql.gz` or `<S3_PREFIX=''>/<S3_FILE_NAME>_<database>.sql.gz` accordingly.
If you wish to make these filenames static, you can use the `S3_FILE_NAME` variable, which will change these formats to `<S3_PREFIX=''>/<S3_FILE_NAME>.<extension>` or `<S3_PREFIX=''>/<S3_FILE_NAME>_<database>.<extension>` accordingly.

### Backup All Databases

Expand All @@ -65,4 +66,4 @@ You can specify an alternate endpoint by setting `S3_ENDPOINT` environment varia

### Encryption

You can additionally set the `ENCRYPTION_PASSWORD` environment variable like `-e ENCRYPTION_PASSWORD="superstrongpassword"` to encrypt the backup. It can be decrypted using `openssl aes-256-cbc -d -in backup.sql.gz.enc -out backup.sql.gz`.
You can additionally set the `ENCRYPTION_PASSWORD` environment variable like `-e ENCRYPTION_PASSWORD="superstrongpassword"` to encrypt the backup. It can be decrypted using `openssl aes-256-cbc -d -in backup.sql.gz.enc -out backup.sql.gz`.
38 changes: 30 additions & 8 deletions postgres-backup-s3/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ fi
if [ "${POSTGRES_BACKUP_ALL}" == "true" ]; then
SRC_FILE=dump.sql.gz
DEST_FILE=all_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz

if [ "${S3_FILE_NAME}" != "**None**" ]; then
DEST_FILE=${S3_FILE_NAME}.sql.gz
fi

echo "Creating dump of all databases from ${POSTGRES_HOST}..."
pg_dumpall -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER | gzip > $SRC_FILE
pg_dumpall $POSTGRES_HOST_OPTS | gzip > $SRC_FILE

if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
echo "Encrypting ${SRC_FILE}"
Expand All @@ -90,22 +90,44 @@ if [ "${POSTGRES_BACKUP_ALL}" == "true" ]; then
echo "SQL backup uploaded successfully"
rm -rf $SRC_FILE
else
if [ "${POSTGRES_FORMAT}" = "**None**"]; then
POSTGRES_FORMAT="p"
fi

case "$POSTGRES_FORMAT" in
(p|c|t) ;;
(d) echo "Directory format not supported, only single-file formats are supported: p, c, t" >&2; exit 1 ;;
(*) echo "Unknown pg_dump format '$POSTGRES_FORMAT'. Please use one within: p, c, d, t" >&2; exit 1 ;;
esac

POSTGRES_HOST_OPTS+=" -F${POSTGRES_FORMAT}"

FILE_EXT="sql.gz"
case "$POSTGRES_FORMAT" in
(c) FILE_EXT="dump";;
(t) FILE_EXT="tar";;
esac

OIFS="$IFS"
IFS=','
for DB in $POSTGRES_DATABASE
do
IFS="$OIFS"

SRC_FILE=dump.sql.gz
DEST_FILE=${DB}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
SRC_FILE="dump.${FILE_EXT}"
DEST_FILE="${DB}_$(date +"%Y-%m-%dT%H:%M:%SZ").${FILE_EXT}"

if [ "${S3_FILE_NAME}" != "**None**" ]; then
DEST_FILE=${S3_FILE_NAME}_${DB}.sql.gz
DEST_FILE="${S3_FILE_NAME}_${DB}.${FILE_EXT}"
fi

echo "Creating dump of ${DB} database from ${POSTGRES_HOST}..."
pg_dump $POSTGRES_HOST_OPTS $DB | gzip > $SRC_FILE

if [ "$POSTGRES_FORMAT" = "p" ]; then
pg_dump $POSTGRES_HOST_OPTS $DB | gzip > ${SRC_FILE}
else
pg_dump $POSTGRES_HOST_OPTS $DB > ${SRC_FILE}
fi

if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
echo "Encrypting ${SRC_FILE}"
openssl enc -aes-256-cbc -in $SRC_FILE -out ${SRC_FILE}.enc -k $ENCRYPTION_PASSWORD
Expand Down
2 changes: 1 addition & 1 deletion postgres-backup-s3/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
set -eo pipefail

apk update
apk add openssl aws-cli
apk add openssl aws-cli
apk add postgresql-client --repository=https://dl-cdn.alpinelinux.org/alpine/v3.18/main

# cleanup
Expand Down