[Tutor] send os.system() output to tarfile?

Brian Jones bkjones at gmail.com
Tue Aug 7 21:55:03 CEST 2007


Hi all,

I've been lurking on this list for some time. It's great. Thanks for all the
help.

I'm a sysadmin by trade, and have slowly started using Python more and more
in my work. However, this is my first experience with using the tarfile
module.

I'm currently writing a script to backup a mysql database. On the cli, I'd
do something like this:
'mysqldump dbname | gzip -9 > dbname-date.gz'

Note that "gzip -9" could just as easily be "tar cvzf" for example.

Anyway, what I'm trying to do is figure out the right way to redirect the
output generated by "os.system(mysqldump dbname)" to my tarfile object for
compressing. What I tried were a few variations on this:
======================
#!/usr/bin/env python

import os
import time
import tarfile
import sys

filename = time.strftime("%Y%m%d") + ".tgz"
tarball = tarfile.open(filename, "w|gz", fileobj = sys.stdout)
os.system( "ls -alrt" )
tarball.close()

======================

I also played around with redirecting stdout to the tarfile object, and some
other things that are probably stuff you'd expect from a python n00b.

What's the right way to do this? I was hoping to not be forced down the road
of creating temp files, compressing *those*, and then blowing them away at
the end.

Any help greatly appreciated.
brian.


-- 
Brian K. Jones
Python Magazine  http://www.pythonmagazine.com
My Blog          http://m0j0.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070807/1d0b99d7/attachment.html 


More information about the Tutor mailing list