Shehzad009
Shehzad009

Reputation: 1607

Can't seem to dump a mysql database

I seem to be having a problem. I have a view where I can allow staff users to download the MySQL database for that program, however it is not working at all. I get an error which says Errno 2] No such file or directory: '/usr/local/src/djcode/c2duo_mms/backup.gz'.

I don't know why I get the error, but it the likely answer is because the I can't dump the database properly. It can't find backup.gz, because it cannot find the file beacause the step where it supposed to dump the file does not work.

views.py

@login_required
def dbbackup(request):
    if not (request.user.is_authenticated() and request.user.is_staff):
        raise http.Http404
    os.popen3("mysqldump -u *username* -p*password* *database* > /usr/local/src/djcode/c2duo_mms/backup.sql")
    os.popen3("gzip -c /usr/local/src/djcode/c2duo_mms/backup.sql > /usr/local/src/djcode/c2duo_mms/backup.gz"
    dataf = open('/usr/local/src/djcode/c2duo_mms/backup.gz', 'r')
    return HttpResponse(dataf.read(), mimetype='application/x-gzip')

EDIT: I have tried running a small python script. Now the following python file below works (saves a file named backup.gz in the c2duo_mms directory). So why can I not do the same thing from my views.py file!?

#!/usr/bin/env python
import os

    os.popen3("mysqldump -u *username* -p*password* *database* > /usr/local/src/djcode/c2duo_mms/backup.sql")
    os.popen3("gzip -c /usr/local/src/djcode/c2duo_mms/backup.sql > /usr/local/src/djcode/c2duo_mms/backup.gz")

Upvotes: 2

Views: 2137

Answers (4)

Shehzad009
Shehzad009

Reputation: 1607

The webserver was running as a different user than root (it needs to be the same), so I did not have permissions to save in that folder. I changed the ownership of the folder I wanted to save to which has worked now.

chown -R "apache" c2duo_mms

Upvotes: -1

Gabriel Ross
Gabriel Ross

Reputation: 5198

Try something like this:

import subprocess    
command = "mysqldump -u *username* -p*password* *database* > /usr/local/src/djcode/c2duo_mms/backup.sql"
p = subprocess.Popen(command, shell=True, bufsize=0, stdout=subprocess.PIPE, universal_newlines=True)
p.wait()
output = p.stdout.read()
p.stdout.close()

The var "output" will give you access to any error messages from the command.

Upvotes: 1

Danny Staple
Danny Staple

Reputation: 7322

Popen opens a process, but it does not create a shell around it. Since I don't expect an intermediate shell, then I don't expect that the redirects there would be interpreted. Popen does return file handles to the various streams in/out of the process - it would be stdout that you get without the redirects.

If you read and the store the content from those pipe handles, you can do the redirect inside the python code.

Perhaps you could consider the subprocess module - http://docs.python.org/library/subprocess.html - and you can specify what shell to use with it, which then can interpret the redirects.

Upvotes: 0

Matt Rogers
Matt Rogers

Reputation: 109

Use a full path here:

 os.popen3("mysqldump --add-drop-table -u " + settings.DATABASE_USER + " -p" + settings.DATABASE_PASSWORD + " " + settings.DATABASE_NAME + " >  backup.sql")

i.e. Where you are saving down the sql.

Upvotes: 1

Related Questions