How can I exclude folders with this backup script?

I use this wonderful script to backup my folders on my server. However, there are several folders that I want to exclude from the backup. How can I exclude 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; } 
+5
source share
4 answers

enter the $no_zip path you want to 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; } 
+2
source

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 zipData() parameter called $nozip , which is an array containing the name (or relative path) of the folders you want to exclude. Then, to match, I added a function called match() that uses regex for a strong match, and it zips up the file / folder if it is not in $nozip .

It definitely works in local

+2
source

Here are some good answers. Here is mine so you can choose the best for you.

I am just adding a new var $exclude , you can set the folders and files you want to exclude. For instance. You want to exclude folder 3, 7 and file 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; } 

if the current file / folder is in the array, and then just continue, do not execute the code if its file allows you to check whether this folder file is in the exclude array.

No additional functions, one more parameter, and there =)

+2
source

try adding an array of excluded folders to your zip function. Of course, excluded folders should be subfolders of your $ 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
source

Source: https://habr.com/ru/post/1215784/


All Articles