Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup directory #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM alpine:${ALPINE_VERSION}
ARG TARGETARCH

ADD src/install.sh install.sh
RUN sh install.sh && rm install.sh
RUN sh install.sh && rm install.sh && mkdir /backup

ENV POSTGRES_DATABASE ''
ENV POSTGRES_HOST ''
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
POSTGRES_DATABASE: dbname
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- /mnt/path/to/backup/folder:/backup # optional
```

- Images are tagged by the major PostgreSQL version supported: `12`, `13`, `14`, `15` or `16`.
Expand Down
12 changes: 6 additions & 6 deletions src/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ pg_dump --format=custom \
-U $POSTGRES_USER \
-d $POSTGRES_DATABASE \
$PGDUMP_EXTRA_OPTS \
> db.dump
> backup/db.dump

timestamp=$(date +"%Y-%m-%dT%H:%M:%S")
s3_uri_base="s3://${S3_BUCKET}/${S3_PREFIX}/${POSTGRES_DATABASE}_${timestamp}.dump"

if [ -n "$PASSPHRASE" ]; then
echo "Encrypting backup..."
rm -f db.dump.gpg
gpg --symmetric --batch --passphrase "$PASSPHRASE" db.dump
rm db.dump
local_file="db.dump.gpg"
rm -f backup/db.dump.gpg
gpg --symmetric --batch --passphrase "$PASSPHRASE" backup/db.dump
rm backup/db.dump
local_file="backup/db.dump.gpg"
s3_uri="${s3_uri_base}.gpg"
else
local_file="db.dump"
local_file="backup/db.dump"
s3_uri="$s3_uri_base"
fi

Expand Down
10 changes: 5 additions & 5 deletions src/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ else
fi

echo "Fetching backup from S3..."
aws $aws_args s3 cp "${s3_uri_base}/${key_suffix}" "db${file_type}"
aws $aws_args s3 cp "${s3_uri_base}/${key_suffix}" "backup/db${file_type}"

if [ -n "$PASSPHRASE" ]; then
echo "Decrypting backup..."
gpg --decrypt --batch --passphrase "$PASSPHRASE" db.dump.gpg > db.dump
rm db.dump.gpg
gpg --decrypt --batch --passphrase "$PASSPHRASE" backup/db.dump.gpg > backup/db.dump
rm backup/db.dump.gpg
fi

conn_opts="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER -d $POSTGRES_DATABASE"

echo "Restoring from backup..."
pg_restore $conn_opts --clean --if-exists db.dump
rm db.dump
pg_restore $conn_opts --clean --if-exists backup/db.dump
rm backup/db.dump

echo "Restore complete."