1
1
#! /bin/bash
2
2
set -e
3
3
4
+ # Docker volume for backups
5
+ export BACKUP_PATH=/backups
6
+
7
+ NOTIFICATION_SETTINGS=()
8
+ if [[ -n ${SENTRY_DSN:- } ]]; then
9
+ NOTIFICATION_SETTINGS=(-n sentry " --sentry-dsn=${SENTRY_DSN:? } " )
10
+ fi
11
+
4
12
log () {
5
13
local message
6
14
message=$1
@@ -19,48 +27,52 @@ upload_backup() {
19
27
remote_path=" ${SOFTLAYER_PATH:? } /$( date +%Y/%m) "
20
28
21
29
log " Uploading backup"
22
- monsoon ${NOTIFICATION_SETTINGS} upload softlayer \
23
- -u " ${SOFTLAYER_USER} " \
24
- -p " ${SOFTLAYER_API_KEY} " \
25
- -d " ${SOFTLAYER_DATACENTER} " \
26
- -c " ${SOFTLAYER_CONTAINER} " \
27
- /tmp/ " ${filename:? } " \
30
+ monsoon " ${NOTIFICATION_SETTINGS[@]} " upload softlayer \
31
+ -u " ${SOFTLAYER_USER:? } " \
32
+ -p " ${SOFTLAYER_API_KEY:? } " \
33
+ -d " ${SOFTLAYER_DATACENTER:? } " \
34
+ -c " ${SOFTLAYER_CONTAINER:? } " \
35
+ " ${BACKUP_PATH :? } / ${filename:? } " \
28
36
" ${remote_path:? } /${filename:? } " \
29
- && rm -f /tmp/ " ${filename:? } "
37
+ && rm -f " ${BACKUP_PATH :? } / ${filename:? } "
30
38
log " Done: Uploading backup"
31
39
}
32
40
33
41
back_up_mongo () {
42
+ local filename
43
+
34
44
if [[ -z " ${MONGO_HOST:- } " ]]; then
35
45
log " Skip backing up Mongo because no Mongo host specified"
36
46
return
37
47
fi
38
48
39
- FILENAME =mongo_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.gz
49
+ filename =mongo_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.gz
40
50
41
51
log " Taking mongo backup"
42
- monsoon ${NOTIFICATION_SETTINGS} backup mongo \
52
+ monsoon " ${NOTIFICATION_SETTINGS[@]} " backup mongo \
43
53
-u " ${MONGO_BACKUP_USER} " \
44
54
-p " ${MONGO_BACKUP_PASSWORD} " \
45
55
--host=" ${MONGO_HOST} " \
46
- --archive=/tmp/ " ${FILENAME } " \
56
+ --archive=" ${BACKUP_PATH :? } / ${filename }" \
47
57
--gzip
48
58
log " Done: Taking mongo backup"
49
59
50
- upload_backup " ${FILENAME :? } "
60
+ upload_backup " ${filename :? } "
51
61
}
52
62
53
63
back_up_mysql () {
64
+ local filename
65
+
54
66
if [[ -z " ${MYSQL_HOST:- } " ]]; then
55
67
log " Skip backing up MySQL because no MySQL host specified"
56
68
return
57
69
fi
58
70
59
- FILENAME =mysql_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.gz
71
+ filename =mysql_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.gz
60
72
61
73
log " Taking mysql backup"
62
- monsoon ${NOTIFICATION_SETTINGS} backup mysql \
63
- --output=/tmp/ " ${FILENAME } " \
74
+ monsoon " ${NOTIFICATION_SETTINGS[@]} " backup mysql \
75
+ --output=" ${BACKUP_PATH :? } / ${filename }" \
64
76
--gzip \
65
77
--all-databases \
66
78
--single-transaction \
@@ -70,7 +82,7 @@ back_up_mysql() {
70
82
" --password=${MYSQL_PASSWORD:? } "
71
83
log " Done: Taking mysql backup"
72
84
73
- upload_backup " ${FILENAME :? } "
85
+ upload_backup " ${filename :? } "
74
86
}
75
87
76
88
# PGDATABASE: gru,nsa,picard,pony,savant,sentry,usher
@@ -80,63 +92,59 @@ back_up_mysql() {
80
92
# PGUSER: dswb
81
93
82
94
back_up_postgresql () {
95
+ local filename
83
96
local databases
97
+
84
98
if [[ -z " ${PGHOST:- } " ]]; then
85
99
log " Skip backing up PostgreSQL because no PostgreSQL host specified"
86
100
return
87
101
fi
88
102
89
- databases=" $( echo ${PGDATABASE} | tr ' ,' ' ' ) "
103
+ databases=" $( echo " ${PGDATABASE:? } " | tr ' ,' ' ' ) "
90
104
91
105
for database in $databases ; do # TODO
92
- FILENAME=postgresql_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.gz
106
+ log " Taking PostgreSQL backup of ${database} "
107
+
108
+ filename=postgresql_backup_${database:? } _$( date +" %Y%m%d_%H%M%S" ) .archive.gz
93
109
94
- log " Taking mysql backup "
95
- monsoon ${NOTIFICATION_SETTINGS} backup postgresql \
96
- --output=/tmp/ " ${FILENAME } " \
110
+ # Assume existence of the PGPASSWORD environment variable
111
+ monsoon " ${NOTIFICATION_SETTINGS[@]} " backup postgresql \
112
+ --output=" ${BACKUP_PATH :? } / ${filename }" \
97
113
--gzip \
98
- --all-databases \
99
- --single-transaction \
100
- " --host=${MYSQL_HOST:? } " \
101
- " --port=${MYSQL_PORT:? } " \
102
- " --user=${MYSQL_USER:? } " \
103
- " --password=${MYSQL_PASSWORD:? } "
104
- log " Done: Taking mysql backup"
105
-
106
- upload_backup " ${FILENAME:? } "
114
+ " --host=${PGHOST:? } " \
115
+ " --port=${PGPORT:? } " \
116
+ " --username=${PGUSER:? } "
117
+ log " Done: Taking PostgreSQL backup of ${database:? } "
118
+
119
+ upload_backup " ${filename:? } "
107
120
done
108
121
}
109
122
110
123
back_up_files () {
124
+ local filename
125
+
111
126
if [[ -z " ${BACKUP_LOCAL_PATHS:- } " ]]; then
112
127
log " Skip backing up files because no local paths specified"
113
128
return
114
129
fi
115
130
116
- FILENAME =files_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.tgz
131
+ filename =files_backup_$( date +" %Y%m%d_%H%M%S" ) .archive.tgz
117
132
118
133
log " Taking file backup"
119
- monsoon ${NOTIFICATION_SETTINGS} backup files \
120
- --output=/tmp/" ${FILENAME} " \
134
+ # shellcheck disable=SC2086
135
+ monsoon " ${NOTIFICATION_SETTINGS[@]} " backup files \
136
+ --output=" ${BACKUP_PATH:? } /${filename} " \
121
137
${BACKUP_LOCAL_PATHS:? } # space-separated list
122
138
log " Done: Taking file backup"
123
139
124
- upload_backup " ${FILENAME :? } "
140
+ upload_backup " ${filename :? } "
125
141
}
126
142
127
143
main () {
144
+ back_up_files
128
145
back_up_mongo
129
146
back_up_mysql
130
- back_up_files
147
+ back_up_postgresql
131
148
}
132
149
133
-
134
- NOTIFICATION_SETTINGS=" "
135
-
136
- if [[ -n ${SENTRY_DSN:- } ]]; then
137
- NOTIFICATION_SETTINGS=" -n sentry --sentry-dsn=${SENTRY_DSN} "
138
- fi
139
-
140
-
141
-
142
150
main
0 commit comments