BackupController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace application\controllers;
  3. class BackupController {
  4. // BackupFile: YYYY-MM-DD-prod_domain.tar.gz
  5. // DBBackupFile: YYYY-MM-DD-prod_domain.sql
  6. public static function listBackups($data):void {
  7. $username = $data['username'] ?? '';
  8. $domain = $data['domain'] ?? '';
  9. if (empty($username) || empty($domain)) {
  10. error_log("listBackups: ERROR: No username or domain provided");
  11. http_response_code(400);
  12. echo json_encode(['error' => 'Missing required parameters: username or domainname']);
  13. return;
  14. }
  15. $backupDir = "/home/$username/backups";
  16. error_log(" Starting function listbackups for " . $username . " and " . $domain . " : BackupDir " . $backupDir);
  17. if (!is_dir($backupDir)) {
  18. error_log(" ERROR: Backup Dir does not exist");
  19. http_response_code(404);
  20. echo json_encode(['error' => 'Backup directory not found']);
  21. return;
  22. }
  23. $files = array_diff(scandir($backupDir), ['.', '..']);
  24. $backupList = [];
  25. foreach ($files as $file) {
  26. if (preg_match('/^(\d{4}-\d{2}-\d{2})-' . preg_quote($domain, '/') . '\.tar\.gz$/', $file, $matches)) {
  27. $originalDate = $matches[1];
  28. $swissDate = DateTime::createFromFormat('Y-m-d', $originalDate)->format('d.m.Y');
  29. $sizeMB = round(filesize("$backupDir/$file") / (1024 * 1024), 2);
  30. $backupList[] = [
  31. 'backup_date' => $originalDate,
  32. 'swiss_date' => $swissDate,
  33. 'size_mb' => $sizeMB,
  34. 'filename' => $file
  35. ];
  36. }
  37. }
  38. echo json_encode(['backups' => $backupList]);
  39. }
  40. // BackupFile: YYYY-MM-DD-prod_domain.tar.gz
  41. // DBBackupFile: YYYY-MM-DD-prod_domain.sql
  42. public static function restoreBackup($data):void {
  43. $username = $data['username'] ?? '';
  44. $domain = $data['domain'] ?? '';
  45. if (empty($username) || empty($data['backup_date']) || empty($domain)) {
  46. error_log("restoreBackup: ERROR: No username, domain provided or backup_date provided");
  47. http_response_code(400);
  48. echo json_encode(['error' => 'Missing required parameters']);
  49. return;
  50. }
  51. error_log(" Starting function restoreBackup for " . $username . " and " . $domain . " : Backup-Date " . $data['backup_date']);
  52. $backupDir = "/home/$username/backups";
  53. $webrootDir = "/home/$username/prod.$domain";
  54. $backupFile = $data['backup_date'] ?? '';
  55. $backupFile = $backupFile . "-" . $domain . ".tar.gz";
  56. $dbBackupFile = $backupDir ." /" . $data['backup_date'] . "-prod_" . str_replace(['-', '.'], ["_", "_"], $domain);
  57. $prodDatabaseName = $username . "_prod_";
  58. $prodDatabaseName = $prodDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain);
  59. if (!file_exists($backupFile)) {
  60. error_log("restoreBackup: ERROR: Backup file $backupFile does not exist");
  61. http_response_code(404);
  62. echo json_encode(['error' => 'Web Backup file not found']);
  63. return;
  64. }
  65. if (!file_exists($dbBackupFile)) {
  66. error_log("restoreBackup: ERROR: Database backup file $dbBackupFile does not exist");
  67. http_response_code(404);
  68. echo json_encode(['error' => 'Database Backup file not found']);
  69. return;
  70. }
  71. exec("sudo /usr/bin/tar -xzf $backupFile -C $webrootDir", $output, $returnCode);
  72. if ($returnCode !== 0) {
  73. error_log("restoreBackup: ERROR: Failed to restore webroot, details' =>" . implode("\n", $output));
  74. http_response_code(500);
  75. echo json_encode(['error' => 'Failed to restore webroot', 'details' => implode("\n", $output)]);
  76. return;
  77. }
  78. exec("sudo /usr/bin/mysql $prodDatabaseName < $dbBackupFile", $output, $returnCode);
  79. if ($returnCode !== 0) {
  80. error_log("restoreBackup: ERROR: Failed to restore database, details' =>" . implode("\n", $output));
  81. http_response_code(500);
  82. echo json_encode(['error' => 'Failed to restore database', 'details' => implode("\n", $output)]);
  83. return;
  84. }
  85. echo json_encode(['success' => 'Backup restored successfully']);
  86. }
  87. }