Created
February 29, 2024 14:30
-
-
Save mcucen/18a54d258aa946f12556d2aeb00f1e82 to your computer and use it in GitHub Desktop.
Backup MySQL data to Minio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ################################ | |
| # MySQL Backup to Minio Server # | |
| ################################ | |
| # We will use timestamp to create unique filenames. Also it allows us to know backup time. | |
| TIMESTAMP=$(date +%Y%m%d%H%M%S) | |
| # Source MySQL server connection information. | |
| MYSQL_HOSTNAME=127.0.0.1 | |
| MYSQL_USERNAME=root | |
| MYSQL_PASSWORD=root | |
| MYSQL_DATABASE=test | |
| # This will be the filename of our database dump. | |
| DUMP_FILENAME="${MYSQL_DATABASE}_backup_${TIMESTAMP}.dump.sql" | |
| # Dump data to .sql file. | |
| mysqldump -h $MYSQL_HOSTNAME -u $MYSQL_USERNAME -p$MYSQL_PASSWORD --databases $MYSQL_DATABASE > $DUMP_FILENAME | |
| ################################################# | |
| # Now we will compress the output before upload # | |
| ################################################# | |
| # Just add .tar.gz to SQL_FILENAME to create a tar filename. | |
| TAR_FILENAME="$DUMP_FILENAME.tar.gz" | |
| # Comporessing... | |
| tar -czvf $TAR_FILENAME $DUMP_FILENAME | |
| # Min.io server connection information. | |
| MINIO_HOST=example.min.io | |
| MINIO_BUCKET=bucket-name | |
| MINIO_ACCESS_KEY=access-key | |
| MINIO_SECRET_KEY=secret-key | |
| ################### | |
| # Request details # | |
| ################### | |
| # Remote file target. I will use a directory structure like YYYYMM -> 202402 | |
| TARGET="/$MINIO_BUCKET/$(date +%Y%m)/$(basename $TAR_FILENAME)" | |
| CONTENT_TYPE="application/octet-stream" | |
| DATE=$(date -R) | |
| # Minio request signature. | |
| SIGNATURE="PUT\n\n$CONTENT_TYPE\n$DATE\n$TARGET" | |
| SIGNATURE_ENCODED=$(echo -en $SIGNATURE | openssl sha1 -hmac $MINIO_SECRET_KEY -binary | base64) | |
| # Send tar file to minio. | |
| curl -v -X PUT -T "$TAR_FILENAME" \ | |
| -H "Host: $MINIO_HOST" \ | |
| -H "Date: $DATE" \ | |
| -H "Content-Type: $CONTENT_TYPE" \ | |
| -H "Authorization: AWS $MINIO_ACCESS_KEY:$SIGNATURE_ENCODED" \ | |
| https://$MINIO_HOST$TARGET | |
| # Delete local files after upload. | |
| rm $SQL_FILENAME $TAR_FILENAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment