Skip to content

Instantly share code, notes, and snippets.

@sodonnell
Created September 6, 2018 19:58
Show Gist options
  • Select an option

  • Save sodonnell/f6d4b64bb09e5e6ca65550483fc75f50 to your computer and use it in GitHub Desktop.

Select an option

Save sodonnell/f6d4b64bb09e5e6ca65550483fc75f50 to your computer and use it in GitHub Desktop.
Restore/Re-Deploy an Amazon RDS (MySQL) Database Instance from a specified snapshot.
#!/usr/bin/env bash
#
# Restore/Re-Deploy an Amazon RDS (MySQL) Database Instance from a specified snapshot.
#
# This script is intended to be run on an AWS EC2 instance
# within an isolated VPC infrastructure, to restore or re-deploy a
# test/staging database instance from a the latest
# 'production database' snapshot.
#
# This script requires the latest aws-cli program.
#
# Sean O'Donnell <sean@seanodonnell.com>
#
# production db instance name
RDS_PROD="sod-prod-db";
# test/dev db instance name
RDS_TEST="sod-dev-db";
# RDS Storage Type
STORAGE_TYPE="Provisioned IOPS";
# RDS VPC Subnet Group for your test/dev instances
SUBNET_GROUP="vpc-subnet-group-????????";
# Instance Class
INSTANCE_CLASS="db.m4.xlarge";
#
# Determine whether or not there is an existing RDS instance defined as $RDS_TEST
#
EXISTING_ID=`aws rds describe-db-instances --db-instance-identifier ${RDS_TEST} 2> /dev/null | grep -i \"dbinstanceidentifier\" | awk {'print $2'} | sed s/[\".,]//g`;
#
# Retrieve the latest snapshot of the production instance
#
SNAPSHOT_ID=`aws rds describe-db-snapshots --db-instance-identifier ${RDS_PROD} | grep -i DBSnapshotIdentifier | awk {'print $2'} | sed s/[\".,]//g | tail -n 1`;
if [ ! -z "$SNAPSHOT_ID" ]; then
echo -e "Current Snapshot ID: $SNAPSHOT_ID\n";
if [ ! -z "$EXISTING_ID" ]; then
#
# If the instance-id has been returned (properly), then delete the current staging/test instance.
#
echo -e "Existing instance identified. Deleting: ${RDS_TEST}\n";
aws rds delete-db-instance --db-instance-identifier ${RDS_TEST} --skip-final-snapshot; # > /dev/null 2>&1;
#
# Wait until the instance has been deleted, before proceeding to the restore process.
#
# This is really cool that they supply this command via cli/api, as polling/sleeping
# would otherwise require more work than is preferred for such a trivial task.
#
echo -e "Instance ${RDS_TEST} started the delete process. Waiting for it to be removed from AWS systems.\n";
aws rds wait db-instance-deleted --db-instance-identifier ${RDS_TEST};
fi
#
# Deploy the snapshot in-place of the testing/staging instance that was just deleted.
#
echo -e "Beginning the Instance restore process for: ${RDS_TEST}, from snapshot: ${SNAPSHOT_ID} \n";
aws rds restore-db-instance-from-db-snapshot \
--db-instance-identifier ${RDS_TEST} \
--db-snapshot-identifier=${SNAPSHOT_ID} \
--db-instance-class ${INSTANCE_CLASS} \
--no-multi-az \
--no-auto-minor-version-upgrade \
--db-subnet-group-name ${SUBNET_GROUP};
aws rds wait db-instance-available --db-instance-identifier ${RDS_TEST};
echo -e "The db instance ${RDS_TEST} has been restored from the snapshot ${SNAPSHOT_ID}.\nDone!";
else
echo -e "Snapshot ID could not be determined. Process halted.\n";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment