25
25
use Illuminate \Queue \Middleware \WithoutOverlapping ;
26
26
use Illuminate \Queue \SerializesModels ;
27
27
use Illuminate \Support \Str ;
28
+ use App \Models \InstanceSettings ;
28
29
29
30
class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
30
31
{
@@ -493,12 +494,15 @@ private function upload_to_s3(): void
493
494
} else {
494
495
$ network = $ this ->database ->destination ->network ;
495
496
}
496
- $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ this ->backup_location : $ this ->backup_location :ro ghcr.io/coollabsio/coolify-helper " ;
497
+
498
+ $ this ->ensureHelperImageAvailable ();
499
+
500
+ $ fullImageName = $ this ->getFullImageName ();
501
+ $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ this ->backup_location : $ this ->backup_location :ro {$ fullImageName }" ;
497
502
$ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc config host add temporary {$ endpoint } $ key $ secret " ;
498
503
$ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc cp $ this ->backup_location temporary/ $ bucket {$ this ->backup_dir }/ " ;
499
504
instant_remote_process ($ commands , $ this ->server );
500
505
$ this ->add_to_backup_output ('Uploaded to S3. ' );
501
- ray ('Uploaded to S3. ' .$ this ->backup_location .' to s3:// ' .$ bucket .$ this ->backup_dir );
502
506
} catch (\Throwable $ e ) {
503
507
$ this ->add_to_backup_output ($ e ->getMessage ());
504
508
throw $ e ;
@@ -507,4 +511,40 @@ private function upload_to_s3(): void
507
511
instant_remote_process ([$ command ], $ this ->server );
508
512
}
509
513
}
514
+
515
+ private function ensureHelperImageAvailable (): void
516
+ {
517
+ $ fullImageName = $ this ->getFullImageName ();
518
+
519
+ $ imageExists = $ this ->checkImageExists ($ fullImageName );
520
+
521
+ if (!$ imageExists ) {
522
+ $ this ->pullHelperImage ($ fullImageName );
523
+ }
524
+ }
525
+
526
+ private function checkImageExists (string $ fullImageName ): bool
527
+ {
528
+ $ result = instant_remote_process (["docker image inspect {$ fullImageName } >/dev/null 2>&1 && echo 'exists' || echo 'not exists' " ], $ this ->server , false );
529
+ return trim ($ result ) === 'exists ' ;
530
+ }
531
+
532
+ private function pullHelperImage (string $ fullImageName ): void
533
+ {
534
+ try {
535
+ instant_remote_process (["docker pull {$ fullImageName }" ], $ this ->server );
536
+ } catch (\Exception $ e ) {
537
+ $ errorMessage = "Failed to pull helper image: " . $ e ->getMessage ();
538
+ $ this ->add_to_backup_output ($ errorMessage );
539
+ throw new \RuntimeException ($ errorMessage );
540
+ }
541
+ }
542
+
543
+ private function getFullImageName (): string
544
+ {
545
+ $ settings = InstanceSettings::get ();
546
+ $ helperImage = config ('coolify.helper_image ' );
547
+ $ latestVersion = $ settings ->helper_version ;
548
+ return "{$ helperImage }: {$ latestVersion }" ;
549
+ }
510
550
}
0 commit comments