-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdmysql-import-database
More file actions
executable file
·55 lines (43 loc) · 1.94 KB
/
Copy pathdmysql-import-database
File metadata and controls
executable file
·55 lines (43 loc) · 1.94 KB
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
#!/usr/bin/python
import sys
from subprocess import call
import os
import argparse
def runContainer(containerName, path, database = ""):
''' Runs the container calling docker run and passing
the necessary arguments. The database is optional since
we can import an .sql file without creating first a database or
specifying one.
'''
# Split the path given and create a list out of it
pathList = path.split("/")
# Take the last element of the list because this is the file
fileName = pathList[-1]
# Read the path and get the directory
dirname = os.path.dirname(path)
if database:
print 'Importing {file} into database {database} using container {container}'.format(file=fileName, database=database, container=containerName)
call("docker run -it --link="+containerName+":mysql -v "+ dirname +":/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p "+ database +" < /tmp/import/" + fileName + "'", shell=True)
else:
print 'Importing {file} into container {container}'.format(file=fileName, container=containerName)
call("docker run -it --link="+containerName+":mysql -v "+ dirname +":/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p < /tmp/import/" + fileName + "'", shell=True)
return
def main():
''' Interpret the command line arguments
and pass the options to runContainer
'''
parser = argparse.ArgumentParser(
description="Import a .sql file into a database",
prog="dmysql-import-database")
# Create the options
parser.add_argument("container", help="The mysql container to use")
parser.add_argument("filepath", help="File to the sql file you want to import")
parser.add_argument("--database", help="Database to import the file into")
args = parser.parse_args()
if args.database:
runContainer(args.container, args.filepath, args.database)
else:
runContainer(args.container, args.filepath)
return
# Execute main
main()