Skip to content

Commit

Permalink
Merge pull request #20 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 3.1.0
  • Loading branch information
dalenewby authored May 6, 2024
2 parents 99e7336 + ac8aefa commit 0ecc492
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FROM python:3-alpine

# Variables set with ARG can be overridden at image build time with
# "--build-arg var=value". They are not available in the running container.
ARG B2_VERSION=v3.19.1

# Current version of s3cmd is in edge/testing repo
RUN echo https://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories

Expand All @@ -11,7 +15,10 @@ RUN apk update \
py3-magic \
py3-dateutil \
py3-six \
s3cmd
s3cmd \
&& wget -O /usr/local/bin/b2 \
https://github.com/Backblaze/B2_Command_Line_Tool/releases/download/${B2_VERSION}/b2-linux \
&& chmod +x /usr/local/bin/b2

COPY application/ /data/
WORKDIR /data
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
start: restore backup

restore: db
docker-compose up -d restore
docker compose up -d restore

backup: db
docker-compose up -d backup
docker compose up -d backup

db:
docker-compose up -d db phpmyadmin
docker compose up -d db phpmyadmin

clean:
docker-compose kill
docker compose kill
docker system prune -f
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# mysql-backup-restore
Service to backup and/or restore mysql databases to/from S3
Service to backup and/or restore mysql databases to/from S3 and optionally to B2

## How to use it
1. Create an S3 bucket to hold your backups
2. Turn versioning on for that bucket
2. (Optional) Create a B2 bucket to hold your backups
3. Supply all appropriate environment variables
4. Run a backup and check your bucket for that backup
4. Run a backup and check your bucket(s) for that backup

### Environment variables
`MODE` Valid values: `backup`, `restore`
`MODE` Valid values: `backup`, `restore`. Restores are implemented **only** from S3.

`DB_NAMES` list of the database names

Expand All @@ -18,19 +19,27 @@ Service to backup and/or restore mysql databases to/from S3

`MYSQL_DUMP_ARGS` (optional) additional arguments to the mysqldump command, e.g., `--max_allowed_packet=50M`

`S3_BUCKET` e.g., _s3://database-backups_ **NOTE: no trailing slash**

>**It's recommended that your S3 bucket have versioning turned on.** Each backup creates a file of the form _dbname_.sql.gz. If versioning is not turned on, the previous backup file will be replaced with the new one, resulting in a single level of backups.
`AWS_ACCESS_KEY` used for S3 interactions

`AWS_SECRET_KEY` used for S3 interactions

`S3_BUCKET` e.g., _s3://database-backups_ **NOTE: no trailing slash**
`B2_BUCKET` (optional) Name of the Backblaze B2 bucket, e.g., _database-backups_. When `B2_BUCKET` is defined, the backup file is copied to the B2 bucket in addition to the S3 bucket.

>**It's recommended that your B2 bucket have versioning and encryption turned on.** Each backup creates a file of the form _dbname_.sql.gz. If versioning is not turned on, the previous backup file will be replaced with the new one, resulting in a single level of backups. Encryption may offer an additional level of protection from attackers. It also has the side effect of preventing downloads of the file via the Backblaze GUI (you'll have to use the `b2` command or the Backblaze API).
`B2_APPLICATION_KEY_ID` (optional; required if `B2_BUCKET` is defined) Backblaze application key ID

>**It's recommended that your S3 bucket have versioning turned on.**
`B2_APPLICATION_KEY` (optional; required if `B2_BUCKET` is defined) Backblaze application key secret

## Docker Hub
This image is built automatically on Docker Hub as [silintl/mysql-backup-restore](https://hub.docker.com/r/silintl/mysql-backup-restore/).

## Playing with it locally
You'll need [Docker](https://www.docker.com/get-docker), [Docker Compose](https://docs.docker.com/compose/install/), and [Make](https://www.gnu.org/software/make/).
You'll need [Docker Engine](https://docs.docker.com/engine/) with the Docker Compose plugin and [Make](https://www.gnu.org/software/make/).

1. cd .../mysql-backup-restore
3. Upload test/world.sql.gz to the S3 bucket.
Expand Down
14 changes: 14 additions & 0 deletions application/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ for dbName in ${DB_NAMES}; do
else
echo "mysql-backup-restore: Copy backup to ${S3_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
fi

if [ "${B2_BUCKET}" != "" ]; then
start=$(date +%s)
b2 upload-file --noProgress --quiet ${B2_BUCKET} /tmp/${dbName}.sql.gz ${dbName}.sql.gz
STATUS=$?
end=$(date +%s)
if [ $STATUS -ne 0 ]; then
echo "mysql-backup-restore: FATAL: Copy backup to ${B2_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "mysql-backup-restore: Copy backup to ${B2_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
fi
fi

done

echo "mysql-backup-restore: backup: Completed"
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '2'
services:
data:
image: silintl/data-volume:latest
Expand Down
3 changes: 3 additions & 0 deletions local.env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
AWS_ACCESS_KEY=
AWS_SECRET_KEY=
S3_BUCKET=
B2_APPLICATION_KEY_ID=
B2_APPLICATION_KEY=
B2_BUCKET=

0 comments on commit 0ecc492

Please sign in to comment.