-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrestorecommon.py
247 lines (229 loc) · 11.7 KB
/
restorecommon.py
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
import os, sys, pytz
from tempfile import mkstemp, TemporaryFile, mkdtemp
from datetime import datetime, timedelta
from backupcommon import BackupLogger, info, debug, error, exception, Configuration, BackupTemplate, create_snapshot_class, scriptpath
from oraexec import OracleExec
from ConfigParser import SafeConfigParser
from subprocess import check_call
from tzlocal import get_localzone
class RestoreDB(object):
_restoretemplate = None
_sourcesnapid = 'unknown'
_mountdest = None
_restoredest = None
_exec = None
_snap = None
_dbparams = {}
_configname = None
_validatecorruption = False
verifyseconds = -1
sourcesnapid = ""
_successful_clone = False
_successful_mount = False
_mountpaths = []
targettime = None
def __init__(self, configname):
self._restoretemplate = BackupTemplate('restoretemplate.cfg')
self._configname = configname
self._snap = create_snapshot_class(configname)
def set_mount_path(self, mountdest):
if mountdest is None or not os.path.exists(mountdest) or not os.path.isdir(mountdest):
raise Exception('restore', "Mount directory %s not found or is not a proper directory" % mountdest)
self._mountdest = mountdest
self._root_mountdest = mountdest
def set_restore_path(self, restoredest):
if restoredest is None or not os.path.exists(restoredest) or not os.path.isdir(restoredest):
raise Exception('restore', "Restore directory %s not found or is not a proper directory" % restoredest)
self._restoredest = restoredest
def set_restore_target_time(self, targettime):
if targettime.tzinfo is None:
raise Exception('restore', 'set_restore_target_time expects a datetime object with time zone information')
self.targettime = targettime
self.sourcesnapid = self._snap.search_recovery_snapid(targettime.astimezone(pytz.utc))
if self.sourcesnapid is None:
raise Exception('restore', 'Suitable snapshot not found. If requested time is after the latest backup was taken, please use zsnapper.py to create a new snapshot first.')
# Helpers for executing Oracle commands
def _exec_rman(self, commands):
finalscript = "%s\n%s\n%s" % (self._restoretemplate.get('rmanheader'), commands, self._restoretemplate.get('rmanfooter'))
self._exec.rman(finalscript)
def _exec_sqlplus(self, commands, headers=True, returnoutput=False):
if headers:
finalscript = "%s\n%s\n%s" % (self._restoretemplate.get('sqlplusheader'), commands, self._restoretemplate.get('sqlplusfooter'))
else:
finalscript = commands
return self._exec.sqlplus(finalscript, silent=returnoutput)
# Restore actions
def _createinitora(self):
filename = self._initfile
with open(filename, 'w') as f:
contents = self._restoretemplate.get('autoinitora')
if 'cdb' in Configuration.substitutions and Configuration.substitutions['cdb'].upper() == 'TRUE':
contents+= self._restoretemplate.get('cdbinitora')
debug("ACTION: Generated init file %s\n%s" % (filename, contents))
f.write(contents)
def clone(self, autorestore=True):
if autorestore:
self.sourcesnapid = self._snap.autoclone()
else:
self.clonename = "restore_%s_%s" % (self._configname, datetime.now().strftime("%Y%m%d_%H%M%S"))
self._snap.clone(self.sourcesnapid, self.clonename)
self.mountstring = self._snap.mountstring(self.clonename)
self._successful_clone = True
def _mount(self):
check_call(['mount', self._root_mountdest])
self._successful_mount = True
def _unmount(self):
check_call(['umount', self._root_mountdest])
def _set_parameters(self):
# Detect if mountpath is actually a namespace containing multiple volumes
orig_mount_dest = self._mountdest
if not os.path.isfile(os.path.join(orig_mount_dest, 'autorestore.cfg')):
location_correct = False
for item in os.listdir(orig_mount_dest):
item_full_path = os.path.join(orig_mount_dest, item)
if os.path.isdir(item_full_path):
self._mountpaths.append(item_full_path)
if os.path.isfile(os.path.join(item_full_path, 'autorestore.cfg')):
location_correct = True
self._mountdest = item_full_path
if not location_correct:
raise Exception('restore', 'Mount path is not correct, autorestore.cfg was not found')
else:
self._mountpaths.append(self._mountdest)
debug("All datafile mountpaths: %s" % self._mountpaths)
debug("Main datafile mountpath: %s" % self._mountdest)
debug("Location for temporary init.ora and other files: %s" % self._restoredest)
#
dbconfig = SafeConfigParser()
dbconfig.read(os.path.join(self._mountdest, 'autorestore.cfg'))
self._dbparams['dbname'] = dbconfig.get('dbparams','db_name')
if self.targettime is None:
self._dbparams['restoretarget'] = datetime.strptime(dbconfig.get('dbparams','lasttime'), '%Y-%m-%d %H:%M:%S')
else:
self._dbparams['restoretarget'] = self.targettime.astimezone(get_localzone())
self._dbparams['bctfile'] = dbconfig.get('dbparams','bctfile')
catalogstatements = []
for item in self._mountpaths:
catalogstatements.append("catalog start with '%s/archivelog/' noprompt;" % item)
catalogstatements.append("catalog start with '%s/data_' noprompt;" % item)
Configuration.substitutions.update({
'db_name': self._dbparams['dbname'],
'db_compatible': dbconfig.get('dbparams','compatible'),
'db_files': dbconfig.get('dbparams','db_files'),
'db_undotbs': dbconfig.get('dbparams','undo_tablespace'),
'db_block_size': dbconfig.get('dbparams','db_block_size'),
# 'lastscn': dbconfig.get('dbparams','lastscn'),
'lasttime': self._dbparams['restoretarget'].strftime('%Y-%m-%d %H:%M:%S'),
'dbid': Configuration.get('dbid', self._configname),
'instancenumber': Configuration.get('autorestoreinstancenumber', self._configname),
'thread': Configuration.get('autorestorethread', self._configname),
'pga_size': Configuration.get('pga_size', 'autorestore'),
'sga_size': Configuration.get('sga_size', 'autorestore'),
'backupfinishedtime': dbconfig.get('dbparams','backup-finished'),
'bctfile': self._dbparams['bctfile'],
'autorestoredestination': self._restoredest,
'mountdestination': self._mountdest,
'catalogstatements': "\n".join(catalogstatements)
})
try:
Configuration.substitutions.update({'cdb': dbconfig.get('dbparams','enable_pluggable_database')})
except:
Configuration.substitutions.update({'cdb': 'FALSE'})
self._initfile = os.path.join(self._restoredest, 'init.ora')
Configuration.substitutions.update({
'initora': self._initfile,
})
def _run_restore(self):
debug('ACTION: startup nomount')
self._exec_sqlplus(self._restoretemplate.get('startupnomount'))
debug('ACTION: mount database and catalog files')
self._exec_rman(self._restoretemplate.get('rmanmount'))
self._exec_sqlplus(self._restoretemplate.get('clearlogs'))
self._exec_rman(self._restoretemplate.get('rmancatalog'))
if self._dbparams['bctfile']:
debug('ACTION: disable block change tracking')
self._exec_sqlplus(self._restoretemplate.get('disablebct'))
debug('ACTION: create missing datafiles')
output = self._exec_sqlplus(self._restoretemplate.get('switchdatafiles'), returnoutput=True)
switchdfscript = ""
for line in output.splitlines():
if line.startswith('RENAMEDF-'):
switchdfscript+= "%s\n" % line.strip()[9:]
debug('ACTION: switch and recover')
self._exec_rman("%s\n%s" % (switchdfscript, self._restoretemplate.get('recoverdatafiles')))
# Orchestrator
def pit_restore(self, mountpath, sid):
self.set_mount_path(mountpath)
self.set_restore_path(mkdtemp(prefix="restore", dir=self._mountdest))
#
self._restoresid = sid
self._set_parameters()
self._createinitora()
self._exec = OracleExec(oraclehome=Configuration.get('oraclehome', 'generic'),
tnspath=os.path.join(scriptpath(), Configuration.get('tnsadmin', 'generic')),
sid=sid)
self._run_restore()
def run(self):
self.starttime = datetime.now()
info("Starting to restore")
#
success = False
self.clone()
try:
self._mount()
except:
self.cleanup()
raise Exception('restore', 'Mount failed')
self._set_parameters()
self._createinitora()
self._exec = OracleExec(oraclehome=Configuration.get('oraclehome', 'generic'),
tnspath=os.path.join(scriptpath(), Configuration.get('tnsadmin', 'generic')),
sid=self._dbparams['dbname'])
#
self._run_restore()
def verify(self, tolerancechecking=True):
debug('ACTION: opening database to verify the result')
if tolerancechecking:
maxtolerance = timedelta(minutes=int(Configuration.get('autorestoremaxtoleranceminutes','autorestore')))
Configuration.substitutions.update({
'customverifydate': Configuration.get('customverifydate', self._configname),
})
output = self._exec_sqlplus(self._restoretemplate.get('openandverify'), returnoutput=True)
for line in output.splitlines():
if line.startswith('CUSTOM VERIFICATION TIME:'):
self.verifytime = datetime.strptime(line.split(':', 1)[1].strip(), '%Y-%m-%d %H:%M:%S')
if self.verifytime is None:
raise Exception('restore', 'Reading verification time failed.')
self.verifydiff = self._dbparams['restoretarget'].replace(tzinfo=None) - self.verifytime
self.verifyseconds = int(self.verifydiff.seconds + self.verifydiff.days * 24 * 3600)
debug("Expected time: %s" % self._dbparams['restoretarget'])
debug("Verified time: %s" % self.verifytime)
debug("VERIFY: Time difference %s" % self.verifydiff)
if tolerancechecking and self.verifydiff > maxtolerance:
raise Exception('restore', "Verification time difference %s is larger than allowed tolerance %s" % (verifydiff, maxtolerance))
def blockcheck(self):
info("ACTION: Validating database for corruptions")
# The following command will introduce some corruption to test database validation
# check_call(['dd','if=/dev/urandom','of=/nfs/autorestore/mnt/data_D-ORCL_I-1373437895_TS-SOE_FNO-5_0sqov4pv','bs=8192','count=10','seek=200','conv=notrunc' ])
try:
self._exec_rman(self._restoretemplate.get('validateblocks'))
self._validatecorruption = True
finally:
self._exec_sqlplus(self._restoretemplate.get('showcorruptblocks'))
def cleanup(self):
try:
debug('ACTION: In case instance is still running, aborting it')
self._exec_sqlplus(self._restoretemplate.get('shutdownabort'))
except:
pass
if self._successful_mount:
try:
self._unmount()
except:
exception("Error unmounting")
if self._successful_clone:
try:
self._snap.dropautoclone()
except:
exception("Error dropping clone")
self.endtime = datetime.now()