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