Based on this awesome gist
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 :-) ** Using the AWS CLI (aws command) **
1 - Install aws-cli more info on the official repo
# Install aws-cli
pip install awscli
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
Assuming the backup script is stored in /var/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/s3mysqlbackup.sh >/dev/null 2>&1
# In order to store a log of the upload use this
0 3 * * * bash /var/s3mysqlbackup.sh >> /var/db-backup.log 2>&1
This a example of the foler crated in the S3 bucket that you specify:
1497712981 - Saturday 17 June 2017 @ 1523
Inside that folder you will find your backups with a filename like this:
1497712981 - Saturday 17 June 2017 @ 1523 - database_name.sql.gz
Remeber to check the path in which the aws-cli was installed the script assumes that it is intalled in
/usr/local/bin/aws
Also please remember to update the basic variables in the script:
- mysqlpass
- bucket