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

66 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Livewire\Project\Database;
use App\Models\ScheduledDatabaseBackup;
use Livewire\Component;
class CreateScheduledBackup extends Component
{
public $database;
public $frequency;
public bool $enabled = true;
2023-09-30 20:26:42 +02:00
public bool $save_s3 = false;
2023-08-11 16:13:53 +02:00
public $s3_storage_id;
public $s3s;
protected $rules = [
'frequency' => 'required|string',
'save_s3' => 'required|boolean',
];
protected $validationAttributes = [
'frequency' => 'Backup Frequency',
'save_s3' => 'Save to S3',
];
2023-10-20 09:38:13 +02:00
public function mount() {
if ($this->s3s->count() > 0) {
$this->s3_storage_id = $this->s3s->first()->id;
}
}
public function submit(): void
{
try {
$this->validate();
$isValid = validate_cron_expression($this->frequency);
if (!$isValid) {
2023-08-11 16:13:53 +02:00
$this->emit('error', 'Invalid Cron / Human expression.');
return;
}
2023-10-13 16:32:59 +02:00
$payload = [
'enabled' => true,
'frequency' => $this->frequency,
'save_s3' => $this->save_s3,
2023-08-11 16:13:53 +02:00
's3_storage_id' => $this->s3_storage_id,
'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
];
if ($this->database->type() === 'standalone-postgresql') {
$payload['databases_to_backup'] = $this->database->postgres_db;
2023-10-24 14:31:28 +02:00
} else if ($this->database->type() === 'standalone-mysql') {
$payload['databases_to_backup'] = $this->database->mysql_database;
}else if ($this->database->type() === 'standalone-mariadb') {
$payload['databases_to_backup'] = $this->database->mariadb_database;
2023-10-13 16:32:59 +02:00
}
ScheduledDatabaseBackup::create($payload);
$this->emit('refreshScheduledBackups');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
handleError($e, $this);
} finally {
$this->frequency = '';
$this->save_s3 = true;
}
}
}