Skip to content

Instantly share code, notes, and snippets.

@jpbarto
Created October 30, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbarto/b947615c3adcc1ce2154dd99ea8d14d5 to your computer and use it in GitHub Desktop.
Save jpbarto/b947615c3adcc1ce2154dd99ea8d14d5 to your computer and use it in GitHub Desktop.
Bash Shell Script to restore and time the restoration of an RDS database using Point in Time Recovery (PITR)
#!/bin/bash
set -e
AWS_REGION=eu-west-2
SOURCE_DATABASE_NAME=database-1
RESTORE_TIME='2020-10-28T22:02:01+00:00'
NEW_DATABASE_NAME="rds-db-$RANDOM"
echo [`date`] Beginning database restoration
DB_NAME=$(aws rds restore-db-instance-to-point-in-time --target-db-instance-identifier $NEW_DATABASE_NAME --source-db-instance-identifier $SOURCE_DATABASE_NAME --restore-time $RESTORE_TIME --region $AWS_REGION --query 'DBInstance.DBInstanceIdentifier' --output text)
echo [`date`] Creation of $DB_NAME started...
DB_STATUS=$(aws rds describe-db-instances --db-instance-identifier $DB_NAME --region $AWS_REGION --query 'DBInstances[0].DBInstanceStatus' --output text)
while [ $DB_STATUS != 'available' ]
do
echo [`date`] Status is $DB_STATUS
sleep 60
DB_STATUS=$(aws rds describe-db-instances --db-instance-identifier $DB_NAME --region $AWS_REGION --query 'DBInstances[0].DBInstanceStatus' --output text)
done
echo [`date`] Database is restored
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment