-
Notifications
You must be signed in to change notification settings - Fork 6
/
run-backup.sh
30 lines (21 loc) · 1008 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
28
29
30
# 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.gz"
echo "> Running pg_dump"
PGPASSWORD="$POSTGRES_ENV_POSTGRES_PASSWORD" pg_dump --no-owner --clean --if-exists --no-privileges --no-acl -h "$POSTGRES_PORT_5432_TCP_ADDR" -U "$POSTGRES_ENV_POSTGRES_USER" "$POSTGRES_ENV_POSTGRES_DB" > $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 "> Zipping dump file"
gzip -9 $GPG_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.gz" "$S3_URI"
# Clean up
rm $FILE
rm "$GPG_FILE.gz"
echo "Done."