diff options
Diffstat (limited to 'src/service/Backup.php')
| -rw-r--r-- | src/service/Backup.php | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/src/service/Backup.php b/src/service/Backup.php index 313d1f6..63368b5 100644 --- a/src/service/Backup.php +++ b/src/service/Backup.php | |||
| @@ -8,12 +8,12 @@ use Symfony\Component\Process\Process; // protection injection dans le shell | |||
| 8 | 8 | ||
| 9 | class Backup | 9 | class Backup |
| 10 | { | 10 | { |
| 11 | static private string $backup_dir = '../var/backups'; | 11 | static public string $backup_dir = '../var/backups'; |
| 12 | static private int $amount_to_keep = 30; | 12 | static private int $amount_to_keep = 30; |
| 13 | 13 | ||
| 14 | static public function mySQLdump(EntityManager $entityManager): string | 14 | static public function mySQLdump(EntityManager $entityManager, string $type): string |
| 15 | { | 15 | { |
| 16 | $file_path = self::$backup_dir . '/db_' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '.sql'; | 16 | $file_path = self::$backup_dir . '/' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '_' . $type . '.sql'; |
| 17 | 17 | ||
| 18 | // les versions de mysql sont comme ci: 8.0.36 | 18 | // les versions de mysql sont comme ci: 8.0.36 |
| 19 | // celles de mariadb sont comme ça: 10.11.6-MariaDB | 19 | // celles de mariadb sont comme ça: 10.11.6-MariaDB |
| @@ -89,4 +89,64 @@ class Backup | |||
| 89 | unlink($file); | 89 | unlink($file); |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | |||
| 93 | static public function restoreDatabase(EntityManager $entityManager, string $file_name): void | ||
| 94 | { | ||
| 95 | // backup de sécurité | ||
| 96 | Backup::mySQLdump($entityManager, 'before-restore'); | ||
| 97 | |||
| 98 | $version = $entityManager->getConnection()->fetchOne('SELECT VERSION()'); | ||
| 99 | $engine = stripos($version, 'mariadb') !== false ? 'mariadb' : 'mysql'; | ||
| 100 | |||
| 101 | // choisir les tables à restaurer | ||
| 102 | $tables = $entityManager->getConnection()->createSchemaManager()->listTableNames(); | ||
| 103 | foreach($tables as $key => $elem){ | ||
| 104 | if($elem === TABLE_PREFIX . 'user'){ | ||
| 105 | unset($tables[$key]); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | // sécurité cas pas normal | ||
| 109 | if(empty($tables)){ | ||
| 110 | throw new Exception("Aucune table à supprimer"); | ||
| 111 | } | ||
| 112 | |||
| 113 | $tmp = tempnam('../var', 'tmp_db_codes_'); // crée un fichier avec un nom aléatoire et des droits 600 (concurrence) | ||
| 114 | file_put_contents($tmp, | ||
| 115 | "[client]\n | ||
| 116 | user=" . Config::$user . "\n | ||
| 117 | password=" . Config::$password . "\n | ||
| 118 | host=" . Config::$db_host . "\n"); | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | $command = new Process([ | ||
| 123 | $engine, // mariadb ou mysql | ||
| 124 | '--defaults-extra-file=' . $tmp, // pour ne pas enregistrer les codes dans l'historique de la console ou dans les processus de l'OS | ||
| 125 | Config::$database | ||
| 126 | ]); | ||
| 127 | $command->setInput(file_get_contents(Backup::$backup_dir . '/' . $file_name)); // l'entrée < | ||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | try{ | ||
| 132 | // tout effacer | ||
| 133 | $tables_with_backquotes = array_map(fn($t) => '`' . $t . '`', $tables); | ||
| 134 | $sql = "SET FOREIGN_KEY_CHECKS=0; DROP TABLE " . implode(', ', $tables_with_backquotes) . "; SET FOREIGN_KEY_CHECKS=1;"; | ||
| 135 | $entityManager->getConnection()->executeStatement($sql); | ||
| 136 | |||
| 137 | // la table user restante va poser problème | ||
| 138 | $entityManager->getConnection()->executeStatement('RENAME TABLE `' . TABLE_PREFIX . 'user` TO `' . TABLE_PREFIX . 'user_dont_touch`;'); | ||
| 139 | |||
| 140 | // restaurer | ||
| 141 | $command->mustRun(); // comme run() mais lance une ProcessFailedException | ||
| 142 | |||
| 143 | // remettre table user comme avant | ||
| 144 | $entityManager->getConnection()->executeStatement('DROP TABLE `' . TABLE_PREFIX . 'user`;'); | ||
| 145 | $entityManager->getConnection()->executeStatement('RENAME TABLE `' . TABLE_PREFIX . 'user_dont_touch` TO `' . TABLE_PREFIX . 'user`;'); | ||
| 146 | } | ||
| 147 | finally{ | ||
| 148 | // exécuté même quand situé après "return" | ||
| 149 | unlink($tmp); | ||
| 150 | } | ||
| 151 | } | ||
| 92 | } \ No newline at end of file | 152 | } \ No newline at end of file |
