@@ -31,6 +31,8 @@ final class Restic
31
31
{
32
32
public const RESTIC_VERSION = '0.18.0 ' ;
33
33
34
+ private const RESTIC_BINARY_NAME = 'restic_ ' .self ::RESTIC_VERSION ;
35
+
34
36
private const DOWNLOAD_URL = 'https://github.com/restic/restic/releases/download/v{version}/ ' ;
35
37
36
38
private const SHA_SUMS_FILE = __DIR__ .'/../config/restic_sha256sums.txt ' ;
@@ -93,7 +95,7 @@ private function __construct(
93
95
* @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
94
96
* @param LoggerInterface $logger An optional PSR logger
95
97
*/
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
97
99
{
98
100
\assert ('' !== $ repository , 'The repository cannot be empty. ' );
99
101
\assert ('' !== $ password , 'The password cannot be empty. ' );
@@ -151,20 +153,6 @@ public function getResticVersion(): string
151
153
return $ command ->getResult ()->getVersion ();
152
154
}
153
155
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
-
168
156
public function getItemsToBackup (): array
169
157
{
170
158
if ([] === $ this ->backupItemsConfig ) {
@@ -301,16 +289,11 @@ private function determineCwd(): string
301
289
*/
302
290
private function ensureSetup (): void
303
291
{
304
- $ this ->ensureDirectory ( );
292
+ ( new Filesystem ())-> mkdir ( $ this ->pathForResticDir () );
305
293
$ this ->ensureBinary ();
306
294
$ this ->ensureInit ();
307
295
}
308
296
309
- private function ensureDirectory (): void
310
- {
311
- (new Filesystem ())->mkdir ($ this ->pathForResticDir ());
312
- }
313
-
314
297
private function pathForResticDir (string ...$ paths ): string
315
298
{
316
299
return Path::join (
@@ -323,9 +306,9 @@ private function pathForResticDir(string ...$paths): string
323
306
/**
324
307
* @throws CouldNotDetermineBinaryException|CouldNotDownloadException|CouldNotExtractBinaryException|CouldNotVerifyFileIntegrityException
325
308
*/
326
- private function ensureBinary (bool $ forceUpdate = false ): void
309
+ private function ensureBinary (): void
327
310
{
328
- if (! $ forceUpdate && file_exists ($ this ->getResticBinary ())) {
311
+ if (file_exists ($ this ->getResticBinary ())) {
329
312
return ;
330
313
}
331
314
@@ -337,9 +320,18 @@ private function ensureBinary(bool $forceUpdate = false): void
337
320
throw new CouldNotDetermineBinaryException ($ uname );
338
321
}
339
322
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
340
330
$ 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 , '' );
343
335
344
336
$ client = HttpClient::create ();
345
337
@@ -353,8 +345,8 @@ private function ensureBinary(bool $forceUpdate = false): void
353
345
}
354
346
fclose ($ fileHandle );
355
347
} 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 );
358
350
359
351
throw new CouldNotDownloadException ('Could not download ' .$ binarySourceName , 0 , $ e );
360
352
}
@@ -370,22 +362,13 @@ private function ensureBinary(bool $forceUpdate = false): void
370
362
}
371
363
372
364
// 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 );
380
367
}
381
368
382
- private function dumpEmptyResticBinary ( string $ binaryName = ' restic ' ): string
369
+ private function getResticBinary ( ): string
383
370
{
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 );
389
372
}
390
373
391
374
private function ensureInit (): void
0 commit comments