1

Hi, this script is suppose to back up most if not all directories, all files within each directory then compressed those files and directories in a zip file and save it to a directory.

A typical web server can be in the hundreds of megs, the script works except it is missing alot of directores and or alot of the files within those directories which results in a server back up that is very short of it's actual size. I was hoping someone could help get this script working I would appreciate it.

<?php

// ZIP Everything into one zip file

// Create a new directory called "mybackup".
// It's important to use that name because you don't want the
// backup script to backup previous backups.  It will ignore
// the directory where your backup zipped files are located.

// Call this script something like "backup123.php" and put it into your main website directory.
// Make this script name something that nobody can easily figure out.  Don't call it "backup.php"

$file="backup".time().".zip";
if(Zip('./', './mybackup/'.$file)===TRUE){
echo "Zip Complete";
}
else{
echo "Did not zip";
}

function Zip($source, $destination){
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', realpath($source));

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

        foreach ($files as $file){
            $file = str_replace('\\', '/', $file);

            // Ignore "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..','mybackup')) )
                continue;

            $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();
}
?> 
2
Contributors
2
Replies
23
Views
3 Years
Discussion Span
Last Post by Siberian
1

Hello,

OK a couple of things.
Using PHP to do your backups is a bad idea because the only files that PHP can access are the files that a web user can access and that is why your backup is smaller that the actual directory structure. And this script isdesigned for a single sitte not multiple sites.

Are you running this on Windows or Linux based server? In either case there are Linux shell scripts and Windows Power Shell scripts that will do this properly and retain the permission and ownership settings of the files when they do it. Tell us which OS and we will make suggestions on how to do it properly.

0

Hi, I'm running a Linux server. I hope you can help, I want to do an automatic backup on a weekly basis with all my directories being backed up.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.