Skip to content

Instantly share code, notes, and snippets.

@ryanhs
Created February 1, 2026 10:47
Show Gist options
  • Select an option

  • Save ryanhs/4f9dda7ea75b177ed9c25299a934ee6f to your computer and use it in GitHub Desktop.

Select an option

Save ryanhs/4f9dda7ea75b177ed9c25299a934ee6f to your computer and use it in GitHub Desktop.
mysql-backup-s3.sh
#!/bin/bash
# Replace the placeholders with your MySQL server details
MYSQL_HOST="localhost"
MYSQL_USER="xxx"
MYSQL_PASSWORD="xxx"
DATABASE_NAME="xxx"
# Set your Amazon S3 bucket name
S3_BUCKET="xxx"
AWS_PROFILE="xxx"
# ----------------------------------------
# ref: https://simplebackups.com/blog/the-ultimate-mysql-database-backup-script
# Set the backup directory with the current date as the subfolder name
DIR=$(date +%Y-%m-%d_%H%M)
DEST=~/db_backups/$DIR
DEST_FULLPATH=$DEST/$DATABASE_NAME.sql
mkdir -p $DEST
# Use mysqldump to create a SQL backup file
mysqldump -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE_NAME > $DEST_FULLPATH
# Upload the backup file to Amazon S3
aws --profile $AWS_PROFILE sts get-caller-identity
aws --profile $AWS_PROFILE s3 cp $DEST_FULLPATH s3://$S3_BUCKET/$DIR/$DATABASE_NAME.sql
# Remove local backups older than 3 days
find ~/db_backups -type d -mtime +3 -exec rm -rf {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment