PHP Recursive Backup Script

旧巷老猫 提交于 2019-11-30 00:58:47
Alix Axel

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.

user267599

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

You can use recursion.

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

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

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

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

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';
?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!