-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrinst.py
326 lines (276 loc) · 10.5 KB
/
crinst.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
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
import zipfile
import os.path
import os
import lxml.etree as ET
import pyodbc
import time
import psycopg2
import sys
# ARCHIVE UNPACKING MODULE
def inputpath():
flag = False
while flag == False:
print('Enter path to the zip:')
zippath = input()
print(zippath)
flag = os.path.isfile(zippath)
print(flag)
return (zippath)
def inputexportpath():
flag = False
while flag == False:
print('Input export path')
export_path = input()
if os.path.exists(export_path):
print(export_path)
return (export_path)
else:
print('directory not exist, make it?(y/n)')
answ = input()
if (answ == 'y'):
try:
os.makedirs(export_path)
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
print("Создать директорию %s не удалось" % export_path)
else:
print("Успешно создана директория %s , подождите пожалуйста. Идет распаковка..." % export_path)
return (export_path)
else:
print('Input proper path')
def export(zippath, exp_path):
try:
fantasy_zip = zipfile.ZipFile(zippath)
fantasy_zip.extractall(exp_path)
fantasy_zip.close()
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
else:
print("sucessfull unpackage to %s" % exp_path)
def read_connection(exp_path):
string_path = exp_path + '\\' + 'ConnectionStrings.config'
f = open(string_path, 'r+')
for line in f:
print(line)
def input_redis():
print('input redis port between 0 and 85')
flag = False
while flag == False:
port = input()
if port.isdigit():
port = int(port)
if (port > 0 and port < 85):
flag = True
return port
else:
print("wrong port format, must be between 0 and 85")
else:
print("wrong format,not a number")
def input_subd():
print('select database postgres[1] or mssql [2]')
# here we make default names for database server, change it for your company
defserv1 = "###"
defserv2 = "###"
defserv3 = "###"
flag_subd = False
while flag_subd == False:
db_name = input()
if int(db_name) == 1:
print("Postgres")
print(
"write postgres server adress, enter ip adress or server name. Also you can write 1 for the first default server {}".format(
defserv3))
servadr = input()
if servadr == '1':
servadr = defserv3
print("Ok it will be", servadr)
return ("Postgres", servadr)
flag_subd = True
if int(db_name) == 2:
print(
"write server adress, enter ip adress or server name. Also you can write 1 for the first default server {} or 2 for the second default server {}".format(
defserv1, defserv2))
servadr = input()
if servadr == '1':
servadr = defserv1
if servadr == '2':
servadr = defserv2
print("Ok it will be", servadr)
return ("MSSQL", servadr)
flag_subd = True
else:
print(" choose 1 or 2")
def input_db(bd):
bd = bd
print('write dbname')
db_name = input()
print("database is:", db_name)
print(subd)
if bd[0] == 'MSSQL':
print('if you want to make Default connection for MSSQL database type "yes" or "y"')
defcon = input()
if (defcon == "yes") or (defcon == "y"):
db = {'name': db_name, 'user': '###', 'password': '###'}
return (db)
print('input db user')
db_user = input()
print("user is:", db_user)
print('input db password')
db_psswd = input()
print('******')
db = {'name': db_name, 'user': db_user, 'password': db_psswd}
return (db)
def read_connection(db, subd, redis_port, exp_path):
string_path = exp_path + '\\' + 'ConnectionStrings.config'
with open(string_path, encoding="utf-8") as f:
tree = ET.parse(f)
root = tree.getroot()
for elem in root.getiterator():
server = subd[1]
if elem.attrib.get("name") == 'db':
try:
if subd[0] == 'MSSQL':
sb_conn = "Data Source={}; Initial Catalog={}; Persist Security Info=True; MultipleActiveResultSets=True; user={}; password={}; Pooling = true; Max Pool Size = 100; Async = true; Connection Timeout=500".format(
server, db['name'], db['user'], db['password'])
else:
sb_conn = "Server={};Port=5432;Database={};User ID={};password={};Timeout=500; CommandTimeout=400;MaxPoolSize=1024;".format(
server, db['name'], db['user'], db['password'])
elem.attrib['connectionString'] = str(sb_conn)
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
elif elem.attrib.get("name") == 'redis':
try:
new_redis = "host=localhost;db={};port=6379;maxReadPoolSize=100;maxWritePoolSize=500".format(
redis_port)
elem.attrib['connectionString'] = new_redis
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
try:
tree.write(string_path, xml_declaration=True,
method='xml', encoding="utf-8")
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
def dumpcheck(exp_path):
try:
pathdb = os.walk(exp_path + '\\db\\')
for elem in pathdb:
if len(elem[2]) == 1:
pathdb = exp_path + '\\db\\' + str(elem[2][0])
print(pathdb)
return (True, pathdb)
else:
print('not one file in db directory')
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
def open_dump(db, subd, pathdb):
pathdb = pathdb[1]
subd = subd
server = subd[1]
database = db['name']
username = '###
password = '123654-qQ'
try:
cnxn = pyodbc.connect(('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +
server + ';UID=' + username + ';PWD=' + password), autocommit=True)
cursor = cnxn.cursor()
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
try:
sql = "CREATE DATABASE {}".format(database)
print(type(database))
print('database {} will be created now'.format(database))
cursor.execute(sql)
print("created")
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
try:
sql = "USE master; RESTORE DATABASE {} FROM DISK='{}' WITH RECOVERY, REPLACE ; ".format(
database, pathdb)
cursor.execute(sql)
print(sql)
time.sleep(5)
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
cnxn.close()
# postgre database restoring module
def conndb(subd, bd):
con = psycopg2.connect(
database="postgres",
user="{}".format(bd['user']),
password="{}".format(bd['password']),
host="{}".format(subd[1]),
port="5432"
)
print(con)
con.autocommit = True
return (con.cursor())
def checkdb(con, testname):
print("Database opened successfully")
data = con.execute("select datname from pg_database;")
rows = con.fetchall()
listx = list()
for elem in rows:
listx.append((elem[0]))
print(listx)
if testname in listx:
print('Database name exist in the list,already')
return (False)
print('Name not in the list')
return (True)
def createdb(exist, cur, testname):
if exist == True:
sql = "CREATE DATABASE {} WITH OWNER postgres TEMPLATE template0;".format(testname)
data = cur.execute(sql)
if checkdb(cur, testname) == False:
print("db $s created sucessfull".format(testname))
return True
else:
print("db not created, check it mannualy")
return False
def dumpcheck(exp_path):
pathdb = os.walk(exp_path + '\\db\\')
for elem in pathdb:
if len(elem[2]) == 1:
pathdb = exp_path + '\\db\\' + str(elem[2][0])
print(pathdb)
return (True, pathdb)
else:
print('not one file in db directory')
def dump_open(pathdb, cur, psql_path):
if pathdb[0] == True:
try:
print("Database {} dump will be restoring into server, type password and wait".format(
testname))
os.system(
psql_path + " -h 116.202.197.### -p 5432 -U postgres -d {} {} ".format(testname, pathdb[1]))
except Exception as e:
print('Got Exception', type(e), e.args, type(e.args))
finally:
print('Success')
def db_create_postgre(exp_path, psql_path, testname, subd, bd):
testname = bd['name']
con = conndb(subd, bd)
# checking thant dbname is free
exist = checkdb(con, testname)
# creating new empty db
createdb(exist, con, testname)
# recieve path for db dump in creatio directory
pathdb = dumpcheck(exp_path)
dump_open(pathdb, con, psql_path)
if __name__ == '__main__':
psql_path = "C:\\Progra~1\\PostgreSQL\\12\\bin\\pg_restore.exe"
zippath = inputpath()
exp_path = inputexportpath()
export(zippath, exp_path)
subd = input_subd()
bd = input_db(subd)
testname = bd['name']
redis_port = input_redis()
read_connection(bd, subd, redis_port, exp_path)
print(subd[0])
if subd[0] == 'Postgres':
db_create_postgre(exp_path, psql_path, testname, subd, bd)
print('oky')
else:
pathdb = dumpcheck(exp_path)
open_dump(bd, subd, pathdb)