Skip to content

Instantly share code, notes, and snippets.

@studiotomi
Created February 7, 2017 16:33
Show Gist options
  • Save studiotomi/231c321ba2a2abf41b7cba02c568f7a5 to your computer and use it in GitHub Desktop.
Save studiotomi/231c321ba2a2abf41b7cba02c568f7a5 to your computer and use it in GitHub Desktop.
Back up postgres db to google cloud storage
#!/bin/bash
# Requirements:
# - gcloud/gsutil is installed on the box
# - gcloud is logged in as a user with write access to Google Cloud Storage
# - The file has execution rights so that it can be run in cron
# - The Google Cloud Storage bucket already exits
# Exit on any error
set -e
BUCKET='gs://some-bucket/'
JOB_TIMESTAMP=`date +%Y%m%d-%H%M`
DATABASE='the database name'
DIR='/home/postgres'
cd $DIR
/usr/bin/pg_dump $DATABASE > $JOB_TIMESTAMP-pad.sql
/bin/tar -cvzf $JOB_TIMESTAMP.tar.gz $JOB_TIMESTAMP-pad.sql
/usr/bin/gsutil cp $JOB_TIMESTAMP.tar.gz $BUCKET
rm -f $JOB_TIMESTAMP-pad.sql $JOB_TIMESTAMP.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment