-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap_and_restore_cluster.sh
executable file
·102 lines (85 loc) · 5.18 KB
/
snap_and_restore_cluster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
if ! type mapfile > /dev/null 2>&1 ; then
echo "This script need bash 4.x exiting"
exit 2
fi
AWS_CLI="docker run --rm -it -v $(pwd):/aws -v $HOME/.aws/:/root/.aws -v $HOME/.ssh/:/root/.ssh -v $(pwd):/aws -e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} -e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} -e AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} mesosphere/aws-cli"
NEEDED_ENV="AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION"
if [ -z ${AWS_ACCESS_KEY_ID} ]; then echo "AWS_ACCESS_KEY_ID is unset."; ENVERROR=true; fi
if [ -z ${AWS_SECRET_ACCESS_KEY} ]; then echo "AWS_SECRET_ACCESS_KEY is unset"; ENVERROR=true; fi
if [ -z ${AWS_DEFAULT_REGION} ]; then echo "AWS_DEFAULT_REGION is unset"; ENVERROR=true; fi
if [ $ENVERROR ]; then
echo "Please add to your environment the following variables: ${NEEDED_ENV}"; exit 1;
fi
function wait_db_instance_available {
while [ "${exit_status}" != "0" ]
do
echo "$(date) - Wait availability of $1"
sleep 3
${AWS_CLI} rds wait db-instance-available --db-instance-identifier=$1
local exit_status="$?"
done
}
PARSED_OPTIONS=$(getopt -n "$0" -o h --long "clustername:,rprefix:,rpostfix:,instancename:,instancetype:" -- "$@")
source $HOME/.bash_profile
while true;
do
case "$1" in
--clustername )
CLUSTER_NAME=$2
shift 2;;
--rprefix )
RESTORE_PREFIX="$2-"
shift 2;;
--rpostfix )
RESTORE_POSTFIX="-$2"
shift 2;;
--instancename )
INSTANCE_NAME=$2
shift 2;;
--instancetype )
INSTANCE_TYPE=$2
shift 2;;
-- )
shift
break;;
* ) break ;;
esac
done
SNAP_ID=${RESTORE_PREFIX}${CLUSTER_NAME}${RESTORE_POSTFIX}-$(date +"%m-%d-%y-%H-%M-%S")
RESTORE_CLUSTER_NAME=${RESTORE_PREFIX}${CLUSTER_NAME}${RESTORE_POSTFIX}
RESTORE_INSTANCE_NAME=${RESTORE_PREFIX}${INSTANCE_NAME}${RESTORE_POSTFIX}
# Creates RDS cluster snapshot if not exists
if [ $(${AWS_CLI} rds describe-db-cluster-snapshots --db-cluster-identifier=${CLUSTER_NAME} --snapshot-type=manual --db-cluster-snapshot-identifier=${SNAP_ID} | jq '.DBClusterSnapshots[0].Status' | tr -d '"') != "available" ]; then
${AWS_CLI} rds create-db-cluster-snapshot --db-cluster-snapshot-identifier=${SNAP_ID} --db-cluster-identifier=${CLUSTER_NAME}
while [[ $(${AWS_CLI} rds describe-db-cluster-snapshots --db-cluster-identifier=${CLUSTER_NAME} --snapshot-type=manual --db-cluster-snapshot-identifier=${SNAP_ID} | jq '.DBClusterSnapshots[0].Status' | tr -d '"') != "available"
]]; do
sleep 2
done
fi
echo "$(date) - Snapshot ${SNAP_ID} is available"
ENGINE=$(${AWS_CLI} rds describe-db-cluster-snapshots --db-cluster-identifier=${CLUSTER_NAME} --snapshot-type=manual --db-cluster-snapshot-identifier=${SNAP_ID} | jq '.DBClusterSnapshots[0].Engine' | tr -d '"')
# Creates cluster from snapshot
echo "$(date) - Starting restore of ${RESTORE_CLUSTER_NAME} from ${SNAP_ID} with engine ${ENGINE}"
${AWS_CLI} rds restore-db-cluster-from-snapshot --db-cluster-identifier=${RESTORE_CLUSTER_NAME} --snapshot-identifier=${SNAP_ID} --engine=${ENGINE}
# Adds instance to that cluster
${AWS_CLI} rds create-db-instance --db-instance-identifier=${RESTORE_INSTANCE_NAME} --db-instance-class=${INSTANCE_TYPE} --engine=${ENGINE} --db-cluster-identifier=${RESTORE_CLUSTER_NAME}
wait_db_instance_available ${RESTORE_INSTANCE_NAME}
echo "$(date) - Cluster ${RESTORE_CLUSTER_NAME} is available for customization"
# Customizes the cluster
DB_CLUSTER_PARAMETER_GROUP=$(${AWS_CLI} rds describe-db-clusters --db-cluster-identifier=${CLUSTER_NAME} | jq '.DBClusters[0].DBClusterParameterGroup' | tr -d '"')
SECURITY_GROUPS=$(${AWS_CLI} rds describe-db-clusters --db-cluster-identifier=${CLUSTER_NAME} | jq '.DBClusters[0].VpcSecurityGroups[].VpcSecurityGroupId'| tr -d '"')
DB_PARAMETER_GROUP=$(${AWS_CLI} rds describe-db-instances --db-instance-identifier=${INSTANCE_NAME} | jq '.DBInstances[0].DBParameterGroups[].DBParameterGroupName'| tr -d '"')
# SET instance publicly-accessible and parameter-group
echo "$(date) - ${AWS_CLI} rds modify-db-instance --db-instance-identifier=${RESTORE_INSTANCE_NAME} --db-parameter-group-name=${DB_PARAMETER_GROUP} --publicly-accessible --apply-immediately"
${AWS_CLI} rds modify-db-instance --db-instance-identifier=${RESTORE_INSTANCE_NAME} --db-parameter-group-name=${DB_PARAMETER_GROUP} --publicly-accessible --apply-immediately
wait_db_instance_available ${RESTORE_INSTANCE_NAME}
# SET cluster db-cluster-parameter-group and vpc-security-groups
echo "$(date) - ${AWS_CLI} rds modify-db-cluster --db-cluster-identifier=${RESTORE_CLUSTER_NAME} --db-cluster-parameter-group-name=${DB_CLUSTER_PARAMETER_GROUP} --vpc-security-group-ids ${SECURITY_GROUPS} --apply-immediately"
${AWS_CLI} rds modify-db-cluster --db-cluster-identifier=${RESTORE_CLUSTER_NAME} --db-cluster-parameter-group-name=${DB_CLUSTER_PARAMETER_GROUP} --vpc-security-group-ids ${SECURITY_GROUPS} --apply-immediately
wait_db_instance_available ${RESTORE_INSTANCE_NAME}
# Reboot instance
echo "$(date) - ${AWS_CLI} rds reboot-db-instance --db-instance-identifier=${RESTORE_INSTANCE_NAME}"
${AWS_CLI} rds reboot-db-instance --db-instance-identifier=${RESTORE_INSTANCE_NAME}
wait_db_instance_available ${RESTORE_INSTANCE_NAME}
echo "$(date) - Cluster ${RESTORE_CLUSTER_NAME} is available"