Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Recursive Backup Script

Tags:

php

backup

I wrote a basic content-management system for my website, including an administration panel. I understand basic file IO as well as copying via PHP, but my attempts at a backup script callable from the script have failed. I tried doing this:

//... authentication, other functions
for(scandir($homedir) as $buffer){
    if(is_dir($buffer)){
        //Add $buffer to an array
    }
    else{
        //Back up the file
    }
}
for($founddirectories as $dir){
    for(scandir($dir) as $b){
        //Backup as above, adding to $founddirectories
    }
}

But it did not seem to work.

I know that I can do this using FTP, but I want a completely server-side solution that can be accessed anywhere with sufficient authorization.

like image 758
Hawkcannon Avatar asked Feb 07 '10 20:02

Hawkcannon


5 Answers

Here is an alternative though: why don't you Zip the source directory instead?

function Zip($source, $destination)
{
    if (extension_loaded('zip') === true)
    {
        if (file_exists($source) === true)
        {
            $zip = new ZipArchive();

            if ($zip->open($destination, ZIPARCHIVE::CREATE) === true)
            {
                $source = realpath($source);

                if (is_dir($source) === true)
                {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

                    foreach ($files as $file)
                    {
                        $file = realpath($file);

                        if (is_dir($file) === true)
                        {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        }

                        else if (is_file($file) === true)
                        {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                }

                else if (is_file($source) === true)
                {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }

            return $zip->close();
        }
    }

    return false;
}

You can even unzip it afterwards and archive the same effect, although I must say I prefer having my backups compressed in zip file format.

like image 118
Alix Axel Avatar answered Nov 08 '22 22:11

Alix Axel


if you have access to execute tar binary file through exec function it would be faster and better i think:

exec('tar -zcvf ' . realpath('some directory') .'/*);

or

chdir('some directory')
exec('tar -zcvf ./*');
like image 4
user267599 Avatar answered Nov 08 '22 23:11

user267599


You can use recursion.

for(scandir($dir) as $dir_contents){
    if(is_dir($dir_contents)){
        backup($dir_contents);
    }else{
        //back up the file
    }
}
like image 2
Anon. Avatar answered Nov 08 '22 22:11

Anon.


you got it almost right

$dirs = array($homedir);
$files = array();

while(count($dirs)) {
   $dir = array_shift($dirs);
   foreach(glob("$dir/*") as $e)
      if(is_dir($e)) 
         $dirs[] = $e;
      else
         $files[] = $e;
}
// here $files[] contains all files from $homedir and below

glob() is better than scandir() because of more consistent output

like image 2
user187291 Avatar answered Nov 09 '22 00:11

user187291


I us something called UPHP. Just call zip() to do that. here:

<?php
    include "uphplib.php";
    $folder = "data";
    $dest = "backup/backup.zip";
    zip($folder, $dest);
?>

UPHP is a PHP library. download: here

like image 1
Tanner Ottinger Avatar answered Nov 08 '22 22:11

Tanner Ottinger