Skip to content

Commit 8712af7

Browse files
committed
fix: move mc command to coolify image from helper
1 parent 1a40beb commit 8712af7

File tree

4 files changed

+15
-51
lines changed

4 files changed

+15
-51
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Illuminate\Queue\Middleware\WithoutOverlapping;
2626
use Illuminate\Queue\SerializesModels;
2727
use Illuminate\Support\Str;
28-
use App\Models\InstanceSettings;
28+
use Visus\Cuid2\Cuid2;
2929

3030
class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
3131
{
@@ -399,6 +399,7 @@ private function backup_standalone_postgresql(string $database): void
399399
$backupCommand .= " $this->container_name pg_dump --format=custom --no-acl --no-owner --username {$this->database->postgres_user} $database > $this->backup_location";
400400

401401
$commands[] = $backupCommand;
402+
ray($commands);
402403
$this->backup_output = instant_remote_process($commands, $this->server);
403404
$this->backup_output = trim($this->backup_output);
404405
if ($this->backup_output === '') {
@@ -480,6 +481,7 @@ private function remove_old_backups(): void
480481
private function upload_to_s3(): void
481482
{
482483
try {
484+
ray($this->backup_location);
483485
if (is_null($this->s3)) {
484486
return;
485487
}
@@ -489,62 +491,20 @@ private function upload_to_s3(): void
489491
$bucket = $this->s3->bucket;
490492
$endpoint = $this->s3->endpoint;
491493
$this->s3->testConnection(shouldSave: true);
492-
if (data_get($this->backup, 'database_type') === 'App\Models\ServiceDatabase') {
493-
$network = $this->database->service->destination->network;
494-
} else {
495-
$network = $this->database->destination->network;
496-
}
497-
498-
$this->ensureHelperImageAvailable();
494+
$configName = new Cuid2;
499495

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}";
502-
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";
503-
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
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}/'";
504499
instant_remote_process($commands, $this->server);
505500
$this->add_to_backup_output('Uploaded to S3.');
506501
} catch (\Throwable $e) {
507502
$this->add_to_backup_output($e->getMessage());
508503
throw $e;
509504
} finally {
510-
$command = "docker rm -f backup-of-{$this->backup->uuid}";
511-
instant_remote_process([$command], $this->server);
512-
}
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);
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);
523508
}
524509
}
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-
}
550510
}

docker-compose.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ services:
1919
PUSHER_APP_SECRET: "${PUSHER_APP_SECRET:-coolify}"
2020
volumes:
2121
- .:/var/www/html/:cached
22+
- /data/coolify/backups/:/var/www/html/storage/app/backups
2223
postgres:
2324
pull_policy: always
2425
ports:

docker/coolify-helper/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN if [[ ${TARGETPLATFORM} == 'linux/arm64' ]]; then \
3434
chmod +x ~/.docker/cli-plugins/docker-compose /usr/bin/docker /usr/local/bin/pack /root/.docker/cli-plugins/docker-buildx \
3535
;fi
3636

37-
COPY --from=minio/mc:RELEASE.2024-03-13T23-51-57Z /usr/bin/mc /usr/bin/mc
37+
COPY --from=minio/mc:RELEASE.2024-09-09T07-53-10Z /usr/bin/mc /usr/bin/mc
3838
RUN chmod +x /usr/bin/mc
3939

4040
ENTRYPOINT ["/sbin/tini", "--"]

docker/dev/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ RUN /bin/bash -c "if [[ ${TARGETPLATFORM} == 'linux/arm64' ]]; then \
3737
curl -L https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/cloudflared-linux-arm64 -o /usr/local/bin/cloudflared && chmod +x /usr/local/bin/cloudflared \
3838
;fi"
3939

40+
COPY --from=minio/mc:RELEASE.2024-09-09T07-53-10Z /usr/bin/mc /usr/bin/mc
41+
RUN chmod +x /usr/bin/mc
42+
4043
RUN { \
4144
echo 'upload_max_filesize=256M'; \
4245
echo 'post_max_size=256M'; \

0 commit comments

Comments
 (0)