-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
executable file
·125 lines (103 loc) · 3.2 KB
/
migrate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
set -e
set -o pipefail
if [[ -n ${DEBUG} ]] ; then
echo "DEBUG: VCAP_SERVICES: ${VCAP_SERVICES}"
echo "DEBUG: fromService: ${fromService}"
echo "DEBUG: toService: ${toService}"
fi
if [[ -z ${VCAP_SERVICES} ]] ; then
echo "\${VCAP_SERVICES} not set!"
exit 1
fi
if [[ -z ${fromService} ]] ; then
echo "\${fromService} not set!"
exit 1
fi
if [[ -z ${toService} ]] ; then
echo "\${toService} not set!"
exit 1
fi
declare -A from
declare -A to
from[service_type_name]=${OLD_SERVICE_TYPE_NAME:-redis}
to[service_type_name]=${NEW_SERVICE_TYPE_NAME:-redis}
from[host]=$( echo "${VCAP_SERVICES}" | jq ".\"${from[service_type_name]}\"[] | select( .name == \"${fromService}\" ) | .credentials.host" | tr -d '"')
from[password]=$( echo "${VCAP_SERVICES}" | jq ".\"${from[service_type_name]}\"[] | select( .name == \"${fromService}\" ) | .credentials.password" | tr -d '"')
from[port]=$( echo "${VCAP_SERVICES}" | jq ".\"${from[service_type_name]}\"[] | select( .name == \"${fromService}\" ) | .credentials.port" | tr -d '"')
to[host]=$( echo "${VCAP_SERVICES}" | jq ".\"${to[service_type_name]}\"[] | select( .name == \"${toService}\" ) | .credentials.host" | tr -d '"')
to[password]=$( echo "${VCAP_SERVICES}" | jq ".\"${to[service_type_name]}\"[] | select( .name == \"${toService}\" ) | .credentials.password" | tr -d '"')
to[port]=$( echo "${VCAP_SERVICES}" | jq ".\"${to[service_type_name]}\"[] | select( .name == \"${toService}\" ) | .credentials.port" | tr -d '"')
if [[ -n ${DEBUG} ]] ; then
echo "DEBUG: from[host]: ${from[host]}"
echo "DEBUG: from[password]: ${from[password]}"
echo "DEBUG: from[port]: ${from[port]}"
echo "DEBUG: to[host]: ${to[host]}"
echo "DEBUG: to[password]: ${to[password]}"
echo "DEBUG: to[port]: ${to[port]}"
fi
if [[ -z ${from[host]} ]] ; then
echo "could not resolve details of \"${fromService}\" from \${VCAP_SERVICES}!"
exit 1
fi
if [[ -z ${to[host]} ]] ; then
echo "could not resolve details of \"${toService}\" from \${VCAP_SERVICES}!"
exit 1
fi
echo "Starting redis Migration"
echo " from: ${fromService}"
echo " to: ${toService}"
echo ""
if [[ -n ${DEBUG} ]] ; then
echo "Going to execute the following command:
redis-cli \
-h ${to[host]} \
-p ${to[port]} \
-a ${to[password]} \
flushall
redis-stream \
-h ${from[host]} \
-p ${from[port]} \
-a ${from[password]} \
--flushdb \
--pipe | \
redis-cli \
-h ${to[host]} \
-p ${to[port]} \
-a ${to[password]} \
--pipe
"
fi
redis-cli \
-h ${to[host]} \
-p ${to[port]} \
-a ${to[password]} \
flushall
redis-stream \
-h ${from[host]} \
-p ${from[port]} \
-a ${from[password]} \
--flushdb \
--pipe | \
redis-cli \
-h ${to[host]} \
-p ${to[port]} \
-a ${to[password]} \
--pipe
sourceDb="$(redis-cli -h ${from[host]} -p ${from[port]} -a ${from[password]} DBSIZE)"
targetDb="$(redis-cli -h ${to[host]} -p ${to[port]} -a ${to[password]} DBSIZE)"
dbsizesame=0
if [ "$sourceDb" != "$targetDb" ]; then
dbsizesame=1
fi
RC=$?
echo "Finished redis Migration with RC: $?"
if [[ ${RC} -eq 0 && ${dbsizesame} -eq 0 ]]; then
echo "MIGRATION SUCCESSFULL"
else
echo "MIGRATION FAILED"
fi
while true ; do
echo "This App can now be deleted"
sleep 60
done