Web Hosting Talk







View Full Version : TAR & GZIP a folder


Dominic
10-28-2001, 03:10 AM
How can I TAR & GZIP a folder in PHP or Perl?
I want to have my own daily backup...
As the daily backup create by CPanel include many things I don't want...
I only need the public_html...:D

nexcess.net
10-28-2001, 05:31 AM
Just use the 'exec', 'passthru' or system fuctions..

i.e.

<?php
$archive = "/home/mysite.com/backup-102701.tar";
$directory = "/home/mysite.com/public_html";
exec( "tar cf $archive $directory");
exec( "gzip $archive");
?>

then you can just FTP the backup to you local machine or alternately put it in your webspace to download via http (just make sure you delete it!).

you may also want to add auto-downloading & deletion of this file once it's created to make it a one-click backup.

Chris

Dominic
10-28-2001, 01:27 PM
Thanks Chris...;)

Dominic
10-29-2001, 08:35 PM
>>you may also want to add auto-downloading & deletion of this file once it's created to make it a one-click backup.

Can php save the result of gzip into a variable?
And then I can echo the result to download,
so no file is created?
Thanks...:)

Lawrence
10-29-2001, 10:10 PM
I think you could do all the archiving in one hit using the option on tar to filter the archive through gzip, and the option to output the resulting archive to STDOUT (ie: your browser, so you could download it straight away).

I don't know PHP, but I just checked tar --help, and perhaps you could try this (copying from nexcess above):

<?php
$directory = "/home/mysite.com/public_html";
exec("tar -cOz $directory");
?>

I'm not sure if PHP would then send the output for you to download or not... perhaps you'd have to pipe like this or something:

<?php
$directory = "/home/mysite.com/public_html";
exec( "tar -cOz $directory > STDOUT");
?>

But I'm guessing. You'll need a PHP and Linux guru to finish it off for you! :)

nexcess.net
10-29-2001, 11:40 PM
ok, i did some digging in the phpmyadmin code since they do something similar but on db dumps and wrote this little snippet.

<?php
// this should work on php 4.0.4 and above

// got part of this from phpmyadmin.
header('Content-Type: application/x-gzip');
$content_disp = ( ereg('MSIE ([0-9].[0-9]{1,2})', $HTTP_USER_AGENT) == 'IE') ? 'inline' : 'attachment';
header('Content-Disposition: ' . $content_disp . '; filename="backup.tar.gz"');
header('Pragma: no-cache');
header('Expires: 0');

// create the gzipped tarfile.
passthru( "tar cz /your/directory/here");
?>

Now, the only problem is that most hosts have a memory cap limiit (for good reason) and if the backup is large (I tried it on our web dirs and it was too big) it will fail.. So, while this is a quick and easy piece to use it may still be better to handle files on the backend to get around the mem limit.

Chris

Dominic
10-30-2001, 12:15 AM
Thanks!!!

I think the memory cap limiit will not affect me,
as my backup is < 10 Mb...^^"