Skip to content

Commit 208bfd2

Browse files
committed
initial commit
0 parents  commit 208bfd2

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM alpine:latest
2+
LABEL maintainer="Johannes Schickling <schickling.j@gmail.com>"
3+
4+
ADD install.sh install.sh
5+
RUN sh install.sh && rm install.sh
6+
7+
ENV MYSQLDUMP_OPTIONS --quote-names --quick --add-drop-table --add-locks --allow-keywords --disable-keys --extended-insert --single-transaction --create-options --comments --net_buffer_length=16384
8+
ENV MYSQLDUMP_DATABASE --all-databases
9+
ENV MYSQL_HOST **None**
10+
ENV MYSQL_PORT 3306
11+
ENV MYSQL_USER **None**
12+
ENV MYSQL_PASSWORD **None**
13+
ENV S3_ACCESS_KEY_ID **None**
14+
ENV S3_SECRET_ACCESS_KEY **None**
15+
ENV S3_BUCKET **None**
16+
ENV S3_REGION us-west-1
17+
ENV S3_ENDPOINT **None**
18+
ENV S3_S3V4 no
19+
ENV S3_PREFIX 'backup'
20+
ENV S3_FILENAME **None**
21+
ENV MULTI_FILES no
22+
ENV SCHEDULE **None**
23+
24+
ADD run.sh run.sh
25+
ADD backup.sh backup.sh
26+
27+
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 (c) 2017 Johannes Schickling
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
> Forked from https://github.com/schickling/dockerfiles/tree/master/mysql-backup-s3
2+
3+
# mysql-backup-s3
4+
5+
Backup MySQL to S3 (supports periodic backups & mutli files)
6+
7+
## Basic usage
8+
9+
```sh
10+
$ docker run -e S3_ACCESS_KEY_ID=key -e S3_SECRET_ACCESS_KEY=secret -e S3_BUCKET=my-bucket -e S3_PREFIX=backup -e MYSQL_USER=user -e MYSQL_PASSWORD=password -e MYSQL_HOST=localhost schickling/mysql-backup-s3
11+
```
12+
13+
## Environment variables
14+
15+
- `MYSQLDUMP_OPTIONS` mysqldump options (default: --quote-names --quick --add-drop-table --add-locks --allow-keywords --disable-keys --extended-insert --single-transaction --create-options --comments --net_buffer_length=16384)
16+
- `MYSQLDUMP_DATABASE` list of databases you want to backup (default: --all-databases)
17+
- `MYSQL_HOST` the mysql host *required*
18+
- `MYSQL_PORT` the mysql port (default: 3306)
19+
- `MYSQL_USER` the mysql user *required*
20+
- `MYSQL_PASSWORD` the mysql password *required*
21+
- `S3_ACCESS_KEY_ID` your AWS access key *required*
22+
- `S3_SECRET_ACCESS_KEY` your AWS secret key *required*
23+
- `S3_BUCKET` your AWS S3 bucket path *required*
24+
- `S3_PREFIX` path prefix in your bucket (default: 'backup')
25+
- `S3_FILENAME` a consistent filename to overwrite with your backup. If not set will use a timestamp.
26+
- `S3_REGION` the AWS S3 bucket region (default: us-west-1)
27+
- `S3_ENDPOINT` the AWS Endpoint URL, for S3 Compliant APIs such as [minio](https://minio.io) (default: none)
28+
- `S3_S3V4` set to `yes` to enable AWS Signature Version 4, required for [minio](https://minio.io) servers (default: no)
29+
- `MULTI_FILES` Allow to have one file per database if set `yes` default: no)
30+
- `SCHEDULE` backup schedule time, see explainatons below
31+
32+
### Automatic Periodic Backups
33+
34+
You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="@daily"` to run the backup automatically.
35+
36+
More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).

backup.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#! /bin/sh
2+
3+
set -e
4+
5+
if [ "${S3_ACCESS_KEY_ID}" == "**None**" ]; then
6+
echo "Warning: You did not set the S3_ACCESS_KEY_ID environment variable."
7+
fi
8+
9+
if [ "${S3_SECRET_ACCESS_KEY}" == "**None**" ]; then
10+
echo "Warning: You did not set the S3_SECRET_ACCESS_KEY environment variable."
11+
fi
12+
13+
if [ "${S3_BUCKET}" == "**None**" ]; then
14+
echo "You need to set the S3_BUCKET environment variable."
15+
exit 1
16+
fi
17+
18+
if [ "${MYSQL_HOST}" == "**None**" ]; then
19+
echo "You need to set the MYSQL_HOST environment variable."
20+
exit 1
21+
fi
22+
23+
if [ "${MYSQL_USER}" == "**None**" ]; then
24+
echo "You need to set the MYSQL_USER environment variable."
25+
exit 1
26+
fi
27+
28+
if [ "${MYSQL_PASSWORD}" == "**None**" ]; then
29+
echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL."
30+
exit 1
31+
fi
32+
33+
if [ "${S3_IAMROLE}" != "true" ]; then
34+
# env vars needed for aws tools - only if an IAM role is not used
35+
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
36+
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
37+
export AWS_DEFAULT_REGION=$S3_REGION
38+
fi
39+
40+
MYSQL_HOST_OPTS="-h $MYSQL_HOST -P $MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD"
41+
DUMP_START_TIME=$(date +"%Y-%m-%dT%H%M%SZ")
42+
43+
copy_s3 () {
44+
SRC_FILE=$1
45+
DEST_FILE=$2
46+
47+
if [ "${S3_ENDPOINT}" == "**None**" ]; then
48+
AWS_ARGS=""
49+
else
50+
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
51+
fi
52+
53+
echo "Uploading ${DEST_FILE} on S3..."
54+
55+
cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
56+
57+
if [ $? != 0 ]; then
58+
>&2 echo "Error uploading ${DEST_FILE} on S3"
59+
fi
60+
61+
rm $SRC_FILE
62+
}
63+
# Multi file: yes
64+
if [ ! -z "$(echo $MULTI_FILES | grep -i -E "(yes|true|1)")" ]; then
65+
if [ "${MYSQLDUMP_DATABASE}" == "--all-databases" ]; then
66+
DATABASES=`mysql $MYSQL_HOST_OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys|innodb)"`
67+
else
68+
DATABASES=$MYSQLDUMP_DATABASE
69+
fi
70+
71+
for DB in $DATABASES; do
72+
echo "Creating individual dump of ${DB} from ${MYSQL_HOST}..."
73+
74+
DUMP_FILE="/tmp/${DB}.sql.gz"
75+
76+
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS --databases $DB | gzip > $DUMP_FILE
77+
78+
if [ $? == 0 ]; then
79+
if [ "${S3_FILENAME}" == "**None**" ]; then
80+
S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
81+
else
82+
S3_FILE="${S3_FILENAME}.${DB}.sql.gz"
83+
fi
84+
85+
copy_s3 $DUMP_FILE $S3_FILE
86+
else
87+
>&2 echo "Error creating dump of ${DB}"
88+
fi
89+
done
90+
# Multi file: no
91+
else
92+
echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..."
93+
94+
DUMP_FILE="/tmp/dump.sql.gz"
95+
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > $DUMP_FILE
96+
97+
if [ $? == 0 ]; then
98+
if [ "${S3_FILENAME}" == "**None**" ]; then
99+
S3_FILE="${DUMP_START_TIME}.dump.sql.gz"
100+
else
101+
S3_FILE="${S3_FILENAME}.sql.gz"
102+
fi
103+
104+
copy_s3 $DUMP_FILE $S3_FILE
105+
else
106+
>&2 echo "Error creating dump of all databases"
107+
fi
108+
fi
109+
110+
echo "SQL backup finished"

install.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /bin/sh
2+
3+
# exit if a command fails
4+
set -e
5+
6+
7+
apk update
8+
9+
# install mysqldump
10+
apk add mysql-client
11+
12+
# install s3 tools
13+
apk add python py-pip
14+
pip install awscli
15+
apk del py-pip
16+
17+
# install go-cron
18+
apk add curl
19+
curl -L --insecure https://github.com/odise/go-cron/releases/download/v0.0.6/go-cron-linux.gz | zcat > /usr/local/bin/go-cron
20+
chmod u+x /usr/local/bin/go-cron
21+
apk del curl
22+
23+
24+
# cleanup
25+
rm -rf /var/cache/apk/*

0 commit comments

Comments
 (0)