Datenbank sichern: Backup als .tar.gz mit Server-Ablage und Client-Download

- Neuer POST /api/backup Endpoint (BackupEndpoint.cs)
- Packt gott.db in data/backup/gott-{timestamp}.tar.gz
- Gibt Archiv gleichzeitig als Download an Client zurück
- Button 'Datenbank sichern' im Header-Dropdown-Menü
- Content-Disposition Header-Parsing mit filename* UTF-8 Support
This commit is contained in:
Meleki committed 2026-02-28 18:18:51 +01:00
1 parent 5b2e8f4a11
commit d434c3ff2a
5 files changed
+91

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Gott.Endpoints;
public static class BackupEndpoint
{
public static void MapBackupEndpoint(this IEndpointRouteBuilder app)
{
app.MapPost("/api/backup", HandleBackup);
}
private static async System.Threading.Tasks.Task<IResult> HandleBackup()
{
var dataDir = Path.Combine(AppContext.BaseDirectory, "data");
var dbPath = Path.Combine(dataDir, "gott.db");
if (!File.Exists(dbPath))
return Results.NotFound(new { error = "Datenbank nicht gefunden" });
var backupDir = Path.Combine(dataDir, "backup");
Directory.CreateDirectory(backupDir);
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var archiveName = $"gott-{timestamp}.tar.gz";
var archivePath = Path.Combine(backupDir, archiveName);
var psi = new ProcessStartInfo
{
FileName = "tar",
ArgumentList = { "-czf", archivePath, "-C", dataDir, "gott.db" },
RedirectStandardError = true,
UseShellExecute = false
};
using (var process = Process.Start(psi))
{
if (process == null)
return Results.StatusCode(500);
var stderr = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
if (process.ExitCode != 0)
return Results.Problem($"tar fehlgeschlagen: {stderr}");
}
var bytes = await File.ReadAllBytesAsync(archivePath);
return Results.File(bytes, "application/gzip", archiveName);
}
}
+1
View File
@@ -25,5 +25,6 @@ app.MapSessionEndpoints();
app.MapAgentEndpoints(); app.MapAgentEndpoints();
app.MapWorkspaceEndpoint(); app.MapWorkspaceEndpoint();
app.MapInfoEndpoint(); app.MapInfoEndpoint();
app.MapBackupEndpoint();
app.Run("http://0.0.0.0:19000"); app.Run("http://0.0.0.0:19000");
+1
View File
@@ -147,6 +147,7 @@
<div class="header-menu-dropdown" id="chatMenuDropdown" hidden> <div class="header-menu-dropdown" id="chatMenuDropdown" hidden>
<button class="header-menu-item" id="systemPromptBtn">System-Prompt anzeigen</button> <button class="header-menu-item" id="systemPromptBtn">System-Prompt anzeigen</button>
<button class="header-menu-item" id="archiveBtn">Archivieren</button> <button class="header-menu-item" id="archiveBtn">Archivieren</button>
<button class="header-menu-item" id="backupDbBtn">Datenbank sichern</button>
</div> </div>
</div> </div>
</header> </header>
+18
View File
@@ -128,3 +128,21 @@ async function apiGetChain(chainId) {
if (!resp.ok) return null; if (!resp.ok) return null;
return resp.json(); return resp.json();
} }
async function apiBackupDb() {
const resp = await fetch('/api/backup', { method: 'POST' });
if (!resp.ok) throw new Error('Backup fehlgeschlagen');
const blob = await resp.blob();
const cd = resp.headers.get('content-disposition') || '';
const filename = cd.match(/filename\*=UTF-8''(.+?)(?:;|$)/i)?.[1]
|| cd.match(/filename="?([^";]+)"?/)?.[1]
|| 'gott-backup.tar.gz';
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
+16
View File
@@ -190,6 +190,22 @@ registerPage('chat', {
clearChat(); clearChat();
}); });
// Datenbank sichern
const backupDbBtn = document.getElementById('backupDbBtn');
if (backupDbBtn) backupDbBtn.addEventListener('click', async () => {
if (chatMenuDropdown) chatMenuDropdown.hidden = true;
backupDbBtn.disabled = true;
try {
await apiBackupDb();
setMeta('✅ Backup erstellt und heruntergeladen');
} catch (e) {
console.error('Backup fehlgeschlagen:', e);
setMeta('❌ Backup fehlgeschlagen');
} finally {
backupDbBtn.disabled = false;
}
});
// State zurücksetzen und History laden // State zurücksetzen und History laden
messages.length = 0; messages.length = 0;
lastKnownMessageId = 0; lastKnownMessageId = 0;