From a8b77b389a9b69086acc2bf38110b0b6894d3620 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:38:16 +0100 Subject: [PATCH] feat: use new backup deletion functions --- app/Livewire/Project/Database/BackupEdit.php | 91 ++++++------------- .../Project/Database/BackupExecutions.php | 22 ++--- .../database/backup-executions.blade.php | 1 + 3 files changed, 40 insertions(+), 74 deletions(-) diff --git a/app/Livewire/Project/Database/BackupEdit.php b/app/Livewire/Project/Database/BackupEdit.php index 0dea0496c..77009002c 100644 --- a/app/Livewire/Project/Database/BackupEdit.php +++ b/app/Livewire/Project/Database/BackupEdit.php @@ -43,16 +43,28 @@ class BackupEdit extends Component #[Validate(['string'])] public string $timezone = ''; - #[Validate(['required', 'integer', 'min:1'])] - public int $numberOfBackupsLocally = 1; + #[Validate(['required', 'integer'])] + public int $databaseBackupRetentionAmountLocally = 0; + + #[Validate(['required', 'integer'])] + public ?int $databaseBackupRetentionDaysLocally = 0; + + #[Validate(['required', 'integer'])] + public ?int $databaseBackupRetentionAmountS3 = 0; + + #[Validate(['required', 'integer'])] + public ?int $databaseBackupRetentionDaysS3 = 0; + + #[Validate(['required', 'integer'])] + public ?int $databaseBackupRetentionMaxStorageS3 = 0; #[Validate(['required', 'boolean'])] public bool $saveS3 = false; - #[Validate(['nullable', 'integer'])] + #[Validate(['required', 'integer'])] public ?int $s3StorageId = 1; - #[Validate(['nullable', 'string'])] + #[Validate(['required', 'string'])] public ?string $databasesToBackup = null; #[Validate(['required', 'boolean'])] @@ -73,7 +85,11 @@ class BackupEdit extends Component if ($toModel) { $this->backup->enabled = $this->backupEnabled; $this->backup->frequency = $this->frequency; - $this->backup->number_of_backups_locally = $this->numberOfBackupsLocally; + $this->backup->database_backup_retention_amount_locally = $this->databaseBackupRetentionAmountLocally; + $this->backup->database_backup_retention_days_locally = $this->databaseBackupRetentionDaysLocally; + $this->backup->database_backup_retention_amount_s3 = $this->databaseBackupRetentionAmountS3; + $this->backup->database_backup_retention_days_s3 = $this->databaseBackupRetentionDaysS3; + $this->backup->database_backup_retention_max_storage_s3 = $this->databaseBackupRetentionMaxStorageS3; $this->backup->save_s3 = $this->saveS3; $this->backup->s3_storage_id = $this->s3StorageId; $this->backup->databases_to_backup = $this->databasesToBackup; @@ -84,7 +100,11 @@ class BackupEdit extends Component $this->backupEnabled = $this->backup->enabled; $this->frequency = $this->backup->frequency; $this->timezone = data_get($this->backup->server(), 'settings.server_timezone', 'Instance timezone'); - $this->numberOfBackupsLocally = $this->backup->number_of_backups_locally; + $this->databaseBackupRetentionAmountLocally = $this->backup->database_backup_retention_amount_locally; + $this->databaseBackupRetentionDaysLocally = $this->backup->database_backup_retention_days_locally; + $this->databaseBackupRetentionAmountS3 = $this->backup->database_backup_retention_amount_s3; + $this->databaseBackupRetentionDaysS3 = $this->backup->database_backup_retention_days_s3; + $this->databaseBackupRetentionMaxStorageS3 = $this->backup->database_backup_retention_max_storage_s3; $this->saveS3 = $this->backup->save_s3; $this->s3StorageId = $this->backup->s3_storage_id; $this->databasesToBackup = $this->backup->databases_to_backup; @@ -104,10 +124,10 @@ class BackupEdit extends Component try { if ($this->delete_associated_backups_locally) { - $this->deleteAssociatedBackupsLocally(); + deleteOldBackupsLocally($this->backup); } - if ($this->delete_associated_backups_s3) { - $this->deleteAssociatedBackupsS3(); + if ($this->delete_associated_backups_s3 && $this->backup->s3) { + deleteOldBackupsFromS3($this->backup); } $this->backup->delete(); @@ -160,63 +180,12 @@ class BackupEdit extends Component } } - private function deleteAssociatedBackupsLocally() - { - $executions = $this->backup->executions; - $backupFolder = null; - - foreach ($executions as $execution) { - if ($this->backup->database->getMorphClass() === \App\Models\ServiceDatabase::class) { - $server = $this->backup->database->service->destination->server; - } else { - $server = $this->backup->database->destination->server; - } - - if (! $backupFolder) { - $backupFolder = dirname($execution->filename); - } - - delete_backup_locally($execution->filename, $server); - $execution->delete(); - } - - if (str($backupFolder)->isNotEmpty()) { - $this->deleteEmptyBackupFolder($backupFolder, $server); - } - } - - private function deleteAssociatedBackupsS3() - { - // Add function to delete backups from S3 - } - - private function deleteAssociatedBackupsSftp() - { - // Add function to delete backups from SFTP - } - - private function deleteEmptyBackupFolder($folderPath, $server) - { - $checkEmpty = instant_remote_process(["[ -z \"$(ls -A '$folderPath')\" ] && echo 'empty' || echo 'not empty'"], $server); - - if (trim($checkEmpty) === 'empty') { - instant_remote_process(["rmdir '$folderPath'"], $server); - - $parentFolder = dirname($folderPath); - $checkParentEmpty = instant_remote_process(["[ -z \"$(ls -A '$parentFolder')\" ] && echo 'empty' || echo 'not empty'"], $server); - - if (trim($checkParentEmpty) === 'empty') { - instant_remote_process(["rmdir '$parentFolder'"], $server); - } - } - } - public function render() { return view('livewire.project.database.backup-edit', [ 'checkboxes' => [ ['id' => 'delete_associated_backups_locally', 'label' => __('database.delete_backups_locally')], - // ['id' => 'delete_associated_backups_s3', 'label' => 'All backups associated with this backup job from this database will be permanently deleted from the selected S3 Storage.'] + ['id' => 'delete_associated_backups_s3', 'label' => 'All backups associated with this backup job for this database will be permanently deleted from the selected S3 Storage.'], // ['id' => 'delete_associated_backups_sftp', 'label' => 'All backups associated with this backup job from this database will be permanently deleted from the selected SFTP Storage.'] ], ]); diff --git a/app/Livewire/Project/Database/BackupExecutions.php b/app/Livewire/Project/Database/BackupExecutions.php index ce168a352..9ed608ed0 100644 --- a/app/Livewire/Project/Database/BackupExecutions.php +++ b/app/Livewire/Project/Database/BackupExecutions.php @@ -18,9 +18,9 @@ class BackupExecutions extends Component public $setDeletableBackup; - public $delete_backup_s3 = true; + public $delete_backup_s3 = false; - public $delete_backup_sftp = true; + public $delete_backup_sftp = false; public function getListeners() { @@ -57,18 +57,14 @@ class BackupExecutions extends Component return; } - if ($execution->scheduledDatabaseBackup->database->getMorphClass() === \App\Models\ServiceDatabase::class) { - delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->service->destination->server); - } else { - delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->destination->server); - } + $server = $execution->scheduledDatabaseBackup->database->getMorphClass() === \App\Models\ServiceDatabase::class + ? $execution->scheduledDatabaseBackup->database->service->destination->server + : $execution->scheduledDatabaseBackup->database->destination->server; - if ($this->delete_backup_s3) { - // Add logic to delete from S3 - } + deleteBackupsLocally($execution->filename, $server); - if ($this->delete_backup_sftp) { - // Add logic to delete from SFTP + if ($this->delete_backup_s3 && $execution->scheduledDatabaseBackup->s3) { + deleteBackupsS3($execution->filename, $server, $execution->scheduledDatabaseBackup->s3); } $execution->delete(); @@ -143,7 +139,7 @@ class BackupExecutions extends Component return view('livewire.project.database.backup-executions', [ 'checkboxes' => [ ['id' => 'delete_backup_s3', 'label' => 'Delete the selected backup permanently form S3 Storage'], - ['id' => 'delete_backup_sftp', 'label' => 'Delete the selected backup permanently form SFTP Storage'], + // ['id' => 'delete_backup_sftp', 'label' => 'Delete the selected backup permanently form SFTP Storage'], ], ]); } diff --git a/resources/views/livewire/project/database/backup-executions.blade.php b/resources/views/livewire/project/database/backup-executions.blade.php index ff2bc15b2..7f8350a3e 100644 --- a/resources/views/livewire/project/database/backup-executions.blade.php +++ b/resources/views/livewire/project/database/backup-executions.blade.php @@ -47,6 +47,7 @@ @endif