2020-04-04 19:26:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Backups;
|
|
|
|
|
2020-04-20 02:43:41 +00:00
|
|
|
use Carbon\Carbon;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-04-19 06:26:59 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-04-26 23:07:36 +00:00
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
2020-04-05 03:09:33 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-26 23:07:36 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2020-04-05 03:09:33 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2020-04-26 19:21:14 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
|
2020-04-20 02:43:41 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
2020-04-04 19:26:39 +00:00
|
|
|
|
|
|
|
class InitiateBackupService
|
|
|
|
{
|
|
|
|
/**
|
2020-04-19 06:26:59 +00:00
|
|
|
* @var string[]|null
|
2020-04-04 19:26:39 +00:00
|
|
|
*/
|
|
|
|
private $ignoredFiles;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-04-05 03:09:33 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository
|
|
|
|
*/
|
|
|
|
private $daemonBackupRepository;
|
|
|
|
|
2020-04-26 23:07:36 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $backupManager;
|
|
|
|
|
2020-04-04 19:26:39 +00:00
|
|
|
/**
|
|
|
|
* InitiateBackupService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
|
2020-04-05 03:09:33 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonBackupRepository $daemonBackupRepository
|
2020-04-26 23:07:36 +00:00
|
|
|
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
|
2020-04-04 19:26:39 +00:00
|
|
|
*/
|
2020-04-05 03:09:33 +00:00
|
|
|
public function __construct(
|
|
|
|
BackupRepository $repository,
|
|
|
|
ConnectionInterface $connection,
|
2020-04-26 23:07:36 +00:00
|
|
|
DaemonBackupRepository $daemonBackupRepository,
|
|
|
|
BackupManager $backupManager
|
2020-04-05 03:09:33 +00:00
|
|
|
) {
|
2020-04-04 19:26:39 +00:00
|
|
|
$this->repository = $repository;
|
2020-04-05 03:09:33 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->daemonBackupRepository = $daemonBackupRepository;
|
2020-04-26 23:07:36 +00:00
|
|
|
$this->backupManager = $backupManager;
|
2020-04-04 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the files to be ignored by this backup.
|
|
|
|
*
|
2020-04-19 06:26:59 +00:00
|
|
|
* @param string[]|null $ignored
|
2020-04-04 19:26:39 +00:00
|
|
|
* @return $this
|
|
|
|
*/
|
2020-04-19 06:26:59 +00:00
|
|
|
public function setIgnoredFiles(?array $ignored)
|
2020-04-04 19:26:39 +00:00
|
|
|
{
|
2020-04-19 06:26:59 +00: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 19:26:39 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates the backup process for a server on the daemon.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param string|null $name
|
|
|
|
* @return \Pterodactyl\Models\Backup
|
|
|
|
*
|
2020-04-05 03:09:33 +00:00
|
|
|
* @throws \Throwable
|
2020-04-26 19:21:14 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
|
2020-04-04 19:26:39 +00:00
|
|
|
*/
|
|
|
|
public function handle(Server $server, string $name = null): Backup
|
|
|
|
{
|
2020-04-26 19:21:14 +00:00
|
|
|
// Do not allow the user to continue if this server is already at its limit.
|
|
|
|
if (! $server->backup_limit || $server->backups()->count() >= $server->backup_limit) {
|
|
|
|
throw new TooManyBackupsException($server->backup_limit);
|
|
|
|
}
|
|
|
|
|
2020-04-20 02:43:41 +00:00
|
|
|
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
|
|
|
|
if ($previous->count() >= 2) {
|
|
|
|
throw new TooManyRequestsHttpException(
|
|
|
|
Carbon::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
|
|
|
|
'Only two backups may be generated within a 10 minute span of time.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-05 03:09:33 +00:00
|
|
|
return $this->connection->transaction(function () use ($server, $name) {
|
|
|
|
/** @var \Pterodactyl\Models\Backup $backup */
|
|
|
|
$backup = $this->repository->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
|
|
'name' => trim($name) ?: sprintf('Backup at %s', CarbonImmutable::now()->toDateTimeString()),
|
2020-04-19 06:26:59 +00:00
|
|
|
'ignored_files' => is_array($this->ignoredFiles) ? array_values($this->ignoredFiles) : [],
|
2020-04-26 23:07:36 +00:00
|
|
|
'disk' => $this->backupManager->getDefaultAdapter(),
|
2020-04-05 03:09:33 +00:00
|
|
|
], true, true);
|
|
|
|
|
2020-04-26 23:07:36 +00:00
|
|
|
$url = $this->getS3PresignedUrl(sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid));
|
|
|
|
|
|
|
|
$this->daemonBackupRepository->setServer($server)
|
|
|
|
->setBackupAdapter($this->backupManager->getDefaultAdapter())
|
|
|
|
->backup($backup, $url);
|
2020-04-04 19:26:39 +00:00
|
|
|
|
2020-04-05 03:09:33 +00:00
|
|
|
return $backup;
|
|
|
|
});
|
2020-04-04 19:26:39 +00:00
|
|
|
}
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a presigned URL for the wings daemon to upload the completed archive
|
|
|
|
* to. We use a 30 minute expiration on these URLs to avoid issues with large backups
|
|
|
|
* that may take some time to complete.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
protected function getS3PresignedUrl(string $path)
|
|
|
|
{
|
|
|
|
$adapter = $this->backupManager->adapter();
|
|
|
|
if (! $adapter instanceof AwsS3Adapter) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = $adapter->getClient();
|
|
|
|
|
|
|
|
$request = $client->createPresignedRequest(
|
|
|
|
$client->getCommand('PutObject', [
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => $path,
|
|
|
|
'ContentType' => 'binary/octet-stream',
|
|
|
|
]),
|
|
|
|
CarbonImmutable::now()->addMinutes(30)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $request->getUri()->__toString();
|
|
|
|
}
|
2020-04-04 19:26:39 +00:00
|
|
|
}
|