Browse Source

Allow for custom options to pg_dumpall and custom formats

pull/164/head
Finwë 8 months ago
parent
commit
6560ec1970
  1. 1
      postgres-backup-s3/Dockerfile
  2. 5
      postgres-backup-s3/README.md
  3. 32
      postgres-backup-s3/backup.sh

1
postgres-backup-s3/Dockerfile

@ -11,6 +11,7 @@ ENV POSTGRES_PORT 5432
ENV POSTGRES_USER **None** ENV POSTGRES_USER **None**
ENV POSTGRES_PASSWORD **None** ENV POSTGRES_PASSWORD **None**
ENV POSTGRES_EXTRA_OPTS '' ENV POSTGRES_EXTRA_OPTS ''
ENV POSTGRES_FORMAT **None**
ENV S3_ACCESS_KEY_ID **None** ENV S3_ACCESS_KEY_ID **None**
ENV S3_SECRET_ACCESS_KEY **None** ENV S3_SECRET_ACCESS_KEY **None**
ENV S3_BUCKET **None** ENV S3_BUCKET **None**

5
postgres-backup-s3/README.md

@ -36,6 +36,7 @@ pgbackups3:
POSTGRES_USER: user POSTGRES_USER: user
POSTGRES_PASSWORD: password POSTGRES_PASSWORD: password
POSTGRES_EXTRA_OPTS: '--schema=public --blobs' POSTGRES_EXTRA_OPTS: '--schema=public --blobs'
POSTGRES_FORMAT: 'c'
``` ```
### Automatic Periodic Backups ### Automatic Periodic Backups
@ -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). More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).
### Backup File Name / Path ### 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 ### Backup All Databases

32
postgres-backup-s3/backup.sh

@ -71,7 +71,7 @@ if [ "${POSTGRES_BACKUP_ALL}" == "true" ]; then
fi fi
echo "Creating dump of all databases from ${POSTGRES_HOST}..." 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 if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
echo "Encrypting ${SRC_FILE}" echo "Encrypting ${SRC_FILE}"
@ -90,21 +90,43 @@ if [ "${POSTGRES_BACKUP_ALL}" == "true" ]; then
echo "SQL backup uploaded successfully" echo "SQL backup uploaded successfully"
rm -rf $SRC_FILE rm -rf $SRC_FILE
else 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" OIFS="$IFS"
IFS=',' IFS=','
for DB in $POSTGRES_DATABASE for DB in $POSTGRES_DATABASE
do do
IFS="$OIFS" 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 if [ "${S3_FILE_NAME}" != "**None**" ]; then
DEST_FILE=${S3_FILE_NAME}_${DB}.sql.gz
DEST_FILE="${S3_FILE_NAME}_${DB}.${FILE_EXT}"
fi fi
echo "Creating dump of ${DB} database from ${POSTGRES_HOST}..." 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 if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
echo "Encrypting ${SRC_FILE}" echo "Encrypting ${SRC_FILE}"

Loading…
Cancel
Save