Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude folders with this backup script?

I'm using this great script to backup my folders on my server, However there is a couple of folders that I want to exclude from the backup. How would I go about excluding them?

Thanks

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

// Start the backup!
zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip');
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination) {
    if (extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $iterator = new RecursiveDirectoryIterator($source);
                    // skip dot files while iterating 
                    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        $file = realpath($file);
                        if (is_dir($file)) {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        } else if (is_file($file)) {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}
like image 693
Exoon Avatar asked Mar 20 '15 19:03

Exoon


2 Answers

put in $no_zip the path that you want exclude. and see the line if(!in_array($file, $no_zip) {

   <?php
    /*
     * PHP: Recursively Backup Files & Folders to ZIP-File
     * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
     * contribution: Drew Toddsby
    */

    // Make sure the script can handle large folders/files
    ini_set('max_execution_time', 600);
    ini_set('memory_limit','1024M');

    // Start the backup!
    zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip');
    echo 'Finished.';

    $no_zip = array('path_1', 'path_2');

    // Here the magic happens :) Edit: Very Funny ;-)
    function zipData($source, $destination) {
        if (extension_loaded('zip')) {
            if (file_exists($source)) {
                $zip = new ZipArchive();
                if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                    $source = realpath($source);
                    if (is_dir($source)) {
                        $iterator = new RecursiveDirectoryIterator($source);
                        // skip dot files while iterating 
                        $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                        $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                        foreach ($files as $file) {

                            //check 
                            if(!in_array($file, $no_zip)) {

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

                        }

                        }
                    } else if (is_file($source)) {
                        $zip->addFromString(basename($source), file_get_contents($source));
                    }
                }
                return $zip->close();
            }
        }
        return false;
    }
like image 90
Adrian Cid Almaguer Avatar answered Nov 15 '22 05:11

Adrian Cid Almaguer


I tried this way

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

// Start the backup!
zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip'); //add a third array parameter with names/relative paths of what you want to exclude
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination, $nozip = array()) {
    if (is_array($nozip) && extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $iterator = new RecursiveDirectoryIterator($source);
                    // skip dot files while iterating 
                    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        $file = realpath($file);
                        if(!match($file, $nozip)) //Check if it has to zip the file/folder
                        {
                            if (is_dir($file)) {
                                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                            } else if (is_file($file)) {
                                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                            }
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}


//Returns true if $item is matched once through $array by preg_match()
function match($item, $array)
{
    $matching = array("\\" => "[\/|\\\]", "/" => "[\/|\\\]");
    foreach($array as $i)
    {
        $str = strtr($i, $matching); //creates the regex
        if(preg_match("/".$str."/i", $item))
            return true;
    }

    return false;
}
?>

I added a parameter to zipData() called $nozip, which is an array containing the name (or relative path) of the folders you want to exclude. Then to do the matching I added a function called match() that uses regex to match strongly and it zips the file/folder if it's not in $nozip.

It definetly works in local

like image 36
Phate01 Avatar answered Nov 15 '22 03:11

Phate01