fix: scheduled backup for other service databases (supabase)

This commit is contained in:
Andras Bacsai 2024-03-26 13:50:44 +01:00
parent 2bf102cdf1
commit 6045870398
4 changed files with 30 additions and 29 deletions

View File

@ -95,7 +95,7 @@ public function handle(): void
$databaseType = $this->database->databaseType(); $databaseType = $this->database->databaseType();
$serviceUuid = $this->database->service->uuid; $serviceUuid = $this->database->service->uuid;
$serviceName = str($this->database->service->name)->slug(); $serviceName = str($this->database->service->name)->slug();
if ($databaseType === 'standalone-postgresql') { if (str($databaseType)->contains('postgres')) {
$this->container_name = "{$this->database->name}-$serviceUuid"; $this->container_name = "{$this->database->name}-$serviceUuid";
$this->directory_name = $serviceName . '-' . $this->container_name; $this->directory_name = $serviceName . '-' . $this->container_name;
$commands[] = "docker exec $this->container_name env | grep POSTGRES_"; $commands[] = "docker exec $this->container_name env | grep POSTGRES_";
@ -120,7 +120,7 @@ public function handle(): void
} else { } else {
$databasesToBackup = $this->database->postgres_user; $databasesToBackup = $this->database->postgres_user;
} }
} else if ($databaseType === 'standalone-mysql') { } else if (str($databaseType)->contains('mysql')) {
$this->container_name = "{$this->database->name}-$serviceUuid"; $this->container_name = "{$this->database->name}-$serviceUuid";
$this->directory_name = $serviceName . '-' . $this->container_name; $this->directory_name = $serviceName . '-' . $this->container_name;
$commands[] = "docker exec $this->container_name env | grep MYSQL_"; $commands[] = "docker exec $this->container_name env | grep MYSQL_";
@ -143,7 +143,7 @@ public function handle(): void
} else { } else {
throw new \Exception('MYSQL_DATABASE not found'); throw new \Exception('MYSQL_DATABASE not found');
} }
} else if ($databaseType === 'standalone-mariadb') { } else if (str($databaseType)->contains('mariadb')) {
$this->container_name = "{$this->database->name}-$serviceUuid"; $this->container_name = "{$this->database->name}-$serviceUuid";
$this->directory_name = $serviceName . '-' . $this->container_name; $this->directory_name = $serviceName . '-' . $this->container_name;
$commands[] = "docker exec $this->container_name env"; $commands[] = "docker exec $this->container_name env";
@ -190,32 +190,32 @@ public function handle(): void
} }
if (is_null($databasesToBackup)) { if (is_null($databasesToBackup)) {
if ($databaseType === 'standalone-postgresql') { if (str($databaseType)->contains('postgres')) {
$databasesToBackup = [$this->database->postgres_db]; $databasesToBackup = [$this->database->postgres_db];
} else if ($databaseType === 'standalone-mongodb') { } else if (str($databaseType)->contains('mongodb')) {
$databasesToBackup = ['*']; $databasesToBackup = ['*'];
} else if ($databaseType === 'standalone-mysql') { } else if (str($databaseType)->contains('mysql')) {
$databasesToBackup = [$this->database->mysql_database]; $databasesToBackup = [$this->database->mysql_database];
} else if ($databaseType === 'standalone-mariadb') { } else if (str($databaseType)->contains('mariadb')) {
$databasesToBackup = [$this->database->mariadb_database]; $databasesToBackup = [$this->database->mariadb_database];
} else { } else {
return; return;
} }
} else { } else {
if ($databaseType === 'standalone-postgresql') { if (str($databaseType)->contains('postgres')) {
// Format: db1,db2,db3 // Format: db1,db2,db3
$databasesToBackup = explode(',', $databasesToBackup); $databasesToBackup = explode(',', $databasesToBackup);
$databasesToBackup = array_map('trim', $databasesToBackup); $databasesToBackup = array_map('trim', $databasesToBackup);
} else if ($databaseType === 'standalone-mongodb') { } else if (str($databaseType)->contains('mongodb')) {
// Format: db1:collection1,collection2|db2:collection3,collection4 // Format: db1:collection1,collection2|db2:collection3,collection4
$databasesToBackup = explode('|', $databasesToBackup); $databasesToBackup = explode('|', $databasesToBackup);
$databasesToBackup = array_map('trim', $databasesToBackup); $databasesToBackup = array_map('trim', $databasesToBackup);
ray($databasesToBackup); ray($databasesToBackup);
} else if ($databaseType === 'standalone-mysql') { } else if (str($databaseType)->contains('mysql')) {
// Format: db1,db2,db3 // Format: db1,db2,db3
$databasesToBackup = explode(',', $databasesToBackup); $databasesToBackup = explode(',', $databasesToBackup);
$databasesToBackup = array_map('trim', $databasesToBackup); $databasesToBackup = array_map('trim', $databasesToBackup);
} else if ($databaseType === 'standalone-mariadb') { } else if (str($databaseType)->contains('mariadb')) {
// Format: db1,db2,db3 // Format: db1,db2,db3
$databasesToBackup = explode(',', $databasesToBackup); $databasesToBackup = explode(',', $databasesToBackup);
$databasesToBackup = array_map('trim', $databasesToBackup); $databasesToBackup = array_map('trim', $databasesToBackup);
@ -235,7 +235,7 @@ public function handle(): void
$size = 0; $size = 0;
ray('Backing up ' . $database); ray('Backing up ' . $database);
try { try {
if ($databaseType === 'standalone-postgresql') { if (str($databaseType)->contains('postgres')) {
$this->backup_file = "/pg-dump-$database-" . Carbon::now()->timestamp . ".dmp"; $this->backup_file = "/pg-dump-$database-" . Carbon::now()->timestamp . ".dmp";
$this->backup_location = $this->backup_dir . $this->backup_file; $this->backup_location = $this->backup_dir . $this->backup_file;
$this->backup_log = ScheduledDatabaseBackupExecution::create([ $this->backup_log = ScheduledDatabaseBackupExecution::create([
@ -244,7 +244,7 @@ public function handle(): void
'scheduled_database_backup_id' => $this->backup->id, 'scheduled_database_backup_id' => $this->backup->id,
]); ]);
$this->backup_standalone_postgresql($database); $this->backup_standalone_postgresql($database);
} else if ($databaseType === 'standalone-mongodb') { } else if (str($databaseType)->contains('mongodb')) {
if ($database === '*') { if ($database === '*') {
$database = 'all'; $database = 'all';
$databaseName = 'all'; $databaseName = 'all';
@ -263,7 +263,7 @@ public function handle(): void
'scheduled_database_backup_id' => $this->backup->id, 'scheduled_database_backup_id' => $this->backup->id,
]); ]);
$this->backup_standalone_mongodb($database); $this->backup_standalone_mongodb($database);
} else if ($databaseType === 'standalone-mysql') { } else if (str($databaseType)->contains('mysql')) {
$this->backup_file = "/mysql-dump-$database-" . Carbon::now()->timestamp . ".dmp"; $this->backup_file = "/mysql-dump-$database-" . Carbon::now()->timestamp . ".dmp";
$this->backup_location = $this->backup_dir . $this->backup_file; $this->backup_location = $this->backup_dir . $this->backup_file;
$this->backup_log = ScheduledDatabaseBackupExecution::create([ $this->backup_log = ScheduledDatabaseBackupExecution::create([
@ -272,7 +272,7 @@ public function handle(): void
'scheduled_database_backup_id' => $this->backup->id, 'scheduled_database_backup_id' => $this->backup->id,
]); ]);
$this->backup_standalone_mysql($database); $this->backup_standalone_mysql($database);
} else if ($databaseType === 'standalone-mariadb') { } else if (str($databaseType)->contains('mariadb')) {
$this->backup_file = "/mariadb-dump-$database-" . Carbon::now()->timestamp . ".dmp"; $this->backup_file = "/mariadb-dump-$database-" . Carbon::now()->timestamp . ".dmp";
$this->backup_location = $this->backup_dir . $this->backup_file; $this->backup_location = $this->backup_dir . $this->backup_file;
$this->backup_log = ScheduledDatabaseBackupExecution::create([ $this->backup_log = ScheduledDatabaseBackupExecution::create([

View File

@ -11,15 +11,17 @@
</a> </a>
</div> </div>
@else @else
<div class="box">
<div @class([ <div @class([
'border-coollabs' => 'border-coollabs' =>
data_get($backup, 'id') === data_get($selectedBackup, 'id'), data_get($backup, 'id') === data_get($selectedBackup, 'id'),
'flex flex-col box border-l-2 border-transparent', 'flex flex-col border-l-2 border-transparent',
]) wire:click="setSelectedBackup('{{ data_get($backup, 'id') }}')"> ]) wire:click="setSelectedBackup('{{ data_get($backup, 'id') }}')">
<div>Frequency: {{ $backup->frequency }}</div> <div>Frequency: {{ $backup->frequency }}</div>
<div>Last backup: {{ data_get($backup->latest_log, 'status', 'No backup yet') }}</div> <div>Last backup: {{ data_get($backup->latest_log, 'status', 'No backup yet') }}</div>
<div>Number of backups to keep (locally): {{ $backup->number_of_backups_locally }}</div> <div>Number of backups to keep (locally): {{ $backup->number_of_backups_locally }}</div>
</div> </div>
</div>
@endif @endif
@empty @empty
<div>No scheduled backups configured.</div> <div>No scheduled backups configured.</div>

View File

@ -75,7 +75,7 @@
<div class="text-xs">{{ $application->status }}</div> <div class="text-xs">{{ $application->status }}</div>
</div> </div>
<div class="flex items-center px-4"> <div class="flex items-center px-4">
<a class="mx-4 font-bold hover:underline" <a class="mx-4 text-xs font-bold hover:underline"
href="{{ route('project.service.index', [...$parameters, 'stack_service_uuid' => $application->uuid]) }}"> href="{{ route('project.service.index', [...$parameters, 'stack_service_uuid' => $application->uuid]) }}">
Settings Settings
</a> </a>
@ -116,7 +116,7 @@
<div class="text-xs">{{ $database->status }}</div> <div class="text-xs">{{ $database->status }}</div>
</div> </div>
<div class="flex items-center px-4"> <div class="flex items-center px-4">
<a class="mx-4 font-bold hover:underline" <a class="mx-4 text-xs font-bold hover:underline"
href="{{ route('project.service.index', [...$parameters, 'stack_service_uuid' => $database->uuid]) }}"> href="{{ route('project.service.index', [...$parameters, 'stack_service_uuid' => $database->uuid]) }}">
Settings Settings
</a> </a>

View File

@ -17,10 +17,9 @@
@click.prevent="activeTab = 'scheduled-tasks'; window.location.hash = 'scheduled-tasks'" @click.prevent="activeTab = 'scheduled-tasks'; window.location.hash = 'scheduled-tasks'"
href="#">Scheduled Tasks href="#">Scheduled Tasks
</a> </a>
@if ( @if (str($serviceDatabase?->databaseType())->contains('mysql') ||
$serviceDatabase?->databaseType() === 'standalone-mysql' || str($serviceDatabase?->databaseType())->contains('postgres') ||
$serviceDatabase?->databaseType() === 'standalone-postgresql' || str($serviceDatabase?->databaseType())->contains('mariadb'))
$serviceDatabase?->databaseType() === 'standalone-mariadb')
<a :class="activeTab === 'backups' && 'dark:text-white'" <a :class="activeTab === 'backups' && 'dark:text-white'"
@click.prevent="activeTab = 'backups'; window.location.hash = 'backups'" href="#">Backups</a> @click.prevent="activeTab = 'backups'; window.location.hash = 'backups'" href="#">Backups</a>
@endif @endif