From 6b2400187686a9f64a07834adc62319c8c89d208 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 10 Jan 2024 15:42:54 +0100 Subject: [PATCH] feat: import backups --- app/Livewire/Project/Database/Import.php | 40 ++++++---- .../project/database/configuration.blade.php | 11 +-- .../project/database/import.blade.php | 77 ++++++++++++------- .../shared/scheduled-task/add.blade.php | 2 +- .../shared/scheduled-task/all.blade.php | 3 +- 5 files changed, 81 insertions(+), 52 deletions(-) diff --git a/app/Livewire/Project/Database/Import.php b/app/Livewire/Project/Database/Import.php index 5b7c7de88..697b14646 100644 --- a/app/Livewire/Project/Database/Import.php +++ b/app/Livewire/Project/Database/Import.php @@ -11,6 +11,7 @@ use App\Models\StandaloneMongodb; use App\Models\StandaloneMysql; use App\Models\StandalonePostgresql; use App\Models\StandaloneRedis; +use Illuminate\Support\Facades\Storage; class Import extends Component { @@ -19,6 +20,7 @@ class Import extends Component public $file; public $resource; public $parameters; + public $containers; public bool $validated = true; public bool $scpInProgress = false; public bool $importRunning = false; @@ -26,7 +28,17 @@ class Import extends Component public Server $server; public string $container; public array $importCommands = []; + 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 $mariadbRestoreCommand = 'mariadb -u $MARIADB_USER -p $MARIADB_PASSWORD $MARIADB_DATABASE'; + public function getListeners() + { + $userId = auth()->user()->id; + return [ + "echo-private:user.{$userId},DatabaseStatusChanged" => '$refresh', + ]; + } public function mount() { $this->parameters = get_route_parameters(); @@ -59,7 +71,7 @@ class Import extends Component $this->resource = $resource; $this->server = $this->resource->destination->server; $this->container = $this->resource->uuid; - if (str(data_get($this,'resource.status'))->startsWith('running')) { + if (str(data_get($this, 'resource.status'))->startsWith('running')) { $this->containers->push($this->container); } @@ -68,14 +80,15 @@ class Import extends Component $this->validationMsg = 'The database service has more than one container running. Cannot import.'; } - if ($this->resource->getMorphClass() == 'App\Models\StandaloneRedis' - || $this->resource->getMorphClass() == 'App\Models\StandaloneMongodb') { + if ( + $this->resource->getMorphClass() == 'App\Models\StandaloneRedis' + || $this->resource->getMorphClass() == 'App\Models\StandaloneMongodb' + ) { $this->validated = false; $this->validationMsg = 'This database type is not currently supported.'; } - } - + public function runImport() { $this->validate([ @@ -87,7 +100,7 @@ class Import extends Component try { $uploadedFilename = $this->file->store('backup-import'); - $path = \Storage::path($uploadedFilename); + $path = Storage::path($uploadedFilename); $tmpPath = '/tmp/' . basename($uploadedFilename); // SCP the backup file to the server. @@ -98,17 +111,17 @@ class Import extends Component switch ($this->resource->getMorphClass()) { case 'App\Models\StandaloneMariadb': - $this->importCommands[] = "docker exec {$this->container} sh -c 'mariadb -u\$MARIADB_USER -p\$MARIADB_PASSWORD \$MARIADB_DATABASE < {$tmpPath}'"; + $this->importCommands[] = "docker exec {$this->container} sh -c '{$this->mariadbRestoreCommand} < {$tmpPath}'"; $this->importCommands[] = "rm {$tmpPath}"; - break; + break; case 'App\Models\StandaloneMysql': - $this->importCommands[] = "docker exec {$this->container} sh -c 'mysql -u\$MYSQL_USER -p\$MYSQL_PASSWORD \$MYSQL_DATABASE < {$tmpPath}'"; + $this->importCommands[] = "docker exec {$this->container} sh -c '{$this->mysqlRestoreCommand} < {$tmpPath}'"; $this->importCommands[] = "rm {$tmpPath}"; - break; + break; case 'App\Models\StandalonePostgresql': - $this->importCommands[] = "docker exec {$this->container} sh -c 'pg_restore -U \$POSTGRES_USER -d \$POSTGRES_DB {$tmpPath}'"; + $this->importCommands[] = "docker exec {$this->container} sh -c '{$this->postgresqlRestoreCommand} {$tmpPath}'"; $this->importCommands[] = "rm {$tmpPath}"; - break; + break; } $this->importCommands[] = "docker exec {$this->container} sh -c 'rm {$tmpPath}'"; @@ -122,8 +135,5 @@ class Import extends Component $this->validated = false; $this->validationMsg = $e->getMessage(); } - } - - } diff --git a/resources/views/livewire/project/database/configuration.blade.php b/resources/views/livewire/project/database/configuration.blade.php index 14360fbb9..4ed308b40 100644 --- a/resources/views/livewire/project/database/configuration.blade.php +++ b/resources/views/livewire/project/database/configuration.blade.php @@ -31,6 +31,11 @@ window.location.hash = 'storages'" href="#">Storages + Import + Backup + Webhooks @@ -39,11 +44,7 @@ window.location.hash = 'resource-limits'" href="#">Resource Limits - Import - + -
- +

Import Backup

+
+ This is a destructive action, existing data will be replaced!
- @if (!$validated) -
{{ $validationMsg }}
- @else - @if (!$importRunning) -
-
- - @error('file') {{ $message }} @enderror - Import -
- + @if (str(data_get($resource, 'status'))->startsWith('running')) + @if (!$validated) +
{{ $validationMsg }}
+ @else + + @if ($resource->type() === 'standalone-postgresql') + + @elseif ($resource->type() === 'standalone-mysql') + + @elseif ($resource->type() === 'standalone-mariadb') + + @endif +
+ + +

Max file size: 100MB +

+ + @error('file') + {{ $message }} + @enderror +
+ +
-
- + Import Backup + @endif - @endif - @if ($scpInProgress) -
Database backup is being copied to server..
- @endif + @if ($scpInProgress) +
Database backup is being copied to server...
+ @endif -
- -
+
+ +
+ @else +
Database must be running to import a backup.
+ @endif
diff --git a/resources/views/livewire/project/shared/scheduled-task/add.blade.php b/resources/views/livewire/project/shared/scheduled-task/add.blade.php index 133c8cead..7cd74e0bf 100644 --- a/resources/views/livewire/project/shared/scheduled-task/add.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/add.blade.php @@ -4,7 +4,7 @@ - + Save diff --git a/resources/views/livewire/project/shared/scheduled-task/all.blade.php b/resources/views/livewire/project/shared/scheduled-task/all.blade.php index b860e00b1..32ed50bb1 100644 --- a/resources/views/livewire/project/shared/scheduled-task/all.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/all.blade.php @@ -8,7 +8,6 @@