-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbackup.py
executable file
·294 lines (266 loc) · 11.5 KB
/
backup.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
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
#!/usr/bin/python2
import os, ConfigParser, sys
from datetime import datetime, timedelta
from subprocess import Popen, PIPE, STDOUT
from tempfile import TemporaryFile
from backupcommon import BackupLock, BackupLogger, info, debug, error, exception, scriptpath, Configuration, BackupTemplate, create_snapshot_class
from oraexec import OracleExec
# Check command line arguments
uioptions = ['config','setschedule','backupimagecopy','report','validatebackup','generaterestore','imagecopywithsnap','missingarchlog']
def printhelp():
print "Usage: backup.py <config> <%s>" % '|'.join(uioptions)
sys.exit(2)
if len(sys.argv) != 3:
printhelp()
else:
scriptaction = sys.argv[2].lower()
if not scriptaction in uioptions:
printhelp()
# Check environment
if (scriptaction == 'setschedule') and (os.getenv('OSPASSWORD') is None):
print "Environment variable OSPASSWORD must be set."
sys.exit(2)
# Directory where the executable script is located
scriptpath = scriptpath()
# Read configuration
configsection = sys.argv[1]
Configuration.init(configsection, additionaldefaults={'primarytns': configsection})
# Database specific configuration
if Configuration.get('backupdestshared', 'generic').upper() == 'TRUE':
backupdest = os.path.join(Configuration.get('backupdest', 'generic'), configsection)
else:
backupdest = Configuration.get('backupdest', 'generic')
archdir = os.path.join(backupdest, 'archivelog')
hasdataguard = Configuration.get('hasdataguard').upper() == 'TRUE'
dosnapshot = Configuration.get('dosnapshot').upper() == 'TRUE'
gimanaged = Configuration.get('gimanaged').upper() == 'TRUE'
registercatalog = Configuration.get('registercatalog').upper() == 'TRUE'
# Log file for this session
logdir = os.path.join(backupdest, 'backup_logs')
logfile = os.path.join(logdir, "%s_%s_%s.log" % (configsection, datetime.now().strftime('%Y%m%dT%H%M%S'), scriptaction) )
print "Log file for this session: %s" % logfile
BackupLogger.init(logfile, configsection)
BackupLogger.clean()
# Oracle environment variables
oraexec = OracleExec(Configuration.get('oraclehome', 'generic'), os.path.join(scriptpath, Configuration.get('tnsadmin', 'generic')))
# Prepare a dictionary of all possible template substitutions
Configuration.substitutions.update({ 'recoverywindow': Configuration.get('recoverywindow'),
'parallel': Configuration.get('parallel'),
'backupdest': backupdest,
'archdir': archdir,
'catalogconnect': Configuration.get('catalog', 'rman'),
'configname': configsection,
'osuser': Configuration.get('osuser', 'generic'),
'ospassword': os.getenv('OSPASSWORD'),
'scriptpath': scriptpath,
'schedulebackup': Configuration.get('schedulebackup'),
'schedulearchlog': Configuration.get('schedulearchlog'),
'dbid': int(Configuration.get('dbid')),
'oraclehome': oraexec.oraclehome,
'tnspath': oraexec.tnspath,
'logfile': logfile,
'backupjobenabled': 'true' if Configuration.get('backupjobenabled').upper() == 'TRUE' else 'false',
'sectionsize': "section size %s" % Configuration.get('sectionsize', 'rman') if Configuration.get('sectionsize', 'rman') else ''
})
# Read RMAN templates
rmantemplateconfig = BackupTemplate('rmantemplate.cfg')
# Initialize snapshot class
snap = create_snapshot_class(configsection)
# Execute RMAN with script as input
def exec_rman(rmanscript):
# Modify rman script with common headers
finalscript = rmantemplateconfig.get('header')
if registercatalog:
finalscript+= "\n%s" % rmantemplateconfig.get('headercatalog')
finalscript+= "\n%s" % rmanscript
finalscript+= "\n%s" % rmantemplateconfig.get('footer')
# print finalscript
oraexec.rman(finalscript)
# Execute sqlplus with a given script
def exec_sqlplus(sqlplusscript, silent=False, header=True, primary=False):
global configsection
Configuration.substitutions['sqlplusconnection'] = '/@%s as sysdba' % (Configuration.get('primarytns') if primary else configsection)
script = ""
if header:
script+= "%s\n" % rmantemplateconfig.get('sqlplusheader')
script+= "%s\n" % sqlplusscript
if header:
script+= "%s\n" % rmantemplateconfig.get('sqlplusfooter')
return oraexec.sqlplus(script, silent)
##############
# User actions
##############
def configure():
rmanscript = ''
# Create directory for archive logs
if not os.path.exists(archdir):
os.makedirs(archdir)
# Register database in catalog if needed
if registercatalog:
alreadyregistered = False
info("Checking from RMAN catalog if database is already registered")
output = exec_sqlplus(rmantemplateconfig.get('isdbregisteredincatalog'), silent=True, header=False)
for line in output.splitlines():
if line.startswith('DATABASE IS REGISTERED IN RC'):
alreadyregistered = True
if not alreadyregistered:
rmanscript+= rmantemplateconfig.get('registerdatabase')
# Configure archivelog deletion policy
if hasdataguard:
rmanscript+= "\n%s" % rmantemplateconfig.get('configdelaldg')
else:
rmanscript+= "\n%s" % rmantemplateconfig.get('configdelalnodg')
# configures rman default settings
rmanscript+= "\n%s" % rmantemplateconfig.get('config')
info("Running RMAN configuration")
exec_rman(rmanscript)
info("Running additional configuration from SQL*Plus")
exec_sqlplus(rmantemplateconfig.get('configfromsqlplus'))
def backup(level):
entry = 'backup'
if level == '1c':
entry+= 'cumulative'
elif level == '1d':
entry+= 'diff'
elif level == 'arch':
entry+= 'archivelog'
elif level == 'imagecopy':
entry+= 'imagecopy'
else:
entry+= 'full'
# Execute backup commands inside run block
rmanscript = "run {\n%s\n%s\n}\n" % (rmantemplateconfig.get(entry), rmantemplateconfig.get('backupfooter'))
exec_rman(rmanscript)
def backup_missing_archlog():
output = exec_sqlplus(rmantemplateconfig.get('archivelogmissing'), silent=True)
archlogscript = ""
for line in output.splitlines():
if line.startswith('BACKUP force as copy'):
archlogscript+= "%s\n" % line.strip()
if archlogscript:
info("- Copying missing archivelogs")
exec_rman("run {\n%s\n%s\n}" % (rmantemplateconfig.get('allocatearchlogchannel'), archlogscript))
def delete_expired_datafilecopy():
output = exec_sqlplus(rmantemplateconfig.get('deletedatafilecopy'), silent=True)
rmanscript = ""
for line in output.splitlines():
if line.startswith('DELETECOPY: '):
rmanscript+= "%s\n" % line.strip()[12:]
if rmanscript:
info("- Deleting expired datafile copies")
exec_rman(rmanscript)
def imagecopywithsnap():
starttime = datetime.now()
restoreparamfile = os.path.join(backupdest, 'autorestore.cfg')
#
info("Check if there are missing archivelogs")
backup_missing_archlog()
#
info("Switch current log")
output = exec_sqlplus(rmantemplateconfig.get('archivecurrentlogs'), silent=True, primary=True)
if os.path.isfile(restoreparamfile):
with open(restoreparamfile, 'a') as f:
for line in output.splitlines():
if line.startswith('CURRENT DATABASE SCN:'):
f.write("lastscn: %s\n" % line.strip()[22:])
elif line.startswith('CURRENT DATABASE TIME:'):
f.write("lasttime: %s\n" % line.strip()[23:])
elif line.startswith('BCT FILE:'):
f.write("bctfile: %s\n" % line.strip()[10:])
#
if dosnapshot:
info("Snap the current backup area")
snapid = snap.snap()
debug("Created snapshot: %s" % snapid)
#
info("Checking for expired datafile copies")
delete_expired_datafilecopy()
#
info("Refresh imagecopy")
backup('imagecopy')
exec_sqlplus(rmantemplateconfig.get('archivecurrentlogs'), primary=True)
#
if dosnapshot:
info("Clean expired snapshots")
cleaningresult = snap.clean()
for r in cleaningresult:
debug(r['infostring'])
#
info("Dump additional information about the environment to the log file")
if gimanaged:
p = Popen([os.path.join(scriptpath, 'dbinfo.py'), configsection], stdout=PIPE, stderr=None, stdin=None)
output,outerr = p.communicate()
debug(output)
# Write ORACLE_HOME patch information to log file
p = Popen([os.path.join(Configuration.get('oraclehome', 'generic'), 'OPatch', 'opatch'), 'lsinventory'], stdout=PIPE, stderr=None, stdin=None)
output,outerr = p.communicate()
debug(output)
#
info("Write database parameters for autorestore")
with open(restoreparamfile, 'w') as f:
f.write("[dbparams]\n")
output = exec_sqlplus(rmantemplateconfig.get('autorestoreparameters'), silent=True)
for line in output.splitlines():
if line.startswith('dbconfig-'):
f.write("%s\n" % line[9:])
#
endtime = datetime.now()
info("------------ TOTAL ------------")
info("Total execution time: %s" % (endtime-starttime))
info("Execution started: %s" % starttime)
info("Execution finished: %s" % endtime)
def exec_template(template_name):
rmanscript = ''
if registercatalog:
rmanscript+= "%s\n" % rmantemplateconfig.get('resynccatalog')
rmanscript+= rmantemplateconfig.get(template_name)
exec_rman(rmanscript)
def generate_restore():
print "\n============="
info(rmantemplateconfig.get('headerrestore'))
if registercatalog:
info(rmantemplateconfig.get('headercatalog'))
info(rmantemplateconfig.get('fullrestore'))
info(rmantemplateconfig.get('restorefooter'))
def setschedule():
# Detect if we are running from a CDB and get the common user prefix
output = exec_sqlplus(rmantemplateconfig.get('cdbdetect'), silent=True)
commonprefix = ""
for line in output.splitlines():
if line.startswith('CDB-DETECT:') and line.strip() <> 'CDB-DETECT: NO':
commonprefix = line.strip()[12:]
Configuration.substitutions.update({'scheduleuserprefix': commonprefix})
#
script = "%s\n" % rmantemplateconfig.get('createuser')
script+= "%s\n" % rmantemplateconfig.get('dropschedule')
script+= "%s\n" % rmantemplateconfig.get('createschedule')
exec_sqlplus(script)
################################################
### Main section
################################################
info("Configuration file: %s" % Configuration.configfilename)
lock = BackupLock(lockdir=backupdest, maxlockwait=int(Configuration.get('maxlockwait', 'generic')))
try:
# User interface action execution
if scriptaction == 'config':
configure()
elif scriptaction == 'generaterestore':
generate_restore()
elif scriptaction == 'imagecopywithsnap':
imagecopywithsnap()
elif scriptaction == 'setschedule':
setschedule()
elif scriptaction == 'missingarchlog':
backup_missing_archlog()
else:
exec_template(scriptaction)
finally:
lock.release()
if (os.getenv('BACKUP_LOG_TO_SCREEN')) and (os.environ['BACKUP_LOG_TO_SCREEN'] == 'TRUE'):
BackupLogger.close(True)
print "\n\n======================\nBACKUP LOG FILE OUTPUT\n======================\n\n"
if os.path.isfile(logfile):
with open(logfile, 'r') as tmplogf:
print tmplogf.read()
else:
print "Log file not found"