Skip to content

Commit 644ba8b

Browse files
committed
added backup prune command
1 parent fc1da4e commit 644ba8b

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Classes/Command/BackupCommandController.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function restoreCommand(string $name, bool $noConfirm = false): void
5656
}
5757

5858
if ($noConfirm === false) {
59-
$shouldRestore = $this->output->askConfirmation('Are you sure you want to restore this Backup?', false);
59+
$shouldRestore = $this->output->askConfirmation('Are you sure you want to restore this Backup? ', false);
6060
}
6161

6262
if(!$shouldRestore) {
@@ -104,7 +104,7 @@ public function deleteCommand(string $name, bool $noConfirm = false): void
104104
$confirmed = true;
105105

106106
if (!$noConfirm) {
107-
$confirmed = $this->output->askConfirmation('Are you sure you want to delete this backup?');
107+
$confirmed = $this->output->askConfirmation('Are you sure you want to delete this backup? ');
108108
}
109109

110110
if (!$confirmed) {
@@ -123,6 +123,43 @@ public function deleteCommand(string $name, bool $noConfirm = false): void
123123
}
124124
}
125125

126+
/**
127+
* deletes all backups, but can keep X latest backups
128+
*
129+
* @param int $keep keep the latest X backups
130+
* @param bool $noConfirm If true, you dant have to confirm the delete action
131+
*/
132+
public function pruneCommand(int $keep = 0, bool $noConfirm = false): void
133+
{
134+
$confirmed = true;
135+
136+
if (!$noConfirm) {
137+
$question = ($keep > 0 ? ', except the latest '.$keep.' backups': '');
138+
$question = 'Are you sure you want to delete all backups'.$question.'? ';
139+
$confirmed = $this->output->askConfirmation($question);
140+
}
141+
142+
if (!$confirmed) {
143+
$this->outputLine();
144+
$this->outputLine('<error>Canceled by user</error>');
145+
$this->quit();
146+
}
147+
148+
$backupCount = $this->backupService->getCount();
149+
$backups = $this->backupService->getBackups($keep, $backupCount);
150+
151+
$this->output('Deleting '.($backupCount - $keep <= 0 ? '0': $backupCount - $keep).' backups...');
152+
153+
try {
154+
foreach ($backups as $backup) {
155+
$this->backupService->deleteBackup($backup['name']);
156+
}
157+
$this->outputLine('<success>success</success>');
158+
} catch (\Exception $e) {
159+
$this->outputError($e->getMessage());
160+
}
161+
}
162+
126163
protected function outputError(string $message): void
127164
{
128165
$this->outputLine('<error>failed</error>');

Classes/Service/BackupIndexService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public function deleteBackup(string $name)
128128
return $success;
129129
}
130130

131+
public function getCount(): int
132+
{
133+
return $this->getReader()->count();
134+
}
135+
131136
public function getBackups(int $start = 0, int $limit = 25): array
132137
{
133138
$reader = $this->getReader();

Classes/Service/BackupService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public function getStepsInstances(string $backupPath, array $whiteList = null):
191191
return $steps;
192192
}
193193

194+
public function getCount(): int
195+
{
196+
return $this->indexService->getCount();
197+
}
194198

195199
public function noStepsConfigured(): bool
196200
{

Documentation/commands.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,20 @@ Deletes a single backup.
4949
#### Options
5050
`--no-confirm` <br />
5151
If this flag is set, you dont have to confirm this action by pressing <kbd>Y</kbd> + <kbd>Return</kbd>.
52+
53+
54+
55+
### `backup:prune`
56+
57+
Deletes all backups, but you can keep the latest X Backups with the `--keep` option.
58+
59+
```bash
60+
./flow backup:prune
61+
```
62+
63+
#### Options
64+
`--no-confirm` <br />
65+
If this flag is set, you dont have to confirm this action by pressing <kbd>Y</kbd> + <kbd>Return</kbd>.
66+
67+
`--keep x` <br />
68+
Keep the latest X Backups

0 commit comments

Comments
 (0)