-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateRMANScript.py
executable file
·130 lines (109 loc) · 4.58 KB
/
createRMANScript.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
#!/usr/bin/python
#
import os
import sys
import errno
import json
import getopt
import oracle_utils
def print_usage():
print("Usage: " + sys.argv[0] + " -s ORACLE_SID -t backup_tag -d /backup/dir -a | -b")
sys.exit(1)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def main():
orasid = None
bkuptag = None
bkupdir = None
arch_mode = False
bkup_mode = True
try:
options, remainder = getopt.getopt(sys.argv[1:], "habs:t:d:", ["archive", "backup", "sid=", "tag=", "dir="])
except getopt.GetoptError as e:
print("Invalid arguments: %s" % str(e))
print_usage()
for opt, arg in options:
if opt in ('-h'):
print_usage()
elif opt in ('-s', '--sid'):
orasid = arg
elif opt in ('-t', '--tag'):
bkuptag = arg
elif opt in ('-d', '--dir'):
bkupdir = arg
elif opt in ('-a', '--archive'):
arch_mode = True
bkup_mode = False
elif opt in ('-b', '--backup'):
bkup_mode = True
arch_mode = False
if not orasid or not bkuptag or not bkupdir:
print_usage()
os.environ['ORACLE_SID'] = orasid
if arch_mode:
if not os.path.isdir(bkupdir + "/archivelog"):
try:
mkdir_p(bkupdir + "/archivelog")
except OSError as e:
print("Error: %s" % e)
sys.exit(1)
arch_script = []
sql_session = oracle_utils.sqlplus()
sql_session.start()
arch_currnt = sql_session.run_query("select thread#, sequence# from v$log where status = 'CURRENT' or status = 'CLEARING_CURRENT' union select thread#, max(sequence#) from v$log where status = 'INACTIVE' group by thread# order by thread#, sequence# ;")
sql_session.end()
arch_script.append("run")
arch_script.append("{")
arch_script.append("ALLOCATE CHANNEL CH01 DEVICE TYPE DISK FORMAT '" + bkupdir + "/archivelog/%U' ;")
arch_script.append("SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';")
for x in range(len(arch_currnt['results'])):
arch_script.append("BACKUP AS COPY ARCHIVELOG SEQUENCE " + arch_currnt['results'][x]['sequence#'] + " THREAD " + arch_currnt['results'][x]['thread#'] + ";")
arch_script.append("CHANGE COPY OF ARCHIVELOG LIKE '" + bkupdir + "/archivelog/%' UNCATALOG ;")
arch_script.append("}")
for x in range(len(arch_script)):
print(arch_script[x])
if bkup_mode:
bkup_script = []
sql_session = oracle_utils.sqlplus()
sql_session.start()
dbinfo = sql_session.run_query("select * from v$database;")
sql_session.end()
dbname = dbinfo['results'][0]['db_unique_name']
if not os.path.isdir(bkupdir + "/" + dbname):
try:
mkdir_p(bkupdir + "/" + dbname)
except OSError as e:
print("Error: %s" % e)
sys.exit(1)
bkup_script.append("run")
bkup_script.append("{")
bkup_script.append("set nocfau;")
bkup_script.append("ALLOCATE CHANNEL CH01 DEVICE TYPE DISK FORMAT '" + bkupdir + "/" + dbname + "/%U';")
bkup_script.append("CROSSCHECK COPY TAG '" + bkuptag + "';")
bkup_script.append("CROSSCHECK BACKUP TAG '" + bkuptag + "';")
bkup_script.append("CATALOG start with '" + bkupdir + "/" + dbname + "' NOPROMPT ;")
bkup_script.append("CATALOG start with '" + bkupdir + "/archivelog' NOPROMPT ;")
bkup_script.append("BACKUP CHANNEL CH01 INCREMENTAL LEVEL 1 FOR RECOVER OF COPY WITH TAG '" + bkuptag + "' DATABASE ;")
bkup_script.append("RECOVER COPY OF DATABASE WITH TAG '" + bkuptag + "';")
bkup_script.append("BACKUP AS COPY CURRENT CONTROLFILE TAG '" + bkuptag + "' FORMAT '" + bkupdir + "/" + dbname + "/control01.ctl' REUSE ;")
bkup_script.append("DELETE NOPROMPT BACKUPSET TAG '" + bkuptag + "';")
bkup_script.append("CHANGE COPY OF DATABASE TAG '" + bkuptag + "' UNCATALOG ;")
bkup_script.append("CHANGE COPY OF CONTROLFILE TAG '" + bkuptag + "' UNCATALOG ;")
bkup_script.append("CHANGE COPY OF ARCHIVELOG LIKE '" + bkupdir + "/archivelog/%' UNCATALOG ;")
bkup_script.append("}")
for x in range(len(bkup_script)):
print(bkup_script[x])
if __name__ == '__main__':
try:
main()
except SystemExit as e:
if e.code == 0:
os._exit(0)
else:
os._exit(e.code)