Skip to content

Commit 52efb07

Browse files
committed
Automatically detect old version
1 parent d34c9d4 commit 52efb07

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

src/Restic.php

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class Restic
3131
{
3232
public const RESTIC_VERSION = '0.18.0';
3333

34+
private const RESTIC_BINARY_NAME = 'restic_'.self::RESTIC_VERSION;
35+
3436
private const DOWNLOAD_URL = 'https://github.com/restic/restic/releases/download/v{version}/';
3537

3638
private const SHA_SUMS_FILE = __DIR__.'/../config/restic_sha256sums.txt';
@@ -93,7 +95,7 @@ private function __construct(
9395
* @param string $resticDirectory The name where the Restic binary is downloaded to. Relative to the backup directory (will be excluded from backups automatically). Default: var/restic
9496
* @param LoggerInterface $logger An optional PSR logger
9597
*/
96-
public static function create(string $backupDirectory, #[\SensitiveParameter] string $repository, #[\SensitiveParameter] string $password, array $excludes, #[\SensitiveParameter] array $env = [], string $resticDirectory = 'var/restic', LoggerInterface $logger = new NullLogger(),): self
98+
public static function create(string $backupDirectory, #[\SensitiveParameter] string $repository, #[\SensitiveParameter] string $password, array $excludes, #[\SensitiveParameter] array $env = [], string $resticDirectory = 'var/restic', LoggerInterface $logger = new NullLogger()): self
9799
{
98100
\assert('' !== $repository, 'The repository cannot be empty.');
99101
\assert('' !== $password, 'The password cannot be empty.');
@@ -151,20 +153,6 @@ public function getResticVersion(): string
151153
return $command->getResult()->getVersion();
152154
}
153155

154-
/**
155-
* @throws CouldNotDetermineResticVersionException|CouldNotDetermineBinaryException|CouldNotDownloadException|CouldNotExtractBinaryException|CouldNotVerifyFileIntegrityException
156-
*/
157-
public function updateRestic(): void
158-
{
159-
$this->ensureBinary();
160-
161-
if (self::RESTIC_VERSION === $this->getResticVersion()) {
162-
return;
163-
}
164-
165-
$this->ensureBinary(true);
166-
}
167-
168156
public function getItemsToBackup(): array
169157
{
170158
if ([] === $this->backupItemsConfig) {
@@ -301,16 +289,11 @@ private function determineCwd(): string
301289
*/
302290
private function ensureSetup(): void
303291
{
304-
$this->ensureDirectory();
292+
(new Filesystem())->mkdir($this->pathForResticDir());
305293
$this->ensureBinary();
306294
$this->ensureInit();
307295
}
308296

309-
private function ensureDirectory(): void
310-
{
311-
(new Filesystem())->mkdir($this->pathForResticDir());
312-
}
313-
314297
private function pathForResticDir(string ...$paths): string
315298
{
316299
return Path::join(
@@ -323,9 +306,9 @@ private function pathForResticDir(string ...$paths): string
323306
/**
324307
* @throws CouldNotDetermineBinaryException|CouldNotDownloadException|CouldNotExtractBinaryException|CouldNotVerifyFileIntegrityException
325308
*/
326-
private function ensureBinary(bool $forceUpdate = false): void
309+
private function ensureBinary(): void
327310
{
328-
if (!$forceUpdate && file_exists($this->getResticBinary())) {
311+
if (file_exists($this->getResticBinary())) {
329312
return;
330313
}
331314

@@ -337,9 +320,18 @@ private function ensureBinary(bool $forceUpdate = false): void
337320
throw new CouldNotDetermineBinaryException($uname);
338321
}
339322

323+
$fs = new Filesystem();
324+
325+
// Cleanup binary directory first, so we don't keep old Restic releases
326+
$fs->remove($this->pathForResticDir('binary'));
327+
$fs->mkdir($this->pathForResticDir('binary'));
328+
329+
// Determine compressed and uncompressed target names and create empty files
340330
$binarySourceName = str_replace('{version}', self::RESTIC_VERSION, $binarySourceName);
341-
$targetCompressed = $this->dumpEmptyResticBinary($binarySourceName);
342-
$targetUncompressed = $this->dumpEmptyResticBinary();
331+
$targetCompressed = $this->pathForResticDir('binary', $binarySourceName);
332+
$targetUncompressed = $this->getResticBinary();
333+
$fs->dumpFile($targetCompressed, '');
334+
$fs->dumpFile($targetUncompressed, '');
343335

344336
$client = HttpClient::create();
345337

@@ -353,8 +345,8 @@ private function ensureBinary(bool $forceUpdate = false): void
353345
}
354346
fclose($fileHandle);
355347
} catch (\Symfony\Contracts\HttpClient\Exception\ExceptionInterface $e) {
356-
(new Filesystem())->remove($targetCompressed);
357-
(new Filesystem())->remove($targetUncompressed);
348+
$fs->remove($targetCompressed);
349+
$fs->remove($targetUncompressed);
358350

359351
throw new CouldNotDownloadException('Could not download '.$binarySourceName, 0, $e);
360352
}
@@ -370,22 +362,13 @@ private function ensureBinary(bool $forceUpdate = false): void
370362
}
371363

372364
// Ensure file permissions and cleanup
373-
(new Filesystem())->chmod($targetUncompressed, 744);
374-
(new Filesystem())->remove($targetCompressed);
375-
}
376-
377-
private function getResticBinary(string $binaryName = 'restic'): string
378-
{
379-
return $this->pathForResticDir('binary', $binaryName);
365+
$fs->chmod($targetUncompressed, 744);
366+
$fs->remove($targetCompressed);
380367
}
381368

382-
private function dumpEmptyResticBinary(string $binaryName = 'restic'): string
369+
private function getResticBinary(): string
383370
{
384-
$target = $this->getResticBinary($binaryName);
385-
$fs = new Filesystem();
386-
$fs->dumpFile($target, '');
387-
388-
return $target;
371+
return $this->pathForResticDir('binary', self::RESTIC_BINARY_NAME);
389372
}
390373

391374
private function ensureInit(): void

0 commit comments

Comments
 (0)