An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat: restore backup from server file
Andras Bacsai40d9e05c0b06
diff --git a/app/Events/RestoreJobFinished.php b/app/Events/RestoreJobFinished.phpnew file mode 100644index 0000000000..d3adb77984--- /dev/null+++ b/app/Events/RestoreJobFinished.php@@ -0,0 +1,34 @@+<?php++namespace App\Events;++use App\Models\Server;+use Illuminate\Broadcasting\InteractsWithSockets;+use Illuminate\Foundation\Events\Dispatchable;+use Illuminate\Queue\SerializesModels;++class RestoreJobFinished+{+ use Dispatchable, InteractsWithSockets, SerializesModels;++ public function __construct($data)+ {+ $scriptPath = data_get($data, 'scriptPath');+ $tmpPath = data_get($data, 'tmpPath');+ $container = data_get($data, 'container');+ $serverId = data_get($data, 'serverId');+ if (filled($scriptPath) && filled($tmpPath) && filled($container) && filled($serverId)) {+ if (str($tmpPath)->startsWith('/tmp/')+ && str($scriptPath)->startsWith('/tmp/')+ && ! str($tmpPath)->contains('..')+ && ! str($scriptPath)->contains('..')+ && strlen($tmpPath) > 5 // longer than just "/tmp/"+ && strlen($scriptPath) > 5+ ) {+ $commands[] = "docker exec {$container} sh -c 'rm {$scriptPath}'";+ $commands[] = "docker exec {$container} sh -c 'rm {$tmpPath}'";+ instant_remote_process($commands, Server::find($serverId), throwError: true);+ }+ }+ }+}diff --git a/app/Livewire/Project/Database/Import.php b/app/Livewire/Project/Database/Import.phpindex b76e7ec2c4..6eecb7a1e4 100644--- a/app/Livewire/Project/Database/Import.php+++ b/app/Livewire/Project/Database/Import.php@@ -41,6 +41,8 @@ class Import extends Componentpublic string $restoreCommandText = '';+ public string $customLocation = '';+public string $postgresqlRestoreCommand = 'pg_restore -U $POSTGRES_USER -d $POSTGRES_DB';public string $mysqlRestoreCommand = 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE';@@ -60,6 +62,9 @@ class Import extends Componentpublic function mount(){+ if (isDev()) {+ $this->customLocation = '/data/coolify/pg-dump-all-1736245863.gz';+ }$this->parameters = get_route_parameters();$this->getContainers();}@@ -140,6 +145,24 @@ EOD;}}+ public function checkFile()+ {+ if (filled($this->customLocation)) {+ try {+ $result = instant_remote_process(["ls -l {$this->customLocation}"], $this->server, throwError: false);+ if (blank($result)) {+ $this->dispatch('error', 'The file does not exist or has been deleted.');++ return;+ }+ $this->filename = $this->customLocation;+ $this->dispatch('success', 'The file exists.');+ } catch (\Throwable $e) {+ return handleError($e, $this);+ }+ }+ }+public function runImport(){if ($this->filename === '') {@@ -148,17 +171,24 @@ EOD;return;}try {- $uploadedFilename = "upload/{$this->resource->uuid}/restore";- $path = Storage::path($uploadedFilename);- if (! Storage::exists($uploadedFilename)) {- $this->dispatch('error', 'The file does not exist or has been deleted.');-- return;+ $this->importCommands = [];+ if (filled($this->customLocation)) {+ $backupFileName = '/tmp/restore_'.$this->resource->uuid;+ $this->importCommands[] = "docker cp {$this->customLocation} {$this->container}:{$backupFileName}";+ $tmpPath = $backupFileName;+ } else {+ $backupFileName = "upload/{$this->resource->uuid}/restore";+ $path = Storage::path($backupFileName);+ if (! Storage::exists($backupFileName)) {+ $this->dispatch('error', 'The file does not exist or has been deleted.');++ return;+ }+ $tmpPath = '/tmp/'.basename($backupFileName).'_'.$this->resource->uuid;+ instant_scp($path, $tmpPath, $this->server);+ Storage::delete($backupFileName);+ $this->importCommands[] = "docker cp {$tmpPath} {$this->container}:{$tmpPath}";}- $tmpPath = '/tmp/'.basename($uploadedFilename);- instant_scp($path, $tmpPath, $this->server);- Storage::delete($uploadedFilename);- $this->importCommands[] = "docker cp {$tmpPath} {$this->container}:{$tmpPath}";// Copy the restore command to a script file$scriptPath = "/tmp/restore_{$this->resource->uuid}.sh";@@ -202,18 +232,22 @@ EOD;$this->importCommands[] = "docker cp {$scriptPath} {$this->container}:{$scriptPath}";$this->importCommands[] = "docker exec {$this->container} sh -c '{$scriptPath}'";- $this->importCommands[] = "docker exec {$this->container} sh -c 'rm {$scriptPath} && rm {$tmpPath}'";-$this->importCommands[] = "docker exec {$this->container} sh -c 'echo \"Import finished with exit code $?\"'";if (! empty($this->importCommands)) {- $activity = remote_process($this->importCommands, $this->server, ignore_errors: true);+ $activity = remote_process($this->importCommands, $this->server, ignore_errors: true, callEventOnFinish: 'RestoreJobFinished', callEventData: [+ 'scriptPath' => $scriptPath,+ 'tmpPath' => $tmpPath,+ 'container' => $this->container,+ 'serverId' => $this->server->id,+ ]);$this->dispatch('activityMonitor', $activity->id);}} catch (\Throwable $e) {return handleError($e, $this);} finally {$this->filename = null;+ $this->importCommands = [];}}}diff --git a/resources/views/livewire/project/database/import.blade.php b/resources/views/livewire/project/database/import.blade.phpindex 47afe611d8..795c0a4209 100644--- a/resources/views/livewire/project/database/import.blade.php+++ b/resources/views/livewire/project/database/import.blade.php@@ -12,7 +12,7 @@parallelChunkUploads: false,init: function() {let button = this.element.querySelector('button');- button.innerText = 'Select or Drop a backup file here'+ button.innerText = 'Select or drop a backup file here.'this.on('sending', function(file, xhr, formData) {const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');formData.append("_token", token);@@ -80,15 +80,24 @@<x-forms.checkbox label="Includes all databases" wire:model.live='dumpAll'></x-forms.checkbox></div>@endif-+ <h3 class="pt-6">Backup File</h3>+ <form class="flex gap-2 items-end">+ <x-forms.input label="Location of the backup file on the server"+ placeholder="e.g. /home/user/backup.sql.gz" wire:model='customLocation'></x-forms.input>+ <x-forms.button class="w-full" wire:click='checkFile'>Check File</x-forms.button>+ </form>+ <div class="pt-2 text-center text-xl font-bold">+ Or+ </div><form action="/upload/backup/{{ $resource->uuid }}" class="dropzone" id="my-dropzone" wire:ignore>@csrf</form><div x-show="isUploading"><progress max="100" x-bind:value="progress" class="progress progress-warning"></progress></div>+ <h3 class="pt-6" x-show="filename && !error">File Information</h3><div x-show="filename && !error">- <div>File: <span x-text="filename ?? 'N/A'"></span> <span x-text="filesize">/ </span></div>+ <div>Location: <span x-text="filename ?? 'N/A'"></span> <span x-text="filesize">/ </span></div><x-forms.button class="w-full my-4" wire:click='runImport'>Restore Backup</x-forms.button></div><div class="container w-full mx-auto">