-
-
Save thalweg/956118 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
#configurables | |
DBINSTANCE="yourdbinstanceID" | |
ENDPOINT="XXXXXXXXXXXX.us-east-1.rds.amazonaws.com" | |
USER='yourUser' | |
PASSWD='yourPassword' | |
DB1="yourDatabase1 yourDatabase2" | |
BUCKET='s3://your-bucket' | |
BACKUP_HOME='/mnt/backups' | |
S3CMD="/usr/bin/s3cmd" | |
SECURITYGROUP="Mysecuritygroup" | |
#below this line be dragons | |
#misc | |
UUID="$(uuid)" | |
SNAPID="${UUID}" | |
BACKUPDBID="${UUID}" | |
#rds verifying commands | |
SNAPSHOT_AVAILABILITY="rds-describe-db-snapshots -s $SNAPID | grep available | wc -l" | |
INSTANCE_AVAILABILITY="rds-describe-db-instances $BACKUPDBID | grep available | wc -l" | |
SEC_CHANGES="rds-describe-db-instances ${BACKUPDBID} | grep SECGROUP | grep -i $SECURITYGROUP | grep active |wc -l" | |
wait_until() | |
{ | |
result=`eval $* | sed 's/ //g'` | |
if [[ $result == 0 ]] | |
then | |
sleep 60 | |
wait_until $* | |
fi | |
} | |
rds-create-db-snapshot ${DBINSTANCE} --db-snapshot-identifier $SNAPID | |
wait_until $SNAPSHOT_AVAILABILITY | |
rds-restore-db-instance-from-db-snapshot $BACKUPDBID --db-snapshot-identifier $SNAPID --db-instance-class db.m1.small | |
wait_until $INSTANCE_AVAILABILITY | |
rds-modify-db-instance ${BACKUPDBID} --db-security-groups ${SECURITYGROUP} | |
wait_until $SEC_CHANGES | |
for db in $DB1 | |
do | |
cpDate=`date -u +%Y%m%d%H%M` | |
mysqldump --quick --single-transaction -u$USER -p$PASSWD -h${BACKUPDBID}.${ENDPOINT} -P 3306 ${db} | bzip2 > ${BACKUP_HOME}/${db}-${cpDate}.sql.bz2 | |
${S3CMD} put ${BACKUP_HOME}/${db}-${cpDate}.sql.bz2 ${BUCKET} | |
uploaded=`${S3CMD} ls $BUCKET | grep ${db}-${cpDate}.tar.bz2 | wc -l | sed 's/ //g'` | |
if [[ $uploaded == 1 ]]; then | |
rm ${BACKUP_HOME}/${db}-${cpDate}.tar.bz2 | |
fi | |
done | |
rds-delete-db-instance $BACKUPDBID --skip-final-snapshot -f | |
rds-delete-db-snapshot $SNAPID -f | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment