Skip to content

Commit 6066fa4

Browse files
committed
Prune server backups from the DB after 30 minutes if they still have not completed
1 parent f144ba8 commit 6066fa4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Pterodactyl\Console\Commands\Maintenance;
4+
5+
use Carbon\CarbonImmutable;
6+
use InvalidArgumentException;
7+
use Illuminate\Console\Command;
8+
use Pterodactyl\Repositories\Eloquent\BackupRepository;
9+
10+
class PruneOrphanedBackupsCommand extends Command
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected $signature = 'p:maintenance:prune-backups {--since-minutes=30}';
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $description = 'Removes all backups that have existed for more than "n" minutes which are not marked as completed.';
21+
22+
/**
23+
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
24+
*/
25+
public function handle(BackupRepository $repository)
26+
{
27+
$since = $this->option('since-minutes');
28+
if (!is_digit($since)) {
29+
throw new InvalidArgumentException('The --since-minutes option must be a valid numeric digit.');
30+
}
31+
32+
$query = $repository->getBuilder()
33+
->whereNull('completed_at')
34+
->whereDate('created_at', '<=', CarbonImmutable::now()->subMinutes($since));
35+
36+
$count = $query->count();
37+
if (!$count) {
38+
$this->info('There are no orphaned backups to be removed.');
39+
40+
return;
41+
}
42+
43+
$this->warn("Deleting {$count} backups that have not been marked as completed in the last {$since} minutes.");
44+
45+
$query->delete();
46+
}
47+
}

app/Console/Kernel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ protected function commands()
2222
*/
2323
protected function schedule(Schedule $schedule)
2424
{
25+
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
2526
$schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping();
27+
28+
// Every 30 minutes, run the backup pruning command so that any abandoned backups can be removed
29+
// from the UI view for the server.
30+
$schedule->command('p:maintenance:prune-backups', [
31+
'--since-minutes' => '30',
32+
])->everyThirtyMinutes();
33+
34+
// Every day cleanup any internal backups of service files.
2635
$schedule->command('p:maintenance:clean-service-backups')->daily();
2736
}
2837
}

0 commit comments

Comments
 (0)