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

106 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Database;
use App\Models\S3Storage;
use App\Models\ScheduledDatabaseBackup;
use App\Models\ServiceDatabase;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2024-05-24 17:28:05 +02:00
use Illuminate\Support\Collection;
2024-11-04 11:53:32 +01:00
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
class CreateScheduledBackup extends Component
{
use AuthorizesRequests;
#[Validate(['required', 'string'])]
public $frequency;
2024-06-10 20:43:34 +00:00
#[Validate(['required', 'boolean'])]
2024-11-04 11:53:32 +01:00
public bool $saveToS3 = false;
2024-06-10 20:43:34 +00:00
2024-11-04 11:53:32 +01:00
#[Locked]
public $database;
2024-06-10 20:43:34 +00:00
2024-11-04 11:53:32 +01:00
public bool $enabled = true;
2024-11-14 18:20:26 +01:00
#[Validate(['nullable', 'integer'])]
public ?int $s3StorageId = null;
2024-06-10 20:43:34 +00:00
2024-11-04 11:53:32 +01:00
public Collection $definedS3s;
2024-06-10 20:43:34 +00:00
2023-11-07 12:11:47 +01:00
public function mount()
{
2024-11-04 11:53:32 +01:00
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 09:38:13 +02:00
}
}
2024-11-04 11:53:32 +01:00
public function submit()
{
try {
$this->authorize('manageBackups', $this->database);
$this->validate();
2024-11-04 11:53:32 +01:00
if ($this->saveToS3) {
$s3StorageExists = ! is_null($this->s3StorageId)
&& S3Storage::where('team_id', currentTeam()->id)
->where('is_usable', true)
->whereKey($this->s3StorageId)
->exists();
if (! $s3StorageExists) {
$this->dispatch('error', 'Please select a valid S3 storage to enable S3 backups.');
return;
}
}
$isValid = validate_cron_expression($this->frequency);
2024-06-10 20:43:34 +00:00
if (! $isValid) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Invalid Cron / Human expression.');
2024-06-10 20:43:34 +00:00
return;
}
2023-10-13 16:32:59 +02:00
$payload = [
'enabled' => true,
'frequency' => $this->frequency,
2024-11-04 11:53:32 +01:00
'save_s3' => $this->saveToS3,
's3_storage_id' => $this->s3StorageId,
'database_id' => $this->database->id,
'database_type' => $this->database->getMorphClass(),
2023-08-22 17:44:49 +02:00
'team_id' => currentTeam()->id,
2023-10-13 16:32:59 +02:00
];
2023-10-13 16:32:59 +02: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 14:31:28 +02: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 14:31:28 +02:00
$payload['databases_to_backup'] = $this->database->mariadb_database;
2023-10-13 16:32:59 +02:00
}
2023-11-07 12:11:47 +01:00
$databaseBackup = ScheduledDatabaseBackup::create($payload);
if ($this->database->getMorphClass() === ServiceDatabase::class) {
2023-12-07 19:06:32 +01:00
$this->dispatch('refreshScheduledBackups', $databaseBackup->id);
2023-11-07 12:11:47 +01:00
} else {
2023-12-07 19:06:32 +01:00
$this->dispatch('refreshScheduledBackups');
2023-11-07 12:11:47 +01:00
}
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2024-11-04 11:53:32 +01:00
return handleError($e, $this);
2024-11-14 18:27:06 +01:00
} finally {
$this->frequency = '';
}
}
}