From 0e1ae1045d33009605cd0e307b67f913f93f0e00 Mon Sep 17 00:00:00 2001 From: Alex Toff Date: Sat, 14 Oct 2023 20:16:19 +0100 Subject: [PATCH] feat(postgres-backup-s3): Support custom backup filename (#160) * Add support for back file name override * apply to full dump too. include DB name * readme updates * consistency --- postgres-backup-s3/Dockerfile | 2 +- postgres-backup-s3/README.md | 5 +++++ postgres-backup-s3/backup.sh | 8 ++++++++ postgres-backup-s3/install.sh | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/postgres-backup-s3/Dockerfile b/postgres-backup-s3/Dockerfile index 25acf90..747f90c 100644 --- a/postgres-backup-s3/Dockerfile +++ b/postgres-backup-s3/Dockerfile @@ -14,8 +14,8 @@ ENV POSTGRES_EXTRA_OPTS '' ENV S3_ACCESS_KEY_ID **None** ENV S3_SECRET_ACCESS_KEY **None** ENV S3_BUCKET **None** +ENV S3_FILE_NAME **None** ENV S3_REGION us-west-1 -ENV S3_PATH 'backup' ENV S3_ENDPOINT **None** ENV S3_S3V4 no ENV SCHEDULE **None** diff --git a/postgres-backup-s3/README.md b/postgres-backup-s3/README.md index 25e6aa0..0e73f69 100644 --- a/postgres-backup-s3/README.md +++ b/postgres-backup-s3/README.md @@ -44,6 +44,11 @@ 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 `/all_.sql.gz`. When using `POSTGRES_DATABASE`, each database listed will be backed up to the object path `/_.sql.gz`. + +If you wish to make these filenames static, you can use the `S3_FILE_NAME` variable, which will change these formats to `/.sql.gz` or `/_.sql.gz` accordingly. + ### Backup All Databases You can backup all available databases by setting `POSTGRES_BACKUP_ALL="true"`. diff --git a/postgres-backup-s3/backup.sh b/postgres-backup-s3/backup.sh index 86af4e9..4ae6cff 100644 --- a/postgres-backup-s3/backup.sh +++ b/postgres-backup-s3/backup.sh @@ -65,6 +65,10 @@ 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 @@ -94,6 +98,10 @@ else SRC_FILE=dump.sql.gz DEST_FILE=${DB}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz + + if [ "${S3_FILE_NAME}" != "**None**" ]; then + DEST_FILE=${S3_FILE_NAME}_${DB}.sql.gz + fi echo "Creating dump of ${DB} database from ${POSTGRES_HOST}..." pg_dump $POSTGRES_HOST_OPTS $DB | gzip > $SRC_FILE diff --git a/postgres-backup-s3/install.sh b/postgres-backup-s3/install.sh index 15ca915..5e63e1a 100644 --- a/postgres-backup-s3/install.sh +++ b/postgres-backup-s3/install.sh @@ -4,7 +4,8 @@ set -eo pipefail apk update -apk add postgresql-client openssl aws-cli +apk add openssl aws-cli +apk add postgresql-client --repository=https://dl-cdn.alpinelinux.org/alpine/v3.18/main # cleanup rm -rf /var/cache/apk/*