-
Notifications
You must be signed in to change notification settings - Fork 12
/
backup.sh
73 lines (53 loc) · 1.84 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /bin/sh
set -e
set -o pipefail
# Environment checks
if [ "${POSTGRES_DATABASE}" = "**None**" ]; then
echo "You need to set the POSTGRES_DATABASE environment variable."
exit 1
fi
if [ "${POSTGRES_HOST}" = "**None**" ]; then
if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
else
echo "You need to set the POSTGRES_HOST environment variable."
exit 1
fi
fi
if [ "${POSTGRES_USER}" = "**None**" ]; then
echo "You need to set the POSTGRES_USER environment variable."
exit 1
fi
if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
echo "You need to set the POSTGRES_PASSWORD environment variable."
exit 1
fi
if [ "${GCLOUD_KEYFILE_BASE64}" = "**None**" ]; then
echo "You need to set the GCLOUD_KEYFILE_BASE64 environment variable."
exit 1
fi
if [ "${GCLOUD_PROJECT_ID}" = "**None**" ]; then
echo "You need to set the GCLOUD_PROJECT_ID environment variable."
exit 1
fi
if [ "${GCS_BACKUP_BUCKET}" = "**None**" ]; then
echo "You need to set the GCS_BACKUP_BUCKET environment variable."
exit 1
fi
# Google Cloud Auth
echo "Authenticating to Google Cloud..."
echo $GCLOUD_KEYFILE_BASE64 | base64 -d > /key.json
gcloud auth activate-service-account --key-file /key.json --project "$GCLOUD_PROJECT_ID" -q
# Postgres dumping
DATE=`date +"%Y-%m-%d_%H-%M-%S"`
FILENAME="${DATE}.sql.gz"
export PGPASSWORD=$POSTGRES_PASSWORD
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | gzip > $FILENAME
# Upload to GCS
echo "Uploading dump to $GCS_BACKUP_BUCKET..."
gsutil cp $FILENAME $GCS_BACKUP_BUCKET/$FILENAME
rm $FILENAME # delete old file
echo "SQL backup uploaded successfully."