forked from birotaio/mongodb-gcs-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
115 lines (101 loc) · 2.78 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
109
110
111
112
113
114
115
#!/bin/bash
set -o pipefail
set -o errexit
set -o errtrace
set -o nounset
# set -o xtrace
JOB_NAME=${JOB_NAME:-default-name}
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:-}
MONGODB_HOST=${MONGODB_HOST:-localhost}
MONGODB_PORT=${MONGODB_PORT:-27017}
MONGODB_DB=${MONGODB_DB:-}
MONGODB_USER=${MONGODB_USER:-}
MONGODB_PASSWORD=${MONGODB_PASSWORD:-}
MONGODB_OPLOG=${MONGODB_OPLOG:-}
SLACK_ALERTS=${SLACK_ALERTS:-}
SLACK_AUTHOR_NAME=${SLACK_AUTHOR_NAME:-mongodb-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="$JOB_NAME-backup-$date.tar.gz"
cmd_auth_part=""
if [[ ! -z $MONGODB_USER ]] && [[ ! -z $MONGODB_PASSWORD ]]
then
cmd_auth_part="--username=\"$MONGODB_USER\" --password=\"$MONGODB_PASSWORD\""
fi
cmd_db_part=""
if [[ ! -z $MONGODB_DB ]]
then
cmd_db_part="--db=\"$MONGODB_DB\""
fi
cmd_oplog_part=""
if [[ $MONGODB_OPLOG = "true" ]]
then
cmd_oplog_part="--oplog"
fi
cmd="mongodump --host=\"$MONGODB_HOST\" --port=\"$MONGODB_PORT\" $cmd_auth_part $cmd_db_part $cmd_oplog_part --gzip --archive=$BACKUP_DIR/$archive_name"
echo "starting to backup MongoDB host=$MONGODB_HOST port=$MONGODB_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 --silent --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" "${JOB_NAME} - Error while performing mongodb backup" "$err_msg"
fi
}
cleanup() {
rm $BACKUP_DIR/$archive_name
}
trap err ERR
backup
upload_to_gcs
cleanup
echo "backup done!"