Fonction backup avec exclusion tmp et backup

This commit is contained in:
Fred Tempez 2019-12-16 17:11:40 +01:00
parent 1b83c4bb5e
commit e87c5c492a

View File

@ -199,18 +199,34 @@ class config extends common {
* Sauvegarde des données * Sauvegarde des données
*/ */
public function backup() { public function backup() {
// Creation du ZIP // Creation du ZIP
$fileName = date('Y-m-d-h-i-s', time()) . '.zip'; $fileName = str_replace('/','',helper::baseUrl(false,false)) . '-'. date('Y-m-d-h-i-s', time()) . '.zip';
$zip = new ZipArchive(); $zip = new ZipArchive();
if($zip->open(self::TEMP_DIR . $fileName, ZipArchive::CREATE) === TRUE){ $zip->open(self::TEMP_DIR . $fileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach(configHelper::scanDir('site/') as $file) { $directory = 'site/';
$zip->addFile($file); $filter = array('backup','tmp');
} $files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$directory,
RecursiveDirectoryIterator::SKIP_DOTS
),
function ($fileInfo, $key, $iterator) use ($filter) {
return $fileInfo->isFile() || !in_array($fileInfo->getBaseName(), $filter);
}
)
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen(realpath('site/')) + 1);
$zip->addFile($filePath, $relativePath);
}
} }
$zip->close(); $zip->close();
// Téléchargement du ZIP // Téléchargement du ZIP
header('Content-Transfer-Encoding: binary'); header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize(self::TEMP_DIR . $fileName)); header('Content-Length: ' . filesize(self::TEMP_DIR . $fileName));
readfile(self::TEMP_DIR . $fileName); readfile(self::TEMP_DIR . $fileName);