Skip to content

Commit a90081c

Browse files
committed
Add support for custom format
1 parent 34dabca commit a90081c

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ ENV DELETE_OLDER_THAN **None**
4141
ENV BACKUP_FILE **None**
4242
ENV CREATE_DATABASE no
4343
ENV DROP_DATABASE no
44+
ENV USE_CUSTOM_FORMAT no
4445
ENV COMPRESSION_CMD 'gzip'
4546
ENV DECOMPRESSION_CMD 'gunzip -c'
47+
ENV PARALLEL_JOBS 1
4648

4749
ADD run.sh run.sh
4850
ADD backup.sh backup.sh

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ spec:
9090
| S3_ENDPOINT | | | The AWS Endpoint URL, for S3 Compliant APIs such as [minio](https://minio.io) |
9191
| S3_S3V4 | no | | Set to `yes` to enable AWS Signature Version 4, required for [minio](https://minio.io) servers |
9292
| SCHEDULE | | | Backup schedule time, see explainatons below |
93-
| ENCRYPTION_PASSWORD | | | Password to encrypt/decrypt the backup |
93+
| ENCRYPTION_PASSWORD | | | Password to encrypt/decrypt the backup |
9494
| DELETE_OLDER_THAN | | | Delete old backups, see explanation and warning below |
95-
| COMPRESSION_CMD | gzip | | Command used to compress the backup (e.g. `pigz` for parallel compression) |
96-
| DECOMPRESSION_CMD | gunzip -c | | Command used to decompress the backup (e.g. `pigz -dc` for parallel decompression) |
95+
| USE_CUSTOM_FORMAT | no | | Use PostgreSQL's custom format (-Fc) instead of plain text with compression |
96+
| COMPRESSION_CMD | gzip | | Command used to compress the backup (e.g. `pigz` for parallel compression) - ignored when USE_CUSTOM_FORMAT=yes |
97+
| DECOMPRESSION_CMD | gunzip -c | | Command used to decompress the backup (e.g. `pigz -dc` for parallel decompression) - ignored when USE_CUSTOM_FORMAT=yes |
98+
| PARALLEL_JOBS | 1 | | Number of parallel jobs for pg_restore when using custom format backups |
9799
| BACKUP_FILE | | Y* | Required for restore. The path to the backup file in S3, format: S3_PREFIX/filename |
98100
| CREATE_DATABASE | no | | For restore: Set to `yes` to create the database if it doesn't exist |
99101
| DROP_DATABASE | no | | For restore: Set to `yes` to drop the database before restoring (caution: destroys existing data). Use with CREATE_DATABASE=yes to recreate it |
@@ -114,12 +116,36 @@ WARNING: this will delete all files in the S3_PREFIX path, not just those create
114116

115117
You can additionally set the `ENCRYPTION_PASSWORD` environment variable like `-e ENCRYPTION_PASSWORD="superstrongpassword"` to encrypt the backup. The restore process will automatically detect encrypted backups and decrypt them when the `ENCRYPTION_PASSWORD` environment variable is set correctly. It can be manually decrypted using `openssl aes-256-cbc -d -in backup.sql.gz.enc -out backup.sql.gz`.
116118

117-
### Compression Options
119+
### Backup Format and Compression Options
118120

119-
By default, backups are compressed with `gzip` and decompressed with `gunzip -c`. For improved performance on multi-core systems, you can use `pigz` (parallel gzip) instead:
121+
There are two options for backup format:
122+
123+
1. **Plain text format with compression** (default):
124+
- Uses plain SQL text output compressed with gzip/pigz
125+
- Standard and widely compatible
126+
127+
2. **PostgreSQL custom format**:
128+
- Enable with `-e USE_CUSTOM_FORMAT=yes`
129+
- Significantly faster than plain text format
130+
- Produces smaller backup files (built-in compression)
131+
- Supports parallel restoration for faster restores
132+
- Allows selective table/schema restoration
133+
- Recommended for larger databases
134+
135+
For plain text format, backups are compressed with `gzip` by default. For improved performance on multi-core systems, you can use `pigz` (parallel gzip) instead:
120136

121137
```sh
122138
$ docker run ... -e COMPRESSION_CMD=pigz ... itbm/postgres-backup-s3
123139

124140
$ docker run ... -e DECOMPRESSION_CMD="pigz -dc" ... itbm/postgres-backup-s3
125141
```
142+
143+
When using custom format with parallel restore:
144+
145+
```sh
146+
$ docker run ... -e USE_CUSTOM_FORMAT=yes ... itbm/postgres-backup-s3
147+
148+
$ docker run ... -e PARALLEL_JOBS=4 -e BACKUP_FILE=backup/dbname_0000-00-00T00:00:00Z.dump ... itbm/postgres-backup-s3
149+
```
150+
151+
Note: Custom format is not available when using `POSTGRES_DATABASE=all` as pg_dumpall does not support this format.

backup.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTG
6060

6161
echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
6262

63-
SRC_FILE=dump.sql.gz
64-
DEST_FILE=${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
65-
66-
COMPRESSION_CMD=${COMPRESSION_CMD:-gzip}
67-
68-
if [ "${POSTGRES_DATABASE}" == "all" ]; then
69-
pg_dumpall $POSTGRES_HOST_OPTS | $COMPRESSION_CMD > $SRC_FILE
63+
if [ "$USE_CUSTOM_FORMAT" = "yes" ]; then
64+
SRC_FILE=dump.dump
65+
DEST_FILE=${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").dump
66+
if [ "${POSTGRES_DATABASE}" == "all" ]; then
67+
echo "ERROR: Custom format (-Fc) is not supported with pg_dumpall."
68+
exit 1
69+
else
70+
pg_dump -Fc $POSTGRES_HOST_OPTS $POSTGRES_DATABASE > $SRC_FILE
71+
fi
7072
else
71-
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | $COMPRESSION_CMD > $SRC_FILE
73+
SRC_FILE=dump.sql.gz
74+
DEST_FILE=${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
75+
if [ "${POSTGRES_DATABASE}" == "all" ]; then
76+
pg_dumpall $POSTGRES_HOST_OPTS | $COMPRESSION_CMD > $SRC_FILE
77+
else
78+
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | $COMPRESSION_CMD > $SRC_FILE
79+
fi
7280
fi
7381

7482

restore.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,28 @@ if [ "${CREATE_DATABASE}" = "yes" ]; then
127127
fi
128128

129129
if [[ "$DOWNLOAD_PATH" == *.sql.gz ]]; then
130-
DECOMPRESSION_CMD=${DECOMPRESSION_CMD:-"gunzip -c"}
131130
if [ "${POSTGRES_DATABASE}" == "all" ]; then
132131
echo "Restoring all databases"
133132
$DECOMPRESSION_CMD $DOWNLOAD_PATH | psql $POSTGRES_HOST_OPTS -d postgres
134133
else
135134
echo "Restoring database ${POSTGRES_DATABASE}"
136135
$DECOMPRESSION_CMD $DOWNLOAD_PATH | psql $POSTGRES_HOST_OPTS -d $POSTGRES_DATABASE
137136
fi
137+
elif [[ "$DOWNLOAD_PATH" == *.dump ]]; then
138+
if [ "${POSTGRES_DATABASE}" == "all" ]; then
139+
echo "ERROR: Custom format backup cannot be used to restore all databases."
140+
exit 1
141+
else
142+
echo "Restoring database ${POSTGRES_DATABASE} from custom format"
143+
if [ "$PARALLEL_JOBS" -gt 1 ]; then
144+
echo "Using parallel restore with $PARALLEL_JOBS jobs"
145+
pg_restore -j $PARALLEL_JOBS $POSTGRES_HOST_OPTS -d $POSTGRES_DATABASE $DOWNLOAD_PATH
146+
else
147+
pg_restore $POSTGRES_HOST_OPTS -d $POSTGRES_DATABASE $DOWNLOAD_PATH
148+
fi
149+
fi
138150
else
139-
echo "ERROR: Unsupported backup format. Expected *.sql.gz file."
151+
echo "ERROR: Unsupported backup format. Expected *.sql.gz or *.dump file."
140152
exit 1
141153
fi
142154

0 commit comments

Comments
 (0)