Skip to content

Instantly share code, notes, and snippets.

@blacksaildivision
Last active August 14, 2017 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacksaildivision/3491273f59a42b6bed40a23df8e6694d to your computer and use it in GitHub Desktop.
Save blacksaildivision/3491273f59a42b6bed40a23df8e6694d to your computer and use it in GitHub Desktop.
Backup MySQL database to AWS S3 bucket
#!/bin/bash
##
# There are two requirements for this script to work:
# 1. You must have AWS CLI installed andd configured with ./aws/credentials and ./aws/config files
# 2. You must have .my.cnf file configured for MySQL with credentials for user that has at least SELECT privileges for database to backup
#
# Usage:
# 1. Make sure that AWS CLI and S3 bucket is working properly
# 2. Make sure that you have user for creating backups in MySQL and .my.cnf file configured properly
# 3. Update variables in configuration part according to your needs
# 4. Run the script with ./mysql-backup-database-to-s3.sh
#
# Crontab:
# To make daily backups just add new cronjob:
# @daily bash /path/to/mysql-backup-database-to-s3.sh >> /path/to/log/file/where/you/want/to/store/output/of/this/command.log 2>&1
##
##### CONFIGURATION PART ######
# Name of database to backup
databaseName="website1"
# Directory (it must exists) where backup files will be stored
backupDirectory="/var/db"
# If set to true, backup will be compressed with gzip
compressBackup=true
# If set to true, backup file will be transfered to S3 bucket
uploadToS3=true
# Name of your S3 bucket
s3BucketName="website1-mysql-backups"
# If set to true, only given amount of files will be kept. Old files will be removed.
backupsRotate=true
# Number of backup files to keep
rotate=30
##### EXECUTION PART #####
##
# Please do not modify code in this section
##
# Generate backup file name, ie. website1.2017-08-03_20-07.sql
backupFileName="$databaseName.$(date +'%Y-%m-%d_%H-%M').sql"
# Create full path where backup file will be stored, ie. /var/db/website1.2017-08-03_20-07.sql
backupFilePath="$backupDirectory/$backupFileName"
# Output some debug options
echo "Backup of \"$databaseName\" database started at $(date +'%d/%m/%Y %T')"
# Dump database without locking with mysqldump command
echo "Dumping database..."
mysqldump --single-transaction --quick "$databaseName" > "$backupFilePath"
# Compress file with gzip
if [ "$compressBackup" = true ] ; then
echo "Compressing backup file..."
gzip -f "$backupFilePath"
# Update backupFilePath with new path of compressed file for s3 upload
backupFilePath="$backupFilePath.gz"
fi
# Upload to s3 bucket
if [ "$uploadToS3" = true ] ; then
echo "Uploading to S3 bucket..."
aws s3 cp --storage-class STANDARD_IA "$backupFilePath" s3://"$s3BucketName"
fi
# Remove old backup files
if [ "$backupsRotate" = true ] ; then
echo "Removing old backup files..."
ls --reverse "$backupDirectory/$databaseName".* | sed -n "$(( rotate + 1 )),\$p" | xargs rm -f
fi
# End the script with current date time
echo "Backup of \"$databaseName\" database finished at $(date +'%d/%m/%Y %T')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment