backup.class.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. class backup
  3. {
  4. public static function create()
  5. {
  6. try {
  7. $nameNewFolder = date("Ymd-His");
  8. $newFolder = "/" . DIR_TEMP . $nameNewFolder;
  9. mkdir($newFolder, 0777);
  10. self::dumpMysql($newFolder);
  11. file::copyFolder("/" . DIR_DATAS, $newFolder . "/datas");
  12. $localZip = file::zip($newFolder, DIR_TEMP . $nameNewFolder);
  13. file::deleteFolder($newFolder);
  14. rename($localZip, DIR_BACKUP . $nameNewFolder . ".zip");
  15. return TRUE;
  16. } catch (Exception $ex) {
  17. return FALSE;
  18. }
  19. }
  20. public static function restore(string $_zip)
  21. {
  22. $tmpZip = str_replace(DIR_BACKUP, DIR_TEMP, $_zip);
  23. $tmpDatas = date("Ymd-His") . "-datas";
  24. copy($_zip, $tmpZip);
  25. $folder = file::unzip($tmpZip, DIR_TEMP);
  26. if(self::dumpMysqlRestore(DIR_TEMP . $folder . "/" . DB_NAME . ".Mysql")){
  27. rename(DIR_DATAS, DIR_TEMP . $tmpDatas);
  28. file::copyFolder(DIR_TEMP . $folder . "/datas", DIR_DATAS);
  29. if(is_dir(DIR_DATAS)){
  30. file::deleteFolder(DIR_TEMP . $tmpDatas);
  31. unlink($tmpZip);
  32. file::deleteFolder(DIR_TEMP . $folder);
  33. }
  34. return TRUE;
  35. }
  36. return FALSE;
  37. }
  38. public static function dumpMysql(string $_dir)
  39. {
  40. $backupFile = DB_NAME . ".Mysql";
  41. $command = "mysqldump --opt -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " > " . $_dir . "/" . $backupFile;
  42. try {
  43. system($command);
  44. return $backupFile;
  45. } catch (Exception $ex) {
  46. return FALSE;
  47. }
  48. }
  49. public static function dumpMysqlRestore(string $_backupFile)
  50. {
  51. $command = "mysql -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " < " . $_backupFile;
  52. try {
  53. system($command);
  54. return TRUE;
  55. } catch (Exception $ex) {
  56. return FALSE;
  57. }
  58. }
  59. public static function jsonRestore()
  60. {
  61. json::create("salaries");
  62. json::create("excel");
  63. json::create("excel-proweb");
  64. json::create("events");
  65. json::create("users");
  66. json::create("salaries-proweb");
  67. }
  68. }