Skip to content

Commit 175b89c

Browse files
committed
revert: databasebackup
1 parent 2313fed commit 175b89c

File tree

1 file changed

+80
-8
lines changed

1 file changed

+80
-8
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Actions\Database\StopDatabase;
66
use App\Events\BackupCreated;
7+
use App\Models\InstanceSettings;
78
use App\Models\S3Storage;
89
use App\Models\ScheduledDatabaseBackup;
910
use App\Models\ScheduledDatabaseBackupExecution;
@@ -478,10 +479,37 @@ private function remove_old_backups(): void
478479
}
479480
}
480481

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+
// }
481510
private function upload_to_s3(): void
482511
{
483512
try {
484-
ray($this->backup_location);
485513
if (is_null($this->s3)) {
486514
return;
487515
}
@@ -491,20 +519,64 @@ private function upload_to_s3(): void
491519
$bucket = $this->s3->bucket;
492520
$endpoint = $this->s3->endpoint;
493521
$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+
}
495527

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}/";
499534
instant_remote_process($commands, $this->server);
500535
$this->add_to_backup_output('Uploaded to S3.');
501536
} catch (\Throwable $e) {
502537
$this->add_to_backup_output($e->getMessage());
503538
throw $e;
504539
} 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);
508553
}
509554
}
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+
}
510582
}

0 commit comments

Comments
 (0)