Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup MySQL databases to Amason S3. Related tutorial: https://antonshell.me/post/aws-s3-mysql-backup
#! /bin/bash
TIMESTAMP=$(date +"%F-%T")
BACKUP_DIR="/var/backups/databases/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQL_HOST="localhost"
AWS=/usr/bin/aws
MYSQLDUMP=/usr/bin/mysqldump databases=`$MYSQL --user=$MYSQL_USER -h$MYSQL_HOST -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
mkdir -p "$BACKUP_DIR"
for db in $databases; do
echo $db
time $MYSQLDUMP -Qc --add-drop-table --single-transaction --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db.gz"
done
$AWS s3 cp $BACKUP_DIR s3://my-website-backups/databases/$TIMESTAMP --recursive
#rm -rf /var/backups/databases/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment