-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfailover.sh
127 lines (111 loc) · 4.01 KB
/
failover.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
126
127
#!/bin/bash
###
# failover.sh
###
# |||
# +------ooOO-(O O)-OOoo------+
# | (_) |
# | Sylvain Arbaudie |
# | [email protected] |
# +---------------------------+
###
# original code & doc by Sylvain Arbaudie
# github repo : https://github.com/SylvainA77/GaleraSlaveSwitcher
###
# this bash script is intended to be triggered by maxscale event API
# up to 4 args are expected : failed master ip --initiator=$INITIATOR
# format of $initiator is : [IP]:port
# list of slaves to switchover --children=$CHILDREN
# format of $children is : [IP]:port,[IP]:port,*
# name of the monitor which detect the master_down event : --monitor=monitorname
# (optional) target new master : --target=[IP]:port
# target disable new master lookup in maxscale
###
# alternatively can be called from command line with only 2 args :
# target new master : --target=[IP]:port
# list of slaves to switch over : --children=[IP]:port,[IP]:port,*
###
#TODO
###
# stderr & logs are sent to /var/log/failover.err
set +x
exec 2>/var/log/failover.err
# we need all those juicy functions, don't we ?
source /var/lib/lib-switchover.sh
source /etc/.credentials
# create debug environment
debug=0
[ $debug ] && { \
DEBUG_FILE="/var/log/maxscale/debug.log"
echo "DEBUG MODE ENABLED"
[ -f ${DEBUG_FILE} ] || \
touch ${DEBUG_FILE} || \
echoerr "ERROR : unable to create ${DEBUG_FILE}"
}
ARGS=$(getopt -o '' --long 'initiator:,children:,monitor:,target:' -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
--initiator)
shift;
initiator=$1
shift;
;;
--children)
shift;
children=$1
shift;
;;
--monitor)
shift;
monitor=$1
shift;
;;
--target)
shift;
target=$1
shift;
;;
--)
shift;
break;
;;
esac
done
#1 stopping slave to try and preserve relaylogs
# format of $children is : [IP]:port,[IP]:port,*
# so we have to break the string into an array of strings using , as a separator
IFS=',' read -ra childrens <<< "$children"
for child in "${childrens[@]}"
do
[[ -n "$debug" ]] && echoerr "child:$child"
thischild=$( echo $child | cut -d'[' -f2 | cut -d']' -f1 )
sqlexec $thischild "stop slave"
[[ -n "$debug" ]] && echoerr "sqlexec $thischild stop slave $?"
done
#2 find the new master
[ $debug ] && echo "DEBUG : Find new master : failedmaster : $failedmaster " >> ${DEBUG_FILE}
[ $debug ] && echo "DEBUG : Find new master : masterip : $masterip " >> ${DEBUG_FILE}
#2.1 if --target, then bypass maxscale
[[ -n "$target" ]] && masterip=$( echo $target | cut -d'[' -f2 | cut -d']' -f1 )
[[ -n "$debug" ]] && echoerr "target:$target"
#2.2 if --initiator, then call maxscale
[[ -n $"initiator" ]] && failedmaster=$( echo $initiator | cut -d'[' -f2 | cut -d']' -f1 )
[[ -n $"initiator" ]] && masterip=$( findnewmaster $failedmaster $monitor )
[[ -n "$debug" ]] && echoerr "initiator:$initiator"
[[ -n "$debug" ]] && echoerr "masterip:$masterip"
#3 perform the switchover on every oprhaned child to $masterip
# format of $children is : [IP]:port,[IP]:port,*
# so we have to break the string into an array of strings using , as a separator
IFS=',' read -ra childrens <<< "$children"
[ $debug ] && echo "DEBUG : IFS 2 : $IFS " >> ${DEBUG_FILE}
for child in "${childrens[@]}"
do
# format of $child is still [IP]:port
# so we have to extract the ip using both brackets as separators
thischild=$( echo $child | cut -d'[' -f2 | cut -d']' -f1 )
switchover $thischild $masterip
[[ -n "$debug" ]] && echoerr "switchover $thischild $masterip $?"
done
[ $debug ] && echo "DEBUG : End of file failover.sh " >> ${DEBUG_FILE}
exit $?