Friday, April 29, 2011

Backup ec2 mysql server on S3 using mysqldump and s3

Below is script to take backup of complete database on S3, also remove 10 day old backup file from S3

You need install s3cmd, and configure it before using this script, also need to create S3 bucket on aws
also change mysql username and password accordingly.



 #!/bin/bash
#below is path of variable to put dump of mysql
DATA=/data
#filename of today
file_name=db_`date +'%d-%m-%Y'`.sql.gz
#filename of today
file_name_10_day_old=db_`date --date='10 days ago' +'%d-%m-%Y'`.sql.gz
#take dump of mysql
mysqldump -u mysql_unsername -p mysql_password --all-databases | gzip > $DATA/$file_name
#push to s3
s3cmd put $DATA/$file_name s3://backup_bucket
#cleanup of file
rm $DATA/$file_name
s3cmd del s3://backup_bucket/$file_name_10_day_old