An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix: storage retention setting
peaklabs-deve9f691bf4527
diff --git a/app/Livewire/Project/Database/BackupEdit.php b/app/Livewire/Project/Database/BackupEdit.phpindex 853b918b82..6ebbf951e5 100644--- a/app/Livewire/Project/Database/BackupEdit.php+++ b/app/Livewire/Project/Database/BackupEdit.php@@ -49,14 +49,17 @@ class BackupEdit extends Component#[Validate(['required', 'integer'])]public ?int $databaseBackupRetentionDaysLocally = 0;+ #[Validate(['required', 'numeric', 'min:0'])]+ public ?float $databaseBackupRetentionMaxStorageLocally = 0;+#[Validate(['required', 'integer'])]public ?int $databaseBackupRetentionAmountS3 = 0;#[Validate(['required', 'integer'])]public ?int $databaseBackupRetentionDaysS3 = 0;- #[Validate(['required', 'integer'])]- public ?int $databaseBackupRetentionMaxStorageS3 = 0;+ #[Validate(['required', 'numeric', 'min:0'])]+ public ?float $databaseBackupRetentionMaxStorageS3 = 0;#[Validate(['required', 'boolean'])]public bool $saveS3 = false;@@ -87,6 +90,7 @@ class BackupEdit extends Component$this->backup->frequency = $this->frequency;$this->backup->database_backup_retention_amount_locally = $this->databaseBackupRetentionAmountLocally;$this->backup->database_backup_retention_days_locally = $this->databaseBackupRetentionDaysLocally;+ $this->backup->database_backup_retention_max_storage_locally = $this->databaseBackupRetentionMaxStorageLocally;$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;@@ -102,6 +106,7 @@ class BackupEdit extends Component$this->timezone = data_get($this->backup->server(), 'settings.server_timezone', 'Instance timezone');$this->databaseBackupRetentionAmountLocally = $this->backup->database_backup_retention_amount_locally;$this->databaseBackupRetentionDaysLocally = $this->backup->database_backup_retention_days_locally;+ $this->databaseBackupRetentionMaxStorageLocally = $this->backup->database_backup_retention_max_storage_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;diff --git a/bootstrap/helpers/databases.php b/bootstrap/helpers/databases.phpindex 90c18dc6d5..6a834ee6ff 100644--- a/bootstrap/helpers/databases.php+++ b/bootstrap/helpers/databases.php@@ -263,8 +263,9 @@ function deleteOldBackupsLocally($backup): Collection$retentionAmount = $backup->database_backup_retention_amount_locally;$retentionDays = $backup->database_backup_retention_days_locally;+ $maxStorageGB = $backup->database_backup_retention_max_storage_locally;- if ($retentionAmount === 0 && $retentionDays === 0) {+ if ($retentionAmount === 0 && $retentionDays === 0 && $maxStorageGB === 0) {return collect();}@@ -281,6 +282,26 @@ function deleteOldBackupsLocally($backup): Collection$backupsToDelete = $backupsToDelete->merge($oldBackups);}+ if ($maxStorageGB > 0) {+ $maxStorageBytes = $maxStorageGB * pow(1024, 3);+ $totalSize = 0;+ $backupsOverLimit = collect();++ $backupsToCheck = $successfulBackups->skip(1);++ foreach ($backupsToCheck as $backupExecution) {+ $totalSize += (int) $backupExecution->size;+ if ($totalSize > $maxStorageBytes) {+ $backupsOverLimit = $successfulBackups->filter(+ fn ($b) => $b->created_at->utc() <= $backupExecution->created_at->utc()+ )->skip(1);+ break;+ }+ }++ $backupsToDelete = $backupsToDelete->merge($backupsOverLimit);+ }+$backupsToDelete = $backupsToDelete->unique('id');$processedBackups = collect();@@ -345,14 +366,18 @@ function deleteOldBackupsFromS3($backup): Collection}if ($maxStorageGB > 0) {- $maxStorageBytes = $maxStorageGB * 1024 * 1024 * 1024;+ $maxStorageBytes = $maxStorageGB * pow(1024, 3);$totalSize = 0;$backupsOverLimit = collect();- foreach ($successfulBackups as $backup) {- $totalSize += (int) $backup->size;+ $backupsToCheck = $successfulBackups->skip(1);++ foreach ($backupsToCheck as $backupExecution) {+ $totalSize += (int) $backupExecution->size;if ($totalSize > $maxStorageBytes) {- $backupsOverLimit = $successfulBackups->filter(fn ($b) => $b->created_at->utc() <= $backup->created_at->utc());+ $backupsOverLimit = $successfulBackups->filter(+ fn ($b) => $b->created_at->utc() <= $backupExecution->created_at->utc()+ )->skip(1);break;}}diff --git a/database/migrations/2025_01_13_130238_add_backup_retention_fields_to_scheduled_database_backups_table.php b/database/migrations/2025_01_13_130238_add_backup_retention_fields_to_scheduled_database_backups_table.phpindex f5371ee7af..f06bc367eb 100644--- a/database/migrations/2025_01_13_130238_add_backup_retention_fields_to_scheduled_database_backups_table.php+++ b/database/migrations/2025_01_13_130238_add_backup_retention_fields_to_scheduled_database_backups_table.php@@ -12,10 +12,11 @@ return new class extends Migration$table->renameColumn('number_of_backups_locally', 'database_backup_retention_amount_locally');$table->integer('database_backup_retention_amount_locally')->default(0)->nullable(false)->change();$table->integer('database_backup_retention_days_locally')->default(0)->nullable(false);+ $table->decimal('database_backup_retention_max_storage_locally', 17, 7)->default(0)->nullable(false);$table->integer('database_backup_retention_amount_s3')->default(0)->nullable(false);$table->integer('database_backup_retention_days_s3')->default(0)->nullable(false);- $table->integer('database_backup_retention_max_storage_s3')->default(0)->nullable(false);+ $table->decimal('database_backup_retention_max_storage_s3', 17, 7)->default(0)->nullable(false);});}@@ -25,6 +26,7 @@ return new class extends Migration$table->renameColumn('database_backup_retention_amount_locally', 'number_of_backups_locally')->nullable(true)->change();$table->dropColumn(['database_backup_retention_days_locally',+ 'database_backup_retention_max_storage_locally','database_backup_retention_amount_s3','database_backup_retention_days_s3','database_backup_retention_max_storage_s3',diff --git a/resources/views/livewire/project/database/backup-edit.blade.php b/resources/views/livewire/project/database/backup-edit.blade.phpindex 4f3ebe8f8a..f1f604a7d5 100644--- a/resources/views/livewire/project/database/backup-edit.blade.php+++ b/resources/views/livewire/project/database/backup-edit.blade.php@@ -75,6 +75,13 @@<div class="flex gap-4"><x-forms.input label="Number of backups to keep" id="databaseBackupRetentionAmountLocally" type="number" min="0" helper="Keeps only the specified number of most recent backups on the server. Set to 0 for unlimited backups." /><x-forms.input label="Days to keep backups" id="databaseBackupRetentionDaysLocally" type="number" min="0" helper="Automatically removes backups older than the specified number of days. Set to 0 for no time limit." />+ <x-forms.input+ label="Maximum storage (GB)"+ id="databaseBackupRetentionMaxStorageLocally"+ type="number"+ min="0"+ step="0.0000001"+ helper="When total size of all backups in the current backup job exceeds this limit in GB, the oldest backups will be removed. Decimal values are supported (e.g. 0.001 for 1MB). Set to 0 for unlimited storage." /></div></div>@@ -84,7 +91,13 @@<div class="flex gap-4"><x-forms.input label="Number of backups to keep" id="databaseBackupRetentionAmountS3" type="number" min="0" helper="Keeps only the specified number of most recent backups on S3 storage. Set to 0 for unlimited backups." /><x-forms.input label="Days to keep backups" id="databaseBackupRetentionDaysS3" type="number" min="0" helper="Automatically removes S3 backups older than the specified number of days. Set to 0 for no time limit." />- <x-forms.input label="Maximum storage (GB)" id="databaseBackupRetentionMaxStorageS3" type="number" min="0" helper="When total size of all backups in the current backup job exceeds this limit in GB, the oldest backups will be removed. Set to 0 for unlimited storage." />+ <x-forms.input+ label="Maximum storage (GB)"+ id="databaseBackupRetentionMaxStorageS3"+ type="number"+ min="0"+ step="0.0000001"+ helper="When total size of all backups in the current backup job exceeds this limit in GB, the oldest backups will be removed. Decimal values are supported (e.g. 0.5 for 500MB). Set to 0 for unlimited storage." /></div></div>@endif