7

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.

Hawkcannon
  • 227
  • 3
  • 8
  • 2
    This is eerie, I am working on exactly the same issue at the moment, and I asked almost the same question one minute ago :) I deleted it, though, because I was given a good existing question: http://stackoverflow.com/questions/1334613/zip-a-directory-in-php – Pekka Feb 07 '10 at 20:05
  • I am surprised no one mentioned DirectoryIterator for getting your files. – Chris Feb 01 '11 at 13:49

7 Answers7

9

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.

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
4

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 ./*');
slhck
  • 36,575
  • 28
  • 148
  • 201
user267599
  • 789
  • 1
  • 5
  • 5
2

You can use recursion.

for(scandir($dir) as $dir_contents){
    if(is_dir($dir_contents)){
        backup($dir_contents);
    }else{
        //back up the file
    }
}
Anon.
  • 58,739
  • 8
  • 81
  • 86
2

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

user187291
  • 53,363
  • 19
  • 95
  • 127
1

Here is a backup script with ftp, scp, mysqldump, pg_dump and filesystem capabilities https://github.com/skywebro/php-backup

Andi P. Trix
  • 175
  • 1
  • 3
1

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

Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28
0

i use a simple function to backup a file:

<?php
$oldfile = 'myfile.php';
$newfile = 'backuped.php';
copy($oldfile, $newfile) or die("Unable to backup");

echo 'Backup is Completed';
?>
T.Todua
  • 53,146
  • 19
  • 236
  • 237