Skip to content

Commit 0e52741

Browse files
committed
Initial Commit
0 parents  commit 0e52741

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM alpine:3.9
2+
LABEL maintainer="ITBM"
3+
4+
RUN apk update \
5+
&& apk add postgresql-client \
6+
&& apk add python py2-pip && pip install awscli && apk del py2-pip \
7+
&& apk add openssl \
8+
&& apk add curl \
9+
&& curl -L --insecure https://github.com/odise/go-cron/releases/download/v0.0.6/go-cron-linux.gz | zcat > /usr/local/bin/go-cron && chmod u+x /usr/local/bin/go-cron \
10+
&& apk del curl \
11+
&& rm -rf /var/cache/apk/*
12+
13+
ENV POSTGRES_DATABASE **None**
14+
ENV POSTGRES_HOST **None**
15+
ENV POSTGRES_PORT 5432
16+
ENV POSTGRES_USER **None**
17+
ENV POSTGRES_PASSWORD **None**
18+
ENV POSTGRES_EXTRA_OPTS ''
19+
ENV S3_ACCESS_KEY_ID **None**
20+
ENV S3_SECRET_ACCESS_KEY **None**
21+
ENV S3_BUCKET **None**
22+
ENV S3_REGION us-west-1
23+
ENV S3_PREFIX 'backup'
24+
ENV S3_ENDPOINT **None**
25+
ENV S3_S3V4 no
26+
ENV SCHEDULE **None**
27+
ENV ENCRYPTION_PASSWORD **None**
28+
ENV DELETE_OLDER_THAN **None**
29+
30+
ADD run.sh run.sh
31+
ADD backup.sh backup.sh
32+
33+
CMD ["sh", "run.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright for portions of project mysql-backup-s3 are held by Johannes Schickling, 2017 as part of project Dockerfiles. All other copyright for project mysql-back-s3 are held by ITBM, 2019.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# postgres-backup-s3
2+
3+
Backup PostgresSQL to S3 (supports periodic backups)
4+
5+
## Basic Usage
6+
7+
```sh
8+
$ docker run -e S3_ACCESS_KEY_ID=key -e S3_SECRET_ACCESS_KEY=secret -e S3_BUCKET=my-bucket -e S3_PREFIX=backup -e POSTGRES_DATABASE=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_HOST=localhost itbm/postgres-backup-s3
9+
```
10+
11+
## Kubernetes Deployment
12+
13+
```
14+
apiVersion: v1
15+
kind: Namespace
16+
metadata:
17+
name: backup
18+
19+
---
20+
apiVersion: apps/v1
21+
kind: Deployment
22+
metadata:
23+
name: postgresql
24+
namespace: backup
25+
spec:
26+
selector:
27+
matchLabels:
28+
app: postgresql
29+
strategy:
30+
type: Recreate
31+
template:
32+
metadata:
33+
labels:
34+
app: postgresql
35+
spec:
36+
containers:
37+
- name: postgresql
38+
image: itbm/postgresql-backup-s3
39+
imagePullPolicy: Always
40+
env:
41+
- name: POSTGRES_DATABASE
42+
value: ""
43+
- name: POSTGRES_HOST
44+
value: ""
45+
- name: POSTGRES_PORT
46+
value: ""
47+
- name: POSTGRES_PASSWORD
48+
value: ""
49+
- name: POSTGRES_USER
50+
value: ""
51+
- name: S3_ACCESS_KEY_ID
52+
value: ""
53+
- name: S3_SECRET_ACCESS_KEY
54+
value: ""
55+
- name: S3_BUCKET
56+
value: ""
57+
- name: S3_ENDPOINT
58+
value: ""
59+
- name: S3_PREFIX
60+
value: ""
61+
- name: SCHEDULE
62+
value: ""
63+
```
64+
65+
## Environment variables
66+
67+
- `POSTGRES_DATABASE` list of databases you want to backup (default: --all-databases)
68+
- `POSTGRES_HOST` the postgresql host *required*
69+
- `POSTGRES_PORT` the postgresql port (default: )
70+
- `POSTGRES_USER` the postgresql user *required*
71+
- `POSTGRES_PASSWORD` the postgresql password *required*
72+
- `POSTGRES_EXTRA_OPTS` extra postgresql options
73+
- `S3_ACCESS_KEY_ID` your AWS access key *required*
74+
- `S3_SECRET_ACCESS_KEY` your AWS secret key *required*
75+
- `S3_BUCKET` your AWS S3 bucket path *required*
76+
- `S3_PREFIX` path prefix in your bucket (default: 'backup')
77+
- `S3_REGION` the AWS S3 bucket region (default: us-west-1)
78+
- `S3_ENDPOINT` the AWS Endpoint URL, for S3 Compliant APIs such as [minio](https://minio.io) (default: none)
79+
- `S3_S3V4` set to `yes` to enable AWS Signature Version 4, required for [minio](https://minio.io) servers (default: no)
80+
- `SCHEDULE` backup schedule time, see explainatons below
81+
- `ENCRYPTION_PASSWORD` password to encrypt the backup. Can be decrypted using `openssl aes-256-cbc -d -in backup.sql.gz.enc -out backup.sql.gz`
82+
- `DELETE_OLDER_THAN` delete old backups, see explanation and warning below
83+
84+
### Automatic Periodic Backups
85+
86+
You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="@daily"` to run the backup automatically.
87+
88+
More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).
89+
90+
### Delete Old Backups
91+
92+
You can additionally set the `DELETE_OLDER_THAN` environment variable like `-e DELETE_OLDER_THAN="30 days ago"` to delete old backups.
93+
94+
WARNING: this will delete all files in the S3_PREFIX path, not just those created by this script.
95+
96+
### Encryption
97+
98+
You can additionally set the `ENCRYPTION_PASSWORD` environment variable like `-e ENCRYPTION_PASSWORD="superstrongpassword"` to encrypt the backup. It can be decrypted using `openssl aes-256-cbc -d -in backup.sql.gz.enc -out backup.sql.gz`.

backup.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#! /bin/sh
2+
3+
set -e
4+
set -o pipefail
5+
6+
if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then
7+
echo "You need to set the S3_ACCESS_KEY_ID environment variable."
8+
exit 1
9+
fi
10+
11+
if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then
12+
echo "You need to set the S3_SECRET_ACCESS_KEY environment variable."
13+
exit 1
14+
fi
15+
16+
if [ "${S3_BUCKET}" = "**None**" ]; then
17+
echo "You need to set the S3_BUCKET environment variable."
18+
exit 1
19+
fi
20+
21+
if [ "${POSTGRES_DATABASE}" = "**None**" ]; then
22+
echo "You need to set the POSTGRES_DATABASE environment variable."
23+
exit 1
24+
fi
25+
26+
if [ "${POSTGRES_HOST}" = "**None**" ]; then
27+
if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
28+
POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
29+
POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
30+
else
31+
echo "You need to set the POSTGRES_HOST environment variable."
32+
exit 1
33+
fi
34+
fi
35+
36+
if [ "${POSTGRES_USER}" = "**None**" ]; then
37+
echo "You need to set the POSTGRES_USER environment variable."
38+
exit 1
39+
fi
40+
41+
if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
42+
echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES."
43+
exit 1
44+
fi
45+
46+
if [ "${S3_ENDPOINT}" == "**None**" ]; then
47+
AWS_ARGS=""
48+
else
49+
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
50+
fi
51+
52+
# env vars needed for aws tools
53+
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
54+
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
55+
export AWS_DEFAULT_REGION=$S3_REGION
56+
57+
export PGPASSWORD=$POSTGRES_PASSWORD
58+
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
59+
60+
echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
61+
62+
SRC_FILE=dump.sql.gz
63+
DEST_FILE=${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
64+
65+
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | gzip > $SRC_FILE
66+
67+
if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
68+
echo "Encrypting ${SRC_FILE}"
69+
openssl enc -aes-256-cbc -in $SRC_FILE -out ${SRC_FILE}.enc -k $ENCRYPTION_PASSWORD
70+
if [ $? != 0 ]; then
71+
>&2 echo "Error encrypting ${SRC_FILE}"
72+
fi
73+
rm $SRC_FILE
74+
SRC_FILE="${SRC_FILE}.enc"
75+
DEST_FILE="${DEST_FILE}.enc"
76+
fi
77+
78+
echo "Uploading dump to $S3_BUCKET"
79+
80+
cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE || exit 2
81+
82+
if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
83+
aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | grep " PRE " -v | while read -r line;
84+
do
85+
created=`echo $line|awk {'print $1" "$2'}`
86+
created=`date -d "$created" +%s`
87+
older_than=`date -d "$DELETE_OLDER_THAN" +%s`
88+
if [ $created -lt $older_than ]
89+
then
90+
fileName=`echo $line|awk {'print $4'}`
91+
if [ $fileName != "" ]
92+
then
93+
printf 'Deleting "%s"\n' $fileName
94+
aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
95+
fi
96+
fi
97+
done;
98+
fi
99+
100+
echo "SQL backup uploaded successfully"

run.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#! /bin/sh
2+
3+
set -e
4+
5+
if [ "${S3_S3V4}" = "yes" ]; then
6+
aws configure set default.s3.signature_version s3v4
7+
fi
8+
9+
if [ "${SCHEDULE}" = "**None**" ]; then
10+
sh backup.sh
11+
else
12+
exec go-cron "$SCHEDULE" /bin/sh backup.sh
13+
fi

0 commit comments

Comments
 (0)