feat: restore backup from server file
This commit is contained in:
34
app/Events/RestoreJobFinished.php
Normal file
34
app/Events/RestoreJobFinished.php
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,8 @@ class Import extends Component
|
|||||||
|
|
||||||
public string $restoreCommandText = '';
|
public string $restoreCommandText = '';
|
||||||
|
|
||||||
|
public string $customLocation = '';
|
||||||
|
|
||||||
public string $postgresqlRestoreCommand = 'pg_restore -U $POSTGRES_USER -d $POSTGRES_DB';
|
public string $postgresqlRestoreCommand = 'pg_restore -U $POSTGRES_USER -d $POSTGRES_DB';
|
||||||
|
|
||||||
public string $mysqlRestoreCommand = 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE';
|
public string $mysqlRestoreCommand = 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE';
|
||||||
@@ -60,6 +62,9 @@ class Import extends Component
|
|||||||
|
|
||||||
public function mount()
|
public function mount()
|
||||||
{
|
{
|
||||||
|
if (isDev()) {
|
||||||
|
$this->customLocation = '/data/coolify/pg-dump-all-1736245863.gz';
|
||||||
|
}
|
||||||
$this->parameters = get_route_parameters();
|
$this->parameters = get_route_parameters();
|
||||||
$this->getContainers();
|
$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()
|
public function runImport()
|
||||||
{
|
{
|
||||||
if ($this->filename === '') {
|
if ($this->filename === '') {
|
||||||
@@ -148,17 +171,24 @@ EOD;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$uploadedFilename = "upload/{$this->resource->uuid}/restore";
|
$this->importCommands = [];
|
||||||
$path = Storage::path($uploadedFilename);
|
if (filled($this->customLocation)) {
|
||||||
if (! Storage::exists($uploadedFilename)) {
|
$backupFileName = '/tmp/restore_'.$this->resource->uuid;
|
||||||
$this->dispatch('error', 'The file does not exist or has been deleted.');
|
$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;
|
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
|
// Copy the restore command to a script file
|
||||||
$scriptPath = "/tmp/restore_{$this->resource->uuid}.sh";
|
$scriptPath = "/tmp/restore_{$this->resource->uuid}.sh";
|
||||||
@@ -202,18 +232,22 @@ EOD;
|
|||||||
$this->importCommands[] = "docker cp {$scriptPath} {$this->container}:{$scriptPath}";
|
$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 '{$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 $?\"'";
|
$this->importCommands[] = "docker exec {$this->container} sh -c 'echo \"Import finished with exit code $?\"'";
|
||||||
|
|
||||||
if (! empty($this->importCommands)) {
|
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);
|
$this->dispatch('activityMonitor', $activity->id);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return handleError($e, $this);
|
return handleError($e, $this);
|
||||||
} finally {
|
} finally {
|
||||||
$this->filename = null;
|
$this->filename = null;
|
||||||
|
$this->importCommands = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
parallelChunkUploads: false,
|
parallelChunkUploads: false,
|
||||||
init: function() {
|
init: function() {
|
||||||
let button = this.element.querySelector('button');
|
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) {
|
this.on('sending', function(file, xhr, formData) {
|
||||||
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||||
formData.append("_token", token);
|
formData.append("_token", token);
|
||||||
@@ -80,15 +80,24 @@
|
|||||||
<x-forms.checkbox label="Includes all databases" wire:model.live='dumpAll'></x-forms.checkbox>
|
<x-forms.checkbox label="Includes all databases" wire:model.live='dumpAll'></x-forms.checkbox>
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@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>
|
<form action="/upload/backup/{{ $resource->uuid }}" class="dropzone" id="my-dropzone" wire:ignore>
|
||||||
@csrf
|
@csrf
|
||||||
</form>
|
</form>
|
||||||
<div x-show="isUploading">
|
<div x-show="isUploading">
|
||||||
<progress max="100" x-bind:value="progress" class="progress progress-warning"></progress>
|
<progress max="100" x-bind:value="progress" class="progress progress-warning"></progress>
|
||||||
</div>
|
</div>
|
||||||
|
<h3 class="pt-6" x-show="filename && !error">File Information</h3>
|
||||||
<div x-show="filename && !error">
|
<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>
|
<x-forms.button class="w-full my-4" wire:click='runImport'>Restore Backup</x-forms.button>
|
||||||
</div>
|
</div>
|
||||||
<div class="container w-full mx-auto">
|
<div class="container w-full mx-auto">
|
||||||
|
|||||||
Reference in New Issue
Block a user