-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapomatic.cloneSAN4DR
executable file
·295 lines (261 loc) · 10.8 KB
/
snapomatic.cloneSAN4DR
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#! /usr/bin/python3
import sys
pythonversion=sys.version_info
if pythonversion[0] != 3 or (pythonversion[1] < 6 or pythonversion[1] > 11):
userio.message("This script requires Python 3.6 through 3.11")
sys.exit(1)
sys.path.append(sys.path[0] + "/NTAPlib")
import userio
from getSnapmirror import getSnapmirror
from getSnapshots import getSnapshots
from getLUNs import getLUNs
from cloneVolumes import cloneVolumes
from mapLUNs import mapLUNs
import time
import getopt
import re
snapomaticversion='DEV'
validoptions={'target':'multistr',
'igrouptoken':'int',
'snapshot-prefix':'str',
'igroupname':'str',
'skew':'int',
'split':'bool',
'nosleep':'bool',
'debug':'bool',
'restdebug':'bool',
'recoverypoint':'timestamp',
'after':'bool',
'nocgs':'bool',
'before':'bool'}
requiredoptions=['target','igrouptoken']
mutex=[['after','before']]
usage="Version " + snapomaticversion + "\n" + \
"clone4DR --target\n" + \
" (name of target svm and volumes)\n" + \
" (syntax: svm volume or svm volume/lun, wildcards accepted)\n\n" + \
" --snapshot-prefix\n" + \
" (optionally restrict search to snapshots with a prefix* syntax)\n\n" + \
" --igrouptoken\n" + \
" (identifies the _ delimited position of the igroup)\n\n" + \
" --recoverypoint\n" + \
" (recovery timestamp in YYYY-MM-DDTHH:MM:SS+ZZZZ)\n\n" + \
" --after|--before\n" + \
" (used with --recoverypoint)\n" + \
" (look for snapshots before|after specified recoverypoint)\n" + \
" (default behavior is AFTER)\n\n" + \
" --skew\n" + \
" (maximum time difference between cloned volumes, seconds)\n\n" + \
" [--igroupname]\n" + \
" (optionally specifies the igroup name, %=token\n\n" + \
" [--split]\n" + \
" (split the cloned volumes)\n\n" + \
" [--nocgs]\n" + \
" (Skip CG snapshot discovery)\n\n" + \
" [--debug]\n" + \
" (show debug output)\n\n" + \
" [--restdebug]\n" + \
" (show REST API calls and responses)\n\n"
myopts=userio.validateoptions(sys.argv,validoptions,mutex=mutex,usage=usage,required=requiredoptions)
svm=myopts.target[0]
voltargets={}
if len(myopts.target) == 1:
voltargets={'*':['*']}
else:
for item in myopts.target[1:]:
pieces=item.split('/')
if pieces[0] not in voltargets.keys():
voltargets[pieces[0]] = []
if len(pieces) == 1:
voltargets[pieces[0]].append('*')
elif len(pieces) == 2:
voltargets[pieces[0]].append(pieces[1])
else:
userio.fail("Illegal format for target " + item)
for vol in voltargets.keys():
if len(voltargets[vol]) > 1 and '*' in voltargets[vol]:
userio.fail("Volume targets includes a volume/LUN pair and that same volume")
igrouptoken=myopts.igrouptoken
try:
recoverytarget=myopts.recoverypoint
if myopts.before:
recoverBefore=True
else:
recoverBefore=False
except:
recoverytarget=False
prefix=myopts.snapshot_prefix
splitclones=myopts.split
debug=0
if myopts.debug:
debug=debug+1
if myopts.restdebug:
debug=debug+2
igroupname=myopts.igroupname
nosleep=myopts.nosleep
userio.linefeed()
userio.message("Retrieving snapmirrored volumes...")
snapmirror=getSnapmirror(svm,volumes=voltargets.keys(),debug=debug)
if not snapmirror.go():
snapmirror.showDebug()
if len(snapmirror.snapmirrorDestinations.keys()) < 1:
userio.fail("No matching snapmirrored volumes detected")
clonelist=[]
if len(snapmirror.snapmirrorDestinations[svm]['volumes'].keys()) > 0:
userio.message("Found matching snapmirrored volumes")
else:
userio.fail("No matching snapmirrored volumes detected")
for volume in snapmirror.snapmirrorDestinations[svm]['volumes'].keys():
userio.message(">> " + volume)
clonelist.append(volume)
userio.message()
userio.linefeed()
userio.message("Retrieving snapshots...")
if prefix:
snapshots=getSnapshots(svm,volumes=clonelist,name=prefix + "*",nocgs=myopts.nocgs, debug=debug)
else:
snapshots=getSnapshots(svm,volumes=clonelist,nocgs=myopts.nocgs,debug=debug)
if not snapshots.go():
snapshots.showDebug()
for vol in clonelist:
if vol in snapshots.snapshots.keys():
try:
userio.message("Most recent snapshot on volume " + vol + ":")
userio.message(">> " + snapshots.snapshots[vol]['recent'])
except:
userio.message("No matching snapshots on volume " + vol + ":")
else:
userio.message("No matching snapshots found on volume " + vol)
userio.message()
userio.linefeed()
clonetargets={}
unable2clone=[]
ready2clone=[]
userio.message("Verifying snapshot state matches requirements...")
for vol in clonelist:
snapshot2clone=False
timestamp=False
if not recoverytarget:
if len(snapshots.snapshots[vol]['ordered']) > 0:
snapshot2clone=snapshots.snapshots[vol]['recent']
userio.message("Volume " + vol + " most recent snapshot is " + \
snapshots.snapshots[vol]['snapshots'][snapshots.snapshots[vol]['recent']]['createtime'])
clonetargets[vol]=snapshots.snapshots[vol]['snapshots'][snapshot2clone]
ready2clone.append([vol,snapshot2clone])
else:
userio.message("Unable to find snapshot for " + vol + " that matches requested recoverypoint")
unable2clone.append(vol)
else:
#print("Recovery point:" + str(recoverytarget))
for x in range(0,len(snapshots.snapshots[vol]['ordered'])):
#print()
#print("Snapshot:" + str(snapshots.snapshots[vol]['ordered'][x]))
#print("Delta: " + str(snapshots.snapshots[vol]['ordered'][x][1] - recoverytarget))
#print("x=" + str(x))
if recoverBefore:
#if snapshots.snapshots[vol]['ordered'][x][1] < recoverytarget:
#print("1:Snapshot " + snapshots.snapshots[vol]['ordered'][x][0] + " is older than recoverypoint")
#if len(snapshots.snapshots[vol]['ordered']) >= x + 1:
#print("2:Dictionary has another element, length is " + str(len(snapshots.snapshots[vol]['ordered'])))
if snapshots.snapshots[vol]['ordered'][x][1] < recoverytarget:
timestamp=snapshots.snapshots[vol]['snapshots'][snapshots.snapshots[vol]['ordered'][x][0]]['epoch']
snapshot2clone=snapshots.snapshots[vol]['ordered'][x][0]
break
else:
if snapshots.snapshots[vol]['ordered'][x][1] > recoverytarget \
and (len(snapshots.snapshots[vol]['ordered']) == x + 1 \
or snapshots.snapshots[vol]['ordered'][x+1][1] < recoverytarget):
timestamp=snapshots.snapshots[vol]['snapshots'][snapshots.snapshots[vol]['ordered'][x][0]]['epoch']
snapshot2clone=snapshots.snapshots[vol]['ordered'][x][0]
break
if snapshot2clone:
userio.message("Snapshot "+ vol + ":" + snapshot2clone + " with timestamp " + snapshots.snapshots[vol]['snapshots'][snapshot2clone]['date'] + " meets recovery target")
clonetargets[vol]=snapshots.snapshots[vol]['snapshots'][snapshot2clone]
ready2clone.append([vol,snapshot2clone])
else:
userio.message("Unable to find snapshot for " + vol + " that matches requested recoverypoint")
unable2clone.append(vol)
if len(ready2clone) < 1:
userio.fail("No matching volumes detected")
if len(unable2clone) > 0:
userio.linefeed()
if len(ready2clone) > 0:
userio.message("The following volumes are scheduled for cloning")
for vol,snapshot in ready2clone:
userio.message(">> " + vol + ":" + snapshot)
userio.warn("The following volumes do not have snapshot matching requested recoverypoint")
for vol in unable2clone:
userio.message(">> " + vol)
if not userio.yesno("Proceed?"):
userio.message("Exiting...")
sys.exit(0)
if myopts.skew:
userio.linefeed()
userio.message("User specified a maximum skew of " + str(myopts.skew) + " seconds")
skews=[]
for vol in clonetargets.keys():
skews.append([vol,clonetargets[vol]['name'],clonetargets[vol]['date'],clonetargets[vol]['epoch']])
skewgrid=sorted(skews,key=lambda x: int(x[3]))
skew=int(skewgrid[-1][3] - skewgrid[0][3])
skewgrid.insert(0,['Volume','Snapshot','Date','Epoch seconds'])
userio.grid(skewgrid)
if skew > myopts.skew:
userio.warn("Difference in available snapshots exceeds skew time")
if not userio.yesno("Proceed?"):
userio.message("Exiting...")
sys.exit(0)
luns2clone=[]
volumes2clone=[]
userio.message("Retrieving LUNs from current volumes...")
for volume in clonetargets.keys():
lunlist=[]
for volpattern in voltargets.keys():
if volpattern == '*':
volpatternmatch=re.compile('^.*.$')
elif re.findall(r'[?*.^$]',volpattern):
volpatternmatch=re.compile(volpattern)
else:
volpatternmatch=re.compile('^' + volpattern + '$')
if re.match(volpatternmatch,volume):
for lun in voltargets[volpattern]:
lunlist.append("/vol/" + volume + "/" + lun)
matchingluns=getLUNs(svm,lun=lunlist,debug=debug)
if not matchingluns.go():
userio.message("Unable to retrieve LUN list")
matchingluns.showDebug()
sys.exit(1)
else:
for lun in matchingluns.luns.keys():
userio.message(">> " + lun)
luns2clone.append(lun)
volumes2clone.append(lun.split('/')[2])
volumes2clone=list(set(volumes2clone))
userio.linefeed()
userio.message("Cloning volumes...")
clonecompleted=[]
clonefailed=[]
for vol in volumes2clone:
clone=cloneVolumes(svm,(vol,"failover_"+ vol,clonetargets[vol]['uuid']),uuid=True,split=splitclones,debug=debug)
if not clone.go():
clone.showDebug()
clonefailed.append(vol)
else:
clonecompleted.append(vol + "failover_")
userio.message()
if not nosleep:
userio.message("Sleeping 10 seconds for LUN registration to complete")
time.sleep(10)
userio.message()
for lun in luns2clone:
lunpath=lun.split('/')
lunpath[2]='failover_' + lunpath[2]
lun=('/').join(lunpath)
igroupbase=lun.split('_')[igrouptoken]
if igroupname:
igroup=igroupname.replace('%',igroupbase)
else:
igroup=igroupbase
userio.message("Mapping " + lun + " to igroup " + igroup)
mapping=mapLUNs(svm,lun,igroup,debug=debug)
if not mapping.go():
mapping.showDebug()