Skip to content

Commit 723abaf

Browse files
committed
Initial commit
0 parents  commit 723abaf

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM alpine:latest
2+
RUN apk add --no-cache postgresql-client gnupg openssh-client
3+
4+
COPY bin/backup /usr/bin/backup
5+
6+
#ENV PGHOST
7+
#ENV PGUSER
8+
#ENV PGPASSWORD
9+
#ENV BACKUP_NAME
10+
ENV BACKUP_INTERVAL 1h
11+
ENV BACKUP_GPG_KEY_ID 796F7DAA1D643B75
12+
ENV BACKUP_SSH_DESTINATION chaosdorf@backup.finalrewind.org
13+
ENV BACKUP_SSH_REMOTE_DIRECTORY backup
14+
15+
ENTRYPOINT ["/usr/bin/backup"]

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# backup postgres instances from within docker
2+
3+
This image includes postgres-client, openssh-client and GnuPG.
4+
Inspired by [nomaster/postgres-backup-docker](https://github.com/nomaster/postgres-backup-docker).
5+
6+
To encrypt backups, the script first retrieves a GPG key from default keyservers.
7+
Every hour, a full postgres backup is created, compressed, encrypted and pushed to a remote SSH server.
8+
The container can run beneath a postgres database within the same docker network.
9+
10+
Required environment variables:
11+
12+
- `PGHOST`, `PGUSER`, `PGPASSWORD`: Postgres credentials
13+
- `BACKUP_NAME`: To identify the backup on the remote host
14+
15+
Optional variables:
16+
17+
- `BACKUP_INTERVAL` (default: `1h`)
18+
- `BACKUP_GPG_KEY_ID` (chaosdorf default)
19+
- `BACKUP_SSH_DESTINATION` (chaosdorf default, should be `user@host`)
20+
- `BACKUP_SSH_REMOTE_DIRECTORY` (chaosdorf default, directory must exist on remote host)
21+
22+
# Example
23+
24+
Add service to `docker-compose.yml` and provide `id_rsa` to allow ssh to connect to the remote host:
25+
26+
```yml
27+
version: '3.7'
28+
29+
services:
30+
db:
31+
image: postgres:latest
32+
networks:
33+
- internal
34+
[...]
35+
backup:
36+
image: chaosdorf/postgres-gpg-backup:latest
37+
environment:
38+
- PGHOST=db
39+
- PGUSER=postgres
40+
- PGPASSWORD=postgres
41+
- BACKUP_NAME=db-backup
42+
configs:
43+
- source: backup_ssh_key
44+
target: /root/.ssh/id_rsa
45+
uid: '0'
46+
gid: '0'
47+
mode: 0600
48+
networks:
49+
- internal
50+
51+
configs:
52+
backup_ssh_key:
53+
[...]
54+
```

bin/backup

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
set -e
3+
[ -z "${BACKUP_NAME}" ] && { echo "error: BACKUP_NAME not set"; exit 1; }
4+
[ -z "${BACKUP_GPG_KEY_ID}" ] && { echo "error: BACKUP_GPG_KEY_ID not set"; exit 1; }
5+
[ -z "${BACKUP_SSH_DESTINATION}" ] && { echo "error: BACKUP_SSH_DESTINATION not set"; exit 1; }
6+
BACKUP_INTERVAL=${BACKUP_INTERVAL:-1h}
7+
BACKUP_SSH_REMOTE_DIRECTORY=${BACKUP_SSH_REMOTE_DIRECTORY:-backup}
8+
9+
echo "Retrieving GPG key ${BACKUP_GPG_KEY_ID}"
10+
gpg --receive-keys "${BACKUP_GPG_KEY_ID}"
11+
12+
echo "Creating postgres SQL dump every ${BACKUP_INTERVAL}..."
13+
while true
14+
do
15+
DATE=$(date +%y%m%d-%H%M%S)
16+
REMOTE_FILE=${BACKUP_SSH_REMOTE_DIRECTORY}/${BACKUP_NAME}_${DATE}.sql.gz.gpg
17+
echo "[${DATE}] Creating postgres SQL dump ${REMOTE_FILE}"
18+
pg_dumpall \
19+
| gzip \
20+
| gpg --encrypt --always-trust -r "${BACKUP_GPG_KEY_ID}" \
21+
| ssh -q ${BACKUP_SSH_DESTINATION} "cat > ${REMOTE_FILE}"
22+
echo "Done, waiting ${BACKUP_INTERVAL}"
23+
sleep "${BACKUP_INTERVAL}"
24+
done

build-image.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
docker build -t chaosdorf/postgres-gpg-backup:latest .

0 commit comments

Comments
 (0)