Skip to content

Commit 9a1c9f3

Browse files
authored
Merge pull request pterodactyl#2691 from GravityCube/develop
Backup rotation for schedules.
2 parents 7ebe04f + 8fbcbce commit 9a1c9f3

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

app/Jobs/Schedule/RunTaskJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function handle(
6969
$commandRepository->setServer($server)->send($this->task->payload);
7070
break;
7171
case 'backup':
72-
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null);
72+
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
7373
break;
7474
default:
7575
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');

app/Services/Backups/InitiateBackupService.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
1414
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
1515
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
16+
use Pterodactyl\Services\Backups\DeleteBackupService;
1617

1718
class InitiateBackupService
1819
{
@@ -41,24 +42,32 @@ class InitiateBackupService
4142
*/
4243
private $backupManager;
4344

45+
/**
46+
* @var \Pterodactyl\Services\Backups\DeleteBackupService
47+
*/
48+
private $deleteBackupService;
49+
4450
/**
4551
* InitiateBackupService constructor.
4652
*
4753
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
4854
* @param \Illuminate\Database\ConnectionInterface $connection
4955
* @param \Pterodactyl\Repositories\Wings\DaemonBackupRepository $daemonBackupRepository
56+
* @param \Pterodactyl\Services\Backups\DeleteBackupService $deleteBackupService
5057
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
5158
*/
5259
public function __construct(
5360
BackupRepository $repository,
5461
ConnectionInterface $connection,
5562
DaemonBackupRepository $daemonBackupRepository,
63+
DeleteBackupService $deleteBackupService,
5664
BackupManager $backupManager
5765
) {
5866
$this->repository = $repository;
5967
$this->connection = $connection;
6068
$this->daemonBackupRepository = $daemonBackupRepository;
6169
$this->backupManager = $backupManager;
70+
$this->deleteBackupService = $deleteBackupService;
6271
}
6372

6473
/**
@@ -96,13 +105,8 @@ public function setIgnoredFiles(?array $ignored)
96105
* @throws \Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException
97106
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
98107
*/
99-
public function handle(Server $server, string $name = null): Backup
108+
public function handle(Server $server, string $name = null, bool $override = false): Backup
100109
{
101-
// Do not allow the user to continue if this server is already at its limit.
102-
if (! $server->backup_limit || $server->backups()->where('is_successful', true)->count() >= $server->backup_limit) {
103-
throw new TooManyBackupsException($server->backup_limit);
104-
}
105-
106110
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
107111
if ($previous->count() >= 2) {
108112
throw new TooManyRequestsHttpException(
@@ -111,6 +115,18 @@ public function handle(Server $server, string $name = null): Backup
111115
);
112116
}
113117

118+
// Check if the server has reached or exceeded it's backup limit
119+
if (!$server->backup_limit || $server->backups()->where('is_successful', true)->count() >= $server->backup_limit) {
120+
// Do not allow the user to continue if this server is already at its limit and can't override.
121+
if (!$override || $server->backup_limit <= 0) {
122+
throw new TooManyBackupsException($server->backup_limit);
123+
}
124+
125+
// Remove oldest backup
126+
$oldestBackup = $server->backups()->where('is_successful', true)->orderByDesc('created_at')->first();
127+
$this->deleteBackupService->handle($oldestBackup);
128+
}
129+
114130
return $this->connection->transaction(function () use ($server, $name) {
115131
/** @var \Pterodactyl\Models\Backup $backup */
116132
$backup = $this->repository->create([

resources/scripts/components/server/schedules/TaskDetailsModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const TaskDetailsForm = ({ isEditingTask }: { isEditingTask: boolean }) => {
9191
<Label>Ignored Files</Label>
9292
<FormikFieldWrapper
9393
name={'payload'}
94-
description={'Optional. Include the files and folders to be excluded in this backup. By default, the contents of your .pteroignore file will be used.'}
94+
description={'Optional. Include the files and folders to be excluded in this backup. By default, the contents of your .pteroignore file will be used. If you have reached your backup limit, the oldest backup will be rotated.'}
9595
>
9696
<FormikField as={Textarea} name={'payload'} rows={6} />
9797
</FormikFieldWrapper>

0 commit comments

Comments
 (0)