我正在使用这个出色的脚本备份我服务器上的文件夹,但是我想从备份中排除几个文件夹。我该如何排除它们?
谢谢
<?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;
}
答案 0 :(得分:2)
将$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;
}
答案 1 :(得分: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 :(得分:2)
这里有很好的答案。 这是我的最佳选择。
我只需添加一个新的var $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;
}
如果当前文件/文件夹在数组中,那么只需继续,不执行代码,如果是文件,则验证此文件夹的文件是否在exclude数组中。
没有额外的功能,只有一个参数,并且它是=)
答案 3 :(得分: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;
}