Skip to content

Commit 96e1ab5

Browse files
committed
PGP backup encryption
1 parent c961267 commit 96e1ab5

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Docker image to backup Postgres database to S3 using pg_dump and compress using
1010
- [x] Compression is done with pigz (parallel gzip)
1111
- [x] Creates bucket if it's not created
1212
- [x] Can be run in Kubernetes or Docker
13-
- [>] TODO: Add possibility to detect and backup all databases [planned]
14-
- [ ] TODO: OpenSSL encryption
13+
- [ ] TODO: Add possibility to detect and backup all databases [planned]
14+
- [x] PGP encryption
1515
- [ ] TODO: Add other compression methods
1616
- [ ] TODO: Add other dbs (e.g. postgres, mysql)
1717

@@ -21,6 +21,8 @@ S3_BUCK=postgres1-backups
2121
S3_NAME=folder-name/backup-name-prefix
2222
S3_URI=https://s3-key:[email protected]
2323
PG_URI=postgres://mongo-host:5432/db-name
24+
GPG_KEYSERVER=keyserver.ubuntu.com # your hpks keyserver
25+
GPG_KEYID=<key_id> # recipient key, backup will be encrypted if added
2426
```
2527

2628
Or see `docker-compose.yml` file to run this container with Docker.

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
image: backuptools/postgres-backup-s3
66
build: .
77
environment:
8-
- S3_BUCK=mongo1-backups
8+
- S3_BUCK=postgres1-backups
99
- S3_NAME=folder-name/backup-name-prefix
1010
- S3_URI=https://s3-key:[email protected]
1111
- PG_URI=postgres://pg-user:pg-password@postgres-host:5432/db-name

entrypoint.sh

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,30 @@ get_date () {
77
}
88

99
# Script
10+
: ${GPG_KEYSERVER:='keyserver.ubuntu.com'}
11+
: ${GPG_KEYID:=''}
12+
13+
if [ -z "$GPG_KEYID" ]
14+
then
15+
echo "$(get_date) !WARNING! It's strongly recommended to encrypt your backups."
16+
else
17+
echo "$(get_date) Preparing keys: importing from keyserver"
18+
gpg --keyserver ${GPG_KEYSERVER} --recv-keys ${GPG_KEYID}
19+
fi
20+
1021
echo "$(get_date) Postgres backup started"
1122

1223
export MC_HOST_backup=$S3_URI
1324

1425
mc mb backup/${S3_BUCK} --insecure
1526

16-
pg_dump $PG_URI | pigz -9 | mc pipe backup/${S3_BUCK}/${S3_NAME}-`date +%Y-%m-%d_%H-%M-%S`.pgdump --insecure
27+
if [ -z "$GPG_KEYID" ]
28+
then
29+
pg_dump $PG_URI | pigz -9 | mc pipe backup/${S3_BUCK}/${S3_NAME}-`date +%Y-%m-%d_%H-%M-%S`.pgdump --insecure
30+
else
31+
pg_dump $PG_URI | pigz -9 \
32+
| gpg --encrypt -z 0 --recipient ${GPG_KEYID} --trust-model always \
33+
| mc pipe backup/${S3_BUCK}/${S3_NAME}-`date +%Y-%m-%d_%H-%M-%S`.pgdump.pgp --insecure
34+
fi
1735

1836
echo "$(get_date) Postgres backup completed successfully"

0 commit comments

Comments
 (0)