views:

1352

answers:

4

I am creating a php backup script that will dump everything from a database and save it to a file. I have been successful in doing that but now I need to take hundreds of images in a directory and compress them into one simple .tar.gz file.

What is the best way to do this and how is it done? I have no idea where to start.

Thanks in advance

A: 

You can easily gzip a folder using this command:

tar -cvzpf backup.tar.gz /path/to/folder

This command can be ran through phps system()-function.

Don't forget to escapeshellarg() all commands.

alexn
Thank you Alexander for you answer. How would this look like in php? Would it go something along the lines of:system('tar -cvzpf my_backup.tar.gz /images_water/images_rivers/');
VinkoCM
Yes, that would do it.
alexn
Unfortunately this does not work for me because my hosting provider has set security restrictions on exec/system commands.
VinkoCM
A: 

Erm...

Why use PHP for this kind of things?

tar -czvvf somearchive.tar.gz file1 ... filen

I recommend file1 be a common root directory and hence also filen

OOPMan
+7  A: 

If you are using PHP 5.2>, you could use the Zip Library

and then do something along the lines of:

$images = '/path/to/images';
//this folder must be writeable by the server
$backup = '/path/to/backup';
$zip_file = $backup.'/backup.zip';

if ($handle = opendir($images))  
{
    $zip = new ZipArchive();

    if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) 
    {
        exit("cannot open <$filename>\n");
    }

    while (false !== ($file = readdir($handle))) 
    {
        $zip->addFile($file);
        echo "$file\n";
    }
    closedir($handle);
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
    echo 'Zip File:'.$zip_file . "\n";
}
Paul Dixon
Thanks Kev for the answer. I just need to clarify one little thing: did you mean to ask if I had a version of PHP lower than 5.2 or higher than 5.2. I currently have 5.2.6. As I understand your answer, I will not be able to run this because I have a PHP version to high. Is this correct?
VinkoCM
He means that you need a PHP version that's 5.2.0 or later, so you'll be fine.
alexn
Yeah sorry as Alexander says its for 5.2.0 or later
Paul Dixon
I just tried this and I get this error:<br>Fatal error: Class 'ZipArchive' not found in [...] on line 7. <br>What could cause this? I just contacted my web hosting provider to tell me if PHP is installed with the appropriate libraries.
VinkoCM
That would suggest to me that your host doesnt support the ZipArchive class, thy would need to compile PHP with zip support by using the --enable-zip configure option (http://us3.php.net/manual/en/zip.installation.php)
Paul Dixon
Yes, that is what I think too. That is why I am contacting my hosting provider now.
VinkoCM
Well, I finaly got this script to run. I just tried it on a different server. But it does not work. For some reason, the archive is not created. I do not get any errors, the status at the end is show as 0 and it accurately counts the number of files. I have searched high and low for the archive in the server, but I can't find it. I used the script exaclty how it is written except I changed the first line to: if ($handle = opendir('inmobiliarios')) {. All I did was remove the leading slash because the script could not find the directory.
VinkoCM
The Zip should be in the directory with the images. Is the directory writable?
Paul Dixon
Ok, would it be possible to do this instead: have 2 directories (images and backup) and have the script outside of these directories. The script takes the files from the images dir and compresses them into one file in the backup dir. I changed the first line to inmobiliarios/ and the third line to backup/inmobiliarios_backup.zip but it still does not work.
VinkoCM
ive updated the script a bit to do the above, you need to ensure that the backup directory is writeable though. If is isn't the script will still work but wont be able to save the zip and not give any errors
Paul Dixon
Thank you everybody for all the help. I finally go the script to work. All I had to do was change this line: $zip->addFile($file); to this: $zip->addFile('path/to/images/'.$file);
VinkoCM
A: 

You can also use something like this:

exec('tar -czf backup.tar.gz /path/to/dir-to-be-backed-up/');
geerlingguy