2020-04-10 05:08:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Backups;
|
|
|
|
|
2020-05-10 02:19:45 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-05-10 02:19:45 +00:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2021-05-04 04:26:09 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
|
2020-05-10 02:19:45 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
class DeleteBackupService
|
|
|
|
{
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private BackupManager $manager,
|
|
|
|
private DaemonBackupRepository $daemonBackupRepository
|
2020-04-10 05:08:09 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-13 17:26:47 +00:00
|
|
|
* Deletes a backup from the system. If the backup is stored in S3 a request
|
|
|
|
* will be made to delete that backup from the disk as well.
|
2020-04-10 05:08:09 +00:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(Backup $backup): void
|
2020-04-10 05:08:09 +00:00
|
|
|
{
|
2021-06-13 17:26:47 +00:00
|
|
|
// If the backup is marked as failed it can still be deleted, even if locked
|
|
|
|
// since the UI doesn't allow you to unlock a failed backup in the first place.
|
|
|
|
//
|
|
|
|
// I also don't really see any reason you'd have a locked, failed backup to keep
|
|
|
|
// around. The logic that updates the backup to the failed state will also remove
|
|
|
|
// the lock, so this condition should really never happen.
|
2021-08-04 02:45:25 +00:00
|
|
|
if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {
|
2021-05-04 04:26:09 +00:00
|
|
|
throw new BackupLockedException();
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
|
|
|
|
$this->deleteFromS3($backup);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
2020-05-10 02:19:45 +00:00
|
|
|
try {
|
|
|
|
$this->daemonBackupRepository->setServer($backup->server)->delete($backup);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
$previous = $exception->getPrevious();
|
|
|
|
// Don't fail the request if the Daemon responds with a 404, just assume the backup
|
2022-10-14 16:59:20 +00:00
|
|
|
// doesn't actually exist and remove its reference from the Panel as well.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$previous instanceof ClientException || $previous->getResponse()->getStatusCode() !== Response::HTTP_NOT_FOUND) {
|
2020-05-10 02:19:45 +00:00
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$backup->delete();
|
2020-04-10 05:08:09 +00:00
|
|
|
});
|
|
|
|
}
|
2020-05-10 02:43:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a backup from an S3 disk.
|
|
|
|
*
|
2020-05-10 03:01:45 +00:00
|
|
|
* @throws \Throwable
|
2020-05-10 02:43:58 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function deleteFromS3(Backup $backup): void
|
2020-05-10 02:43:58 +00:00
|
|
|
{
|
2020-05-10 03:01:45 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
2022-10-14 16:59:20 +00:00
|
|
|
$backup->delete();
|
2020-05-10 02:43:58 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
/** @var \Pterodactyl\Extensions\Filesystem\S3Filesystem $adapter */
|
2020-05-10 03:01:45 +00:00
|
|
|
$adapter = $this->manager->adapter(Backup::ADAPTER_AWS_S3);
|
|
|
|
|
|
|
|
$adapter->getClient()->deleteObject([
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
|
|
|
|
]);
|
|
|
|
});
|
2020-05-10 02:43:58 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|