-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapomatic.snapshot
executable file
·228 lines (206 loc) · 8.67 KB
/
snapomatic.snapshot
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
#! /usr/bin/python3
import sys
pythonversion=sys.version_info
if pythonversion[0] != 3 or (pythonversion[1] < 6 or pythonversion[1] > 11):
print("This script requires Python 3.6 through 3.11")
sys.exit(1)
sys.path.append(sys.path[0] + "/NTAPlib")
from getSnapshots import getSnapshots
from getCGSnapshots import getCGSnapshots
from createSnapshots import createSnapshots
from createCGSnapshots import createCGSnapshots
from deleteCGSnapshots import deleteCGSnapshots
from deleteSnapshots import deleteSnapshots
import userio
snapomaticversion='DEV'
knownmodes=['show','create','delete','restore']
validoptions={}
validoptions['create']={'name':'str','prefix':'str','target':'multistr','cg':'bool','label':'str', \
'debug':'bool', \
'restdebug':'bool'}
validoptions['restore']={'name':'str','target':'multistr','cg':'bool', \
'debug':'bool', \
'restdebug':'bool'}
validoptions['show']={'name':'str','prefix':'str','target':'multistr','cg':'bool', \
'debug':'bool', \
'restdebug':'bool'}
validoptions['delete']={'name':'str','prefix':'str','target':'multistr','maxcount':'int','maxage':'str','cg':'bool', \
'debug':'bool', \
'force':'bool', \
'restdebug':'bool'}
requiredoptions={'create' :['target',['name','prefix']], \
'restore' :['target','name'], \
'show' :['target'], \
'delete' :['target',['name','prefix','maxcount','maxage']]}
mutex=[['name','prefix']]
usage="Version " + snapomaticversion + "\n" + \
"snapshot show\n" + \
" [--target]\n" + \
" (svm [filesystem, LUN, or volume]\n\n" + \
" (Optional. Name for snapshot. Wildcards accepted.)\n\n" + \
" [--cg]\n" + \
" (Restrict search to CG snapshots. Wildcards accepted.)\n\n" + \
" [--debug]\n" + \
" (show debug output)\n\n" + \
" [--restdebug]\n" + \
" (show REST API debug output)\n\n" \
"snapshot create\n" + \
" [--target]\n" + \
" (svm [filesystem, CG, LUN, or volume]\n\n" + \
" [--name||prefix]\n" + \
" (Name or prefix for snapshot)\n\n" + \
" [--label|]\n" + \
" (Snapmirror label for the snapshot)\n\n" + \
" [--cg]\n" + \
" (Create CG snapshots.)\n\n" + \
" [--debug]\n" + \
" (show debug output)\n\n" + \
" [--restdebug]\n" + \
" (show REST API debug output)\n\n" + \
"snapshot delete\n" + \
" [--target]\n" + \
" (svm [filesystem, LUN, or volume]\n\n" + \
" [--name]\n" + \
" (Optional. Name of snapshot. Wildcards accepted)\n\n" + \
" [--maxcount]\n" + \
" (Optional. Maximum number of snapshots to retain)\n\n" + \
" [--maxage]\n" + \
" (Optional. Maximum age of snapshots to retain.\n" + \
" (Accepts d[ays]/h[ours]/m[inutes]/[s]econds\n\n" + \
" [--force]\n" + \
" (Override all safeguards)\n\n" + \
" [--debug]\n" + \
" (show debug output)\n\n" + \
" [--restdebug]\n" + \
" (show REST API debug output)\n"
myopts=userio.validateoptions(sys.argv,validoptions,modes=knownmodes,usage=usage,mutex=mutex,required=requiredoptions)
svm=myopts.target[0]
voltargets=[]
luntargets=[]
fstargets=[]
if len(myopts.target) == 1:
voltargets.append('*')
else:
for item in myopts.target[1:]:
if not item[0] == '/':
voltargets.append(item)
if myopts.mode == 'create' and voltargets==['*']:
userio.fail("Cannot create snapshots with volumes = *")
debug=0
if myopts.debug:
debug=debug+1
if myopts.restdebug:
debug=debug+2
name=myopts.name
if myopts.mode == 'create':
label=myopts.label
if myopts.prefix is not None:
name=myopts.prefix + '_' + userio.randomtoken()[:32]
if myopts.mode == 'show':
if myopts.prefix is not None:
name=myopts.prefix + '_*'
if myopts.mode == 'delete':
maxcount=myopts.maxcount
maxage=myopts.maxage
force=myopts.force
if myopts.prefix is not None:
name=myopts.prefix + '_*'
try:
if (name == '*' or name == '.*' or name == '.*$') and myopts.force:
pass
except:
userio.fail("Cannot use snapshot wildcard without --force")
cg=myopts.cg
if myopts.mode == 'delete':
if len(voltargets) > 0:
if cg:
snapshots=deleteCGSnapshots(svm,voltargets,name,force=force,maxcount=maxcount,maxage=maxage,debug=debug)
grid=[['CG','Volume','Snapshot','Status']]
else:
snapshots=deleteSnapshots(svm,voltargets,name,force=force,maxcount=maxcount,maxage=maxage,debug=debug)
grid=[['Volume','Snapshot','Status']]
if not snapshots.go():
userio.message("ERROR: " + snapshots.reason)
else:
userio.message("Success")
deleted=list(snapshots.deleted.keys())
deleted.sort()
if cg:
for cg in deleted:
deletedvols=list(snapshots.deleted[cg])
deletedvols.sort()
for volume in deletedvols:
for snap in snapshots.deleted[cg][volume]:
grid.append([cg,volume,snap,'Deleted'])
else:
for volume in deleted:
for snap in snapshots.deleted[volume]:
grid.append([volume,snap,'Deleted'])
failed2delete=list(snapshots.failed.keys())
failed2delete.sort()
if cg:
for cg in failed2delete:
deletedvols=list(snapshots.deleted[cg])
deletedvols.sort()
for volume in deletedvols:
for snap in snapshots.failed[volume]:
grid.append([volume,snap,'FAIL'])
else:
for volume in failed2delete:
for snap in snapshots.failed[volume]:
grid.append([volume,snap,'FAIL'])
if len(failed2delete) > 0 or len(deleted) > 0:
userio.grid(grid)
elif myopts.mode == 'create':
if len(voltargets) > 0:
if cg:
snapshots=createCGSnapshots(svm,voltargets,name,cg=True,label=label,debug=debug)
else:
snapshots=createSnapshots(svm,voltargets,name,label=label,debug=debug)
if not snapshots.go():
userio.message("ERROR: " + snapshots.reason)
else:
userio.message("Success")
if cg:
grid=[['CG','Snapshot','Status']]
else:
grid=[['Volume','Snapshot','Status']]
for volume in snapshots.success:
grid.append([volume,name,'Success'])
for volume in snapshots.failed:
grid.append([volume,name,'Fail'])
userio.grid(grid)
elif myopts.mode == 'show':
if len(voltargets) > 0:
if cg:
snapshots=getCGSnapshots(svm,cgs=voltargets,cg=True,name=name,debug=debug)
else:
snapshots=getSnapshots(svm,volumes=voltargets,name=name,debug=debug)
if not snapshots.go():
userio.message("ERROR: " + snapshots.reason)
sys.exit(1)
else:
if cg:
grid=[['CG','Parent','Volume','Snapshot','Date']]
cglist=list(snapshots.snapshots.keys())
cglist.sort()
for cgname in cglist:
try:
parentfield=snapshots.cgs[cgname]['parent']
except:
parentfield='-'
volumelist=list(snapshots.snapshots[cgname]['volumes'].keys())
volumelist.sort()
for volume in volumelist:
snapshotlist=sorted(snapshots.snapshots[cgname]['ordered'],key=lambda x: int(x[1]))
for snapshot,epoch in snapshotlist:
grid.append([cgname,parentfield,volume,snapshot,snapshots.snapshots[cgname]['snapshots'][snapshot]['createtime']])
else:
grid=[['Volume','Snapshot','Date']]
volumelist=list(snapshots.snapshots.keys())
volumelist.sort()
for volume in volumelist:
snapshotlist=sorted(snapshots.snapshots[volume]['ordered'],key=lambda x: int(x[1]))
for snapshot,epoch in snapshotlist:
grid.append([volume,snapshot,snapshots.snapshots[volume]['snapshots'][snapshot]['createtime']])
userio.grid(grid)