diff --git a/README.md b/README.md index 442c9df..42d593b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Collection of lightweight and ready-to-use docker images * **[Jekyll](https://github.com/schickling/dockerfiles/tree/master/jekyll)** - Lightweight jekyll working environment * **[Latex](https://github.com/schickling/dockerfiles/tree/master/latex)** - Full texlive distribution * **[Mailcatcher](https://github.com/schickling/dockerfiles/tree/master/mailcatcher)** - Extra small mailcatcher image +* **[mysql-backup-s3](https://github.com/schickling/dockerfiles/tree/master/mysql-backup-s3)** - Backup MySQL to S3 (supports periodic backups) * **[NodeJS](https://github.com/schickling/dockerfiles/tree/master/nodejs)** - Minimal NodeJS + NPM image * **[Octave](https://github.com/schickling/dockerfiles/tree/master/octave)** - Lightweight ocatve development environment * **[OpenCV](https://github.com/schickling/dockerfiles/tree/master/opencv)** - Lightweight ready-to use OpenCV image diff --git a/mysql-backup-s3/Dockerfile b/mysql-backup-s3/Dockerfile new file mode 100644 index 0000000..5ebf450 --- /dev/null +++ b/mysql-backup-s3/Dockerfile @@ -0,0 +1,23 @@ +FROM alpine:3.1 +MAINTAINER Johannes Schickling "schickling.j@gmail.com" + +ADD install.sh install.sh +RUN sh install.sh && rm install.sh + +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 +ENV MYSQLDUMP_DATABASE --all-databases +ENV MYSQL_HOST **None** +ENV MYSQL_PORT 3306 +ENV MYSQL_USER **None** +ENV MYSQL_PASSWORD **None** +ENV S3_ACCESS_KEY_ID **None** +ENV S3_SECRET_ACCESS_KEY **None** +ENV S3_BUCKET **None** +ENV S3_PATH 'backup' +ENV S3_REGION us-west-1 +ENV SCHEDULE **None** + +ADD run.sh run.sh +ADD backup.sh backup.sh + +CMD ["sh", "run.sh"] diff --git a/mysql-backup-s3/README.md b/mysql-backup-s3/README.md new file mode 100644 index 0000000..3d652e9 --- /dev/null +++ b/mysql-backup-s3/README.md @@ -0,0 +1,15 @@ +# mysql-backup-s3 + +Backup MySQL to S3 (supports periodic backups) + +## Usage + +```sh +$ 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 +``` + +### Automatic Periodic Backups + +You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="@daily"` to run the backup automatically. + +More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules). diff --git a/mysql-backup-s3/backup.sh b/mysql-backup-s3/backup.sh new file mode 100644 index 0000000..bfb2ff2 --- /dev/null +++ b/mysql-backup-s3/backup.sh @@ -0,0 +1,50 @@ +#! /bin/sh + +set -e + +if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then + echo "You need to set the S3_ACCESS_KEY_ID environment variable." + exit 1 +fi + +if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then + echo "You need to set the S3_SECRET_ACCESS_KEY environment variable." + exit 1 +fi + +if [ "${S3_BUCKET}" = "**None**" ]; then + echo "You need to set the S3_BUCKET environment variable." + exit 1 +fi + +if [ -z "${MYSQL_HOST}" ]; then + echo "You need to set the MYSQL_HOST environment variable." + exit 1 +fi + +if [ -z "${MYSQL_USER}" ]; then + echo "You need to set the MYSQL_USER environment variable." + exit 1 +fi + +if [ -z "${MYSQL_PASSWORD}" ]; then + echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL." + exit 1 +fi + +# env vars needed for aws tools +export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID +export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY +export AWS_DEFAULT_REGION=$S3_REGION + +MYSQL_HOST_OPTS="-h $MYSQL_HOST --port $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD" + +echo "Creating dump of ${MYSQLDUMP_DATABASE} database(s) from ${MYSQL_HOST}..." + +mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > dump.sql.gz + +echo "Uploading dump to $S3_BUCKET" + +cat dump.sql.gz | aws s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$(date +"%Y")/$(date +"%m")/$(date +"%d").sql.gz || exit 2 + +echo "SQL backup uploaded successfully" diff --git a/mysql-backup-s3/install.sh b/mysql-backup-s3/install.sh new file mode 100644 index 0000000..eda916a --- /dev/null +++ b/mysql-backup-s3/install.sh @@ -0,0 +1,25 @@ +#! /bin/sh + +# exit if a command fails +set -e + + +apk update + +# install mysqldump +apk add mysql-client + +# install s3 tools +apk add python py-pip +pip install awscli +apk del py-pip + +# install go-cron +apk add curl +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 +apk del curl + + +# cleanup +rm -rf /var/cache/apk/* diff --git a/mysql-backup-s3/run.sh b/mysql-backup-s3/run.sh new file mode 100644 index 0000000..05dfcaa --- /dev/null +++ b/mysql-backup-s3/run.sh @@ -0,0 +1,9 @@ +#! /bin/sh + +set -e + +if [ "${SCHEDULE}" = "**None**" ]; then + sh backup.sh +else + exec go-cron "$SCHEDULE" /bin/sh backup.sh +fi