Skip to content

Commit

Permalink
Update pg backup for RDS (#36)
Browse files Browse the repository at this point in the history
* Update for RDS

* Sequence
  • Loading branch information
ArmaanT authored Dec 24, 2020
1 parent 7d918fe commit dca1814
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 0 additions & 1 deletion docker/pg-s3-backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ The following environment variables need to be defined:
* AWS_SECRET_ACCESS_KEY
* AWS_DEFAULT_REGION
* S3_BUCKET
* S3_PREFIX
* DATABASE_URL
14 changes: 8 additions & 6 deletions docker/pg-s3-backup/backup
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ set -euo pipefail
STAMP=`date +"%Y%m%d - %A %d %B %Y @ %H:%M"`

# List all databases and remove default databases
DATABASES=`psql $DATABASE_URL/defaultdb -q -t -c 'SELECT datname from pg_database' | grep -v "\(_dodb\|template1\|template0\|defaultdb\)"`
DATABASES=`psql $DATABASE_URL -q -t -c 'SELECT datname from pg_database' | grep -v "\(postgres\|rdsadmin\|template0\|template1\)"`

echo "Starting backup"

# Dump each database and upload to S3
for DB in $DATABASES
do
FILENAME="$STAMP - $DB.sql.gz"
echo "Creating dump of $DB"
pg_dump $DATABASE_URL/$DB | gzip > $DB.dump
echo "Uploading dump of $DB"
cat $DB.dump | aws s3 cp - "s3://$S3_BUCKET/$STAMP/$S3_PREFIX/$FILENAME"
(FILENAME="$STAMP - $DB.sql.gz";
echo "Creating dump of $DB";
pg_dump $DATABASE_URL/$DB | gzip > $DB.dump;
echo "Uploading dump of $DB";
cat $DB.dump | aws s3 cp - "s3://$S3_BUCKET/$STAMP/$FILENAME") &
done

wait

echo "Backup finished successfully"
echo "Total data backed up: `du -hc *.dump | grep total | cut -f1`"

0 comments on commit dca1814

Please sign in to comment.