Skip to content

Commit

Permalink
Support backup to Backblaze B2 bucket
Browse files Browse the repository at this point in the history
* Added Backblaze `b2` command line tool to the Docker image.
* Added environment variables to support use of `b2`.
* Docker Compose is a Docker plugin now instead of a separate binary.
  • Loading branch information
dalenewby committed May 2, 2024
1 parent a1966f5 commit 288980d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 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.10.0

# 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
15 changes: 12 additions & 3 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 @@ -26,6 +27,14 @@ Service to backup and/or restore mysql databases to/from S3

>**It's recommended that your S3 bucket have versioning turned on.**
`B2_APPLICATION_KEY_ID` (optional) Backblaze application key ID

`B2_APPLICATION_KEY` (optional) Backblaze application key secret

`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.

>**It's recommended that your B2 bucket have versioning and encryption turned on.**
## Docker Hub
This image is built automatically on Docker Hub as [silintl/mysql-backup-restore](https://hub.docker.com/r/silintl/mysql-backup-restore/).

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
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 288980d

Please sign in to comment.