2020-04-04 12:26:39 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Backups;
|
|
|
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-04-18 23:26:59 -07:00
|
|
|
use Webmozart\Assert\Assert;
|
2020-04-04 12:26:39 -07:00
|
|
|
use Pterodactyl\Models\Backup;
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-04-04 20:09:33 -07:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-26 16:07:36 -07:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-04 12:26:39 -07:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2020-04-04 20:09:33 -07:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2020-04-26 12:21:14 -07:00
|
|
|
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
|
2020-04-19 19:43:41 -07:00
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
2020-04-04 12:26:39 -07:00
|
|
|
|
|
|
|
class InitiateBackupService
|
|
|
|
{
|
2022-10-14 10:59:20 -06:00
|
|
|
private ?array $ignoredFiles;
|
2020-04-04 20:09:33 -07:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
private bool $isLocked = false;
|
2020-11-09 20:35:57 -03:00
|
|
|
|
2020-04-04 12:26:39 -07:00
|
|
|
/**
|
|
|
|
* InitiateBackupService constructor.
|
|
|
|
*/
|
2020-04-04 20:09:33 -07:00
|
|
|
public function __construct(
|
2022-10-14 10:59:20 -06:00
|
|
|
private BackupRepository $repository,
|
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private DaemonBackupRepository $daemonBackupRepository,
|
|
|
|
private DeleteBackupService $deleteBackupService,
|
|
|
|
private BackupManager $backupManager
|
2020-04-04 20:09:33 -07:00
|
|
|
) {
|
2020-04-04 12:26:39 -07:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:26:09 -07:00
|
|
|
/**
|
|
|
|
* Set if the backup should be locked once it is created which will prevent
|
|
|
|
* its deletion by users or automated system processes.
|
|
|
|
*/
|
|
|
|
public function setIsLocked(bool $isLocked): self
|
|
|
|
{
|
|
|
|
$this->isLocked = $isLocked;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:26:39 -07:00
|
|
|
/**
|
|
|
|
* Sets the files to be ignored by this backup.
|
|
|
|
*
|
2020-04-18 23:26:59 -07:00
|
|
|
* @param string[]|null $ignored
|
2020-04-04 12:26:39 -07:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function setIgnoredFiles(?array $ignored): self
|
2020-04-04 12:26:39 -07:00
|
|
|
{
|
2020-04-18 23:26:59 -07:00
|
|
|
if (is_array($ignored)) {
|
|
|
|
foreach ($ignored as $value) {
|
|
|
|
Assert::string($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the ignored files to be any values that are not empty in the array. Don't use
|
|
|
|
// the PHP empty function here incase anything that is "empty" by default (0, false, etc.)
|
|
|
|
// were passed as a file or folder name.
|
|
|
|
$this->ignoredFiles = is_null($ignored) ? [] : array_filter($ignored, function ($value) {
|
|
|
|
return strlen($value) > 0;
|
|
|
|
});
|
2020-04-04 12:26:39 -07:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-03 21:26:09 -07:00
|
|
|
* Initiates the backup process for a server on Wings.
|
2020-04-04 12:26:39 -07:00
|
|
|
*
|
2020-04-04 20:09:33 -07:00
|
|
|
* @throws \Throwable
|
2020-04-26 12:21:14 -07:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
|
2020-04-04 12:26:39 -07:00
|
|
|
*/
|
2020-11-09 21:14:47 -03:00
|
|
|
public function handle(Server $server, string $name = null, bool $override = false): Backup
|
2020-04-04 12:26:39 -07:00
|
|
|
{
|
2020-12-27 16:41:53 -08:00
|
|
|
$limit = config('backups.throttles.limit');
|
|
|
|
$period = config('backups.throttles.period');
|
|
|
|
if ($period > 0) {
|
|
|
|
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, $period);
|
|
|
|
if ($previous->count() >= $limit) {
|
2021-05-03 21:26:09 -07:00
|
|
|
$message = sprintf('Only %d backups may be generated within a %d second span of time.', $limit, $period);
|
|
|
|
|
|
|
|
throw new TooManyRequestsHttpException(CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addSeconds($period)), $message);
|
2020-12-27 16:41:53 -08:00
|
|
|
}
|
2020-12-06 13:55:45 -07:00
|
|
|
}
|
2020-04-19 19:43:41 -07:00
|
|
|
|
2021-08-03 20:45:25 -06:00
|
|
|
// Check if the server has reached or exceeded its backup limit.
|
|
|
|
// completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups.
|
2021-08-04 21:34:00 -06:00
|
|
|
$successful = $this->repository->getNonFailedBackups($server);
|
2021-05-03 21:26:09 -07:00
|
|
|
if (!$server->backup_limit || $successful->count() >= $server->backup_limit) {
|
2020-11-11 10:52:28 -03:00
|
|
|
// Do not allow the user to continue if this server is already at its limit and can't override.
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$override || $server->backup_limit <= 0) {
|
2020-11-09 20:35:57 -03:00
|
|
|
throw new TooManyBackupsException($server->backup_limit);
|
|
|
|
}
|
2020-11-11 10:52:28 -03:00
|
|
|
|
2021-05-03 21:26:09 -07:00
|
|
|
// Get the oldest backup the server has that is not "locked" (indicating a backup that should
|
|
|
|
// never be automatically purged). If we find a backup we will delete it and then continue with
|
|
|
|
// this process. If no backup is found that can be used an exception is thrown.
|
|
|
|
$oldest = $successful->where('is_locked', false)->orderBy('created_at')->first();
|
|
|
|
if (!$oldest) {
|
|
|
|
throw new TooManyBackupsException($server->backup_limit);
|
|
|
|
}
|
2020-12-07 09:31:44 -07:00
|
|
|
|
2022-11-28 11:56:03 -05:00
|
|
|
/* @var Backup $oldest */
|
2021-05-03 21:26:09 -07:00
|
|
|
$this->deleteBackupService->handle($oldest);
|
2020-11-11 10:52:28 -03:00
|
|
|
}
|
2020-11-09 20:35:57 -03:00
|
|
|
|
2020-04-04 20:09:33 -07:00
|
|
|
return $this->connection->transaction(function () use ($server, $name) {
|
2022-11-28 11:56:03 -05:00
|
|
|
/** @var Backup $backup */
|
2020-04-04 20:09:33 -07:00
|
|
|
$backup = $this->repository->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
|
|
'name' => trim($name) ?: sprintf('Backup at %s', CarbonImmutable::now()->toDateTimeString()),
|
2020-12-27 16:47:51 -08:00
|
|
|
'ignored_files' => array_values($this->ignoredFiles ?? []),
|
2020-04-26 16:07:36 -07:00
|
|
|
'disk' => $this->backupManager->getDefaultAdapter(),
|
2021-05-03 21:26:09 -07:00
|
|
|
'is_locked' => $this->isLocked,
|
2020-04-04 20:09:33 -07:00
|
|
|
], true, true);
|
|
|
|
|
2020-11-09 21:14:47 -03:00
|
|
|
$this->daemonBackupRepository->setServer($server)
|
|
|
|
->setBackupAdapter($this->backupManager->getDefaultAdapter())
|
|
|
|
->backup($backup);
|
2020-04-04 12:26:39 -07:00
|
|
|
|
2020-04-04 20:09:33 -07:00
|
|
|
return $backup;
|
|
|
|
});
|
2020-04-04 12:26:39 -07:00
|
|
|
}
|
|
|
|
}
|