backup.class.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $backupFile = self::dumpMysql($newFolder);
  11. rename($backupFile, $newFolder . "/" . $backupFile);
  12. file::copyFolder("/" . DIR_DATAS, $newFolder . "/datas");
  13. $localZip = self::zip($newFolder, DIR_TEMP . $nameNewFolder);
  14. self::deleteFolder($newFolder);
  15. rename($localZip, DIR_DATAS_BACKUP . $nameNewFolder . ".zip");
  16. } catch (Exception $ex) {
  17. return FALSE;
  18. }
  19. }
  20. public static function deleteFolder(string $_dir)
  21. {
  22. if(is_dir($_dir)){
  23. $command = "rm -r " . $_dir;
  24. try {
  25. system($command);
  26. return TRUE;
  27. } catch (Exception $ex) {
  28. return FALSE;
  29. }
  30. }
  31. }
  32. public static function dumpMysql(string $_dir)
  33. {
  34. $backupFile = DB_NAME . ".Mysql";
  35. $command = "mysqldump --opt -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " > " . $backupFile;
  36. try {
  37. system($command);
  38. return $backupFile;
  39. } catch (Exception $ex) {
  40. return FALSE;
  41. }
  42. }
  43. public static function dumpMysqlRestore(string $_backupFile)
  44. {
  45. $command = "mysql -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " < " . $_backupFile;
  46. try {
  47. system($command);
  48. } catch (Exception $ex) {
  49. return FALSE;
  50. }
  51. }
  52. public static function filesRestore()
  53. {
  54. json::create("salaries");
  55. json::create("excel");
  56. json::create("excel-proweb");
  57. json::create("events");
  58. json::create("users");
  59. json::create("salaries-proweb");
  60. }
  61. public static function zip(string $_dir, string $_name)
  62. {
  63. $zip = new ZipArchive();
  64. $zip_name = $_name . ".zip";
  65. $zip->open($zip_name, ZipArchive::CREATE);
  66. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
  67. foreach ($files as $file) {
  68. if (!$file->isDir()) {
  69. $filePath = $file->getRealPath();
  70. $relativePath = substr($filePath, strlen($_dir));
  71. $zip->addFile($filePath, $relativePath);
  72. }
  73. }
  74. $zip->close();
  75. return $zip_name;
  76. }
  77. }