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

99 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Database;
use Livewire\Component;
2023-11-07 12:11:47 +01:00
use Spatie\Url\Url;
class BackupEdit extends Component
{
public $backup;
2023-08-11 16:13:53 +02:00
public $s3s;
2023-10-10 13:10:43 +02:00
public ?string $status = null;
2023-08-10 16:28:29 +02:00
public array $parameters;
protected $rules = [
'backup.enabled' => 'required|boolean',
'backup.frequency' => 'required|string',
'backup.number_of_backups_locally' => 'required|integer|min:1',
2023-08-10 17:55:03 +02:00
'backup.save_s3' => 'required|boolean',
2023-08-11 16:13:53 +02:00
'backup.s3_storage_id' => 'nullable|integer',
2023-10-13 15:45:24 +02:00
'backup.databases_to_backup' => 'nullable',
];
protected $validationAttributes = [
'backup.enabled' => 'Enabled',
'backup.frequency' => 'Frequency',
'backup.number_of_backups_locally' => 'Number of Backups Locally',
2023-08-10 17:55:03 +02:00
'backup.save_s3' => 'Save to S3',
2023-08-11 16:13:53 +02:00
'backup.s3_storage_id' => 'S3 Storage',
2023-10-13 15:45:24 +02:00
'backup.databases_to_backup' => 'Databases to Backup',
2023-08-11 16:13:53 +02:00
];
protected $messages = [
'backup.s3_storage_id' => 'Select a S3 Storage',
];
2023-08-10 16:28:29 +02:00
public function mount()
{
$this->parameters = get_route_parameters();
2023-08-11 16:13:53 +02:00
if (is_null($this->backup->s3_storage_id)) {
$this->backup->s3_storage_id = 'default';
}
2023-08-10 16:28:29 +02:00
}
2023-08-10 16:25:59 +02:00
public function delete()
{
2023-08-10 16:28:29 +02:00
// TODO: Delete backup from server and add a confirmation modal
2023-08-10 16:25:59 +02:00
$this->backup->delete();
2023-11-07 12:11:47 +01:00
if ($this->backup->database->getMorphClass() === 'App\Models\ServiceDatabase') {
$previousUrl = url()->previous();
$url = Url::fromString($previousUrl);
$url = $url->withoutQueryParameter('selectedBackupId');
$url = $url->withFragment('backups');
$url = $url->getPath() . "#{$url->getFragment()}";
return redirect()->to($url);
} else {
redirect()->route('project.database.backups.all', $this->parameters);
}
2023-08-10 16:25:59 +02:00
}
public function instantSave()
{
2023-08-11 16:13:53 +02:00
try {
$this->custom_validate();
$this->backup->save();
$this->backup->refresh();
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Backup updated successfully');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', $e->getMessage());
2023-08-11 16:13:53 +02:00
}
}
2023-08-11 16:13:53 +02:00
private function custom_validate()
{
2023-08-11 20:48:52 +02:00
if (!is_numeric($this->backup->s3_storage_id)) {
$this->backup->s3_storage_id = null;
}
$isValid = validate_cron_expression($this->backup->frequency);
if (!$isValid) {
2023-08-11 16:13:53 +02:00
throw new \Exception('Invalid Cron / Human expression');
}
$this->validate();
2023-08-11 16:13:53 +02:00
}
public function submit()
{
try {
$this->custom_validate();
2023-10-13 15:45:24 +02:00
if ($this->backup->databases_to_backup == '' || $this->backup->databases_to_backup === null) {
$this->backup->databases_to_backup = null;
}
2023-08-11 16:13:53 +02:00
$this->backup->save();
$this->backup->refresh();
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Backup updated successfully');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-12-07 19:06:32 +01:00
$this->dispatch('error', $e->getMessage());
2023-08-11 16:13:53 +02:00
}
}
}