2

I am trying to save a mysql database dump in a python script. Here is what I have:

filepath = os.path.join(BACKUPS_FOLDER, '%s.sql' % date)
print filepath # /Users/david/Desktop/updates/_backups/2013-04-07.sql
subprocess.call(shlex.split('mysqldump -u root updates > %s' % filepath ))

I get the following error:

mysqldump: Couldn't find table: ">"

What I am doing incorrectly here?

2
  • What happens when you enclose %s in quotes? "%s" Commented Apr 7, 2013 at 22:52
  • I think you don't have the shell when calling mysqldump so it will pass > as a parameter (in this case as a table name) Commented Apr 7, 2013 at 22:57

2 Answers 2

5

You can call mysqldump with the option --result-file=file (subprocess.call(shlex.split('mysqldump -u root updates --result-file=%s' % filepath )))since it will interpret the > as a table name.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for!
Would be good to note that Python 3.5 and higher, subprocess.run is the preferred API - docs.python.org/3.7/library/subprocess.html#subprocess.run
0

This is what I did, using communicate() to get the output and then write to a file:

date = str(datetime.datetime.now().date())
filepath = os.path.join(BACKUPS_FOLDER, '%s.sql' % date)

args = shlex.split("mysqldump -u root updates")
p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
dump_output = p1.communicate()[0]

f = open(filepath, "wb")
f.write(dump_output)
f.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.