Skip to content

Commit

Permalink
fix: local dev s3 uploads
Browse files Browse the repository at this point in the history
fix: hetzner s3 uploads (mc alias instead of mc host)
  • Loading branch information
andrasbacsai committed Oct 2, 2024
1 parent 2be2f0a commit dd782e7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
22 changes: 19 additions & 3 deletions app/Jobs/DatabaseBackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public function handle(): void
}
}
$this->backup_dir = backup_dir().'/databases/'.str($this->team->name)->slug().'-'.$this->team->id.'/'.$this->directory_name;

if ($this->database->name === 'coolify-db') {
$databasesToBackup = ['coolify'];
$this->directory_name = $this->container_name = 'coolify-db';
Expand Down Expand Up @@ -515,10 +514,27 @@ private function upload_to_s3(): void
$this->ensureHelperImageAvailable();

$fullImageName = $this->getFullImageName();
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro {$fullImageName}";
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";

if (isDev()) {
if ($this->database->name === 'coolify-db') {
$backup_location_from = '/var/lib/docker/volumes/coolify_dev_backups_data/_data/databases/coolify/coolify-db-'.$this->server->ip.$this->backup_file;
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $backup_location_from:$this->backup_location:ro {$fullImageName}";
} else {
$backup_location_from = '/var/lib/docker/volumes/coolify_dev_backups_data/_data/databases/'.str($this->team->name)->slug().'-'.$this->team->id.'/'.$this->directory_name.$this->backup_file;
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $backup_location_from:$this->backup_location:ro {$fullImageName}";
}
} else {
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro {$fullImageName}";
}
if ($this->s3->isHetzner()) {
$endpointWithoutBucket = 'https://'.str($endpoint)->after('https://')->after('.')->value();
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc alias set --path=off --api=S3v4 temporary {$endpointWithoutBucket} $key $secret";
} else {
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";
}
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
instant_remote_process($commands, $this->server);

$this->add_to_backup_output('Uploaded to S3.');
} catch (\Throwable $e) {
$this->add_to_backup_output($e->getMessage());
Expand Down
10 changes: 10 additions & 0 deletions app/Models/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public function awsUrl()
return "{$this->endpoint}/{$this->bucket}";
}

public function isHetzner()
{
return str($this->endpoint)->contains('your-objectstorage.com');
}

public function isDigitalOcean()
{
return str($this->endpoint)->contains('digitaloceanspaces.com');
}

public function testConnection(bool $shouldSave = false)
{
try {
Expand Down
8 changes: 2 additions & 6 deletions bootstrap/helpers/s3.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?php

use App\Models\S3Storage;
use Illuminate\Support\Str;

function set_s3_target(S3Storage $s3)
{
$is_digital_ocean = false;
if ($s3->endpoint) {
$is_digital_ocean = Str::contains($s3->endpoint, 'digitaloceanspaces.com');
$is_hetzner = Str::contains($s3->endpoint, 'your-objectstorage.com');
}

config()->set('filesystems.disks.custom-s3', [
'driver' => 's3',
'region' => $s3['region'],
Expand All @@ -18,7 +14,7 @@ function set_s3_target(S3Storage $s3)
'bucket' => $s3['bucket'],
'endpoint' => $s3['endpoint'],
'use_path_style_endpoint' => true,
'bucket_endpoint' => $is_digital_ocean || $is_hetzner,
'bucket_endpoint' => $s3->isHetzner() || $s3->isDigitalOcean(),
'aws_url' => $s3->awsUrl(),
]);
}
7 changes: 3 additions & 4 deletions resources/views/components/modal-confirmation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,8 @@ class="w-24 dark:bg-coolgray-200 dark:hover:bg-coolgray-300">
</template>

<template x-if="step === 1">
@if(isDev() && $submitAction === 'delete')
<x-forms.button class="w-auto" isError
@click="$wire.delete('hello')">
@if(isDev() && str($submitAction)->contains('delete'))
<x-forms.button class="w-auto" isError wire:click="{{ $submitAction }}('hello')">
<span x-text="step3ButtonText"></span>
</x-forms.button>
@else
Expand All @@ -309,7 +308,7 @@ class="w-24 dark:bg-coolgray-200 dark:hover:bg-coolgray-300">
</template>

<template x-if="step === 2">
<x-forms.button x-bind:disabled="confirmationText !== '' && confirmWithText && userConfirmationText !== confirmationText"
<x-forms.button x-bind:disabled="confirmationText !== '' && userConfirmationText !== confirmationText"
class="w-auto" isError
@click="
if (dispatchEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,11 @@
<x-forms.button class="dark:hover:bg-coolgray-400"
x-on:click="download_file('{{ data_get($execution, 'id') }}')">Download</x-forms.button>
@endif
<x-modal-confirmation
title="Confirm Backup Deletion?"
buttonTitle="Delete"
isErrorButton
submitAction="deleteBackup({{ data_get($execution, 'id') }})"
{{-- :checkboxes="$checkboxes" --}}
:actions="[
'This backup will be permanently deleted from local storage.'
]"
confirmationText="{{ data_get($execution, 'filename') }}"
confirmationLabel="Please confirm the execution of the actions by entering the Backup Filename below"
shortConfirmationLabel="Backup Filename"
step3ButtonText="Permanently Delete"
/>
<x-modal-confirmation title="Confirm Backup Deletion?" buttonTitle="Delete" isErrorButton
submitAction="deleteBackup({{ data_get($execution, 'id') }})"
:actions="['This backup will be permanently deleted from local storage.']" confirmationText="{{ data_get($execution, 'filename') }}"
confirmationLabel="Please confirm the execution of the actions by entering the Backup Filename below"
shortConfirmationLabel="Backup Filename" step3ButtonText="Permanently Delete" />
</div>
</div>
@empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
<x-forms.input placeholder="0 0 * * * or daily" id="frequency"
helper="You can use every_minute, hourly, daily, weekly, monthly, yearly or a cron expression." label="Frequency"
required />
<x-forms.checkbox id="save_s3" label="Save to S3" />
<x-forms.select id="selected_storage_id">
@if ($s3s->count() === 0)
<option value="0">No S3 Storages found.</option>
@else
@foreach ($s3s as $s3)
<option value="{{ $s3->id }}">{{ $s3->name }}</option>
@endforeach
@if ($s3s->count() === 0)
<div class="text-red-500">No validated S3 Storages found.</div>
@else
<x-forms.checkbox wire:model.live="save_s3" label="Save to S3" />
@if ($save_s3)
<x-forms.select id="selected_storage_id" label="Select a validated S3 storage">
@foreach ($s3s as $s3)
<option value="{{ $s3->id }}">{{ $s3->name }}</option>
@endforeach
</x-forms.select>
@endif
</x-forms.select>
@endif
<x-forms.button type="submit" @click="modalOpen=false">
Save
</x-forms.button>
Expand Down

0 comments on commit dd782e7

Please sign in to comment.