Skip to content

Instantly share code, notes, and snippets.

@SQLadmin
Created November 7, 2018 05:11
Show Gist options
  • Save SQLadmin/86fa1ba72799f71377a0ec1d6e00660f to your computer and use it in GitHub Desktop.
Save SQLadmin/86fa1ba72799f71377a0ec1d6e00660f to your computer and use it in GitHub Desktop.
#!/bin/bash
# ----------------------------------------------------------
# RECREATE REDSHIFT CLUSTERS FROM RUNNING CLUSTER'S SNAPSHOT
# ----------------------------------------------------------
# Version: 1.0
# Created by: @SQLadmin
# Create IAM user with keys assign Redshift nessessary access
# and SES send raw email access
# READ CAREFULLY
# --------------
# Change the below things:
# AWS CLI must be installed
# YOUR_ACCESS_KEY
# YOUR_SECRET_KEY
# prod-cluster -> Prod/Main cluster name
# dev-cluster -> New Test/DEV cluster name
# REDSHIFT-REGION -> Region where your cluster located
# ses-region -> Region for your SES
# [email protected] -> From Address for SES (this should be verified one)
# [email protected],[email protected] -> Who all are needs to get the email notification
# default.redshift-1.0 -> If you are using custom parameter group then replace this with that name.
# "sg-id1" "sg-id2" -> Security group ids that you want to attach it to Redshift Cluster.
#function for kill the process once its failed
die() { echo >&2 "$0 Err: $@" ; exit 1 ;}
#Export Access Keys
export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
#Input Parameters
#For Cluster Refresh
Snapdate=`date +%Y-%m-%d-%H-%M-%S`
SourceRedshift='prod-cluster'
DestRedshift='dev-cluster'
Region='REDSHIFT-REGION'
#Delete Cluster
echo "Delete Cluster ... Please wait"
aws redshift delete-cluster \
--region $Region \
--cluster-identifier $DestRedshift \
--skip-final-cluster-snapshot || die | aws ses send-email \
--region ses-region \
--from "[email protected]" \
--destination "[email protected],[email protected]" \
--message "Subject={Data=RedShift Refresh Failed,Charset=utf8},Body={Text={Data=Refreshing the redshift cluster is failed.
Step: Delete Cluster,Charset=utf8}}"
sleep 5m
echo "Cluster Deleted !!!"
#Take snapshot
echo "Taking Snapshot ... Please wait"
aws redshift create-cluster-snapshot \
--region $Region \
--cluster-identifier $SourceRedshift \
--snapshot-identifier $SourceRedshift-refresh-snap-$Snapdate || die | aws ses send-email \
--region ses-region \
--from "[email protected]" \
--destination "[email protected],[email protected]" \
--message "Subject={Data=RedShift Refresh Failed,Charset=utf8},Body={Text={Data=Refreshing the redshift cluster is failed.
Step: Take snapshot,Charset=utf8}}"
sleep 15m
echo "Snapshot Created !!!"
#Restore snapshot
echo "Restoring Snapshot... Please wait!"
aws redshift restore-from-cluster-snapshot \
--region $Region \
--cluster-identifier $DestRedshift \
--snapshot-identifier $SourceRedshift-refresh-snap-$Snapdate \
--cluster-subnet-group-name reshiftsubnet \
--cluster-parameter-group-name default.redshift-1.0 \
--vpc-security-group-ids "sg-id1" "sg-id2" || die | aws ses send-email \
--region ses-region \
--from "[email protected]" \
--destination "[email protected],[email protected]" \
--message "Subject={Data=RedShift Refresh Failed,Charset=utf8},Body={Text={Data=Refreshing the redshift cluster is failed.
Step: Restore snapshot,Charset=utf8}}"
sleep 60m
echo "Snapshot Restored !!!"
#Delete old snapshot
echo "Old Snapshot Deleteing!!!"
Deldate=prod-cluster-refresh-snap-`date -d "1 days ago" +%Y-%m-%d`
Delsnap=$(aws redshift describe-cluster-snapshots --region ses-region --query 'Snapshots[].SnapshotIdentifier' --output json | grep $Deldate | sed -n '2p' | sed 's|[",,]||g')
aws redshift delete-cluster-snapshot \
--region $Region \
--snapshot-identifier $Delsnap || die | aws ses send-email \
--region ses-region \
--from "[email protected]" \
--destination "[email protected],[email protected]" \
--message "Subject={Data=RedShift Refresh Failed,Charset=utf8},Body={Text={Data=Refreshing the redshift cluster is failed.
Step: Delete Old snapshot,Charset=utf8}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment