-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapomatic.RACfailover
executable file
·516 lines (470 loc) · 19.3 KB
/
snapomatic.RACfailover
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#! /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")
import os
import platform
import re
from getOracleHome import getOracleHome
from doProcess import doProcess
from discoverLUN import discoverLUN
from doSqlplus import doSqlplus
from getOwner import getOwner
from getOracleBase import getOracleBase
import userio
import string
import time
snapomaticversion='DEV'
validoptions={'volpattern':'str','recoverypoint':'timestamp','snapshot-optimized':'bool','debug':'bool','noscan':'bool','noafd':'bool','noasmlib':'bool','iscsi':'bool'}
requiredoptions=['volpattern']
usage="Version " + snapomaticversion + "\n" + \
"RACfailover --volpattern\n" + \
" (target volume pattern)\n\n" + \
" --recoverypoint\n" + \
" (recovery timestamp in YYYY-MM-DDTHH:MM:SS[TZ])\n\n" + \
" --snapshot-optimized\n" + \
" (use snapshot-optimized recovery procedure instead of backup mode procedure)\n\n" + \
" --debug\n" + \
" (print process STDOUT and STDERR)\n\n" + \
" --iscsi\n" + \
" (Scan for iSCSI LUNs)\n\n" + \
" --noscan\n" + \
" (Bypass LUN scanning)\n\n" + \
" --noafd\n" + \
" (Bypass ASM Filter Driver scanning)\n\n" + \
" --noasmlib\n" + \
" (Bypass ASMlib scanning)\n"
myopts=userio.validateoptions(sys.argv,validoptions,usage=usage,required=requiredoptions)
volpattern=myopts.volpattern
localhost=platform.node().split('.', 1)[0]
if volpattern == '*':
volpatternmatch=re.compile('^.*.$')
elif re.findall(r'[?*.^$]',volpattern):
volpatternmatch=re.compile(volpattern)
else:
print("WARNING: No wildcard detected in --volpattern")
volpatternmatch=re.compile('^' + volpattern + '$')
debug=0
if myopts.debug:
debug=25
noscan=myopts.noscan
noasmlib=myopts.noasmlib
noafd=myopts.noafd
scaniscsi=myopts.iscsi
try:
recoverypoint=myopts.recoverypoint
rmanrecoverysecs=recoverypoint-time.localtime().tm_gmtoff
rmanrecoverypoint=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(rmanrecoverysecs))
snapshottime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(rmanrecoverysecs-15))
except:
rmanrecoverypoint=False
gridhome=getOracleHome(grid=True,debug=debug)
if not gridhome.go():
print("Unable to discover Grid home for host")
if not debug:
gridhome.showDebug()
sys.exit(1)
homes=getOracleHome(debug=debug)
if not homes.getVersion():
print("Unable to discover ORACLE_HOME verions")
if not debug:
homes.showDebug()
sys.exit(1)
versionmapping={}
for home in homes.installed.keys():
versionmapping[homes.installed[home]['release']]=home
if homes.installed[home]['version'] is not None:
versionmapping[homes.installed[home]['version']] = home
if noscan:
print()
print("Bypassing LUN scanning...")
print()
else:
for item in gridhome.olsnodes.keys():
if item == localhost:
ssh=None
else:
ssh=item
if scaniscsi:
print("Updating iSCSI targets")
out=doProcess("/usr/sbin/iscsiadm -m session --rescan",env={'PATH':'/usr/bin:/usr/sbin:/bin'},ssh=ssh,debug=debug)
else:
print("Scanning for FC LUNs")
out=doProcess("/usr/bin/rescan-scsi-bus.sh",env={'PATH':'/usr/bin:/usr/sbin:/bin'},ssh=ssh,debug=debug)
if not debug and out.result > 0:
out.showDebug()
sys.exit(1)
print()
print("Sleeping for 10 seconds while multipath maps are built")
time.sleep(10)
print()
if noafd:
print("Bypassing ASM Filter Driver scanning...")
print()
else:
print("Discovering AFD devices...")
for item in gridhome.olsnodes.keys():
if item == localhost:
ssh=None
else:
ssh=item
out=doProcess(gridhome.home + '/bin/asmcmd afd_lslbl',env={'ORACLE_HOME':gridhome.home,
'ORACLE_BASE':gridhome.base},
ssh=ssh,
debug=debug)
if not debug and out.result > 0:
out.showDebug()
print("Refreshing AFD configuration")
for item in gridhome.olsnodes.keys():
if item == localhost:
ssh=None
else:
ssh=item
out=doProcess(gridhome.home + '/bin/asmcmd afd_refresh',env={'ORACLE_HOME':gridhome.home,
'ORACLE_BASE':gridhome.base},
ssh=ssh,
debug=debug)
success=False
for y in range (0,len(out.stdout)):
try:
if out.stdout[y][-7:] == 'echo $?' and not out.stdout[y+1] == '0':
success=True
except Exception as e:
pass
if not debug and out.result > 0:
out.showDebug()
print()
if noasmlib:
print("Bypassing ASMlib scanning...")
print()
else:
print("Refreshing ASMlib configuration")
for item in gridhome.olsnodes.keys():
if item == localhost:
ssh=None
else:
ssh=item
out=doProcess('/usr/sbin/oracleasm scandisks',ssh=ssh,debug=debug)
success=False
for y in range (0,len(out.stdout)):
try:
if out.stdout[y][-7:] == 'echo $?' and not out.stdout[y+1] == '0':
success=True
except Exception as e:
pass
if not success:
print("Unable to scan ASMlib disks on host " + item)
diskgroups=[]
print("Retrieving ASM diskgroup names")
multipaths=[]
devices=os.listdir('/dev')
for lun in devices:
if len(lun) > 3 and lun[:3] == 'dm-':
out=discoverLUN('/dev/' + lun,debug=debug)
if out.go():
multipaths.append(out)
for item in multipaths:
if item.luntype == 'NETAPP' and re.match(volpatternmatch,item.volume):
header=doProcess(gridhome.home + '/bin/kfed read ' + item.device, env={'ORACLE_HOME':gridhome.home}
, debug=debug)
if header.stdout is not None:
try:
getgrp=[line for line in header.stdout if 'kfdhdb.grpname' in line][0]
diskgroup=getgrp.split()[1]
diskgroups.append(diskgroup)
except:
pass
diskgroups=list(set(diskgroups))
for item in diskgroups:
print(">> Identified diskgroup " + item)
print()
if len(diskgroups) == 0:
print("ERROR: No diskgroups found on volumes that match pattern " + volpattern)
sys.exit(1)
print("Identifying currently mounted ASM diskgroups")
out=doSqlplus('+ASM' + gridhome.olsnodes[localhost],'select name,state from v$asm_diskgroup;',
user=gridhome.user,
priv='sysasm',
home=gridhome.home,
base=gridhome.base,
debug=debug)
if out.result > 0 and not debug:
out.showDebug()
sys.exit(1)
diskgroups2mount=diskgroups.copy()
for item in out.stdout:
try:
name,state = item.split()
if state == 'MOUNTED' and name in diskgroups2mount:
print(">> ASM diskgroup " + name + " is mounted")
diskgroups2mount.remove(name)
except:
pass
print()
print("Mounting ASM diskgroups...")
for item in gridhome.olsnodes.keys():
for diskgroup in diskgroups2mount:
print("Mounting " + diskgroup + " on host " + item)
if item == localhost:
ssh=None
else:
ssh=item
out=doSqlplus('+ASM' + gridhome.olsnodes[item], 'alter diskgroup ' + diskgroup + ' mount;',
user=gridhome.user,
priv='sysasm',
home=gridhome.home,
base=gridhome.base,
ssh=ssh,
feedback=True,
debug=debug)
if out.result > 0:
print("Unable to run sqlplus on host " + item)
if out.result > 0 and not debug:
out.showDebug()
success=False
for line in out.stdout:
if 'Diskgroup altered.' in line or 'is already mounted' in line:
success=True
break
if not success:
print("Unable to mount diskgroup " + diskgroup + " on host " + item)
if not debug:
out.showDebug()
print()
diskgroupmap={}
print("Discovering contents of ASM diskgroup")
print("Retrieving contents of diskgroups...")
print(">> Running asmcmd...")
out=doProcess(gridhome.home + '/bin/asmcmd ls */*/*',
env={'ORACLE_HOME':gridhome.home,
'ORACLE_BASE':gridhome.base},
debug=debug)
if out.result > 0 and not debug:
out.showDebug()
if out.result > 0:
print("Failed to run asmcmd ls */*/*")
sys.exit(1)
for x in range(0,len(out.stdout)):
elements=out.stdout[x].split('/')
if len(elements) > 2 and elements[0][0]=='+' and elements[0][1:] in diskgroups:
if elements[1] not in diskgroupmap.keys():
diskgroupmap[elements[1]]={'DISKGROUPS':[],'SPFILE':None,'PWFILE':None}
if elements[0][1:] not in diskgroupmap[elements[1]]['DISKGROUPS']:
print(">> Found directory " + elements[1] + ' on diskgroup ' + elements[0])
diskgroupmap[elements[1]]['DISKGROUPS'].append(elements[0][1:])
if len(elements) == 4 and elements[0][1:] in diskgroups:
if elements[2] == 'PARAMETERFILE':
diskgroupmap[elements[1]]['SPFILE']=out.stdout[x][:-1] + out.stdout[x+1]
elif elements[2] == 'PASSWORD':
diskgroupmap[elements[1]]['PWFILE']=out.stdout[x][:-1] + out.stdout[x+1]
if debug:
print("Diskgroup map dictionary: " + str(diskgroupmap))
potentialdatabases=list(diskgroupmap.keys())
for key in potentialdatabases:
if diskgroupmap[key]['SPFILE'] is None or diskgroupmap[key]['PWFILE'] is None:
del diskgroupmap[key]
else:
print(">> Identified database " + key)
diskgroupmap[key]['DISKGROUPS']=list(set(diskgroupmap[key]['DISKGROUPS']))
print()
nohome=[]
registered=[]
availversions=list(versionmapping.keys())
availversions.sort()
if debug:
print("Oracle version map: " + str(versionmapping))
print("Extracting spfiles...")
for dbuname in diskgroupmap.keys():
print(">> Attempting to create pfile from spfile " + diskgroupmap[dbuname]['SPFILE'])
oraclehome=False
for version in availversions:
oraclebase=getOracleBase(versionmapping[version])
oraclebase.go()
print(" >> Using ORACLE_HOME " + versionmapping[version])
tmppfile='/tmp/pfile.tmp.' + userio.randomtoken()
out=doSqlplus(dbuname,"create pfile='" + tmppfile \
+ "' from spfile='" \
+ diskgroupmap[dbuname]['SPFILE'] \
+ "';",
user=oraclebase.user,
home=oraclebase.home,
base=oraclebase.base,
local=True,
debug=debug)
if out.result > 0 or out.errorflag:
print("Failed to run sqlplus")
print(out.reason)
else:
print(" >> Parsing pfile")
params=open(tmppfile,'r').read().splitlines()
for line in params:
if 'compatible=' in line:
compatversion=line.split('=')[1].replace('"','').replace("'",'')
print(">> Database " + dbuname + " configured for " + compatversion)
for avail in availversions:
if compatversion == avail[:len(compatversion)]:
oraclehome=versionmapping[avail]
if 'db_name=' in line:
dbname=line.split('=')[1].replace('"','').replace("'",'')
dbname='N' + dbname
print(">> Database " + dbuname + " has db_name of " + dbname)
if oraclehome:
break
if not oraclehome:
print(" >> Unable to find ORACLE_HOME that can support this database")
nohome.append(dbuname)
else:
print(" >> Compatible ORACLE_HOME found at " + oraclehome)
print(">> Creating directory structure")
oracleuser=getOwner(oraclehome)
dirs=[homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/adump", \
homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/dpdump", \
homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/hdump", \
homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/pfile", \
homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/scripts", \
homes.installed[oraclehome]['base'] + "/admin/" + dbuname + "/xdb_wallet", \
homes.installed[oraclehome]['base'] + "/audit/" + dbuname + "/adump"]
mkdirs="/usr/bin/mkdir -p " + ' '.join(dirs)
for item in gridhome.olsnodes.keys():
print(" >> Running mkdir commands on host " + item)
if item == localhost:
out=doProcess(mkdirs, \
user=oracleuser.user, \
env={'PATH':'/usr/bin,/usr/local/bin,/bin'},
debug=debug)
else:
out=doProcess("ssh " + item,user=oracleuser.user,stdin=mkdirs)
print(">> Running svrctl add for " + dbuname)
out=doProcess(oraclehome + '/bin/srvctl add database -db ' + dbuname \
+ ' -dbname ' + dbname \
+ ' -oraclehome ' + oraclehome \
+ ' -spfile ' + diskgroupmap[dbuname]['SPFILE'] \
+ ' -pwfile ' + diskgroupmap[dbuname]['PWFILE'] \
+ ' -server ' + localhost \
+ ' -diskgroup ' + ','.join(diskgroupmap[dbuname]['DISKGROUPS']) \
+ ' -dbtype RACONENODE',
env={'ORACLE_HOME':oraclehome,
'ORACLE_BASE':oraclebase.base},
user=oracleuser.user,
debug=debug)
if out.result > 0 and not debug:
out.showDebug()
if out.result > 0:
print(" >> Error registering database")
else:
print(" >> Database registration successful")
registered.append((dbuname,oraclehome,oraclebase.base,oracleuser.user))
print()
print()
mounted=[]
print("Starting databases...")
for dbuname,oraclehome,oraclebase,oracleuser in registered:
mountcomplete=False
print(">> Mounting database " + dbuname)
out=doProcess(oraclehome + '/bin/srvctl start database -db ' + dbuname + " -startoption mount",
env={'ORACLE_HOME':oraclehome,
'ORACLE_BASE':oraclebase},
user=oracleuser,
debug=debug)
if out.result == 0:
print(" >> Database mounted")
mounted.append((dbuname,oraclehome,oraclebase,oracleuser))
mountcomplete=True
else:
for line in out.stdout:
if 'is already running' in line:
print("Database " + dbuname + " is already running, will attempt recovery")
mounted.append((dbuname,oraclehome,oraclebase,oracleuser))
mountcomplete=True
break
if (dbuname,oraclehome,oraclebase,oracleuser) not in mounted:
print(" >> Error mounting database")
if not debug:
out.showDebug()
if (out.result > 0 or not mountcomplete) and not debug:
out.showDebug()
print()
recovered=[]
print("Recovering databases...")
for dbuname,oraclehome,oraclebase,oracleuser in mounted:
recoverycomplete=False
print(">> Recovering database " + dbuname)
if rmanrecoverypoint:
if myopts.snapshot_optimized:
out=doSqlplus(dbuname + '_1',"recover automatic database until time '" + rmanrecoverypoint + "' snapshot time '" + snapshottime + "';",
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
else:
out=doSqlplus(dbuname + '_1',"recover automatic database until time '" + rmanrecoverypoint + "';",
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
else:
out=doSqlplus(dbuname + '_1','recover automatic;',
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
if out.result == 0 and len(out.stdout) > 0:
for line in out.stdout:
print(line)
if 'Media recovery complete' or 'no recovery required' in line:
print(">> Database " + dbuname + " recovery complete")
recovered.append((dbuname,oraclehome,oraclebase,oracleuser))
recoverycomplete=True
break
if (out.result > 0 or not recoverycomplete) and not debug:
out.showDebug()
print()
opened=[]
alreadyopen=[]
print("Opening databases...")
for dbuname,oraclehome,oraclebase,oracleuseroracle in recovered:
print(">> Opening database " + dbuname)
if rmanrecoverypoint:
out=doSqlplus(dbuname + '_1','alter database open resetlogs;',
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
else:
out=doSqlplus(dbuname + '_1','alter database open;',
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
if out.result > 0 and not debug:
out.showDebug()
else:
for line in out.stdout:
if 'database already open' in line:
print(">> Database " + dbuname + " already open")
check4open=True
alreadyopen.append(dbuname)
break
out=doSqlplus(dbuname + '_1','select status from v$instance',
user=oracleuser,
home=oraclehome,
base=oraclebase,
debug=debug)
if dbuname not in alreadyopen:
if out.result > 0:
print(">> Failed to run sqlplus")
failed.append(dbuname)
elif len(out.stdout) > 0 and out.stdout[-1] == 'OPEN':
print(">> Database " + dbuname + " is open")
opened.append(dbuname)
if out.result > 0 and not debug:
out.showDebug()
print()
print("Results:")
for item in opened:
print('Database ' + item + " failover complete")
for item in alreadyopen:
print('Database ' + item + ' was opened by external user')