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 /bin | |
| parent | 63fb854560c2a437ebd72f3ab8b97349fd3eb61d (diff) | |
| download | cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.tar.gz cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.tar.bz2 cms-0ee622e0adafad3ee7a5afbcdcc519c7cca544d6.zip | |
restauration BDD console bin/restore_db.php
Diffstat (limited to 'bin')
| -rwxr-xr-x[-rw-r--r--] | bin/mysqldump.php | 2 | ||||
| -rwxr-xr-x | bin/restore_db.php | 82 |
2 files changed, 83 insertions, 1 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"; | ||
