Created
October 30, 2017 15:14
-
-
Save eekfonky/0a179fb1fddbefa2155544d5c2dddc0e to your computer and use it in GitHub Desktop.
Backup MySQL running in Docker to S3
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 | |
# .mylogin.cnf created with; | |
# mysql_config_editor set -h localhost -G dbbkup -P 3306 -u root -p | |
# | |
# to workaround a bug in Docker you need to create a small script with; | |
# /usr/bin/mysqldump --login-path=dbbkup --all-databases > mis-core-dev.sql | |
# inside the conatiner at root | |
# set variables | |
TIMESTAMP=$(date +%F-%H.%M) | |
BACKUPDIR="/home/$USER/backup" | |
S3_BUCKET_NAME="s3://<BUCKET_NAME>/" | |
MYSQLFILE="mis-core-dev-$TIMESTAMP.sql" | |
# create usage of script and set container as variable | |
usage() { | |
echo "Usage $0 -c mongo_docker_container_name" | |
} | |
while [[ $# -gt 1 ]] | |
do | |
key="$1" | |
case $key in | |
-c|--container) | |
CONTAINERNAME="$2" | |
shift # past argument | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ -z "${CONTAINERNAME}" ]; then | |
usage | |
exit 1 | |
fi | |
# dump files inside container | |
docker exec "${CONTAINERNAME}" /mysqlbkup.sh | |
# copy files to host | |
docker cp "${CONTAINERNAME}":/mis-core-dev.sql "$BACKUPDIR" | |
# remove backup files inside container | |
docker exec "${CONTAINERNAME}" rm -f /mis-core-dev.sql | |
# create backup directory | |
if [[ ! -d "$BACKUPDIR" ]]; then | |
mkdir -p "$BACKUPDIR" | |
fi | |
# zip files | |
cd "$BACKUPDIR" || exit | |
cd .. | |
tar -C "$BACKUPDIR" -czf "$MYSQLFILE".tar.gz mis-core-dev.sql | |
# upload to S3 | |
aws s3 cp "$MYSQLFILE".tar.gz "$S3_BUCKET_NAME" | |
# delete the backup after sync to S3 | |
mv "$MYSQLFILE".tar.gz "$BACKUPDIR" | |
sudo rm -rf "${BACKUPDIR:?}"/* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment