Skip to content

Commit f9452f0

Browse files
committed
Add maximum option
1 parent 3918aa8 commit f9452f0

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ return [
5858

5959
The backups list isn't paginated because it is not intended to keep hundreds of backups around. If included in a client website, you should include a note specifying an expected frequency of backup creation / deletion (or set up a CRON job).
6060

61+
You can enforce a maximum number of backups by setting the `maximum` option to any integer (default is `false`).
62+
If this number is reached the oldest backup will be deleted automatically whenever a new backup is created.
63+
64+
```php
65+
// site/config.php
66+
return [
67+
'sylvainjule.backups.maximum' => 5,
68+
];
69+
```
70+
71+
6172
There's also a way to disable the "Backups" menu-item for specific user roles:
6273

6374
```yml

index.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@
3333
],
3434
'options' => [
3535
'publicFolder' => 'backups-temp',
36-
'prefix' => 'backup-'
36+
'prefix' => 'backup-',
37+
'maximum' => false,
3738
],
3839
'api' => [
3940
'routes' => [
4041
[
4142
'pattern' => 'backups/create-backup',
4243
'action' => function() {
43-
$output = realpath(kirby()->roots()->accounts() .'/../') . '/backups/'. option('sylvainjule.backups.prefix') .'{{ timestamp }}.zip';
44+
$maximum = option('sylvainjule.backups.maximum');
45+
$b = new SylvainJule\Backups();
46+
$bCount = $b->getBackupsCount();
47+
48+
$output = realpath(kirby()->roots()->accounts() .'/../') . '/backups/'. option('sylvainjule.backups.prefix') .'{{ timestamp }}.zip';
49+
50+
if($maximum && $bCount == $maximum) {
51+
$b->deleteOldestBackup();
52+
}
4453

4554
return janitor()->command('janitor:backupzip --output '. $output .' --quiet');
4655
}

lib/backups.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function getBackupsArray($toApi = false) {
2828
return $backups;
2929
}
3030

31+
public function getBackupsCount() {
32+
return count($this->getBackupsArray());
33+
}
34+
3135
public function getToDeleteArray($stamp) {
3236
$backups = $this->getBackupsArray();
3337

@@ -50,6 +54,13 @@ public function deleteBackup($filename) {
5054
return $deleted;
5155
}
5256

57+
public function deleteOldestBackup() {
58+
$backups = $this->getBackupsArray();
59+
$filename = end($backups);
60+
61+
$this->deleteBackup($filename);
62+
}
63+
5364
public function simulateDeletion($period) {
5465
$stamp = $this->toTimestamp($period);
5566
$backups = $this->getBackupsArray();

0 commit comments

Comments
 (0)