refactor backup edit view

This commit is contained in:
Andras Bacsai
2024-11-04 12:40:10 +01:00
parent 2cbac34877
commit 34c7aa122c
2 changed files with 77 additions and 53 deletions

View File

@@ -4,56 +4,87 @@ namespace App\Livewire\Project\Database;
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup; use App\Models\ScheduledDatabaseBackup;
use Exception;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
use Spatie\Url\Url; use Spatie\Url\Url;
class BackupEdit extends Component class BackupEdit extends Component
{ {
public ?ScheduledDatabaseBackup $backup; public ScheduledDatabaseBackup $backup;
#[Locked]
public $s3s; public $s3s;
#[Locked]
public $parameters;
#[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_locally = false; public bool $delete_associated_backups_locally = false;
#[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_s3 = false; public bool $delete_associated_backups_s3 = false;
#[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_sftp = false; public bool $delete_associated_backups_sftp = false;
#[Rule(['nullable', 'string'])]
public ?string $status = null; public ?string $status = null;
public array $parameters; #[Rule(['required', 'boolean'])]
public bool $backupEnabled = false;
protected $rules = [ #[Rule(['required', 'string'])]
'backup.enabled' => 'required|boolean', public string $frequency = '';
'backup.frequency' => 'required|string',
'backup.number_of_backups_locally' => 'required|integer|min:1',
'backup.save_s3' => 'required|boolean',
'backup.s3_storage_id' => 'nullable|integer',
'backup.databases_to_backup' => 'nullable',
'backup.dump_all' => 'required|boolean',
];
protected $validationAttributes = [ #[Rule(['required', 'integer', 'min:1'])]
'backup.enabled' => 'Enabled', public int $numberOfBackupsLocally = 1;
'backup.frequency' => 'Frequency',
'backup.number_of_backups_locally' => 'Number of Backups Locally',
'backup.save_s3' => 'Save to S3',
'backup.s3_storage_id' => 'S3 Storage',
'backup.databases_to_backup' => 'Databases to Backup',
'backup.dump_all' => 'Backup All Databases',
];
protected $messages = [ #[Rule(['required', 'boolean'])]
'backup.s3_storage_id' => 'Select a S3 Storage', public bool $saveS3 = false;
];
#[Rule(['required', 'integer'])]
public int $s3StorageId = 1;
#[Rule(['nullable', 'string'])]
public ?string $databasesToBackup = null;
#[Rule(['required', 'boolean'])]
public bool $dumpAll = false;
public function mount() public function mount()
{ {
try {
$this->parameters = get_route_parameters(); $this->parameters = get_route_parameters();
if (is_null(data_get($this->backup, 's3_storage_id'))) { $this->syncData();
data_set($this->backup, 's3_storage_id', 'default'); } catch (Exception $e) {
return handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->customValidate();
$this->backup->enabled = $this->backupEnabled;
$this->backup->frequency = $this->frequency;
$this->backup->number_of_backups_locally = $this->numberOfBackupsLocally;
$this->backup->save_s3 = $this->saveS3;
$this->backup->s3_storage_id = $this->s3StorageId;
$this->backup->databases_to_backup = $this->databasesToBackup;
$this->backup->dump_all = $this->dumpAll;
$this->backup->save();
} else {
$this->backupEnabled = $this->backup->enabled;
$this->frequency = $this->backup->frequency;
$this->numberOfBackupsLocally = $this->backup->number_of_backups_locally;
$this->saveS3 = $this->backup->save_s3;
$this->s3StorageId = $this->backup->s3_storage_id;
$this->databasesToBackup = $this->backup->databases_to_backup;
$this->dumpAll = $this->backup->dump_all;
} }
} }
@@ -96,16 +127,14 @@ class BackupEdit extends Component
public function instantSave() public function instantSave()
{ {
try { try {
$this->custom_validate(); $this->syncData(true);
$this->backup->save();
$this->backup->refresh();
$this->dispatch('success', 'Backup updated successfully.'); $this->dispatch('success', 'Backup updated successfully.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->dispatch('error', $e->getMessage()); $this->dispatch('error', $e->getMessage());
} }
} }
private function custom_validate() private function customValidate()
{ {
if (! is_numeric($this->backup->s3_storage_id)) { if (! is_numeric($this->backup->s3_storage_id)) {
$this->backup->s3_storage_id = null; $this->backup->s3_storage_id = null;
@@ -120,19 +149,14 @@ class BackupEdit extends Component
public function submit() public function submit()
{ {
try { try {
$this->custom_validate(); $this->syncData(true);
if ($this->backup->databases_to_backup === '' || $this->backup->databases_to_backup === null) { $this->dispatch('success', 'Backup updated successfully.');
$this->backup->databases_to_backup = null;
}
$this->backup->save();
$this->backup->refresh();
$this->dispatch('success', 'Backup updated successfully');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->dispatch('error', $e->getMessage()); $this->dispatch('error', $e->getMessage());
} }
} }
public function deleteAssociatedBackupsLocally() private function deleteAssociatedBackupsLocally()
{ {
$executions = $this->backup->executions; $executions = $this->backup->executions;
$backupFolder = null; $backupFolder = null;
@@ -152,17 +176,17 @@ class BackupEdit extends Component
$execution->delete(); $execution->delete();
} }
if ($backupFolder) { if (str($backupFolder)->isNotEmpty()) {
$this->deleteEmptyBackupFolder($backupFolder, $server); $this->deleteEmptyBackupFolder($backupFolder, $server);
} }
} }
public function deleteAssociatedBackupsS3() private function deleteAssociatedBackupsS3()
{ {
//Add function to delete backups from S3 //Add function to delete backups from S3
} }
public function deleteAssociatedBackupsSftp() private function deleteAssociatedBackupsSftp()
{ {
//Add function to delete backups from SFTP //Add function to delete backups from SFTP
} }

View File

@@ -19,12 +19,12 @@
@endif @endif
</div> </div>
<div class="w-48 pb-2"> <div class="w-48 pb-2">
<x-forms.checkbox instantSave label="Backup Enabled" id="backup.enabled" /> <x-forms.checkbox instantSave label="Backup Enabled" id="backupEnabled" />
<x-forms.checkbox instantSave label="S3 Enabled" id="backup.save_s3" /> <x-forms.checkbox instantSave label="S3 Enabled" id="saveS3" />
</div> </div>
@if ($backup->save_s3) @if ($backup->save_s3)
<div class="pb-6"> <div class="pb-6">
<x-forms.select id="backup.s3_storage_id" label="S3 Storage" required> <x-forms.select id="s3StorageId" label="S3 Storage" required>
<option value="default">Select a S3 storage</option> <option value="default">Select a S3 storage</option>
@foreach ($s3s as $s3) @foreach ($s3s as $s3)
<option value="{{ $s3->id }}">{{ $s3->name }}</option> <option value="{{ $s3->id }}">{{ $s3->name }}</option>
@@ -37,40 +37,40 @@
<div class="flex gap-2 flex-col "> <div class="flex gap-2 flex-col ">
@if ($backup->database_type === 'App\Models\StandalonePostgresql' && $backup->database_id !== 0) @if ($backup->database_type === 'App\Models\StandalonePostgresql' && $backup->database_id !== 0)
<div class="w-48"> <div class="w-48">
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave /> <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div> </div>
@if (!$backup->dump_all) @if (!$backup->dump_all)
<x-forms.input label="Databases To Backup" <x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one." helper="Comma separated list of databases to backup. Empty will include the default one."
id="backup.databases_to_backup" /> id="databasesToBackup" />
@endif @endif
@elseif($backup->database_type === 'App\Models\StandaloneMongodb') @elseif($backup->database_type === 'App\Models\StandaloneMongodb')
<x-forms.input label="Databases To Include" <x-forms.input label="Databases To Include"
helper="A list of databases to backup. You can specify which collection(s) per database to exclude from the backup. Empty will include all databases and collections.<br><br>Example:<br><br>database1:collection1,collection2|database2:collection3,collection4<br><br> database1 will include all collections except collection1 and collection2. <br>database2 will include all collections except collection3 and collection4.<br><br>Another Example:<br><br>database1:collection1|database2<br><br> database1 will include all collections except collection1.<br>database2 will include ALL collections." helper="A list of databases to backup. You can specify which collection(s) per database to exclude from the backup. Empty will include all databases and collections.<br><br>Example:<br><br>database1:collection1,collection2|database2:collection3,collection4<br><br> database1 will include all collections except collection1 and collection2. <br>database2 will include all collections except collection3 and collection4.<br><br>Another Example:<br><br>database1:collection1|database2<br><br> database1 will include all collections except collection1.<br>database2 will include ALL collections."
id="backup.databases_to_backup" /> id="databasesToBackup" />
@elseif($backup->database_type === 'App\Models\StandaloneMysql') @elseif($backup->database_type === 'App\Models\StandaloneMysql')
<div class="w-48"> <div class="w-48">
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave /> <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div> </div>
@if (!$backup->dump_all) @if (!$backup->dump_all)
<x-forms.input label="Databases To Backup" <x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one." helper="Comma separated list of databases to backup. Empty will include the default one."
id="backup.databases_to_backup" /> id="databasesToBackup" />
@endif @endif
@elseif($backup->database_type === 'App\Models\StandaloneMariadb') @elseif($backup->database_type === 'App\Models\StandaloneMariadb')
<div class="w-48"> <div class="w-48">
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave /> <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div> </div>
@if (!$backup->dump_all) @if (!$backup->dump_all)
<x-forms.input label="Databases To Backup" <x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one." helper="Comma separated list of databases to backup. Empty will include the default one."
id="backup.databases_to_backup" /> id="databasesToBackup" />
@endif @endif
@endif @endif
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<x-forms.input label="Frequency" id="backup.frequency" /> <x-forms.input label="Frequency" id="frequency" />
<x-forms.input label="Number of backups to keep (locally)" id="backup.number_of_backups_locally" /> <x-forms.input label="Number of backups to keep (locally)" id="numberOfBackupsLocally" />
</div> </div>
</div> </div>
</form> </form>