Skip to content

Instantly share code, notes, and snippets.

@sokil
Last active July 30, 2023 19:00
Show Gist options
  • Save sokil/b2f6333cbccee6d0aa115ed19c1d906c to your computer and use it in GitHub Desktop.
Save sokil/b2f6333cbccee6d0aa115ed19c1d906c to your computer and use it in GitHub Desktop.
Backup mysql dtabase to Aamzon Web Services
#/bin/env bash
# First configure aws cli to authenticate on aws.
# See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-creds
CURRENT_DIR=$(dirname $(readlink -f $0))
CURRENT_DATE=$(date +%Y%m%d%H%M)
source .env # path to MySQL credentials
mysqldump -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DB > /tmp/$CURRENT_DATE.sql
gzip /tmp/$CURRENT_DATE.sql
if [[ $1 = "aws" ]]
then
# do not forget to convigure versioning and autoremove policy
aws s3 --profile=$AWS_PROFILE_NAME cp /tmp/$CURRENT_DATE.sql.gz s3://$AWS_PROFILE_NAME/last.sql.gz
rm /tmp/$CURRENT_DATE.sql.gz
else
mv /tmp/$CURRENT_DATE.sql.gz .
# kill backups older two month
find . -type f -name '*.sql.gz' -mtime +60 -exec rm -f {} \;
fi

In order to use AWS S3 dor backups configure your credentials in ~/.aws/credentials:

[bucket-name]
aws_access_key_id = SOME_ACCESS_KEY_ID
aws_secret_access_key = SOMEKEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment