diff options
| author | polo <ordipolo@gmx.fr> | 2026-06-25 23:17:35 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-06-25 23:41:01 +0200 |
| commit | 0ee622e0adafad3ee7a5afbcdcc519c7cca544d6 (patch) | |
| tree | 7e19a8a9569b98948e9b00a2cde68318e5e10652 | |
| parent | 63fb854560c2a437ebd72f3ab8b97349fd3eb61d (diff) | |
| download | cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.tar.gz cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.tar.bz2 cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.zip | |
restauration BDD console bin/restore_db.php
| -rwxr-xr-x[-rw-r--r--] | bin/mysqldump.php | 2 | ||||
| -rwxr-xr-x | bin/restore_db.php | 82 | ||||
| -rw-r--r-- | src/controller/ViewController.php | 2 | ||||
| -rw-r--r-- | src/model/entities/Node.php | 6 | ||||
| -rw-r--r-- | src/service/Backup.php | 2 |
5 files changed, 91 insertions, 3 deletions
diff --git a/bin/mysqldump.php b/bin/mysqldump.php index b7ae3ef..7d8e960 100644..100755 --- a/bin/mysqldump.php +++ b/bin/mysqldump.php | |||
| @@ -10,4 +10,4 @@ Config::load('../config/config.ini'); | |||
| 10 | require '../src/model/doctrine-bootstrap.php'; | 10 | require '../src/model/doctrine-bootstrap.php'; |
| 11 | 11 | ||
| 12 | $file_name = Backup::mySQLdump($entityManager, 'console'); // créer un nouveau backup | 12 | $file_name = Backup::mySQLdump($entityManager, 'console'); // créer un nouveau backup |
| 13 | echo realpath($file_name) . "\n"; \ No newline at end of file | 13 | echo "Un backup a été créé:\n" . realpath($file_name) . "\n"; \ No newline at end of file |
diff --git a/bin/restore_db.php b/bin/restore_db.php new file mode 100755 index 0000000..baa1e67 --- /dev/null +++ b/bin/restore_db.php | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | #!/usr/bin/env php | ||
| 2 | <?php | ||
| 3 | // bin/restore_db.php | ||
| 4 | |||
| 5 | declare(strict_types=1); | ||
| 6 | |||
| 7 | if($argc === 2){ | ||
| 8 | if(pathinfo($argv[1])['extension'] !== 'sql'){ | ||
| 9 | try{ | ||
| 10 | throw new LogicException("Erreur, charger un fichier au format SQL"); | ||
| 11 | } | ||
| 12 | catch(LogicException $e){ | ||
| 13 | echo $e->getMessage() . "\n"; | ||
| 14 | die; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | $file_name = pathinfo($argv[1])['basename']; | ||
| 18 | $file_data = '../' . file_get_contents($argv[1]); | ||
| 19 | } | ||
| 20 | |||
| 21 | chdir(dirname(__FILE__)); // /chemin/absolu/bin/restore_db.php | ||
| 22 | require "../vendor/autoload.php"; | ||
| 23 | Config::load('../config/config.ini'); | ||
| 24 | require '../src/model/doctrine-bootstrap.php'; | ||
| 25 | |||
| 26 | // backup sur le système de fichiers en paramètre | ||
| 27 | if($argc === 2){ | ||
| 28 | // enregistrer le fichier | ||
| 29 | if(!file_put_contents(Backup::$backup_dir . '/' . $file_name, $file_data)){ | ||
| 30 | try{ | ||
| 31 | throw new RuntimeException("Erreur, impossible d'enregistrer le fichier\n"); | ||
| 32 | } | ||
| 33 | catch(RuntimeException $e){ | ||
| 34 | echo $e->getMessage() . "\n"; | ||
| 35 | die; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | echo "Fichier SQL " . pathinfo($file_name)['basename'] . " enregistré dans var/backups/\n"; | ||
| 40 | } | ||
| 41 | // choix parmi les backups sur le serveur | ||
| 42 | elseif($argc === 1){ | ||
| 43 | // obtenir des fichiers | ||
| 44 | try{ | ||
| 45 | $backup_array = Backup::getBackupList(); | ||
| 46 | } | ||
| 47 | catch(RuntimeException $e){ | ||
| 48 | $backup_array = []; | ||
| 49 | echo $e->getMessage() . "\n"; | ||
| 50 | } | ||
| 51 | |||
| 52 | $backup_files = []; | ||
| 53 | $j = 0; | ||
| 54 | for($i = count($backup_array) - 1; $i >= 0; $i--){ | ||
| 55 | echo ($j + 1) . '. ' . $backup_array[$i] . "\n"; | ||
| 56 | $backup_files[$j] = $backup_array[$i]; | ||
| 57 | $j++; | ||
| 58 | } | ||
| 59 | |||
| 60 | // saisie utilisateur | ||
| 61 | $nb = (int)readline("Choisissez un fichier par son numéro : ") - 1; | ||
| 62 | |||
| 63 | if($nb < 0 || $nb >= count($backup_files)){ | ||
| 64 | echo "Erreur, saisir un numéro dans le choix proposé.\n"; | ||
| 65 | die; | ||
| 66 | } | ||
| 67 | $file_name = $backup_files[$nb]; | ||
| 68 | } | ||
| 69 | else{ | ||
| 70 | echo "Erreur: trop de paramètres\n"; | ||
| 71 | die; | ||
| 72 | } | ||
| 73 | |||
| 74 | try{ | ||
| 75 | Backup::restoreDatabase($entityManager, $file_name); | ||
| 76 | } | ||
| 77 | catch(RuntimeException $e){ | ||
| 78 | echo $e->getMessage() . "\n"; | ||
| 79 | die; | ||
| 80 | } | ||
| 81 | echo "La base de donnée a été restaurée avec le backup: " . $file_name . "\n"; | ||
| 82 | echo "En cas d'erreur, vous pouvez revenir en arrière en réalisant une nouvelle restauration avec le fichier:\n" . Config::$database . '_' . (new DateTime)->format('Y-m-d') . "_before-restore.sql\n"; | ||
diff --git a/src/controller/ViewController.php b/src/controller/ViewController.php index 1acb19a..43db6a6 100644 --- a/src/controller/ViewController.php +++ b/src/controller/ViewController.php | |||
| @@ -10,7 +10,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse; | |||
| 10 | 10 | ||
| 11 | class ViewController | 11 | class ViewController |
| 12 | { | 12 | { |
| 13 | static function getWebPage(EntityManager $entityManager, Request $request): RedirectResponse | 13 | static function getWebPage(EntityManager $entityManager, Request $request): Response |
| 14 | { | 14 | { |
| 15 | /* 1/ 1er contrôle des paramètres */ | 15 | /* 1/ 1er contrôle des paramètres */ |
| 16 | 16 | ||
diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index c3e4ec3..fb2d867 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php | |||
| @@ -98,6 +98,8 @@ class Node | |||
| 98 | { | 98 | { |
| 99 | $this->page = $page; | 99 | $this->page = $page; |
| 100 | }*/ | 100 | }*/ |
| 101 | |||
| 102 | // utiliser l'interface de NodeData et EmailForm | ||
| 101 | public function getArticle(): Article | 103 | public function getArticle(): Article |
| 102 | { | 104 | { |
| 103 | return $this->article; | 105 | return $this->article; |
| @@ -106,9 +108,13 @@ class Node | |||
| 106 | { | 108 | { |
| 107 | $this->article = $article; | 109 | $this->article = $article; |
| 108 | }*/ | 110 | }*/ |
| 111 | |||
| 109 | // une interface serait cool! | 112 | // une interface serait cool! |
| 113 | // OU possible un "héritage doctrine", faire en sorte que Node soit la mère de NodeData, EmailForm, Article... | ||
| 114 | // une seulle instance est matérialisée par plusieurs tables en même temps | ||
| 110 | public function getNodeData(): NodeData|EmailForm|null | 115 | public function getNodeData(): NodeData|EmailForm|null |
| 111 | { | 116 | { |
| 117 | // limite du polymorphisme avec doctrine => impossible d'avoir une unique variable $node_data | ||
| 112 | return $this->name_node === 'form' ? $this->email_form : $this->node_data; | 118 | return $this->name_node === 'form' ? $this->email_form : $this->node_data; |
| 113 | } | 119 | } |
| 114 | public function getChildren(): array | 120 | public function getChildren(): array |
diff --git a/src/service/Backup.php b/src/service/Backup.php index e29ff64..60d1b23 100644 --- a/src/service/Backup.php +++ b/src/service/Backup.php | |||
| @@ -176,7 +176,7 @@ class Backup | |||
| 176 | } | 176 | } |
| 177 | // sécurité cas pas normal | 177 | // sécurité cas pas normal |
| 178 | if(empty($tables)){ | 178 | if(empty($tables)){ |
| 179 | throw new Exception("Aucune table à supprimer"); | 179 | throw new RuntimeException("Aucune table à supprimer"); |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | $tmp = tempnam('../var', 'tmp_db_codes_'); // crée un fichier avec un nom aléatoire et des droits 600 (concurrence) | 182 | $tmp = tempnam('../var', 'tmp_db_codes_'); // crée un fichier avec un nom aléatoire et des droits 600 (concurrence) |
