forked from diogopms/postgres-gcs-backup
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.sh
More file actions
126 lines (107 loc) · 3.02 KB
/
backup.sh
File metadata and controls
126 lines (107 loc) · 3.02 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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:-}
STOP_SLAVE=${STOP_SLAVE:-}
SLACK_ALERTS=${SLACK_ALERTS:-}
SLACK_AUTHOR_NAME=${SLACK_AUTHOR_NAME:-postgres-gcs-backup}
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL:-}
backup() {
mkdir -p $BACKUP_DIR
date=$(date "+%Y-%m-%dT%H:%M:%SZ")
archive_name="$JOB_NAME-backup-$date.dump"
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
if [[ $STOP_SLAVE == "true" ]]
then
echo "Pause replication"
cmdp="psql --host=\"$POSTGRES_HOST\" --port=\"$POSTGRES_PORT\" $cmd_auth_part $cmd_db_part --command 'select pg_wal_replay_pause();'"
eval "$cmdp"
fi
cmd="pg_dump --host=\"$POSTGRES_HOST\" --port=\"$POSTGRES_PORT\" $cmd_auth_part $cmd_db_part -Z1 -Fc --file=$BACKUP_DIR/$archive_name"
echo "starting to backup PostGRES host=$POSTGRES_HOST port=$POSTGRES_PORT"
eval "$cmd"
if [[ $STOP_SLAVE == "true" ]]
then
echo "Resume replication"
cmdr="psql --host=\"$POSTGRES_HOST\" --port=\"$POSTGRES_PORT\" $cmd_auth_part $cmd_db_part --command 'select pg_wal_replay_resume();'"
eval "$cmdr"
fi
echo "finish dump $BACKUP_DIR/$archive_name"
#echo "starting gzip $BACKUP_DIR/$archive_name"
# cmd_pack="gzip $BACKUP_DIR/$archive_name"
# eval "$cmd_pack"
}
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 ...'
curl --data-urlencode \
"$(printf 'payload={"attachments": [{"author_name": "%s", "title": "%s", "text": "%s", "color": "%s"}]}' \
"${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!"