7bde1cfdf0
- one locking protocol per location: backup steps and restores hold a shared lock, prune and untrash an exclusive one and refuse while any run (backup, restore, export) exists; starting a backup checks and creates the run under the exclusive lock, closing the race with prune - snapshot entries are validated when read (relative path without '.', '..', NUL, backslash or control characters); raw restore also checks that the resolved directory stays below the target; the restore API and service accept canonical paths only - recovery kit confirmation requires a recent password confirmation and the confirmation code printed in the current kit file (new code per kit version), proving the kit was actually downloaded - prune decides on the authenticated index files on the location and refreshes the local index cache from them - restore job marks the run failed on any error; restore runs without progress for 6 hours are failed so they cannot block cleanup forever - tests for safe paths and a malicious snapshot path
66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
namespace OCA\NgBackup\BackgroundJob;
|
|
|
|
use OCA\NgBackup\Db\Run;
|
|
use OCA\NgBackup\Db\RunMapper;
|
|
use OCA\NgBackup\Service\RestoreService;
|
|
use OCA\NgBackup\Service\TargetService;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\BackgroundJob\QueuedJob;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/** Runs a restore requested from the web interface, outside the web request. */
|
|
class RestoreJob extends QueuedJob {
|
|
public function __construct(
|
|
ITimeFactory $time,
|
|
private RunMapper $runs,
|
|
private TargetService $targets,
|
|
private RestoreService $restore,
|
|
private LoggerInterface $logger,
|
|
private \OCA\NgBackup\Service\AlertService $alerts,
|
|
) {
|
|
parent::__construct($time);
|
|
}
|
|
|
|
protected function run($argument): void {
|
|
$runId = (int)($argument['run'] ?? 0);
|
|
try {
|
|
$run = $this->runs->find($runId);
|
|
} catch (\Throwable $e) {
|
|
$this->logger->error('NG Backup restore job: run ' . $runId . ' not found', ['exception' => $e]);
|
|
return;
|
|
}
|
|
if ($run->getStatus() !== Run::RUNNING) {
|
|
return;
|
|
}
|
|
try {
|
|
$req = json_decode($run->getState(), true, 512, JSON_THROW_ON_ERROR);
|
|
$run->setPhase('restoring');
|
|
$run->setUpdatedAt(time());
|
|
$run = $this->runs->update($run);
|
|
@set_time_limit(0);
|
|
$result = $this->restore->restoreUserFiles($this->targets->get((string)$run->getTargetId()), $req['snapshot'], $req['path'], $req['mode']);
|
|
$run->setStatus(Run::DONE);
|
|
$run->setPhase('done');
|
|
$run->setStats(json_encode($result, JSON_THROW_ON_ERROR));
|
|
} catch (\Throwable $e) {
|
|
$this->logger->error('NG Backup restore ' . $run->getId() . ' failed: ' . $e->getMessage(), ['exception' => $e]);
|
|
$run->setStatus(Run::FAILED);
|
|
$run->setError($e->getMessage());
|
|
try {
|
|
$this->alerts->runFailed($run, '#' . $run->getTargetId());
|
|
} catch (\Throwable) {
|
|
}
|
|
}
|
|
$run->setFinishedAt(time());
|
|
$run->setUpdatedAt(time());
|
|
$this->runs->update($run);
|
|
}
|
|
}
|