Как я могу исключить папки с помощью этого скрипта резервного копирования?

Я использую этот замечательный скрипт для резервного копирования своих папок на моем сервере, однако есть несколько папок, которые я хочу исключить из резервной копии. Как бы я их исключил?

Спасибо

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

5

Решение

Путин $no_zip путь, который вы хотите исключить. и увидеть линию 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;
}
2

Другие решения

Я пробовал таким образом

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

Я добавил параметр в zipData() называется $nozip, который является массивом, содержащим имя (или относительный путь) папок, которые вы хотите исключить. Затем для сопоставления я добавил функцию match() который использует регулярное выражение для сильного соответствия и архивирует файл / папку, если он не находится в $nozip,

Это определенно работает в местном

2

Здесь есть хорошие ответы.
Вот мое для вас, чтобы выбрать лучшее для вас.

Я просто добавляю новый вар $exclude, вы можете установить папки и файлы, которые вы хотите исключить.
Например.
Вы хотите исключить папку 3, 7 и файл 8 / fdsgdfg.js:

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

$dir = '/var/www/hack/to_bk';

$exclude = array("$dir/3","$dir/7", "$dir/8/fdsgdfg.js");

// Start the backup!
zipData($dir, '/var/www/html/uploaded.zip', $exclude);
echo 'Finished.';
// Here the magic happens :)
function zipData($source, $destination, $exclude) {
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) {
if ( in_array($file, $exclude) ) {
continue;
}
if ( is_file($file) ) {
$p = pathinfo($file);
if ( in_array($p['dirname'], $exclude) ) {
continue;
}
}
$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;
}

если текущий файл / папка находится в массиве, просто продолжайте, не выполняйте код, если это файл, давайте проверим, находится ли файл этой папки в массиве исключений.

Никаких дополнительных функций, просто еще один параметр и вот он =)

2

попробуйте добавить массив исключенных папок в вашу функцию zip. Конечно, исключенные папки должны быть подпапками вашего $ source

    ini_set('memory_limit','1024M');

// Start the backup!
zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip', array('/var/www/html/uploaded/sb0','/var/www/html/uploaded/sb1','/var/www/html/uploaded/sb2','/var/www/html/uploaded/sb3'));
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination, $excluded = array()) {
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 (in_array($file,$excluded,true)!=true) { // if (!in_array($file,$excluded,true)) // check if sub-folder is in excluded array.
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;
}
2
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector