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

206 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Database;
use App\Models\ScheduledDatabaseBackup;
use Illuminate\Support\Collection;
2024-08-31 17:31:01 +00:00
use Illuminate\Support\Facades\Auth;
2024-09-23 17:51:31 +00:00
use Livewire\Component;
class BackupExecutions extends Component
{
public ?ScheduledDatabaseBackup $backup = null;
2024-09-23 17:51:31 +00:00
2024-08-16 19:06:36 +00:00
public $database;
2024-09-23 17:51:31 +00:00
public ?Collection $executions;
public int $executions_count = 0;
public int $skip = 0;
public int $defaultTake = 10;
public bool $showNext = false;
public bool $showPrev = false;
public int $currentPage = 1;
2024-09-23 17:51:31 +00:00
2023-10-24 12:31:28 +00:00
public $setDeletableBackup;
2024-06-10 20:43:34 +00:00
public $delete_backup_s3 = false;
2024-09-23 17:51:31 +00:00
public $delete_backup_sftp = false;
2024-08-31 17:31:01 +00:00
public function getListeners()
{
2024-08-31 17:31:01 +00:00
$userId = Auth::id();
2024-06-10 20:43:34 +00:00
return [
"echo-private:team.{$userId},BackupCreated" => 'refreshBackupExecutions',
];
}
2024-04-29 07:38:45 +00:00
public function cleanupFailed()
{
if ($this->backup) {
2024-05-24 15:06:26 +00:00
$this->backup->executions()->where('status', 'failed')->delete();
$this->refreshBackupExecutions();
$this->dispatch('success', 'Failed backups cleaned up.');
}
2024-04-29 07:38:45 +00:00
}
2024-06-10 20:43:34 +00:00
public function cleanupDeleted()
{
if ($this->backup) {
$deletedCount = $this->backup->executions()->where('local_storage_deleted', true)->count();
if ($deletedCount > 0) {
$this->backup->executions()->where('local_storage_deleted', true)->delete();
$this->refreshBackupExecutions();
$this->dispatch('success', "Cleaned up {$deletedCount} backup entries deleted from local storage.");
} else {
$this->dispatch('info', 'No backup entries found that are deleted from local storage.');
}
}
}
public function deleteBackup($executionId, $password, $selectedActions = [])
2023-10-24 12:31:28 +00:00
{
if (! verifyPasswordConfirmation($password, $this)) {
return 'The provided password is incorrect.';
2024-08-31 17:31:01 +00:00
}
$execution = $this->backup->executions()->where('id', $executionId)->first();
2023-10-24 12:31:28 +00:00
if (is_null($execution)) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Backup execution not found.');
2024-09-23 17:51:31 +00:00
2023-10-24 12:31:28 +00:00
return;
}
2024-08-31 17:31:01 +00:00
$server = $execution->scheduledDatabaseBackup->database->getMorphClass() === \App\Models\ServiceDatabase::class
? $execution->scheduledDatabaseBackup->database->service->destination->server
: $execution->scheduledDatabaseBackup->database->destination->server;
2024-08-31 17:31:01 +00:00
2025-01-13 16:21:03 +00:00
try {
if ($execution->filename) {
deleteBackupsLocally($execution->filename, $server);
2024-08-31 17:31:01 +00:00
if ($this->delete_backup_s3 && $execution->scheduledDatabaseBackup->s3) {
deleteBackupsS3($execution->filename, $execution->scheduledDatabaseBackup->s3);
}
2025-01-13 16:21:03 +00:00
}
2024-08-31 17:31:01 +00:00
2025-01-13 16:21:03 +00:00
$execution->delete();
$this->dispatch('success', 'Backup deleted.');
$this->refreshBackupExecutions();
} catch (\Exception $e) {
$this->dispatch('error', 'Failed to delete backup: '.$e->getMessage());
return true;
2025-01-13 16:21:03 +00:00
}
return true;
2023-10-24 12:31:28 +00:00
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function download_file($exeuctionId)
2023-10-25 07:28:26 +00:00
{
2024-04-10 13:00:46 +00:00
return redirect()->route('download.backup', $exeuctionId);
2023-10-25 07:28:26 +00:00
}
2024-06-10 20:43:34 +00:00
public function refreshBackupExecutions(): void
{
$this->loadExecutions();
}
public function reloadExecutions()
{
$this->loadExecutions();
}
public function previousPage(?int $take = null)
{
if ($take) {
$this->skip = $this->skip - $take;
}
$this->skip = $this->skip - $this->defaultTake;
if ($this->skip < 0) {
$this->showPrev = false;
$this->skip = 0;
}
$this->updateCurrentPage();
$this->loadExecutions();
}
public function nextPage(?int $take = null)
{
if ($take) {
$this->skip = $this->skip + $take;
}
$this->showPrev = true;
$this->updateCurrentPage();
$this->loadExecutions();
}
private function loadExecutions()
{
if ($this->backup && $this->backup->exists) {
['executions' => $executions, 'count' => $count] = $this->backup->executionsPaginated($this->skip, $this->defaultTake);
$this->executions = $executions;
$this->executions_count = $count;
} else {
$this->executions = collect([]);
$this->executions_count = 0;
}
$this->showMore();
}
private function showMore()
{
if ($this->executions->count() !== 0) {
$this->showNext = true;
if ($this->executions->count() < $this->defaultTake) {
$this->showNext = false;
}
return;
2024-05-24 15:06:26 +00:00
}
}
2024-08-16 19:06:36 +00:00
private function updateCurrentPage()
{
$this->currentPage = intval($this->skip / $this->defaultTake) + 1;
}
2024-08-16 19:06:36 +00:00
public function mount(ScheduledDatabaseBackup $backup)
{
$this->backup = $backup;
$this->database = $backup->database;
$this->updateCurrentPage();
$this->loadExecutions();
2024-08-16 19:06:36 +00:00
}
public function server()
{
if ($this->database) {
$server = null;
if ($this->database instanceof \App\Models\ServiceDatabase) {
$server = $this->database->service->destination->server;
} elseif ($this->database->destination && $this->database->destination->server) {
$server = $this->database->destination->server;
}
if ($server) {
return $server;
}
}
2024-09-23 17:51:31 +00:00
2024-08-16 19:06:36 +00:00
return null;
}
2024-08-31 17:31:01 +00:00
public function render()
{
return view('livewire.project.database.backup-executions');
2024-08-31 17:31:01 +00:00
}
}