Files

68 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Actions\Backups;
use Illuminate\Http\Response;
2026-10-05 18:10:18 -04:00
use LogicException;
use Pterodactyl\Contracts\Backups\DeletesBackups;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
use Pterodactyl\Extensions\Backups\BackupManager;
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
2026-10-11 14:15:22 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Backup;
use Throwable;
2026-10-05 18:10:18 -04:00
final readonly class DeleteBackup implements DeletesBackups
{
public function __construct(
2022-10-14 10:59:20 -06:00
private BackupManager $manager,
2026-10-05 18:10:18 -04:00
) {}
2026-10-05 18:10:18 -04:00
public function delete(Backup $backup): void
{
// 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.
2026-10-05 18:10:18 -04:00
throw_if($backup->is_locked && ($backup->is_successful && ($backup->completed_at) !== null), BackupLockedException::class);
2026-10-11 14:15:22 -04:00
// The stored archive is removed first, so the row is kept if its disk rejects the request.
2020-05-09 19:43:58 -07:00
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
$this->deleteFromS3($backup);
2026-10-11 14:15:22 -04:00
} else {
try {
Daemon::server($backup->server)->backups()->delete($backup);
} catch (DaemonConnectionException $daemonConnectionException) {
// Don't fail the request if the Daemon responds with a 404, just assume the backup
// doesn't actually exist and remove its reference from the Panel as well.
throw_if($daemonConnectionException->getStatusCode() !== Response::HTTP_NOT_FOUND, $daemonConnectionException);
}
2026-10-08 17:22:14 -04:00
}
2026-10-08 17:22:14 -04:00
$backup->delete();
2026-10-11 14:15:22 -04:00
Activity::event('server:backup.delete')
->subject($backup->server, $backup)
->property(['name' => $backup->name, 'failed' => ! $backup->is_successful])
->log();
}
2020-05-09 19:43:58 -07:00
2026-10-11 23:50:53 -04:00
/** @throws Throwable */
2026-10-05 18:10:18 -04:00
private function deleteFromS3(Backup $backup): void
2020-05-09 19:43:58 -07:00
{
2026-10-08 17:22:14 -04:00
$adapter = $this->manager->adapter(Backup::ADAPTER_AWS_S3);
throw_unless($adapter instanceof S3Filesystem, LogicException::class, 'The S3 backup adapter is not configured correctly.');
2020-05-09 19:43:58 -07:00
2026-10-08 17:22:14 -04:00
$adapter->getClient()->deleteObject([
'Bucket' => $adapter->getBucket(),
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
]);
2020-05-09 19:43:58 -07:00
}
}