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

89 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Livewire\Project\Database;
use Livewire\Component;
class BackupEdit extends Component
{
public $backup;
2023-08-11 16:13:53 +02:00
public $s3s;
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',
];
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',
];
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 17:55:03 +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-08-10 16:28:29 +02:00
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();
$this->emit('success', 'Backup updated successfully');
} catch (\Exception $e) {
$this->emit('error', $e->getMessage());
}
}
2023-08-11 16:13:53 +02:00
private function custom_validate()
{
2023-08-11 16:13:53 +02:00
// if ($this->backup->save_s3) {
// if (!is_numeric($this->selected_storage_id)) {
// throw new \Exception('Invalid S3 Storage');
// } else {
// $this->backup->s3_storage_id = $this->selected_storage_id;
// }
// }
$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()
{
ray($this->backup->s3_storage_id);
try {
$this->custom_validate();
$this->backup->save();
$this->backup->refresh();
$this->emit('success', 'Backup updated successfully');
} catch (\Exception $e) {
$this->emit('error', $e->getMessage());
}
}
}