coolify/app/Livewire/Project/Database/BackupExecutions.php

65 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Database;
use App\Models\ScheduledDatabaseBackup;
use Livewire\Component;
class BackupExecutions extends Component
{
public ?ScheduledDatabaseBackup $backup = null;
2024-06-10 22:43:34 +02:00
2023-12-14 15:01:19 +01:00
public $executions = [];
2024-06-10 22:43:34 +02:00
2023-10-24 14:31:28 +02:00
public $setDeletableBackup;
2024-06-10 22:43:34 +02:00
public function getListeners()
{
$userId = auth()->user()->id;
2024-06-10 22:43:34 +02:00
return [
"echo-private:team.{$userId},BackupCreated" => 'refreshBackupExecutions',
2024-06-10 22:43:34 +02:00
'deleteBackup',
];
}
2024-04-29 09:38:45 +02:00
public function cleanupFailed()
{
if ($this->backup) {
2024-05-24 17:06:26 +02:00
$this->backup->executions()->where('status', 'failed')->delete();
$this->refreshBackupExecutions();
$this->dispatch('success', 'Failed backups cleaned up.');
}
2024-04-29 09:38:45 +02:00
}
2024-06-10 22:43:34 +02:00
2023-10-24 14:31:28 +02:00
public function deleteBackup($exeuctionId)
{
$execution = $this->backup->executions()->where('id', $exeuctionId)->first();
if (is_null($execution)) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Backup execution not found.');
2024-06-10 22:43:34 +02:00
2023-10-24 14:31:28 +02:00
return;
}
2023-11-07 12:11:47 +01:00
if ($execution->scheduledDatabaseBackup->database->getMorphClass() === 'App\Models\ServiceDatabase') {
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->service->destination->server);
} else {
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->destination->server);
}
2023-10-24 14:31:28 +02:00
$execution->delete();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Backup deleted.');
$this->refreshBackupExecutions();
2023-10-24 14:31:28 +02:00
}
2024-06-10 22:43:34 +02:00
2024-04-10 15:00:46 +02:00
public function download_file($exeuctionId)
2023-10-25 09:28:26 +02:00
{
2024-04-10 15:00:46 +02:00
return redirect()->route('download.backup', $exeuctionId);
2023-10-25 09:28:26 +02:00
}
2024-06-10 22:43:34 +02:00
public function refreshBackupExecutions(): void
{
2024-05-24 17:06:26 +02:00
if ($this->backup) {
2024-06-06 10:46:19 +02:00
$this->executions = $this->backup->executions()->get()->sortBy('created_at');
2024-05-24 17:06:26 +02:00
}
}
}