diff --git a/web/app/Console/Commands/RunBackup.php b/web/app/Console/Commands/RunBackup.php index d15db0cf..e0f956b5 100644 --- a/web/app/Console/Commands/RunBackup.php +++ b/web/app/Console/Commands/RunBackup.php @@ -30,6 +30,12 @@ class RunBackup extends Command */ public function handle() { + // Delete backups older than 7 days + $findBackupsForDeleting = Backup::where('created_at', '<', now()->subDays(7))->get(); + foreach ($findBackupsForDeleting as $backup) { + $backup->delete(); + } + // Find Hosting Subscriptions $findHostingSubscriptions = HostingSubscription::all(); if ($findHostingSubscriptions->count() > 0) { @@ -52,12 +58,6 @@ public function handle() } } - // Delete backups older than 7 days - $findBackupsForDeleting = Backup::where('created_at', '<', now()->subDays(7))->get(); - foreach ($findBackupsForDeleting as $backup) { - $backup->delete(); - } - // Check for pending backups $getPendingBackups = Backup::where('status', 'pending') ->get(); diff --git a/web/app/Models/Backup.php b/web/app/Models/Backup.php index 6517fe8e..6d66ec61 100644 --- a/web/app/Models/Backup.php +++ b/web/app/Models/Backup.php @@ -115,7 +115,7 @@ public function startBackup() $findHostingSubscription = HostingSubscription::where('id', $this->hosting_subscription_id)->first(); if ($findHostingSubscription) { - $backupFileName = Str::slug($findHostingSubscription->domain .'-'. date('Y-m-d-H-i-s')) . '.tar.gz'; + $backupFileName = Str::slug($findHostingSubscription->domain .'-'. date('Ymd-His')) . '.tar.gz'; $backupFilePath = $backupPath.'/'.$backupFileName; $backupLogFileName = 'backup.log'; @@ -134,20 +134,33 @@ public function startBackup() $shellFileContent .= 'touch ' . $backupPath. '/backup.done' . PHP_EOL; $shellFileContent .= 'rm -rf ' . $backupTempScript; - $this->path = $backupPath; - $this->filepath = $backupFilePath; - $this->status = 'processing'; - $this->queued = true; - $this->queued_at = now(); - $this->save(); - file_put_contents($backupTempScript, $shellFileContent); - shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' &'); - return [ - 'status' => 'processing', - 'message' => 'Backup started' - ]; + $pid = shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' & echo $!'); + $pid = intval($pid); + + if ($pid > 0 && is_numeric($pid)) { + + $this->path = $backupPath; + $this->filepath = $backupFilePath; + $this->status = 'processing'; + $this->queued = true; + $this->queued_at = now(); + $this->process_id = $pid; + $this->save(); + + return [ + 'status' => 'processing', + 'message' => 'Backup started' + ]; + } else { + $this->status = 'failed'; + $this->save(); + return [ + 'status' => 'failed', + 'message' => 'Backup failed to start' + ]; + } } else { $this->status = 'failed';