Skip to content

Instantly share code, notes, and snippets.

@i97506051502
Last active August 29, 2015 14:02
Show Gist options
  • Save i97506051502/923102b32f3523162329 to your computer and use it in GitHub Desktop.
Save i97506051502/923102b32f3523162329 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# CACHE CLUSTER ID
#
PRI_CACHE_CLUSTER_ID=ec-test-pri
REP_CACHE_CLUSTER_ID=ec-test-rep
#
# REPLICATION GROUP ID
#
REP_GROUP_ID=ec-test-rg
#
# snapshot's name
#
TODAY=`date +"%Y%m%d"`
SNAPSHOT=backup-elasticache-${TODAY}
#
# make read replica
#
aws elasticache create-cache-cluster --cache-cluster-id ${REP_CACHE_CLUSTER_ID} --replication-group-id ${REP_GROUP_ID}
# wait 15 minutes
sleep 900
#
# get endpoint of read replica
#
REP_ENDPOINT=`aws elasticache describe-replication-groups --replication-group-id ${REP_GROUP_ID} | grep Address | grep ${REP_CACHE_CLUSTER_ID} | cut -d ':' -f 2 | sed 's/[^"]*"\([^"]*\)"[^"]*/\1/g'`
#
# check status of replication
#
while true
do
REP_STATUS=`redis-cli -h ${REP_ENDPOINT} info | grep master_sync_in_progress | cut -d ':' -f 2`
#
# take snapshot
#
if [ "${REP_STATUS}" = "0" ];
then
aws elasticache create-snapshot --cache-cluster-id ${REP_CACHE_CLUSTER_ID} --snapshot-name ${SNAPSHOT}
break
fi
done
#
# delete read replica
#
while true
do
SS_STATUS=`aws elasticache describe-snapshots --snapshot-name backup-elasticache-20140604 --max-records 50 | grep SnapshotStatus | cut -d ':' -f 2 | sed 's/[^"]*"\([^"]*\)"[^"]*/\1/g'`
if [ "${SS_STATUS}" = "available" ];
then
aws elasticache delete-cache-cluster --cache-cluster-id ${REP_CACHE_CLUSTER_ID}
break
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment