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

89 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Database;
use App\Models\ScheduledDatabaseBackup;
use App\Models\ServiceDatabase;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
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
#[Locked]
public $database;
2024-06-10 20:43:34 +00:00
public bool $enabled = true;
public function submit()
{
try {
$this->authorize('manageBackups', $this->database);
if (! $this->database->isBackupSolutionAvailable()) {
$this->dispatch('error', 'Scheduled backups are not supported for this database type.');
return;
}
$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' => false,
's3_storage_id' => null,
'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;
} elseif ($this->database->type() === 'standalone-clickhouse') {
$payload['databases_to_backup'] = $this->database->clickhouse_db;
2023-10-13 14:32:59 +00:00
}
2023-11-07 11:11:47 +00:00
$databaseBackup = ScheduledDatabaseBackup::create($payload);
if ($this->database->getMorphClass() === ServiceDatabase::class) {
$service = $this->database->service;
$this->redirectRoute('project.service.database.backup.show', [
'project_uuid' => $service->project()->uuid,
'environment_uuid' => $service->environment->uuid,
'service_uuid' => $service->uuid,
'stack_service_uuid' => $this->database->uuid,
'backup_uuid' => $databaseBackup->uuid,
], navigate: true);
2023-11-07 11:11:47 +00:00
} else {
$this->redirectRoute('project.database.backup.execution', [
'project_uuid' => $this->database->project()->uuid,
'environment_uuid' => $this->database->environment->uuid,
'database_uuid' => $this->database->uuid,
'backup_uuid' => $databaseBackup->uuid,
], navigate: true);
2023-11-07 11:11:47 +00:00
}
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2024-11-14 17:27:06 +00:00
} finally {
$this->frequency = '';
}
}
}