0

I need to execute mysqldump from within a django function. I can do so easily enough from the terminal command line, but when I try to run it from within the python script, I get an error:

sh: mysqldump: command not found 

when running the following.

filestamp = date.today()
dumpcmd = "mysqldump -u root appdb > appdb%s.out" % (filestamp)
os.system(dumpcmd)

I think the problem has something to do with the Path in either the django application or Eclipse, but I can't figure out why mysqldump can't be found from within the django app but it can be from the command line / virtualenv

Ed.
  • 4,439
  • 10
  • 60
  • 78
  • You are better off using the management command `dumpdata` – karthikr Dec 11 '16 at 22:35
  • but why do you want to do that? – e4c5 Dec 12 '16 at 00:12
  • I want to automate the syncing of a database on my local machine with the same database hosted online. The first step I usually pursue is mysqldump, so I'm trying to replicate that programatically. – Ed. Dec 14 '16 at 17:34
  • almost a duplicate: http://stackoverflow.com/questions/38916163/php-regular-backup-of-mysql-data – e4c5 Dec 17 '16 at 14:08

1 Answers1

0

make sure mysqldump is in your path

$ whereis mysqldump; echo $PATH
mysqldump: /usr/bin/mysqldump /usr/share/man/man1/mysqldump.1.gz
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

and/or

using mysqldump and mysql inside python

import subprocess
subprocess.Popen('mysqldump -h localhost -P 3306 -u -root appdb > appdb.sql', shell=True)

Or use the full path in the Python statement. e.g. /usr/bin/mysqldump

Community
  • 1
  • 1
larvarunt
  • 16
  • 3