Skip to content

Instantly share code, notes, and snippets.

@wyliethomas
Created August 3, 2016 15:10
Show Gist options
  • Save wyliethomas/26ebdd395230778506a625a34e2a268d to your computer and use it in GitHub Desktop.
Save wyliethomas/26ebdd395230778506a625a34e2a268d to your computer and use it in GitHub Desktop.
MySQL Backup to S3
#!/bin/bash
NOWDATE=`date +%Y-%m-%d`
BACKUPNAME="$NOWDATE.sql.gz"
echo "Creating backup of database finances to $BACKUPNAME"
mysqldump --user=[username] --password=[password] [database] | gzip -9 > $BACKUPNAME
echo "Succesfully created database backup"
echo "Uploading backup to Amazon S3 bucket…"
s3cmd put $BACKUPNAME s3://[path-to-bucket-and-folder]/$BACKUPNAME
echo "Successfully uploaded backup to S3"
echo "Deleting backup file…"
rm $BACKUPNAME
echo "Done"
@wyliethomas
Copy link
Author

For Ubuntu I used this to install S3cmd:

sudo apt-get install s3cmd

Then in my /home/user directory I create a directory called DATABACKUP and put this shell script in there.

Then this goes in my cron:

0 3 * * * /bin/bash /home/[user]/DATABACKUP/backitup.sh >/dev/null 2>&1

After a while I go through my S3 bucket and delete old archives. I usually keep 30 to 60 days worth of backups depending on the size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment