Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Auto-Backup Questions

by Zoogie (Curate)
on Jun 05, 2000 at 08:48 UTC ( #16361=perlquestion: print w/replies, xml ) Need Help??
Zoogie has asked for the wisdom of the Perl Monks concerning the following question:

Time to seek the wisdom of the Perl Monks again...

I'm trying to write a CGI script that will turn a directory into a tar.gz archive, then return that archive to the user. The problem is, I probably later won't have the disk space to make a temporary file; is there a way I can send the output of tar directly to the browser without buffering?

Also, is there a way to modify the CGI headers such that when the browser pops up the "Save As..." dialog, the default filename is something like "backup.tar.gz" instead of "backup.cgi"?

Replies are listed 'Best First'.
RE: Auto-Backup Questions
by t0mas (Priest) on Jun 05, 2000 at 13:48 UTC
    You can try:
    use CGI; use Archive::Tar; use Compress::Zlib; $tar=Archive::Tar->new(); $tar->add_files("my","list","of","files"); $gz=Compress::Zlib::memGzip($tar->write()); print "Content-type: application/x-gzip\r\n"; print header(-type => 'application/x-gzip'); print $gz;
    Can't help you with the default filename, the tar part works though, but if don't have the diskspace, maybe you won't have the memory...

    /brother t0mas
      You can use a Content-disposition header for the default filename:
      print "Content-disposition: attachment; filename=foobar.tar.gz\n";

      ar0n ]

Re: Auto-Backup Questions
by nashdj (Friar) on Jun 05, 2000 at 11:53 UTC
    >Also, is there a way to modify the CGI headers such that when the browser pops up the "Save As..." dialog, the default filename is something like "backup.tar.gz" instead of "backup.cgi"?

    You can change the way the browser requests the document. However this may not be what you are after.

    If the browser requests it It will prompt to save as backup.tar.gz and still call the backup.cgi script. You could just have calling backup.cgi without any args redirect to that location (well thats one possible solution).
Re: Auto-Backup Questions
by athomason (Curate) on Jun 05, 2000 at 11:58 UTC
    1) To get the output of a command into a file descriptor use something like
    open(TAR, "tar <options> |") or die "couldn't tar"; print while <TAR>;

    2) The webservers I've used will let you trick the browser into thinking a file has a different name by treating the script as a directory. For example, if you have a script at "", you could have the browser pop up with "thisfile.tar.gz" by showing the user the URL "". Also make sure you send the appropriate content-type header. Check your webserver docs and give it a shot, though your mileage may vary.

Re: Auto-Backup Questions
by jjhorner (Hermit) on Jun 05, 2000 at 16:10 UTC

    The first part of answering any problem is asking the full question:

    • How often will the contents change?
    • How many times will this be called?
    • How many users?
    • How large are the files?
    • Is there any reason (from the questions above) that you can't just use a cron job to create tar files periodically, saving system power, and user time?
    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
Re: Auto-Backup Questions
by Corion (Pope) on Jun 05, 2000 at 12:06 UTC

    I think there should be no problem, as your script can simply attach itself to tars output. There come some problems with that, too, as your download cannot be resumed, but I guess you can live with that. Here is some code how I would try it - I've never done this myself, so a look into perlipc could maybe help you a bit more.

    $blocksize = 4096; open( BACKUP, "tar cz $directory |" ) or die "can't fork tar: $!\n"; binmode BACKUP; # just to be on the safe side do { $read = read( BACKUP, $data, $blocksize ) or die "can't read: $!\n +"; if ($read) { print $data; }; } while $read; close BACKUP;

    As with your second problem, the "wrong" filename, you can either instruct your webserver to treat your script as a directory and everything following the script name as a parameter to your script (look in the Apache documentation, as it's been a long time since I did this, I guess under SCRIPTEXEC), and issue a redirect from your script (if called without parameters) to an URL with the parameters : ->

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://16361]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-05-26 02:40 GMT
Find Nodes?
    Voting Booth?