Skip to content

Commit 356dd6e

Browse files
author
willemvd
committed
add mail sending in case of errors
1 parent dcfb3da commit 356dd6e

File tree

4 files changed

+109
-9
lines changed

4 files changed

+109
-9
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.4
1+
FROM alpine:3.5
22
MAINTAINER Willem willemvd@github
33

44
COPY backup.sh /tmp/backup.sh
@@ -11,7 +11,8 @@ RUN apk --no-cache add \
1111
python \
1212
py-pip \
1313
postgresql \
14-
openssl && \
14+
openssl \
15+
heirloom-mailx && \
1516
pip install --upgrade pip awscli && \
1617
chmod -R 777 /tmp
1718

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ AWS_ACCESS_KEY_ID=<access key id got from Amazon>
2929
AWS_SECRET_ACCESS_KEY=<secret access key got from Amazon>
3030
AWS_DEFAULT_REGION=<Amazon region name e.g. eu-central-1>
3131
S3_BUCKET_NAME=<name of the bucket in S3>
32+
33+
# Email settings
34+
ENABLE_ERROR_MAIL=<should an email be send when something goes wrong? other then true will disable this>
35+
ERROR_MAIL_SUBJECT=<subject of the mail, e.g. Failed to run database backup>
36+
SMTP_HOST=<SMTP host e.g. smtp.office365.com>
37+
SMTP_PORT=<SMTP port number, mostly 587 or 25>
38+
SMTP_STARTTLS=<should TLS be used? other then true will disable this>
39+
SMTP_AUTH=<type of auth, login>
40+
SMTP_AUTH_USER=<username to login to the SMTP server>
41+
SMTP_AUTH_PASSWORD=<password for the SMTP user>
42+
SMTP_FROM=<name and email of the sender in the format: Name <full email adres> >
43+
SMTP_TO=<email address to send to, multiple addresses can be used with a comma seperated list>
3244
```

backup.sh

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
#!/bin/sh
22

3-
#exit if one of the commands fail
43
set -e
54

5+
handleError() {
6+
MSG="Failed to run backup of database '$PGDATABASE' from '$PGHOST:$PGPORT/$PGDATABASE' with user '$PGUSER' to 's3://$S3_BUCKET_NAME/$BACKUP_TYPE/$BACKUP_FILE_NAME'"
7+
8+
if [ "x$ENABLE_ERROR_MAIL" = "xtrue" ]; then
9+
10+
OPTS=""
11+
if [ "x$SMTP_STARTTLS" = "xtrue" ]; then
12+
OPTS="-S smtp-use-starttls";
13+
fi
14+
15+
echo ${MSG} | \
16+
17+
mailx -v -s "$ERROR_MAIL_SUBJECT" \
18+
-S smtp="$SMTP_HOST:$SMTP_PORT" \
19+
${OPTS} \
20+
-S smtp-auth=$SMTP_AUTH \
21+
-S smtp-auth-user="$SMTP_AUTH_USER" \
22+
-S smtp-auth-password="$SMTP_AUTH_PASSWORD" \
23+
-r "$SMTP_FROM" \
24+
$SMTP_TO
25+
else
26+
echo ${MSG}
27+
fi
28+
29+
# abort
30+
exit 1
31+
}
32+
633
DATE=$(date +%Y-%m-%d-%H-%M-%S)
734

835
# backup type is used to seperate types like hourly or daily (folders in s3 bucket should be named like that)
@@ -21,7 +48,7 @@ START_DUMP=$(date +%s)
2148
#PGPASSWORD=
2249
#PGHOST=
2350
#PGPORT=
24-
pg_dump --format=custom > $BACKUP_FILE_NAME
51+
pg_dump --format=custom > $BACKUP_FILE_NAME || handleError
2552

2653
DONE_DUMP_IN=$(($(date +%s)-$START_DUMP))
2754
echo "Done dumping ($DONE_DUMP_IN sec) database"
@@ -36,12 +63,12 @@ ENCRYPTION_FILE_NAME_SUFFIX=.enc
3663

3764
echo "Start encrypting database dump file '$BACKUP_FILE_NAME' with cipher type '$OPENSSL_CIPHER_TYPE' and password as defined in environment variable ENCRYPTION_PASS_PHRASE"
3865
START_ENC=$(date +%s)
39-
openssl enc -in $BACKUP_FILE_NAME -out $BACKUP_FILE_NAME$ENCRYPTION_FILE_NAME_SUFFIX -e -$OPENSSL_CIPHER_TYPE -pass pass:$ENCRYPTION_PASS_PHRASE
66+
openssl enc -in $BACKUP_FILE_NAME -out $BACKUP_FILE_NAME$ENCRYPTION_FILE_NAME_SUFFIX -e -$OPENSSL_CIPHER_TYPE -pass pass:$ENCRYPTION_PASS_PHRASE || handleError
4067

4168
DONE_ENC_IN=$(($(date +%s)-$START_ENC))
4269
echo "Done encrypting ($DONE_ENC_IN sec) database dump file, now remove unencrypted file"
4370

44-
rm -f $BACKUP_FILE_NAME
71+
rm -fv $BACKUP_FILE_NAME || handleError
4572

4673
echo ""
4774

@@ -55,7 +82,7 @@ START_UPLOAD=$(date +%s)
5582

5683
# bucket name to upload to
5784
#S3_BUCKET_NAME=
58-
aws s3 cp ./$BACKUP_FILE_NAME$ENCRYPTION_FILE_NAME_SUFFIX s3://$S3_BUCKET_NAME/$BACKUP_TYPE/$BACKUP_FILE_NAME
85+
aws s3 cp ./$BACKUP_FILE_NAME$ENCRYPTION_FILE_NAME_SUFFIX s3://$S3_BUCKET_NAME/$BACKUP_TYPE/$BACKUP_FILE_NAME || handleError
5986

6087
DONE_UPLOAD_IN=$(($(date +%s)-$START_UPLOAD))
6188
echo "Done uploading ($DONE_UPLOAD_IN sec) 's3://$S3_BUCKET_NAME/$BACKUP_TYPE/$BACKUP_FILE_NAME'"

openshift-postgresql-s3-backup-scheduledJob.yaml

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ items:
1616
data:
1717
# don't forget to base64 encode your values
1818
encryption-pass-phrase: ""
19+
- apiVersion: "v1"
20+
kind: "Secret"
21+
metadata:
22+
name: "smtp-authentication-secret-production"
23+
data:
24+
# don't forget to base64 encode your values
25+
smtp-user: ""
26+
smtp-password: ""
1927
- apiVersion: batch/v2alpha1
2028
kind: ScheduledJob
2129
metadata:
@@ -31,7 +39,7 @@ items:
3139
activeDeadlineSeconds: 1800
3240
containers:
3341
- name: postgresql-s3-hourly-backup
34-
image: willemvd/postgresql-client-side-encrypted-s3-backup
42+
image: willemvd/postgresql-client-side-encrypted-s3-backup:1.1.0
3543
env:
3644
- name: PGDATABASE
3745
value:
@@ -66,6 +74,32 @@ items:
6674
value:
6775
- name: BACKUP_TYPE
6876
value: hourly
77+
- name: ENABLE_ERROR_MAIL
78+
value: "true"
79+
- name: ERROR_MAIL_SUBJECT
80+
value: Failed to run database backup
81+
- name: SMTP_HOST
82+
value: ""
83+
- name: SMTP_PORT
84+
value: ""
85+
- name: SMTP_STARTTLS
86+
value: "true"
87+
- name: SMTP_AUTH
88+
value:
89+
- name: SMTP_AUTH_USER
90+
valueFrom:
91+
secretKeyRef:
92+
name: smtp-authentication-secret-production
93+
key: smtp-user
94+
- name: SMTP_AUTH_PASSWORD
95+
valueFrom:
96+
secretKeyRef:
97+
name: smtp-authentication-secret-production
98+
key: smtp-password
99+
- name: SMTP_FROM
100+
value: ""
101+
- name: SMTP_TO
102+
value: ""
69103
restartPolicy: OnFailure
70104
- apiVersion: batch/v2alpha1
71105
kind: ScheduledJob
@@ -82,7 +116,7 @@ items:
82116
activeDeadlineSeconds: 1800
83117
containers:
84118
- name: postgresql-s3-daily-backup
85-
image: willemvd/postgresql-client-side-encrypted-s3-backup
119+
image: willemvd/postgresql-client-side-encrypted-s3-backup:1.1.0
86120
env:
87121
- name: PGDATABASE
88122
value:
@@ -117,4 +151,30 @@ items:
117151
value:
118152
- name: BACKUP_TYPE
119153
value: daily
154+
- name: ENABLE_ERROR_MAIL
155+
value: "true"
156+
- name: ERROR_MAIL_SUBJECT
157+
value: Failed to run database backup
158+
- name: SMTP_HOST
159+
value: ""
160+
- name: SMTP_PORT
161+
value: ""
162+
- name: SMTP_STARTTLS
163+
value: "true"
164+
- name: SMTP_AUTH
165+
value:
166+
- name: SMTP_AUTH_USER
167+
valueFrom:
168+
secretKeyRef:
169+
name: smtp-authentication-secret-production
170+
key: smtp-user
171+
- name: SMTP_AUTH_PASSWORD
172+
valueFrom:
173+
secretKeyRef:
174+
name: smtp-authentication-secret-production
175+
key: smtp-password
176+
- name: SMTP_FROM
177+
value: ""
178+
- name: SMTP_TO
179+
value: ""
120180
restartPolicy: OnFailure

0 commit comments

Comments
 (0)