Backup rotation for schedules.

This commit is contained in:
Gonzalo Chavez 2020-11-09 20:35:57 -03:00
parent 085fa8dcb5
commit 1f01c653f1
3 changed files with 24 additions and 11 deletions

View File

@ -69,7 +69,7 @@ class RunTaskJob extends Job implements ShouldQueue
$commandRepository->setServer($server)->send($this->task->payload);
break;
case 'backup':
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null);
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
break;
default:
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');

View File

@ -13,6 +13,7 @@ use Pterodactyl\Repositories\Eloquent\BackupRepository;
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Pterodactyl\Services\Backups\DeleteBackupService;
class InitiateBackupService
{
@ -41,24 +42,32 @@ class InitiateBackupService
*/
private $backupManager;
/**
* @var \Pterodactyl\Services\Backups\DeleteBackupService
*/
private $deleteBackupService;
/**
* InitiateBackupService constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonBackupRepository $daemonBackupRepository
* @param \Pterodactyl\Services\Backups\DeleteBackupService $deleteBackupService
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
*/
public function __construct(
BackupRepository $repository,
ConnectionInterface $connection,
DaemonBackupRepository $daemonBackupRepository,
DeleteBackupService $deleteBackupService,
BackupManager $backupManager
) {
$this->repository = $repository;
$this->connection = $connection;
$this->daemonBackupRepository = $daemonBackupRepository;
$this->backupManager = $backupManager;
$this->deleteBackupService = $deleteBackupService;
}
/**
@ -96,13 +105,8 @@ class InitiateBackupService
* @throws \Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*/
public function handle(Server $server, string $name = null): Backup
public function handle(Server $server, string $name = null, bool $override = null): Backup
{
// Do not allow the user to continue if this server is already at its limit.
if (! $server->backup_limit || $server->backups()->where('is_successful', true)->count() >= $server->backup_limit) {
throw new TooManyBackupsException($server->backup_limit);
}
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
if ($previous->count() >= 2) {
throw new TooManyRequestsHttpException(
@ -111,6 +115,17 @@ class InitiateBackupService
);
}
if (! $server->backup_limit || $server->backups()->where('is_successful', true)->count() >= $server->backup_limit) {
if($override){
// Remove latest backup
$last_backup = $server->backups()->where('is_successful', true)->oldest()->first();
$this->deleteBackupService->handle($last_backup);
}else{
// Do not allow the user to continue if this server is already at its limit.
throw new TooManyBackupsException($server->backup_limit);
}
}
return $this->connection->transaction(function () use ($server, $name) {
/** @var \Pterodactyl\Models\Backup $backup */
$backup = $this->repository->create([
@ -121,9 +136,7 @@ class InitiateBackupService
'disk' => $this->backupManager->getDefaultAdapter(),
], true, true);
$this->daemonBackupRepository->setServer($server)
->setBackupAdapter($this->backupManager->getDefaultAdapter())
->backup($backup);
$this->daemonBackupRepository->setServer($server)->setBackupAdapter($this->backupManager->getDefaultAdapter())->backup($backup);
return $backup;
});

View File

@ -91,7 +91,7 @@ const TaskDetailsForm = ({ isEditingTask }: { isEditingTask: boolean }) => {
<Label>Ignored Files</Label>
<FormikFieldWrapper
name={'payload'}
description={'Optional. Include the files and folders to be excluded in this backup. By default, the contents of your .pteroignore file will be used.'}
description={'Optional. Include the files and folders to be excluded in this backup. By default, the contents of your .pteroignore file will be used.\nIf you dont have more backups spaces it will OVERRIDE the oldest.'}
>
<FormikField as={Textarea} name={'payload'} rows={6} />
</FormikFieldWrapper>