forked from mattbeedle/docker-postgres-backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-backup.sh
27 lines (19 loc) · 873 Bytes
/
run-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Credit for lots of this goes to: https://github.com/siomiz/PostgreSQL-S3
set -e # stop if any of these commands fail
echo "*** Starting run-backup.sh ***"
DATE=$(date +%Y%m%d_%H%M%S)
FILE="/tmp/$PREFIX-$DATE.sql"
GPG_FILE="/tmp/$PREFIX-$DATE.sql.gpg"
S3_URI="s3://$S3_BUCKET_NAME/$PREFIX-$DATE.sql.gpg"
echo "> Running pg_dumpall"
PGPASSWORD="$POSTGRES_ENV_POSTGRES_PASSWORD" pg_dumpall -h "$POSTGRES_PORT_5432_TCP_ADDR" -U "$POSTGRES_ENV_POSTGRES_USER" > $FILE
echo "> Downloading public key: ${GPG_PUBKEY_ID}"
gpg --keyserver pgp.mit.edu --recv-keys ${GPG_PUBKEY_ID}
echo "> Encrypting dump file using gpg"
gpg --always-trust -v -e -r ${GPG_PUBKEY_ID} -o $GPG_FILE $FILE
echo "> Uploading to S3"
AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" aws s3 cp "$GPG_FILE" "$S3_URI"
# Clean up
rm $FILE
rm $GPG_FILE
echo "Done."