Need to archive a MySQL database using a python script

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Sep 25 19:32:13 EDT 2012


On 26 September 2012 00:17, <bruceg113355 at gmail.com> wrote:

> Python Users Group,
>
> I need to archive a MySQL database using a python script.
> I found a good example at: https://gist.github.com/3175221
>
> The following line executes however, the archive file is empty.
>
> os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" %
>                (user,password,host,database,database+"_"+filestamp))
> Where:
>   User     = “someUser”
>   password = “somePassword”
>   host     = “someRemote.database.server”
>   database = “someDatabase”
>

I hope you're getting those variables from a trusted source as this code is
vulnerable to injection.

Use subprocess.check_call instead of os.popen. os.popen is obsolete and it
also doesn't quite do what you think. It returns a file object. You need to
read the data from the file object and then close it to make the process
actually run.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120926/b560666f/attachment.html>


More information about the Python-list mailing list