如何使用此备份脚本排除文件夹?
How can I exclude folders with this backup script?
我正在使用这个很棒的脚本来备份我服务器上的文件夹,但是有几个文件夹我想从备份中排除。我将如何排除它们?
谢谢
<?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;
}
输入$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;
}
我这样试过
<?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
.
中,它会压缩 file/folder
它绝对适用于本地
这里有很好的答案。
这是我的,供您选择最适合您的。
我只是添加了一个新的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;
}
如果当前file/folder在数组中,则继续,不执行代码,如果是文件,让我们验证此文件夹的文件是否在排除数组中。
没有额外的功能,只是多了一个参数而已=)
尝试在您的 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;
}
我正在使用这个很棒的脚本来备份我服务器上的文件夹,但是有几个文件夹我想从备份中排除。我将如何排除它们?
谢谢
<?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;
}
输入$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;
}
我这样试过
<?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
.
它绝对适用于本地
这里有很好的答案。 这是我的,供您选择最适合您的。
我只是添加了一个新的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;
}
如果当前file/folder在数组中,则继续,不执行代码,如果是文件,让我们验证此文件夹的文件是否在排除数组中。
没有额外的功能,只是多了一个参数而已=)
尝试在您的 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;
}