-
-
Save kemsakurai/be907a0104f9039e2e713b52b4f4f943 to your computer and use it in GitHub Desktop.
Back up postgres db to google cloud storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/usr/bin/pg_dump -w
として、pgpass
を作成、パスワードの入力を求められないようにした。