开发者

Can't seem to dump a mysql database

开发者 https://www.devze.com 2023-03-25 03:59 出处:网络
I seem to be having a problem. I have aview 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

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 like开发者_Python百科ly 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")


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号