forked from bolthole/zrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zrep_failover
198 lines (145 loc) · 4.24 KB
/
zrep_failover
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
#### File: zrep_failover
# run this on 'master' side, to make other side master
# Usage: zrep_failover fs[@snap] [remotehost]
#
zrep_failover(){
typeset local=0 fs snap="" remotehost remotefs check
if [[ "$1" == "-L" ]] ; then
local=1
shift
fi
if [[ "$1" == "" ]] ; then
usage
exit 1
fi
zfs list $1 >/dev/null || zrep_errquit invalid filesystem $1
check=`$ZFSGETLVAL ${ZREPTAG}:master $1`
if [[ "$check" != "yes" ]] ; then
zrep_errquit $1 not master. Cannot fail over
fi
fs="$1"
case $fs in
*@*)
snap=$fs
fs=${srcsnap%@*}
;;
esac
zrep_lock_fs $fs ||zrep_errquit could not lock $fs
if [[ "$2" != "" ]] ; then
remotehost="$2"
else
remotehost=`$ZFSGETVAL ${ZREPTAG}:dest-host $fs`
fi
remotefs=`$ZFSGETVAL ${ZREPTAG}:dest-fs $fs`
echo Setting readonly on local $fs, then syncing
zfs set readonly=on $fs
if ((local ==1)) ; then
echo Failover for $1 in LOCAL mode
if [[ "$snap" == "" ]] ; then
snap=`getlastsnapsent $1`
zfs list $1 >/dev/null ||
zrep_errquit No last synced snap found for $1. Cannot fail over
echo Rolling back to last sync $snap
else
echo Rolling back to specified snap $snap
fi
zfs rollback -Rr $snap ||zrep_errquit Rollback to $snap failed
else
## Need to sync both sides before mode switch!
## If named snap, roll back.
## otherwise, "roll forward" by doing one last sync
if [[ "$snap" != "" ]] ; then
typeset snapname
snapname=${snap#*@}
echo Rolling back to local $snap
zfs rollback -Rr $snap || zrep_errquit Rollback to $snap failed
echo Rolling back $remotehost to $remotefs@$snapname
zrep_ssh $remotehost zfs rollback $remotefs@$snapname ||
zrep_errquit remote rollback failed
else
# makes new snapshot, and syncs
_snapandsync $fs $remotehost $remotefs || zrep_errquit final sync failed. failover failed.
fi
fi
echo Reversing master properties for $Z_LOCAL_HOST:$fs
zfs set ${ZREPTAG}:dest-fs=$fs $fs
zfs set ${ZREPTAG}:dest-host=$Z_LOCAL_HOST $fs
zfs set ${ZREPTAG}:src-fs=$remotefs $fs
zfs set ${ZREPTAG}:src-host=$remotehost $fs
zfs inherit ${ZREPTAG}:master $fs
zrep_unlock_fs $fs
if (( local ==0)) ;then
echo Setting master on $remotehost:$remotefs
zrep_ssh $remotehost $ZREP_PATH takeover -L $remotefs
fi
}
# run this on 'dest' side, to promote it to master
# Usage: zrep_takeover fs[@snap] [remotehost]
#
zrep_takeover(){
typeset fs snap remotehost remotefs check local=0
if [[ "$1" == "-L" ]] ; then
local=1
shift
fi
if [[ "$1" == "" ]] ; then
usage
exit 1
fi
fs="$1"
zfs list $fs >/dev/null || zrep_errquit invalid filesystem $fs
check=`$ZFSGETLVAL ${ZREPTAG}:master $fs`
if [[ "$check" = "yes" ]] ; then
_errprint WARNING: $fs is already master on this host.
_errprint Presuming split-brain recovery mode ...
sleep 5
fi
if [[ "$2" != "" ]] ; then
remotehost="$2"
else
remotehost=`$ZFSGETVAL ${ZREPTAG}:src-host $fs`
fi
remotefs=`$ZFSGETVAL ${ZREPTAG}:src-fs $fs`
if (( local == 0 )) ; then
echo Starting failover from remote side $remotehost
zrep_ssh $remotehost $ZREP_PATH failover $remotefs
# This will ssh back into us to set src host property,
# and the other stuff, so we dont have to do that now.
exit $?
fi
# If here, we must be in local mode.
# So... just set properties!
# (and roll back, if desired)
case $fs in
*@*)
snap=$fs
fs=${srcsnap%@*}
;;
esac
zrep_lock_fs $fs
zfs inherit readonly $fs
if [[ "$snap" != "" ]] ; then
echo "WARNING: Before takeover, we will be rolling $fs"
echo -n " to $snapname, made at: "
$ZFSGETVAL creation $snap
echo ""
echo "All newer snapshots will be destroyed"
echo Continuing in 10 seconds...
sleep 10
zfs rollback -Rr $snap || zrep_errquit Rollback to $snap failed
fi
echo Setting master properties for $Z_LOCAL_HOST:$fs
zfs set ${ZREPTAG}:src-fs=$fs $fs
zfs set ${ZREPTAG}:src-host=$Z_LOCAL_HOST $fs
zfs set ${ZREPTAG}:dest-fs=$remotefs $fs
zfs set ${ZREPTAG}:dest-host=$remotehost $fs
zfs set ${ZREPTAG}:master=yes $fs
# Since we default to creating replicas unmounted... mount it now
if [[ "`$ZFSGETVAL type $fs`" == "filesystem" ]] ; then
if [[ "`$ZFSGETVAL mounted $fs`" == "no" ]] ; then
echo eMounting $Z_LOCAL_HOST:$fs
zfs mount $fs
fi
fi
zrep_unlock_fs $fs
}