Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
walterwootz committed Jan 11, 2023
1 parent 109b997 commit 4f9b4a9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# mysqldump-s3-backup
Docker-enabled tool to backup MySQL database and upload to S3 bucket.
# MDSB - MySQL Dump S3 Backup

MySQL Dump S3 Backup (MDSB) provides scripts to perform dump of a MySQL database and upload the result to an S3 bucket.

## Requirements

- Linux Bash
- Docker

## Configuration

Configure the following variables in [env.sh](./env.sh) file:
| Key | Value |
|--|--|
| MYSQL_VERSION | MySQL version (e.g. 5.7, 8.0) |
| MYSQL_HOST | Hostname of MySQL instance |
| MYSQL_PORT | Port of MySQL instance |
| MYSQL_USER | MySQL username |
| MYSQL_PWD | MySQL password |
| MYSQL_DATABASE | Name of the database to dump |
| AWS_ACCESS_KEY_ID | IAM user's access key |
| AWS_SECRET_ACCESS_KEY | IAM user's secret key |
| AWS_DEFAULT_REGION | S3 bucket region |
| BUCKET_NAME | S3 bucket name |
| FOLDER_IN_BUCKET | S3 folder where to store dump file |
| TZ | Timezone (e.g. Europe/Rome) |

## Run

./run.sh

## Scheduling
You can programmatically run it using a cron job.

## Next?
- Dockerize application
14 changes: 14 additions & 0 deletions env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

MYSQL_VERSION=<mysql-version>
MYSQL_HOST=<mysql-hostname>
MYSQL_PORT=<mysql-port>
MYSQL_DATABASE=<mysql-database-name>
MYSQL_USER=<mysql-username>
MYSQL_PWD=<mysql-password>
AWS_ACCESS_KEY_ID=<aws-access-key>
AWS_SECRET_ACCESS_KEY=<aws-secret-key>
AWS_DEFAULT_REGION=<aws-region>
BUCKET_NAME=<aws-s3-bucket-name>
FOLDER_IN_BUCKET=<aws-s3-folder>
TZ=<timezone>
8 changes: 8 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

source ./env.sh

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
docker run --rm -e MYSQL_PWD=$MYSQL_PWD mysql:$MYSQL_VERSION mysqldump -u $MYSQL_USER -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DATABASE 1> $MYSQL_DATABASE_backup_$current_time.sql
docker run --network host -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION -v "$PWD":/app -w /app amazon/aws-cli s3 cp $MYSQL_DATABASE_backup_$current_time.sql s3://$BUCKET_NAME/$FOLDER_IN_BUCKET/$MYSQL_DATABASE_backup_$current_time.sql
rm $MYSQL_DATABASE_backup_$current_time.sql

0 comments on commit 4f9b4a9

Please sign in to comment.