-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractBackup.py
214 lines (165 loc) · 5.29 KB
/
extractBackup.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
#Written for Python 3.3
import argparse
import os
import re
import shutil
import sys
def inspectLine(line):
if args.inspect or args.verbose:
print(line)
def getDirectory():
return args.sql_dump.replace('.sql', '') + '-extract'
def getTableDirectory():
return os.path.join(getDirectory(), currentDb + '-tables')
def ensureFolder():
directory = getDirectory()
if not os.path.exists(directory):
os.makedirs(directory)
def removeEmptyExtra(extraFile):
file = open(extraFile, "r", encoding="ISO-8859-4");
contents = file.read();
file.close()
contents = re.sub(r'^--.*$', r'', contents, flags=re.MULTILINE)
contents = re.sub(r'^\s*USE\s*`[^`]+`;\s*$', r'', contents, flags=re.MULTILINE)
contents = re.sub(r'\s*', r'', contents, flags=re.MULTILINE)
if contents == '':
if args.verbose:
print("Removing empty file: '", extraFile, "'... ")
os.remove(extraFile)
def getProcedureFile():
return os.path.join(getDirectory(), currentDb + '.procedures.sql')
def writeOneLine(line, file, mode):
if args.inspect:
return
output = open(file, mode)
output.write(line)
output.close()
def writeLine(line):
if args.inspect:
return
ensureFolder();
directory = getDirectory()
#write to the database or misc files
file = os.path.join(directory, currentDb)
if currentDb == '':
file = os.path.join(directory, 'header')
if headerPassed:
file = os.path.join(directory, 'footer')
file += '.sql'
mode = 'a'
writeOneLine(line, file, mode)
if not args.extended:
return
if currentDb == '':
return
if not inProcedure and currentTable == '':
return
tableDir = getTableDirectory()
if not os.path.exists(tableDir):
os.makedirs(tableDir)
file = os.path.join(tableDir, currentDb + '.table.' + currentTable + '.sql')
if inProcedure:
file = getProcedureFile()
writeOneLine(line, file, mode)
################
# End functions.
################
##################################
# Define Arguments
##################################
parser = argparse.ArgumentParser(description="""
Extract data from a SQL dump file
""")
parser.add_argument("-i", "--inspect",
help="Inspects the backup file, printing a list of databases and tables and exits",
action="store_true")
parser.add_argument("-x", "--extended",
help="Extended extract. Includes individual files for tables and procedures",
action="store_true")
parser.add_argument("-v", "--verbose",
help="Shows the output from inspect, but actually does the extraction instead of exiting.",
action="store_true")
parser.add_argument("-r", "--remove",
help="Removes the files created by the extraction for a given dump",
action="store_true")
parser.add_argument('sql_dump', help="The SQL Dump file you would like to extract or inspect")
args = parser.parse_args()
##################################
# End of arguments
##################################
#make sure file doesn't contain a /
if "/" in args.sql_dump:
print("""
`/` Detected in file. Please use a local file.
Aborting.
""")
sys.exit(1)
#remove previous extract
if not args.inspect:
if os.path.exists(getDirectory()):
shutil.rmtree(getDirectory())
if args.remove:
print ("Directory " + getDirectory() + " removed.")
sys.exit(0)
sql_dump = open(args.sql_dump, "r", encoding="ISO-8859-4")
database_re = re.compile(r"Database:\s*`([^`]+)`")
procedure_re = re.compile(r"CREATE.+PROCEDURE\s*`([^`]+)`")
procedures_start_re = re.compile(r"Dumping routines")
table_re = re.compile(r"(?:DROP TABLE|CREATE\s+TABLE|Table\s+structure\s+for).+`([^`]+)`")
end_re = re.compile(r"^\/\*!\d+\sSET\s+(?:SQL|TIME)")
count = 0
procCount = 0
tableCount = 0
dbCount = 0
currentDb = ''
currentTable = ''
inProcedure = False
headerPassed = False
usedDbs = {}
usedTables = {}
for line in sql_dump:
count += 1
isDb = database_re.search(line)
isProc = procedure_re.search(line)
isTable = table_re.search(line)
isEnd = end_re.search(line)
isProcStart = procedures_start_re.search(line)
if isDb:
currentDb = isDb.group(1)
if not currentDb in usedDbs:
tableCount = 0
procCount = 0
usedDbs[currentDb] = 1
inProcedure = False
headerPassed = True
inspectLine("\nStart Database: \n" + currentDb)
dbCount += 1
if isEnd:
if dbCount > 0:
currentDb = ''
if isProcStart:
inProcedure = True
if isProc:
if procCount == 0:
inspectLine("\n" + " " * 4 + "Procedures: ")
inspectLine(" " * 8 + isProc.group(1));
procCount += 1
if isTable:
if tableCount == 0:
inspectLine("\n" + " " + "Tables: ")
currentTable = isTable.group(1)
inProcedure = False
if not currentTable in usedTables:
inspectLine(" " * 4 + currentTable);
tableCount += 1
usedTables[currentTable] = 1
#print(count)
writeLine(line)
sql_dump.close();
#Cleanup extras files
if not args.inspect:
path = getDirectory()
listing = os.listdir(path)
for infile in listing:
if ".procedures.sql" in infile:
removeEmptyExtra(os.path.join(path, infile))