Skip to content

Instantly share code, notes, and snippets.

@kemsakurai
Forked from studiotomi/ps-snapshot.sh
Last active September 16, 2018 16:00
Show Gist options
  • Save kemsakurai/be907a0104f9039e2e713b52b4f4f943 to your computer and use it in GitHub Desktop.
Save kemsakurai/be907a0104f9039e2e713b52b4f4f943 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://your bucket'
JOB_TIMESTAMP=`date +%Y%m%d-%H%M`
DATABASE='your database name'
DIR='your_backup_dir'
GS_UTIL='your gsutil path'
cd $DIR
NUMBER=`"$GS_UTIL" ls "$BUCKET*.gz" | wc -l`
if [ $NUMBER -gt 10 ]; then
FILE_NAME=`"$GS_UTIL" ls "$BUCKET*.gz" | head -n 1`
gsutil rm -f "$FILE_NAME"
else
:
fi
/usr/bin/pg_dump -w $DATABASE > $JOB_TIMESTAMP-pad.sql
/bin/tar -cvzf $JOB_TIMESTAMP.tar.gz $JOB_TIMESTAMP-pad.sql
"$GS_UTIL" cp $JOB_TIMESTAMP.tar.gz "$BUCKET"
rm -f $JOB_TIMESTAMP-pad.sql $JOB_TIMESTAMP.tar.gz
@kemsakurai
Copy link
Author

  • バックアップを取得するようにした。
  • /usr/bin/pg_dump -w として、pgpass を作成、パスワードの入力を求められないようにした。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment