Decompress mysql dumps on the fly using python subprocess during an import to simplify operation
This commit is contained in:
parent
493144e06c
commit
1e2ce363f7
1 changed files with 40 additions and 48 deletions
|
@ -111,6 +111,7 @@ import ConfigParser
|
||||||
import os
|
import os
|
||||||
import pipes
|
import pipes
|
||||||
import stat
|
import stat
|
||||||
|
import subprocess
|
||||||
try:
|
try:
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -166,56 +167,47 @@ def db_import(module, host, user, password, db_name, target, all_databases, port
|
||||||
if not all_databases:
|
if not all_databases:
|
||||||
cmd += " -D %s" % pipes.quote(db_name)
|
cmd += " -D %s" % pipes.quote(db_name)
|
||||||
if os.path.splitext(target)[-1] == '.gz':
|
if os.path.splitext(target)[-1] == '.gz':
|
||||||
gzip_path = module.get_bin_path('gzip')
|
zcat_path = module.get_bin_path('zcat')
|
||||||
if not gzip_path:
|
if not zcat_path:
|
||||||
module.fail_json(msg="gzip command not found")
|
module.fail_json(msg="zcat command not found")
|
||||||
#gzip -d file (uncompress)
|
p1 = subprocess.Popen([zcat_path, target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
rc, stdout, stderr = module.run_command('%s -d %s' % (gzip_path, target))
|
p2 = subprocess.Popen(cmd.split(' '), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
if rc != 0:
|
(stdout2, stderr2) = p2.communicate()
|
||||||
return rc, stdout, stderr
|
p1.stdout.close()
|
||||||
#Import sql
|
p1.wait()
|
||||||
cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
|
if p1.returncode != 0:
|
||||||
try:
|
stderr1 = p1.stderr.read()
|
||||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
return p1.returncode, '', stderr1
|
||||||
if rc != 0:
|
else:
|
||||||
return rc, stdout, stderr
|
return p2.returncode, stdout2, stderr2
|
||||||
finally:
|
|
||||||
#gzip file back up
|
|
||||||
module.run_command('%s %s' % (gzip_path, os.path.splitext(target)[0]))
|
|
||||||
elif os.path.splitext(target)[-1] == '.bz2':
|
elif os.path.splitext(target)[-1] == '.bz2':
|
||||||
bzip2_path = module.get_bin_path('bzip2')
|
bzcat_path = module.get_bin_path('bzcat')
|
||||||
if not bzip2_path:
|
if not bzcat_path:
|
||||||
module.fail_json(msg="bzip2 command not found")
|
module.fail_json(msg="bzcat command not found")
|
||||||
#bzip2 -d file (uncompress)
|
p1 = subprocess.Popen([bzcat_path, target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
rc, stdout, stderr = module.run_command('%s -d %s' % (bzip2_path, target))
|
p2 = subprocess.Popen(cmd.split(' '), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
if rc != 0:
|
(stdout2, stderr2) = p2.communicate()
|
||||||
return rc, stdout, stderr
|
p1.stdout.close()
|
||||||
#Import sql
|
p1.wait()
|
||||||
cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
|
if p1.returncode != 0:
|
||||||
try:
|
stderr1 = p1.stderr.read()
|
||||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
return p1.returncode, '', stderr1
|
||||||
if rc != 0:
|
else:
|
||||||
return rc, stdout, stderr
|
return p2.returncode, stdout2, stderr2
|
||||||
finally:
|
|
||||||
#bzip2 file back up
|
|
||||||
rc, stdout, stderr = module.run_command('%s %s' % (bzip2_path, os.path.splitext(target)[0]))
|
|
||||||
elif os.path.splitext(target)[-1] == '.xz':
|
elif os.path.splitext(target)[-1] == '.xz':
|
||||||
xz_path = module.get_bin_path('xz')
|
xzcat_path = module.get_bin_path('xzcat')
|
||||||
if not xz_path:
|
if not xzcat_path:
|
||||||
module.fail_json(msg="xz command not found")
|
module.fail_json(msg="xzcat command not found")
|
||||||
#xz -d file (uncompress)
|
p1 = subprocess.Popen([xzcat_path, target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
rc, stdout, stderr = module.run_command('%s -d %s' % (xz_path, target))
|
p2 = subprocess.Popen(cmd.split(' '), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
if rc != 0:
|
(stdout2, stderr2) = p2.communicate()
|
||||||
return rc, stdout, stderr
|
p1.stdout.close()
|
||||||
#Import sql
|
p1.wait()
|
||||||
cmd += " < %s" % pipes.quote(os.path.splitext(target)[0])
|
if p1.returncode != 0:
|
||||||
try:
|
stderr1 = p1.stderr.read()
|
||||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
return p1.returncode, '', stderr1
|
||||||
if rc != 0:
|
else:
|
||||||
return rc, stdout, stderr
|
return p2.returncode, stdout2, stderr2
|
||||||
finally:
|
|
||||||
#xz file back up
|
|
||||||
rc, stdout, stderr = module.run_command('%s %s' % (xz_path, os.path.splitext(target)[0]))
|
|
||||||
else:
|
else:
|
||||||
cmd += " < %s" % pipes.quote(target)
|
cmd += " < %s" % pipes.quote(target)
|
||||||
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
rc, stdout, stderr = module.run_command(cmd, use_unsafe_shell=True)
|
||||||
|
|
Loading…
Reference in a new issue