Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 25, 2024
1 parent bdde553 commit 75807c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
12 changes: 6 additions & 6 deletions web/app/Console/Commands/RunBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
39 changes: 26 additions & 13 deletions web/app/Models/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down

0 comments on commit 75807c3

Please sign in to comment.