feat: configuration backup management with manual and auto backup

Add backup/restore functionality for the SQLite database. Users can
trigger manual backups, configure automatic backups on an interval
with retention policies, list/download/delete backups, and restore
from any backup.

- Backup engine using VACUUM INTO (safe with WAL mode)
- Backup metadata tracked in DB, files stored in DATA_DIR/backups/
- Settings: backup_enabled, backup_interval_hours, backup_retention_count
- API: POST/GET/DELETE /api/backups, download, restore endpoints
- Autobackup via cron scheduler with configurable interval
- Retention: prune on startup, after each backup (manual and auto)
- Orphan cleanup: removes backup files without metadata on startup
- Restore: replaces DB and triggers graceful server shutdown
- Settings UI: /settings/backup with toggle, interval, retention config
- Backup list with download, delete, restore actions
- i18n: English and Russian translations
This commit is contained in:
2026-04-02 15:32:15 +03:00
parent 1c37bb2ccf
commit a9c7775bb7
21 changed files with 1230 additions and 17 deletions
+24 -1
View File
@@ -24,7 +24,8 @@ import type {
VolumeScopeInfo,
BrowseResult,
DnsZone,
DnsRecordView
DnsRecordView,
BackupInfo
} from './types';
// ── Helpers ─────────────────────────────────────────────────────────
@@ -292,6 +293,28 @@ export function deleteDnsRecord(fqdn: string): Promise<void> {
return del<void>(`/api/dns/records/${encodeURIComponent(fqdn)}`);
}
// ── Backups ────────────────────────────────────────────────────────
export function listBackups(): Promise<BackupInfo[]> {
return get<BackupInfo[]>('/api/backups');
}
export function triggerBackup(): Promise<BackupInfo> {
return post<BackupInfo>('/api/backups');
}
export function deleteBackup(id: string): Promise<void> {
return del<void>(`/api/backups/${id}`);
}
export function restoreBackup(id: string): Promise<{ status: string; message: string }> {
return post<{ status: string; message: string }>(`/api/backups/${id}/restore`);
}
export function backupDownloadUrl(id: string): string {
return `/api/backups/${id}/download`;
}
// ── Health ──────────────────────────────────────────────────────────
export function getHealth(): Promise<{ docker: DockerHealth }> {
+39
View File
@@ -204,6 +204,7 @@
"registries": "Registries",
"credentials": "Credentials",
"authentication": "Authentication",
"backup": "Backups",
"appearance": "Appearance",
"staleThreshold": "Stale threshold (days)",
"staleThresholdHelp": "Containers inactive for longer than this will be flagged as stale."
@@ -325,6 +326,44 @@
"registriesLink": "Registries",
"registryTokensSuffix": "section. Each registry stores its token encrypted in the database."
},
"settingsBackup": {
"title": "Backup Management",
"description": "Manage database backups and configure automatic backup schedules.",
"autoBackup": "Automatic Backups",
"autoBackupHelp": "Automatically create backups at the configured interval.",
"interval": "Backup Interval",
"intervalHelp": "How often to create automatic backups.",
"intervalHours": "{hours} hours",
"retention": "Retention Count",
"retentionHelp": "Maximum number of backups to keep. Oldest are deleted first.",
"backupNow": "Backup Now",
"creatingBackup": "Creating...",
"backupCreated": "Backup created successfully",
"backupFailed": "Failed to create backup",
"backupList": "Backups",
"noBackups": "No backups yet. Create one manually or enable automatic backups.",
"columnFilename": "Filename",
"columnSize": "Size",
"columnType": "Type",
"columnDate": "Created",
"columnActions": "Actions",
"download": "Download",
"delete": "Delete",
"restore": "Restore",
"deleteConfirm": "Are you sure you want to delete this backup?",
"deleted": "Backup deleted",
"deleteFailed": "Failed to delete backup",
"restoreConfirm": "Are you sure you want to restore from this backup? This will replace the current database and restart the server. All current data will be lost.",
"restoreWarning": "This action cannot be undone!",
"restored": "Database restored. The server is restarting...",
"restoreFailed": "Failed to restore backup",
"typeManual": "Manual",
"typeAuto": "Auto",
"save": "Save",
"saving": "Saving...",
"saved": "Backup settings saved",
"saveFailed": "Failed to save backup settings"
},
"settingsAuth": {
"title": "Authentication Settings",
"description": "Configure authentication mode and manage users.",
+39
View File
@@ -204,6 +204,7 @@
"registries": "Реестры",
"credentials": "Учётные данные",
"authentication": "Аутентификация",
"backup": "Резервные копии",
"appearance": "Внешний вид",
"staleThreshold": "Порог устаревания (дни)",
"staleThresholdHelp": "Контейнеры, неактивные дольше этого срока, будут помечены как устаревшие."
@@ -325,6 +326,44 @@
"registriesLink": "Реестры",
"registryTokensSuffix": ". Каждый реестр хранит свой токен в зашифрованном виде."
},
"settingsBackup": {
"title": "Управление резервными копиями",
"description": "Управление резервными копиями базы данных и настройка автоматического резервного копирования.",
"autoBackup": "Автоматическое резервное копирование",
"autoBackupHelp": "Автоматически создавать резервные копии с заданным интервалом.",
"interval": "Интервал копирования",
"intervalHelp": "Как часто создавать автоматические резервные копии.",
"intervalHours": "{hours} часов",
"retention": "Количество хранимых копий",
"retentionHelp": "Максимальное количество хранимых резервных копий. Старые удаляются первыми.",
"backupNow": "Создать копию",
"creatingBackup": "Создание...",
"backupCreated": "Резервная копия создана",
"backupFailed": "Не удалось создать резервную копию",
"backupList": "Резервные копии",
"noBackups": "Резервных копий пока нет. Создайте вручную или включите автоматическое копирование.",
"columnFilename": "Файл",
"columnSize": "Размер",
"columnType": "Тип",
"columnDate": "Создано",
"columnActions": "Действия",
"download": "Скачать",
"delete": "Удалить",
"restore": "Восстановить",
"deleteConfirm": "Вы уверены, что хотите удалить эту резервную копию?",
"deleted": "Резервная копия удалена",
"deleteFailed": "Не удалось удалить резервную копию",
"restoreConfirm": "Вы уверены, что хотите восстановить из этой копии? Текущая база данных будет заменена и сервер будет перезапущен. Все текущие данные будут потеряны.",
"restoreWarning": "Это действие необратимо!",
"restored": "База данных восстановлена. Сервер перезапускается...",
"restoreFailed": "Не удалось восстановить резервную копию",
"typeManual": "Ручная",
"typeAuto": "Авто",
"save": "Сохранить",
"saving": "Сохранение...",
"saved": "Настройки копирования сохранены",
"saveFailed": "Не удалось сохранить настройки копирования"
},
"settingsAuth": {
"title": "Настройки аутентификации",
"description": "Настройка режима аутентификации и управление пользователями.",
+12
View File
@@ -112,6 +112,9 @@ export interface Settings {
dns_provider: string;
has_cloudflare_api_token: boolean;
cloudflare_zone_id: string;
backup_enabled: boolean;
backup_interval_hours: number;
backup_retention_count: number;
updated_at: string;
}
@@ -132,6 +135,15 @@ export interface DnsRecordView {
status: string;
}
/** A backup metadata record. */
export interface BackupInfo {
id: string;
filename: string;
size_bytes: number;
backup_type: string;
created_at: string;
}
/** An SSL certificate from Nginx Proxy Manager. */
export interface NpmCertificate {
id: number;
+5 -2
View File
@@ -2,7 +2,7 @@
import type { Snippet } from 'svelte';
import { page } from '$app/stores';
import { t } from '$lib/i18n';
import { IconSettings, IconDatabase, IconKey, IconShield } from '$lib/components/icons';
import { IconSettings, IconDatabase, IconKey, IconShield, IconHardDrive } from '$lib/components/icons';
interface Props {
children: Snippet;
@@ -14,7 +14,8 @@
{ href: '/settings', labelKey: 'settings.general', icon: 'general' },
{ href: '/settings/registries', labelKey: 'settings.registries', icon: 'registries' },
{ href: '/settings/credentials', labelKey: 'settings.credentials', icon: 'credentials' },
{ href: '/settings/auth', labelKey: 'settings.authentication', icon: 'auth' }
{ href: '/settings/auth', labelKey: 'settings.authentication', icon: 'auth' },
{ href: '/settings/backup', labelKey: 'settings.backup', icon: 'backup' }
];
let currentPath = $derived($page.url.pathname);
@@ -49,6 +50,8 @@
<IconKey size={16} class="{isActive(item.href) ? 'text-[var(--color-brand-600)]' : 'text-[var(--text-tertiary)]'}" />
{:else if item.icon === 'auth'}
<IconShield size={16} class="{isActive(item.href) ? 'text-[var(--color-brand-600)]' : 'text-[var(--text-tertiary)]'}" />
{:else if item.icon === 'backup'}
<IconHardDrive size={16} class="{isActive(item.href) ? 'text-[var(--color-brand-600)]' : 'text-[var(--text-tertiary)]'}" />
{/if}
{$t(item.labelKey)}
</a>
+274
View File
@@ -0,0 +1,274 @@
<script lang="ts">
import { getSettings, updateSettings, listBackups, triggerBackup, deleteBackup, restoreBackup, backupDownloadUrl } from '$lib/api';
import type { BackupInfo } from '$lib/types';
import FormField from '$lib/components/FormField.svelte';
import ConfirmDialog from '$lib/components/ConfirmDialog.svelte';
import { toasts } from '$lib/stores/toast';
import { t } from '$lib/i18n';
import { IconLoader, IconTrash, IconRefresh } from '$lib/components/icons';
import Skeleton from '$lib/components/Skeleton.svelte';
import { getAuthToken } from '$lib/auth';
let loading = $state(true);
let saving = $state(false);
let creatingBackup = $state(false);
let backupEnabled = $state(false);
let backupIntervalHours = $state('24');
let backupRetentionCount = $state('10');
let backups = $state<BackupInfo[]>([]);
let confirmDeleteId = $state('');
let confirmRestoreId = $state('');
async function loadData() {
loading = true;
try {
const [settings, backupList] = await Promise.all([
getSettings(),
listBackups()
]);
backupEnabled = settings.backup_enabled ?? false;
backupIntervalHours = String(settings.backup_interval_hours ?? 24);
backupRetentionCount = String(settings.backup_retention_count ?? 10);
backups = backupList ?? [];
} catch (err) {
toasts.error(err instanceof Error ? err.message : 'Failed to load backup settings');
} finally {
loading = false;
}
}
async function handleSave() {
saving = true;
try {
await updateSettings({
backup_enabled: backupEnabled,
backup_interval_hours: Math.max(1, parseInt(backupIntervalHours, 10) || 24),
backup_retention_count: Math.max(1, parseInt(backupRetentionCount, 10) || 10)
} as any);
toasts.success($t('settingsBackup.saved'));
} catch (err) {
toasts.error(err instanceof Error ? err.message : $t('settingsBackup.saveFailed'));
} finally {
saving = false;
}
}
async function handleBackupNow() {
creatingBackup = true;
try {
const backup = await triggerBackup();
backups = [backup, ...backups];
toasts.success($t('settingsBackup.backupCreated'));
} catch (err) {
toasts.error(err instanceof Error ? err.message : $t('settingsBackup.backupFailed'));
} finally {
creatingBackup = false;
}
}
async function handleDelete() {
const id = confirmDeleteId;
confirmDeleteId = '';
try {
await deleteBackup(id);
backups = backups.filter(b => b.id !== id);
toasts.success($t('settingsBackup.deleted'));
} catch (err) {
toasts.error(err instanceof Error ? err.message : $t('settingsBackup.deleteFailed'));
}
}
async function handleRestore() {
const id = confirmRestoreId;
confirmRestoreId = '';
try {
await restoreBackup(id);
toasts.success($t('settingsBackup.restored'));
} catch (err) {
toasts.error(err instanceof Error ? err.message : $t('settingsBackup.restoreFailed'));
}
}
function handleDownload(id: string) {
const token = getAuthToken();
const url = backupDownloadUrl(id);
// Open download in new tab with auth header via fetch+blob
fetch(url, { headers: { Authorization: `Bearer ${token}` } })
.then(r => {
if (!r.ok) throw new Error('Download failed');
return r.blob();
})
.then(blob => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = '';
a.click();
URL.revokeObjectURL(a.href);
})
.catch(err => toasts.error(err instanceof Error ? err.message : 'Download failed'));
}
function formatSize(bytes: number): string {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
}
function formatDate(dateStr: string): string {
if (!dateStr) return '';
const d = new Date(dateStr + 'Z');
return d.toLocaleString();
}
$effect(() => { loadData(); });
</script>
<svelte:head>
<title>{$t('settingsBackup.title')} - {$t('app.name')}</title>
</svelte:head>
<div class="space-y-6">
{#if loading}
<div class="space-y-4">
<Skeleton height="2rem" width="12rem" />
<Skeleton height="10rem" />
<Skeleton height="15rem" />
</div>
{:else}
<!-- Backup Settings -->
<div class="rounded-xl border border-[var(--border-primary)] bg-[var(--surface-card)] p-6 shadow-[var(--shadow-sm)]">
<h2 class="mb-1 text-lg font-semibold text-[var(--text-primary)]">{$t('settingsBackup.title')}</h2>
<p class="mb-4 text-sm text-[var(--text-secondary)]">{$t('settingsBackup.description')}</p>
<div class="space-y-4">
<!-- Auto backup toggle -->
<label class="flex items-center gap-3 cursor-pointer">
<input type="checkbox" bind:checked={backupEnabled}
class="h-4 w-4 rounded border-[var(--border-primary)] text-[var(--color-brand-600)] focus:ring-[var(--color-brand-500)]" />
<div>
<span class="text-sm font-medium text-[var(--text-primary)]">{$t('settingsBackup.autoBackup')}</span>
<p class="text-xs text-[var(--text-tertiary)]">{$t('settingsBackup.autoBackupHelp')}</p>
</div>
</label>
{#if backupEnabled}
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 ml-7">
<FormField
label={$t('settingsBackup.interval')}
name="backupIntervalHours"
type="number"
bind:value={backupIntervalHours}
placeholder="24"
helpText={$t('settingsBackup.intervalHelp')}
/>
<FormField
label={$t('settingsBackup.retention')}
name="backupRetentionCount"
type="number"
bind:value={backupRetentionCount}
placeholder="10"
helpText={$t('settingsBackup.retentionHelp')}
/>
</div>
{/if}
<div class="flex items-center gap-3">
<button onclick={handleSave} disabled={saving}
class="inline-flex items-center gap-2 rounded-lg bg-[var(--color-brand-600)] px-4 py-2.5 text-sm font-medium text-white shadow-sm transition-all duration-150 hover:bg-[var(--color-brand-700)] disabled:opacity-50 active:animate-press">
{#if saving}<IconLoader size={16} />{/if}
{saving ? $t('settingsBackup.saving') : $t('settingsBackup.save')}
</button>
<button onclick={handleBackupNow} disabled={creatingBackup}
class="inline-flex items-center gap-2 rounded-lg border border-[var(--color-brand-600)] px-4 py-2.5 text-sm font-medium text-[var(--color-brand-600)] hover:bg-[var(--color-brand-50)] disabled:opacity-50 transition-colors active:animate-press">
{#if creatingBackup}<IconLoader size={16} />{/if}
{creatingBackup ? $t('settingsBackup.creatingBackup') : $t('settingsBackup.backupNow')}
</button>
</div>
</div>
</div>
<!-- Backup List -->
<div class="rounded-xl border border-[var(--border-primary)] bg-[var(--surface-card)] shadow-[var(--shadow-sm)] overflow-hidden">
<div class="flex items-center justify-between px-6 py-4 border-b border-[var(--border-primary)]">
<h2 class="text-lg font-semibold text-[var(--text-primary)]">{$t('settingsBackup.backupList')}</h2>
<button onclick={() => loadData()}
class="inline-flex items-center gap-1.5 rounded-lg border border-[var(--border-primary)] px-3 py-1.5 text-sm text-[var(--text-secondary)] hover:bg-[var(--surface-card-hover)] transition-colors">
<IconRefresh size={14} />
</button>
</div>
{#if backups.length === 0}
<div class="p-8 text-center text-sm text-[var(--text-tertiary)]">
{$t('settingsBackup.noBackups')}
</div>
{:else}
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="border-b border-[var(--border-primary)] bg-[var(--surface-card-hover)]">
<th class="px-4 py-3 text-left font-medium text-[var(--text-secondary)]">{$t('settingsBackup.columnFilename')}</th>
<th class="px-4 py-3 text-left font-medium text-[var(--text-secondary)]">{$t('settingsBackup.columnSize')}</th>
<th class="px-4 py-3 text-left font-medium text-[var(--text-secondary)]">{$t('settingsBackup.columnType')}</th>
<th class="px-4 py-3 text-left font-medium text-[var(--text-secondary)]">{$t('settingsBackup.columnDate')}</th>
<th class="px-4 py-3 text-right font-medium text-[var(--text-secondary)]">{$t('settingsBackup.columnActions')}</th>
</tr>
</thead>
<tbody>
{#each backups as backup}
<tr class="border-b border-[var(--border-primary)] last:border-b-0 hover:bg-[var(--surface-card-hover)] transition-colors">
<td class="px-4 py-3 font-mono text-xs text-[var(--text-primary)]">{backup.filename}</td>
<td class="px-4 py-3 text-[var(--text-secondary)]">{formatSize(backup.size_bytes)}</td>
<td class="px-4 py-3">
<span class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium
{backup.backup_type === 'auto'
? 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400'
: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400'}">
{backup.backup_type === 'auto' ? $t('settingsBackup.typeAuto') : $t('settingsBackup.typeManual')}
</span>
</td>
<td class="px-4 py-3 text-[var(--text-secondary)]">{formatDate(backup.created_at)}</td>
<td class="px-4 py-3 text-right">
<div class="flex items-center justify-end gap-1">
<button onclick={() => handleDownload(backup.id)}
class="rounded-lg px-2 py-1 text-xs text-[var(--color-brand-600)] hover:bg-[var(--color-brand-50)] transition-colors">
{$t('settingsBackup.download')}
</button>
<button onclick={() => { confirmRestoreId = backup.id; }}
class="rounded-lg px-2 py-1 text-xs text-[var(--text-secondary)] hover:bg-[var(--surface-card-hover)] transition-colors">
{$t('settingsBackup.restore')}
</button>
<button onclick={() => { confirmDeleteId = backup.id; }}
class="rounded-lg px-2 py-1 text-xs text-[var(--color-danger)] hover:bg-[var(--color-danger-light)] transition-colors">
<IconTrash size={14} />
</button>
</div>
</td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
</div>
{/if}
</div>
<!-- Delete confirmation -->
<ConfirmDialog
open={confirmDeleteId !== ''}
title={$t('settingsBackup.delete')}
message={$t('settingsBackup.deleteConfirm')}
onconfirm={handleDelete}
oncancel={() => { confirmDeleteId = ''; }}
/>
<!-- Restore confirmation -->
<ConfirmDialog
open={confirmRestoreId !== ''}
title={$t('settingsBackup.restore')}
message={$t('settingsBackup.restoreConfirm') + '\n\n' + $t('settingsBackup.restoreWarning')}
onconfirm={handleRestore}
oncancel={() => { confirmRestoreId = ''; }}
/>