-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdump-structure.py
executable file
·42 lines (33 loc) · 1.14 KB
/
dump-structure.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
import json
import time
import subprocess
import click
import jmespath as jp
@click.command()
@click.option('-u', '--user', help='db user', prompt=True, required=True)
@click.option('-p', '--password', help='db password',
prompt=True, required=True, hide_input=True)
@click.option('-h', '--host', help='Source DB Host', prompt=True, required=True)
@click.option('-m', '--mappings', help='db mappings json file', required=True)
def dumpdbstructure(user, password, host, mappings):
with open(mappings) as f:
db_map = json.load(f)
dbs = jp.search('rules[].["object-locator"][].["schema-name"] | []',
db_map)
dump_cmd = [
'mysqldump',
'--no-data',
'--compact',
'--routines',
f'--host={host}',
f'--user={user}',
f'--password={password}',
'--databases', ' '.join(dbs)
]
print("Executing")
print(" ".join(dump_cmd))
dumpfilename = time.strftime("%Y%m%d-%H%M%S") + '-' + host + '.sql'
# with open(dumpfilename, 'w') as f:
# proc = subprocess.Popen(dump_cmd, stdout=f)
if __name__ == '__main__':
dumpdbstructure()