From e79931432e63a86c5b7ced8a41186a24239794fe Mon Sep 17 00:00:00 2001 From: polo Date: Sat, 16 May 2026 02:10:23 +0200 Subject: =?UTF-8?q?appel=20=C3=A0=20Backup::mySQLdump=20d=C3=A9plac=C3=A9?= =?UTF-8?q?=20=C3=A0=20la=20connexion,=20compatibilit=C3=A9=20PHP=208.2=20?= =?UTF-8?q?(cause=20instanciation=20dans=20une=20chaine=20de=20m=C3=A9thod?= =?UTF-8?q?es),=20gzip=20encode=20disponible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/MaintenanceController.php | 3 ++- src/controller/UserController.php | 1 + src/controller/ViewDirector.php | 3 --- src/service/Backup.php | 31 +++++++++++++++++++------------ src/service/Installation.php | 4 ++-- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/controller/MaintenanceController.php b/src/controller/MaintenanceController.php index d4d2af4..f3352d8 100644 --- a/src/controller/MaintenanceController.php +++ b/src/controller/MaintenanceController.php @@ -85,7 +85,8 @@ class MaintenanceController } //echo $uploaded_file->getSize(); // à garder de côté au cas où - $server_place = Config::$database . '_' . new DateTime()->format('Y-m-d') . '_uploaded.sql'; + $date = new DateTime; + $server_place = Config::$database . '_' . $date->format('Y-m-d') . '_uploaded.sql'; try{ // enregistrer le fichier diff --git a/src/controller/UserController.php b/src/controller/UserController.php index 6928e86..9746a47 100644 --- a/src/controller/UserController.php +++ b/src/controller/UserController.php @@ -92,6 +92,7 @@ class UserController $_SESSION['user']['role'] = $user->getRole(); EmailService::cleanEmails($entityManager); + Backup::mySQLdump($entityManager, 'auto'); // créer un nouveau backup $url = new URL(isset($_GET['from']) ? ['page' => $_GET['from']] : []); isset($_GET['id']) ? $url->addParams(['id' => $_GET['id']]) : ''; diff --git a/src/controller/ViewDirector.php b/src/controller/ViewDirector.php index a572f14..59629c9 100644 --- a/src/controller/ViewDirector.php +++ b/src/controller/ViewDirector.php @@ -61,9 +61,6 @@ class ViewDirector extends AbstractBuilder // ViewDirector est aussi le premier if(CURRENT_PAGE === 'article' && !IS_ADMIN && self::$root_node->getNodeByName('main')->getAdoptedChild() === null){ return new Response($this->html, 302); } - elseif(CURRENT_PAGE === 'maintenance'){ - Backup::mySQLdump($entityManager, 'auto'); // créer un nouveau backup - } /* 4/ construction de la page avec builders et vues */ diff --git a/src/service/Backup.php b/src/service/Backup.php index 4a25f3d..8a3030f 100644 --- a/src/service/Backup.php +++ b/src/service/Backup.php @@ -13,7 +13,8 @@ class Backup static public function mySQLdump(EntityManager $entityManager, string $type): string { - $file_path = self::$backup_dir . '/' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '_' . $type . '.sql'; + $date = new DateTime; + $file_path = self::$backup_dir . '/' . Config::$database . '_' . $date->format('Y-m-d') . '_' . $type . '.sql'; // les versions de mysql sont comme ci: 8.0.36 // celles de mariadb sont comme ça: 10.11.6-MariaDB @@ -42,6 +43,9 @@ class Backup unlink($file_path); } $command->mustRun(); // comme run() mais lance une ProcessFailedException + + //$file_path = self::gzipCompress($file_path); // '.gz' ajouté à la fin + chmod($file_path, 0666); return $file_path; } @@ -50,18 +54,22 @@ class Backup unlink($tmp); self::cleanBackups(); } + } - // compression gzip (gros gain de place sur le serveur), nécessite l'extension zlib - /*try{ + // compression gzip (gros gain de place sur le serveur), nécessite l'extension zlib + static public function gzipCompress(string $file_path): string + { + try{ file_put_contents( $file_path . '.gz', gzencode(file_get_contents($file_path), 5), // plus rapide que 9 et taille identique d'après mes essais ); - return $file_path . '.gz'; + unlink($file_path); + $file_path .= '.gz'; } finally{ - unlink($file_path); - }*/ + return $file_path; + } } static public function getBackupList(): array @@ -96,14 +104,14 @@ class Backup $list_by_database[$exploded[0]][] = $file; } - $today = new DateTime()->format('Y-m-d'); + $date = new DateTime; foreach($sorted_files as $db_name => $from_one_database){ // on garde une "quantité à garder" par BDD if(count($from_one_database) > self::$amount_to_keep){ // nettoyage 1 - foreach($from_one_database as $date => $with_same_date){ + foreach($from_one_database as $date_key => $with_same_date){ // pas touche à aujourd'hui - if($date != $today){ + if($date_key != $date->format('Y-m-d')){ self::cleanBackupsByPriority($with_same_date); } } @@ -150,7 +158,8 @@ class Backup static public function restoreDatabase(EntityManager $entityManager, string $file_name): void { // création d'un backup de sécurité non écrasable - if(!file_exists(self::$backup_dir . '/' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '_before-restore.sql')){ + $date = new DateTime; + if(!file_exists(self::$backup_dir . '/' . Config::$database . '_' . $date->format('Y-m-d') . '_before-restore.sql')){ Backup::mySQLdump($entityManager, 'before-restore'); } @@ -176,7 +185,6 @@ class Backup password=" . Config::$password . "\n host=" . Config::$db_host . "\n"); - $command = new Process([ $engine, // mariadb ou mysql @@ -185,7 +193,6 @@ class Backup ]); $command->setInput(file_get_contents(Backup::$backup_dir . '/' . $file_name)); // l'entrée < - try{ // tout effacer diff --git a/src/service/Installation.php b/src/service/Installation.php index 995ed4a..100001d 100644 --- a/src/service/Installation.php +++ b/src/service/Installation.php @@ -12,12 +12,12 @@ class Installation // ajouter plus tard zlib pour la compression des backups foreach($extensions as $extension){ if(!extension_loaded($extension)){ - echo("

l'extension " . $extension . " est manquante.

"); + echo("

L'extension " . $extension . " est manquante.

"); $flag = true; } } if(!class_exists(DOMDocument::class)){ // théoriquement plus fiable que extension_loaded() - echo("

l'extension dom est manquante.

"); + echo("

L'extension dom est manquante.

"); $flag = true; } -- cgit v1.2.3