From 9955eecfd3b11f5d9954b15f2fcfd9f897e4b786 Mon Sep 17 00:00:00 2001 From: Ricardo Cerljenko Date: Thu, 2 May 2024 07:28:16 +0200 Subject: [PATCH 1/3] added 0 option for unlimited backup storage size --- config/backup.php | 1 + docs/cleaning-up-old-backups/overview.md | 1 + docs/installation-and-setup.md | 1 + src/Tasks/Cleanup/Strategies/DefaultStrategy.php | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config/backup.php b/config/backup.php index d5d2e7bc..ce29f4a2 100644 --- a/config/backup.php +++ b/config/backup.php @@ -319,6 +319,7 @@ /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. + * Set 0 for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/docs/cleaning-up-old-backups/overview.md b/docs/cleaning-up-old-backups/overview.md index cdb8ee28..44e394e9 100644 --- a/docs/cleaning-up-old-backups/overview.md +++ b/docs/cleaning-up-old-backups/overview.md @@ -62,6 +62,7 @@ This portion of the configuration determines which backups should be deleted. /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. + * Set 0 for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/docs/installation-and-setup.md b/docs/installation-and-setup.md index b0ce03d1..b6ffc3ca 100644 --- a/docs/installation-and-setup.md +++ b/docs/installation-and-setup.md @@ -294,6 +294,7 @@ return [ /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. + * Set 0 for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php index 0dd60ddd..e1fb068e 100644 --- a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php +++ b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php @@ -102,7 +102,7 @@ protected function removeOldBackupsUntilUsingLessThanMaximumStorage(BackupCollec $maximumSize = $this->config->get('backup.cleanup.default_strategy.delete_oldest_backups_when_using_more_megabytes_than') * 1024 * 1024; - if (($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize) { + if (!$maximumSize || ($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize) { return; } $oldest->delete(); From 9e261d10da14f5cf069dc334c460517ca6523bde Mon Sep 17 00:00:00 2001 From: Ricardo Cerljenko Date: Thu, 2 May 2024 10:44:47 +0200 Subject: [PATCH 2/3] use null instead of 0 --- config/backup.php | 2 +- docs/cleaning-up-old-backups/overview.md | 2 +- docs/installation-and-setup.md | 2 +- src/Tasks/Cleanup/Strategies/DefaultStrategy.php | 5 ++--- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/config/backup.php b/config/backup.php index ce29f4a2..4805410d 100644 --- a/config/backup.php +++ b/config/backup.php @@ -319,7 +319,7 @@ /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. - * Set 0 for unlimited size. + * Set null for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/docs/cleaning-up-old-backups/overview.md b/docs/cleaning-up-old-backups/overview.md index 44e394e9..8c77c4c7 100644 --- a/docs/cleaning-up-old-backups/overview.md +++ b/docs/cleaning-up-old-backups/overview.md @@ -62,7 +62,7 @@ This portion of the configuration determines which backups should be deleted. /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. - * Set 0 for unlimited size. + * Set null for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/docs/installation-and-setup.md b/docs/installation-and-setup.md index b6ffc3ca..21024710 100644 --- a/docs/installation-and-setup.md +++ b/docs/installation-and-setup.md @@ -294,7 +294,7 @@ return [ /* * After cleaning up the backups remove the oldest backup until * this amount of megabytes has been reached. - * Set 0 for unlimited size. + * Set null for unlimited size. */ 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], diff --git a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php index e1fb068e..4e01631c 100644 --- a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php +++ b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php @@ -99,10 +99,9 @@ protected function removeOldBackupsUntilUsingLessThanMaximumStorage(BackupCollec return; } - $maximumSize = $this->config->get('backup.cleanup.default_strategy.delete_oldest_backups_when_using_more_megabytes_than') - * 1024 * 1024; + $maximumSize = $this->config->get('backup.cleanup.default_strategy.delete_oldest_backups_when_using_more_megabytes_than'); - if (!$maximumSize || ($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize) { + if ($maximumSize === null || ($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize * 1024 * 1024) { return; } $oldest->delete(); From 69747b7c017e978024b6ac49670ad1796b7c268a Mon Sep 17 00:00:00 2001 From: Ricardo Cerljenko Date: Thu, 2 May 2024 10:58:20 +0200 Subject: [PATCH 3/3] extract checks to separate function --- .../Cleanup/Strategies/DefaultStrategy.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php index 4e01631c..81152de4 100644 --- a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php +++ b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php @@ -95,19 +95,33 @@ protected function removeBackupsOlderThan(Carbon $endDate, BackupCollection $bac protected function removeOldBackupsUntilUsingLessThanMaximumStorage(BackupCollection $backups) { - if (! $oldest = $backups->oldest()) { + if (! $this->shouldRemoveOldestBackup($backups)) { return; } + $backups->oldest()->delete(); + + $backups = $backups->filter(fn (Backup $backup) => $backup->exists()); + + $this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups); + } + + protected function shouldRemoveOldestBackup(BackupCollection $backups): bool + { + if (! $backups->oldest()) { + return false; + } + $maximumSize = $this->config->get('backup.cleanup.default_strategy.delete_oldest_backups_when_using_more_megabytes_than'); - if ($maximumSize === null || ($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize * 1024 * 1024) { - return; + if ($maximumSize === null) { + return false; } - $oldest->delete(); - $backups = $backups->filter(fn (Backup $backup) => $backup->exists()); + if (($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize * 1024 * 1024) { + return false; + } - $this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups); + return true; } }