Skip to content

Instantly share code, notes, and snippets.

@muke5hy
Forked from oodavid/README.md
Last active May 8, 2020 07:29
Show Gist options
  • Save muke5hy/fe4fa672ec77591b96fd02d64fc5e285 to your computer and use it in GitHub Desktop.
Save muke5hy/fe4fa672ec77591b96fd02d64fc5e285 to your computer and use it in GitHub Desktop.
Backup MySQL to Amazon S3

Backup MySQL to Amazon S3

This is a simple way to backup your MySQL tables to Amazon S3 for a nightly backup - this is all to be done on your server :-)

Sister Document - Restore MySQL from Amazon S3 - read that next

1 - Install aws cli

2 - Add your script

Upload a copy of s3mysqlbackup.sh (it will need some tweaks for your setup), make it executable and test it

# Add the executable bit
chmod +x s3mysqlbackup.sh
# Run the script to make sure it's all tickety boo
./s3mysqlbackup.sh

3 - Run it every night with CRON

Assuming the backup script is stored in /var/www/s3mysqlbackup.sh we need to add a crontask to run it automatically:

# Edit the crontab
env EDITOR=nano crontab -e
    # Add the following lines:
    # Run the database backup script at 3am
    0 3 * * * bash /var/www/s3mysqlbackup.sh >/dev/null 2>&1

4 - Don't expose the script!

If for some reason you put this script in a public folder (not sure why you would do this), you should add the following to your .htaccess or httpd.conf file to prevent public access to the files:

### Deny public access to shell files
<Files *.sh>
    Order allow,deny
    Deny from all
</Files>
#!/bin/bash
# Based on https://gist.github.com/2206527
# Install AWS CLI
# Create Option file
# [client]
# user=root
# password="pass"
# [mysql]
# user=root
# password="pass"
# [mysqldump]
# user=root
# password="pass"
# [mysqldiff]
# user=root
# password="pass"
# Be pretty
echo -e " "
echo -e " . ____ . ______________________________"
echo -e " |/ \| | |"
echo -e "[| \e[1;31m♥ ♥\e[00m |] | S3 MySQL Backup Script v.0.2 |"
echo -e " |___==___| / © oodavid 2020 |"
echo -e " |______________________________|"
echo -e " "
# Basic variables
bucket="backup"
# Timestamp (sortable AND readable)
stamp=`date +"%s - %A %d %B %Y @ %H%M"`
# List all the databases
databases=`mysql -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|performance_schema\|mysql\|test\|sys\)"`
# Feedback
echo -e "Dumping to \e[1;32m$bucket/$stamp/\e[00m"
# Loop the databases
for db in $databases; do
# Define our filenames
filename="$stamp - $db.sql.gz"
tmpfile="/tmp/$filename"
object="$stamp/$filename"
# Feedback
echo -e "\e[1;34m$db\e[00m"
# Dump and zip
echo -e " creating \e[0;35m$tmpfile\e[00m"
mysqldump --force --opt --databases "$db" | gzip -c > "$tmpfile"
# Upload
echo -e " uploading..."
aws s3api put-object --bucket="$bucket" --key="$object" --body="$tmpfile"
# Delete
rm -f "$tmpfile"
done;
# Jobs a goodun
echo -e "\e[1;32mJobs a goodun\e[00m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment