diff options
Diffstat (limited to 'src/service/Backup.php')
| -rw-r--r-- | src/service/Backup.php | 31 |
1 files changed, 19 insertions, 12 deletions
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 | |||
| 13 | 13 | ||
| 14 | static public function mySQLdump(EntityManager $entityManager, string $type): string | 14 | static public function mySQLdump(EntityManager $entityManager, string $type): string |
| 15 | { | 15 | { |
| 16 | $file_path = self::$backup_dir . '/' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '_' . $type . '.sql'; | 16 | $date = new DateTime; |
| 17 | $file_path = self::$backup_dir . '/' . Config::$database . '_' . $date->format('Y-m-d') . '_' . $type . '.sql'; | ||
| 17 | 18 | ||
| 18 | // les versions de mysql sont comme ci: 8.0.36 | 19 | // les versions de mysql sont comme ci: 8.0.36 |
| 19 | // celles de mariadb sont comme ça: 10.11.6-MariaDB | 20 | // celles de mariadb sont comme ça: 10.11.6-MariaDB |
| @@ -42,6 +43,9 @@ class Backup | |||
| 42 | unlink($file_path); | 43 | unlink($file_path); |
| 43 | } | 44 | } |
| 44 | $command->mustRun(); // comme run() mais lance une ProcessFailedException | 45 | $command->mustRun(); // comme run() mais lance une ProcessFailedException |
| 46 | |||
| 47 | //$file_path = self::gzipCompress($file_path); // '.gz' ajouté à la fin | ||
| 48 | |||
| 45 | chmod($file_path, 0666); | 49 | chmod($file_path, 0666); |
| 46 | return $file_path; | 50 | return $file_path; |
| 47 | } | 51 | } |
| @@ -50,18 +54,22 @@ class Backup | |||
| 50 | unlink($tmp); | 54 | unlink($tmp); |
| 51 | self::cleanBackups(); | 55 | self::cleanBackups(); |
| 52 | } | 56 | } |
| 57 | } | ||
| 53 | 58 | ||
| 54 | // compression gzip (gros gain de place sur le serveur), nécessite l'extension zlib | 59 | // compression gzip (gros gain de place sur le serveur), nécessite l'extension zlib |
| 55 | /*try{ | 60 | static public function gzipCompress(string $file_path): string |
| 61 | { | ||
| 62 | try{ | ||
| 56 | file_put_contents( | 63 | file_put_contents( |
| 57 | $file_path . '.gz', | 64 | $file_path . '.gz', |
| 58 | gzencode(file_get_contents($file_path), 5), // plus rapide que 9 et taille identique d'après mes essais | 65 | gzencode(file_get_contents($file_path), 5), // plus rapide que 9 et taille identique d'après mes essais |
| 59 | ); | 66 | ); |
| 60 | return $file_path . '.gz'; | 67 | unlink($file_path); |
| 68 | $file_path .= '.gz'; | ||
| 61 | } | 69 | } |
| 62 | finally{ | 70 | finally{ |
| 63 | unlink($file_path); | 71 | return $file_path; |
| 64 | }*/ | 72 | } |
| 65 | } | 73 | } |
| 66 | 74 | ||
| 67 | static public function getBackupList(): array | 75 | static public function getBackupList(): array |
| @@ -96,14 +104,14 @@ class Backup | |||
| 96 | $list_by_database[$exploded[0]][] = $file; | 104 | $list_by_database[$exploded[0]][] = $file; |
| 97 | } | 105 | } |
| 98 | 106 | ||
| 99 | $today = new DateTime()->format('Y-m-d'); | 107 | $date = new DateTime; |
| 100 | foreach($sorted_files as $db_name => $from_one_database){ | 108 | foreach($sorted_files as $db_name => $from_one_database){ |
| 101 | // on garde une "quantité à garder" par BDD | 109 | // on garde une "quantité à garder" par BDD |
| 102 | if(count($from_one_database) > self::$amount_to_keep){ | 110 | if(count($from_one_database) > self::$amount_to_keep){ |
| 103 | // nettoyage 1 | 111 | // nettoyage 1 |
| 104 | foreach($from_one_database as $date => $with_same_date){ | 112 | foreach($from_one_database as $date_key => $with_same_date){ |
| 105 | // pas touche à aujourd'hui | 113 | // pas touche à aujourd'hui |
| 106 | if($date != $today){ | 114 | if($date_key != $date->format('Y-m-d')){ |
| 107 | self::cleanBackupsByPriority($with_same_date); | 115 | self::cleanBackupsByPriority($with_same_date); |
| 108 | } | 116 | } |
| 109 | } | 117 | } |
| @@ -150,7 +158,8 @@ class Backup | |||
| 150 | static public function restoreDatabase(EntityManager $entityManager, string $file_name): void | 158 | static public function restoreDatabase(EntityManager $entityManager, string $file_name): void |
| 151 | { | 159 | { |
| 152 | // création d'un backup de sécurité non écrasable | 160 | // création d'un backup de sécurité non écrasable |
| 153 | if(!file_exists(self::$backup_dir . '/' . Config::$database . '_' . new DateTime()->format('Y-m-d') . '_before-restore.sql')){ | 161 | $date = new DateTime; |
| 162 | if(!file_exists(self::$backup_dir . '/' . Config::$database . '_' . $date->format('Y-m-d') . '_before-restore.sql')){ | ||
| 154 | Backup::mySQLdump($entityManager, 'before-restore'); | 163 | Backup::mySQLdump($entityManager, 'before-restore'); |
| 155 | } | 164 | } |
| 156 | 165 | ||
| @@ -176,7 +185,6 @@ class Backup | |||
| 176 | password=" . Config::$password . "\n | 185 | password=" . Config::$password . "\n |
| 177 | host=" . Config::$db_host . "\n"); | 186 | host=" . Config::$db_host . "\n"); |
| 178 | 187 | ||
| 179 | |||
| 180 | 188 | ||
| 181 | $command = new Process([ | 189 | $command = new Process([ |
| 182 | $engine, // mariadb ou mysql | 190 | $engine, // mariadb ou mysql |
| @@ -185,7 +193,6 @@ class Backup | |||
| 185 | ]); | 193 | ]); |
| 186 | $command->setInput(file_get_contents(Backup::$backup_dir . '/' . $file_name)); // l'entrée < | 194 | $command->setInput(file_get_contents(Backup::$backup_dir . '/' . $file_name)); // l'entrée < |
| 187 | 195 | ||
| 188 | |||
| 189 | 196 | ||
| 190 | try{ | 197 | try{ |
| 191 | // tout effacer | 198 | // tout effacer |
