Skip to content

Commit 6f9aa31

Browse files
Leons Petrazickisreevejd
authored andcommitted
PostgreSQL
1 parent 5832ed5 commit 6f9aa31

File tree

4 files changed

+77
-47
lines changed

4 files changed

+77
-47
lines changed

CHANGELOG.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [Unreleased][]
88
### Added
99
- PostgreSQL support
10+
- `/backups` Docker volume
11+
12+
### Changed
13+
- Use `bash` instead of `ash`
14+
- Local backups go to `/backups`, not `/tmp`
15+
16+
### Fixed
17+
- Apply updates for `db` and `ncurses`
1018

1119
## [0.2.1][]
1220
### Fixed
@@ -26,8 +34,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2634
### Fixed
2735
- OS package security updates
2836

29-
## [0.1.0][]
37+
## 0.1.0
3038
### Added
3139
- Mongo, MySQL, and file backup
3240
- Upload to Softlayer
3341
- Travis CI for continuous integration
42+
43+
[Unreleased]: https://github.ibm.com/bdu/gamora/compare/0.2.1...HEAD
44+
[0.2.1]: https://github.ibm.com/bdu/gamora/compare/0.2.0...0.2.1
45+
[0.2.0]: https://github.ibm.com/bdu/gamora/compare/0.1.7...0.2.0
46+
[0.1.7]: https://github.ibm.com/bdu/gamora/compare/0.1.6...0.1.7
47+
[0.1.6]: https://github.ibm.com/bdu/gamora/compare/0.1.0...0.1.6

gamora/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
FROM python:2.7.13-alpine3.6
33
44

5-
# install mongodump and mongorestore
5+
# Install database clients
6+
# Apply security patches
67
RUN echo 'http://dl-3.alpinelinux.org/alpine/edge/community' >> /etc/apk/repositories \
78
&& echo 'http://dl-3.alpinelinux.org/alpine/edge/main' >> /etc/apk/repositories \
89
&& apk add --no-cache \
10+
bash \
911
libressl \
1012
mariadb-client \
1113
mongodb-tools \
1214
postgresql \
1315
&& apk add --upgrade --no-cache \
16+
db \
1417
expat \
1518
freetype \
1619
fontconfig \
1720
libpng \
21+
ncurses \
1822
zlib
1923

2024
VOLUME ["/backups"]
@@ -28,4 +32,4 @@ COPY requirements.txt ./
2832
RUN pip install -r ./requirements.txt --find-links ./pip
2933

3034
COPY ./docker-entrypoint.sh ./entrypoint.sh
31-
CMD ["/bin/ash", "/gamora/entrypoint.sh"]
35+
CMD ["/bin/bash", "/gamora/entrypoint.sh"]

gamora/Dockerfile.build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
FROM python:2.7.13-alpine3.6
33
44

5-
# install mongodump and mongorestore
5+
# Install database clients
6+
# Apply security patches
67
RUN echo 'http://dl-3.alpinelinux.org/alpine/edge/community' >> /etc/apk/repositories \
78
&& echo 'http://dl-3.alpinelinux.org/alpine/edge/main' >> /etc/apk/repositories \
89
&& apk add --no-cache \
10+
bash \
911
libressl \
1012
mariadb-client \
1113
mongodb-tools \
1214
postgresql \
1315
&& apk add --upgrade --no-cache \
16+
db \
1417
expat \
1518
freetype \
1619
fontconfig \
1720
libpng \
21+
ncurses \
1822
zlib
1923

2024
ARG PIP_REPO_HOST_WITH_CREDS

gamora/docker-entrypoint.sh

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/bin/bash
22
set -e
33

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+
412
log() {
513
local message
614
message=$1
@@ -19,48 +27,52 @@ upload_backup() {
1927
remote_path="${SOFTLAYER_PATH:?}/$(date +%Y/%m)"
2028

2129
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:?}" \
2836
"${remote_path:?}/${filename:?}" \
29-
&& rm -f /tmp/"${filename:?}"
37+
&& rm -f "${BACKUP_PATH:?}/${filename:?}"
3038
log "Done: Uploading backup"
3139
}
3240

3341
back_up_mongo() {
42+
local filename
43+
3444
if [[ -z "${MONGO_HOST:-}" ]]; then
3545
log "Skip backing up Mongo because no Mongo host specified"
3646
return
3747
fi
3848

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
4050

4151
log "Taking mongo backup"
42-
monsoon ${NOTIFICATION_SETTINGS} backup mongo \
52+
monsoon "${NOTIFICATION_SETTINGS[@]}" backup mongo \
4353
-u "${MONGO_BACKUP_USER}" \
4454
-p "${MONGO_BACKUP_PASSWORD}" \
4555
--host="${MONGO_HOST}" \
46-
--archive=/tmp/"${FILENAME}" \
56+
--archive="${BACKUP_PATH:?}/${filename}" \
4757
--gzip
4858
log "Done: Taking mongo backup"
4959

50-
upload_backup "${FILENAME:?}"
60+
upload_backup "${filename:?}"
5161
}
5262

5363
back_up_mysql() {
64+
local filename
65+
5466
if [[ -z "${MYSQL_HOST:-}" ]]; then
5567
log "Skip backing up MySQL because no MySQL host specified"
5668
return
5769
fi
5870

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
6072

6173
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}" \
6476
--gzip \
6577
--all-databases \
6678
--single-transaction \
@@ -70,7 +82,7 @@ back_up_mysql() {
7082
"--password=${MYSQL_PASSWORD:?}"
7183
log "Done: Taking mysql backup"
7284

73-
upload_backup "${FILENAME:?}"
85+
upload_backup "${filename:?}"
7486
}
7587

7688
# PGDATABASE: gru,nsa,picard,pony,savant,sentry,usher
@@ -80,63 +92,59 @@ back_up_mysql() {
8092
# PGUSER: dswb
8193

8294
back_up_postgresql() {
95+
local filename
8396
local databases
97+
8498
if [[ -z "${PGHOST:-}" ]]; then
8599
log "Skip backing up PostgreSQL because no PostgreSQL host specified"
86100
return
87101
fi
88102

89-
databases="$(echo ${PGDATABASE} | tr ',' ' ')"
103+
databases="$(echo "${PGDATABASE:?}" | tr ',' ' ')"
90104

91105
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
93109

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}" \
97113
--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:?}"
107120
done
108121
}
109122

110123
back_up_files() {
124+
local filename
125+
111126
if [[ -z "${BACKUP_LOCAL_PATHS:-}" ]]; then
112127
log "Skip backing up files because no local paths specified"
113128
return
114129
fi
115130

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
117132

118133
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}" \
121137
${BACKUP_LOCAL_PATHS:?} # space-separated list
122138
log "Done: Taking file backup"
123139

124-
upload_backup "${FILENAME:?}"
140+
upload_backup "${filename:?}"
125141
}
126142

127143
main() {
144+
back_up_files
128145
back_up_mongo
129146
back_up_mysql
130-
back_up_files
147+
back_up_postgresql
131148
}
132149

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-
142150
main

0 commit comments

Comments
 (0)