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

85 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Database;
use App\Models\ScheduledDatabaseBackup;
2024-05-24 15:28:05 +00:00
use Illuminate\Support\Collection;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
class CreateScheduledBackup extends Component
{
#[Validate(['required', 'string'])]
public $frequency;
2024-06-10 20:43:34 +00:00
#[Validate(['required', 'boolean'])]
public bool $saveToS3 = false;
2024-06-10 20:43:34 +00:00
#[Locked]
public $database;
2024-06-10 20:43:34 +00:00
public bool $enabled = true;
#[Validate(['nullable', 'integer'])]
public ?int $s3StorageId = null;
2024-06-10 20:43:34 +00:00
public Collection $definedS3s;
2024-06-10 20:43:34 +00:00
2023-11-07 11:11:47 +00:00
public function mount()
{
try {
$this->definedS3s = currentTeam()->s3s;
if ($this->definedS3s->count() > 0) {
$this->s3StorageId = $this->definedS3s->first()->id;
}
} catch (\Throwable $e) {
return handleError($e, $this);
2023-10-20 07:38:13 +00:00
}
}
public function submit()
{
try {
$this->validate();
$isValid = validate_cron_expression($this->frequency);
2024-06-10 20:43:34 +00:00
if (! $isValid) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Invalid Cron / Human expression.');
2024-06-10 20:43:34 +00:00
return;
}
2023-10-13 14:32:59 +00:00
$payload = [
'enabled' => true,
'frequency' => $this->frequency,
'save_s3' => $this->saveToS3,
's3_storage_id' => $this->s3StorageId,
'database_id' => $this->database->id,
'database_type' => $this->database->getMorphClass(),
2023-08-22 15:44:49 +00:00
'team_id' => currentTeam()->id,
2023-10-13 14:32:59 +00:00
];
2023-10-13 14:32:59 +00:00
if ($this->database->type() === 'standalone-postgresql') {
$payload['databases_to_backup'] = $this->database->postgres_db;
2024-06-10 20:43:34 +00:00
} elseif ($this->database->type() === 'standalone-mysql') {
2023-10-24 12:31:28 +00:00
$payload['databases_to_backup'] = $this->database->mysql_database;
2024-06-10 20:43:34 +00:00
} elseif ($this->database->type() === 'standalone-mariadb') {
2023-10-24 12:31:28 +00:00
$payload['databases_to_backup'] = $this->database->mariadb_database;
2023-10-13 14:32:59 +00:00
}
2023-11-07 11:11:47 +00:00
$databaseBackup = ScheduledDatabaseBackup::create($payload);
if ($this->database->getMorphClass() === \App\Models\ServiceDatabase::class) {
2023-12-07 18:06:32 +00:00
$this->dispatch('refreshScheduledBackups', $databaseBackup->id);
2023-11-07 11:11:47 +00:00
} else {
2023-12-07 18:06:32 +00:00
$this->dispatch('refreshScheduledBackups');
2023-11-07 11:11:47 +00:00
}
} catch (\Throwable $e) {
return handleError($e, $this);
2024-11-14 17:27:06 +00:00
} finally {
$this->frequency = '';
}
}
}