-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvu-docdb.bash
executable file
·212 lines (149 loc) · 6.26 KB
/
mvu-docdb.bash
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#! /bin/bash
# Clone and MVU a DocDB cluster
# make sure user passed correct number of parameters
if [ $# -ne 4 ] ; then
echo "4 arguments required: <source-cluster-identifier> <clone-cluster-identifier> <clone-instance-class> <clone-instance-count>"
exit 1
fi
DDB_SOURCE_CLUSTER_IDENTIFIER=$1
DDB_CLONE_CLUSTER_IDENTIFIER=$2
DDB_INSTANCE_CLASS=$3
DDB_INSTANCE_COUNT=$4
#DDB_INSTANCE_AZ_PRIMARY=$4
#DDB_INSTANCE_AZ_REPLICAS=$5
#DDB_DB_CLUSTER_PARAMETER_GROUP_NAME=$6
#DDB_VPC_SECURITY_GROUP_ID1=$7
#DDB_DB_SUBNET_GROUP_NAME=$8
#DDB_ENGINE_VERSION=$9
# number of seconds to wait before looking for progress
sleepSeconds=15
# validate instance class
validInstanceClasses=(db.r6g.large db.r6g.xlarge db.r6g.2xlarge db.r6g.4xlarge db.r6g.8xlarge db.r6g.12xlarge db.r6g.16xlarge
db.r5.large db.r5.xlarge db.r5.2xlarge db.r5.4xlarge db.r5.8xlarge db.r5.12xlarge db.r5.16xlarge db.r5.24xlarge
db.t3.medium db.t4g.medium)
validInstance=0
for i in "${validInstanceClasses[@]}" ; do
if [ "$i" == "$DDB_INSTANCE_CLASS" ] ; then
validInstance=1
fi
done
if [ $validInstance -ne 1 ] ; then
echo "invalid instance class requested : $DDB_INSTANCE_CLASS"
exit 1
fi
# bash stuff
# check if not set or empty
# : "${BATCHNUM:?Variable not set or empty}"
# use default value if not set or empty
# : "${BATCHNUM:=3}"
#DDB_BACKUP_RETENTION_PERIOD=1
#DDB_PORT=27017
#DDB_MASTER_USERNAME=${DOCDB_USERNAME:?Environment variable not set or empty}
#DDB_MASTER_USER_PASSWORD=${DOCDB_PASSWORD:?Environment variable not set or empty}
DDB_ENGINE="docdb"
PARAMETER_GROUP="tls-disabled-50"
# clone the existing cluster
echo "... cloning cluster $DDB_SOURCE_CLUSTER_IDENTIFIER to $DDB_CLONE_CLUSTER_IDENTIFIER"
cloneInfo=`aws docdb restore-db-cluster-to-point-in-time \
--source-db-cluster-identifier $DDB_SOURCE_CLUSTER_IDENTIFIER \
--db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER \
--restore-type copy-on-write \
--use-latest-restorable-time`
#echo $cloneInfo
dbClusterArn=`echo $cloneInfo | jq -r '.DBCluster.DBClusterArn'`
# watch for cluster to be "available"
T="$(date +%s)"
clusterStatus='unknown'
while true ; do
clusterInfo=`aws docdb describe-db-clusters --db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER`
clusterStatus=`echo $clusterInfo | jq -r '.DBClusters[0].Status'`
T2="$(($(date +%s)-T))"
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
echo "$thisDuration | waiting for cluster creation to complete"
if [[ "$clusterStatus" == "available" ]] ; then
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
echo "$thisDuration | finished creating cluster in $thisDuration"
break
fi
sleep $sleepSeconds
done
# create the instances
i=1
while [ $i -le $DDB_INSTANCE_COUNT ] ; do
DDB_DB_INSTANCE_IDENTIFIER="${DDB_CLONE_CLUSTER_IDENTIFIER}-${i}"
if [ $i -eq 1 ] ; then
echo "... creating primary instance $DDB_DB_INSTANCE_IDENTIFIER"
createInstanceInfo=`aws docdb create-db-instance \
--db-instance-identifier ${DDB_DB_INSTANCE_IDENTIFIER} \
--db-instance-class $DDB_INSTANCE_CLASS \
--engine $DDB_ENGINE \
--db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER`
else
echo "... creating read replica instance $DDB_DB_INSTANCE_IDENTIFIER"
createInstanceInfo=`aws docdb create-db-instance \
--db-instance-identifier ${DDB_DB_INSTANCE_IDENTIFIER} \
--db-instance-class $DDB_INSTANCE_CLASS \
--engine $DDB_ENGINE \
--db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER`
fi
i=$(($i+1))
done
# watch for all instances to be "available"
instanceStatus='unknown'
instancePendingModifiedValues=1
TTIMER="$(date +%s)"
while true ; do
instanceInfo=`aws docdb describe-db-instances --filters Name=db-cluster-id,Values=${dbClusterArn}`
i=0
availableInstances=0
instanceStatusString=""
while [ $i -lt $DDB_INSTANCE_COUNT ] ; do
instanceStatus=`echo $instanceInfo | jq -r ".DBInstances[${i}].DBInstanceStatus"`
if [[ "$instanceStatus" == "available" ]] ; then
availableInstances=$(($availableInstances+1))
fi
instanceStatusString="$instanceStatusString:$instanceStatus"
i=$(($i+1))
done
T2="$(($(date +%s)-T))"
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
instanceStatusString="$instanceStatusString:"
echo "$thisDuration | $availableInstances instance(s) ready | statuses are $instanceStatusString"
if [[ $availableInstances -eq $DDB_INSTANCE_COUNT ]] ; then
T2="$(($(date +%s)-TTIMER))"
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
echo "$thisDuration | finished creating all instances in $thisDuration"
break
fi
sleep $sleepSeconds
done
echo "... performing MVU on cluster $DDB_CLONE_CLUSTER_IDENTIFIER"
mvuInfo=`aws docdb modify-db-cluster \
--db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER \
--allow-major-version-upgrade \
--engine-version 5.0 \
--apply-immediately \
--db-cluster-parameter-group-name $PARAMETER_GROUP \
--region us-east-1`
#echo $mvuInfo
# watch for cluster to be "available"
clusterStatus='unknown'
TTIMER="$(date +%s)"
sleep $sleepSeconds
sleep $sleepSeconds
sleep $sleepSeconds
sleep $sleepSeconds
while true ; do
clusterInfo=`aws docdb describe-db-clusters --db-cluster-identifier $DDB_CLONE_CLUSTER_IDENTIFIER`
clusterStatus=`echo $clusterInfo | jq -r '.DBClusters[0].Status'`
T2="$(($(date +%s)-T))"
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
echo "$thisDuration | waiting for cluster MVU to complete"
if [[ "$clusterStatus" == "available" ]] ; then
T2="$(($(date +%s)-TTIMER))"
thisDuration=`printf "%02d:%02d:%02d:%02d" "$((T2/86400))" "$((T2/3600%24))" "$((T2/60%60))" "$((T2%60))"`
echo "$thisDuration | finished MVU in $thisDuration"
break
fi
sleep $sleepSeconds
done