forked from SylvainA77/GaleraSlaveSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failover.sh
91 lines (78 loc) · 2.5 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
#!/bin/bash
###
# failover.sh
###
# |||
# +------ooOO-(O O)-OOoo------+
# | (_) |
# | Sylvain Arbaudie |
# | arbaudie.it@gmail.com |
# +---------------------------+
###
# 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
# hence only 2 args are exepected : failed master ip --initiator=$INITIATOR
# and list of slaves to switchover --children=$CHILDREN
#TODO : add optional --target=$NEWMASTERIP parameter for manual triggering w/out maxscale
###
# stderr & logs are sent to /var/log/failover.err
exec 2>/var/log/failover.err
debug=1
# we need all those juicy functions don't we ?
source /var/lib/lib-switchover.sh
source /etc/.credentials
ARGS=$(getopt -o '' --long 'initiator:,children:,monitor:' -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
--initiator)
shift;
initiator=$1
shift;
;;
--children)
shift;
children=$1
shift;
;;
--monitor)
shift;
monitor=$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
failedmaster=$( echo $initiator | cut -d'[' -f2 | cut -d']' -f1 )
masterip=$( findnewmaster $failedmaster $monitor )
[[ -n "$debug" ]] && echoerr "newmasterip:$masterip"
#3 perform the switchover on every oprhaned child
# 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
# 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
exit $?