4
4
5
5
use App\Actions\Database\StopDatabase;
6
6
use App\Events\BackupCreated;
7
+ use App\Models\InstanceSettings;
7
8
use App\Models\S3Storage;
8
9
use App\Models\ScheduledDatabaseBackup;
9
10
use App\Models\ScheduledDatabaseBackupExecution;
@@ -478,10 +479,37 @@ private function remove_old_backups(): void
478
479
}
479
480
}
480
481
482
+ // private function upload_to_s3(): void
483
+ // {
484
+ // try {
485
+ // if (is_null($this->s3)) {
486
+ // return;
487
+ // }
488
+ // $key = $this->s3->key;
489
+ // $secret = $this->s3->secret;
490
+ // // $region = $this->s3->region;
491
+ // $bucket = $this->s3->bucket;
492
+ // $endpoint = $this->s3->endpoint;
493
+ // $this->s3->testConnection(shouldSave: true);
494
+ // $configName = new Cuid2;
495
+
496
+ // $s3_copy_dir = str($this->backup_location)->replace(backup_dir(), '/var/www/html/storage/app/backups/');
497
+ // $commands[] = "docker exec coolify bash -c 'mc config host add {$configName} {$endpoint} $key $secret'";
498
+ // $commands[] = "docker exec coolify bash -c 'mc cp $s3_copy_dir {$configName}/{$bucket}{$this->backup_dir}/'";
499
+ // instant_remote_process($commands, $this->server);
500
+ // $this->add_to_backup_output('Uploaded to S3.');
501
+ // } catch (\Throwable $e) {
502
+ // $this->add_to_backup_output($e->getMessage());
503
+ // throw $e;
504
+ // } finally {
505
+ // $removeConfigCommands[] = "docker exec coolify bash -c 'mc config remove {$configName}'";
506
+ // $removeConfigCommands[] = "docker exec coolify bash -c 'mc alias rm {$configName}'";
507
+ // instant_remote_process($removeConfigCommands, $this->server, false);
508
+ // }
509
+ // }
481
510
private function upload_to_s3(): void
482
511
{
483
512
try {
484
- ray($this->backup_location);
485
513
if (is_null($this->s3)) {
486
514
return;
487
515
}
@@ -491,20 +519,64 @@ private function upload_to_s3(): void
491
519
$bucket = $this->s3->bucket;
492
520
$endpoint = $this->s3->endpoint;
493
521
$this->s3->testConnection(shouldSave: true);
494
- $configName = new Cuid2;
522
+ if (data_get($this->backup, 'database_type') === 'App\Models\ServiceDatabase') {
523
+ $network = $this->database->service->destination->network;
524
+ } else {
525
+ $network = $this->database->destination->network;
526
+ }
495
527
496
- $s3_copy_dir = str($this->backup_location)->replace(backup_dir(), '/var/www/html/storage/app/backups/');
497
- $commands[] = "docker exec coolify bash -c 'mc config host add {$configName} {$endpoint} $key $secret'";
498
- $commands[] = "docker exec coolify bash -c 'mc cp $s3_copy_dir {$configName}/{$bucket}{$this->backup_dir}/'";
528
+ $this->ensureHelperImageAvailable();
529
+
530
+ $fullImageName = $this->getFullImageName();
531
+ $commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro {$fullImageName}";
532
+ $commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";
533
+ $commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
499
534
instant_remote_process($commands, $this->server);
500
535
$this->add_to_backup_output('Uploaded to S3.');
501
536
} catch (\Throwable $e) {
502
537
$this->add_to_backup_output($e->getMessage());
503
538
throw $e;
504
539
} finally {
505
- $removeConfigCommands[] = "docker exec coolify bash -c 'mc config remove {$configName}'";
506
- $removeConfigCommands[] = "docker exec coolify bash -c 'mc alias rm {$configName}'";
507
- instant_remote_process($removeConfigCommands, $this->server, false);
540
+ $command = "docker rm -f backup-of-{$this->backup->uuid}";
541
+ instant_remote_process([$command], $this->server);
542
+ }
543
+ }
544
+
545
+ private function ensureHelperImageAvailable(): void
546
+ {
547
+ $fullImageName = $this->getFullImageName();
548
+
549
+ $imageExists = $this->checkImageExists($fullImageName);
550
+
551
+ if (! $imageExists) {
552
+ $this->pullHelperImage($fullImageName);
508
553
}
509
554
}
555
+
556
+ private function checkImageExists(string $fullImageName): bool
557
+ {
558
+ $result = instant_remote_process(["docker image inspect {$fullImageName} >/dev/null 2>&1 && echo 'exists' || echo 'not exists'"], $this->server, false);
559
+
560
+ return trim($result) === 'exists';
561
+ }
562
+
563
+ private function pullHelperImage(string $fullImageName): void
564
+ {
565
+ try {
566
+ instant_remote_process(["docker pull {$fullImageName}"], $this->server);
567
+ } catch (\Exception $e) {
568
+ $errorMessage = 'Failed to pull helper image: '.$e->getMessage();
569
+ $this->add_to_backup_output($errorMessage);
570
+ throw new \RuntimeException($errorMessage);
571
+ }
572
+ }
573
+
574
+ private function getFullImageName(): string
575
+ {
576
+ $settings = InstanceSettings::get();
577
+ $helperImage = config('coolify.helper_image');
578
+ $latestVersion = $settings->helper_version;
579
+
580
+ return "{$helperImage}:{$latestVersion}";
581
+ }
510
582
}
0 commit comments