forked from bolthole/zrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzrep_failover
179 lines (129 loc) · 3.89 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
#### File: zrep_failover
# run this on 'master' side, to make other side master
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
remotehost=`$ZFSGETVAL ${ZREPTAG}:dest-host $fs`
remotefs=`$ZFSGETVAL ${ZREPTAG}:dest-fs $fs`
print Setting readonly on local $fs, then syncing
zfs set readonly=on $fs
if ((local ==1)) ; then
print 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
print Rolling back to last sync $snap
else
print 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#*@}
print Rolling back to local $snap
zfs rollback -Rr $snap || zrep_errquit Rollback to $snap failed
print 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
print 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
print Setting master on $remotehost:$remotefs
zrep_ssh $remotehost $ZREP_PATH takeover -L $remotefs
fi
}
# run this on 'dest' side, to promote it to master
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
zrep_errquit $fs is already master. Cannot takeover
fi
remotehost=`$ZFSGETVAL ${ZREPTAG}:src-host $fs`
remotefs=`$ZFSGETVAL ${ZREPTAG}:src-fs $fs`
if (( local == 0 )) ; then
print Starting failover from remote side $remotehost
zrep_ssh $remotehost $ZREP_PATH failover $remotefs
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
print "WARNING: Before takeover, we will be rolling $fs"
print -n " to $snapname, made at: "
zfs get -H -o value creation $snap
print ""
print "All newer snapshots will be destroyed"
print Continuing in 10 seconds...
sleep 10
zfs rollback -Rr $snap || zrep_errquit Rollback to $snap failed
fi
print 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 [[ "`zfs get -H -o value type $fs`" == "filesystem" ]] ; then
if [[ "`zfs get -H -o value mounted $fs`" == "no" ]] ; then
print Mounting $Z_LOCAL_HOST:$fs
zfs mount $fs
fi
fi
zrep_unlock_fs $fs
}