Home » Php » PHP Recursive Backup Script

PHP Recursive Backup Script

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&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.

Answer:

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 ./*');

Answer:

You can use recursion.

for(scandir($dir) as $dir_contents){
    if(is_dir($dir_contents)){
        backup($dir_contents);
    }else{
        //back up the file
    }
}

Answer:

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

Answer:

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

Answer:

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

Answer:

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';
?>