-
Notifications
You must be signed in to change notification settings - Fork 16
/
backup.sh
108 lines (96 loc) · 2.63 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
set -o pipefail
set -o errexit
set -o errtrace
set -o nounset
# set -o xtrace
JOB_NAME=${JOB_NAME:-default-job}
BACKUP_DIR=${BACKUP_DIR:-/tmp}
BOTO_CONFIG_PATH=${BOTO_CONFIG_PATH:-/root/.boto}
GCS_BUCKET=${GCS_BUCKET:-}
GCS_KEY_FILE_PATH=${GCS_KEY_FILE_PATH:-}
POSTGRES_HOST=${POSTGRES_HOST:-localhost}
POSTGRES_PORT=${POSTGRES_PORT:-5432}
POSTGRES_DB=${POSTGRES_DB:-}
POSTGRES_USER=${POSTGRES_USER:-}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-}
SLACK_ALERTS=${SLACK_ALERTS:-}
SLACK_AUTHOR_NAME=${SLACK_AUTHOR_NAME:-postgres-gcs-backup}
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL:-}
SLACK_CHANNEL=${SLACK_CHANNEL:-}
SLACK_USERNAME=${SLACK_USERNAME:-}
SLACK_ICON=${SLACK_ICON:-}
backup() {
mkdir -p $BACKUP_DIR
date=$(date "+%Y-%m-%dT%H:%M:%SZ")
archive_name="$date-$JOB_NAME-backup.sql.gz"
cmd_auth_part=""
if [[ ! -z $POSTGRES_USER ]] && [[ ! -z $POSTGRES_PASSWORD ]]
then
cmd_auth_part="--username=\"$POSTGRES_USER\" "
fi
cmd_db_part=""
if [[ ! -z $POSTGRES_DB ]]
then
cmd_db_part="--db=\"$POSTGRES_DB\""
fi
export PGPASSWORD=$POSTGRES_PASSWORD
cmd="pg_dump --host=\"$POSTGRES_HOST\" --port=\"$POSTGRES_PORT\" $cmd_auth_part $cmd_db_part | gzip > $BACKUP_DIR/$archive_name"
echo "starting to backup PostGRES host=$POSTGRES_HOST port=$POSTGRES_PORT"
eval "$cmd"
}
upload_to_gcs() {
if [[ ! "$GCS_BUCKET" =~ gs://* ]]; then
GCS_BUCKET="gs://${GCS_BUCKET}"
fi
if [[ $GCS_KEY_FILE_PATH != "" ]]
then
cat <<EOF > $BOTO_CONFIG_PATH
[Credentials]
gs_service_key_file = $GCS_KEY_FILE_PATH
[Boto]
https_validate_certificates = True
[GoogleCompute]
[GSUtil]
content_language = en
default_api_version = 2
[OAuth2]
EOF
fi
echo "uploading backup archive to GCS bucket=$GCS_BUCKET"
gsutil cp $BACKUP_DIR/$archive_name $GCS_BUCKET
}
send_slack_message() {
local color=${1}
local title=${2}
local message=${3}
echo 'Sending to '${SLACK_CHANNEL}'...'
curl --data-urlencode \
"$(printf 'payload={"channel": "%s", "username": "%s", "link_names": "true", "icon_url": "%s", "attachments": [{"author_name": "%s", "title": "%s", "text": "%s", "color": "%s"}]}' \
"${SLACK_CHANNEL}" \
"${SLACK_USERNAME}" \
"${SLACK_ICON}" \
"${SLACK_AUTHOR_NAME}" \
"${title}" \
"${message}" \
"${color}" \
)" \
${SLACK_WEBHOOK_URL} || true
echo
}
err() {
err_msg="${JOB_NAME} Something went wrong on line $(caller)"
echo $err_msg >&2
if [[ $SLACK_ALERTS == "true" ]]
then
send_slack_message "danger" "Error while performing postgres backup" "$err_msg"
fi
}
cleanup() {
rm $BACKUP_DIR/$archive_name
}
trap err ERR
backup
upload_to_gcs
cleanup
echo "backup done!"