import Edit from '@mui/icons-material/Edit' import PlayArrow from '@mui/icons-material/PlayArrow' import Refresh from '@mui/icons-material/Refresh' import { Alert, Box, Button, Table, TableBody, TableCell, TableHead, TablePagination, TableRow } from '@mui/material' import { useCallback, useEffect, useRef, useState } from 'react' import BackupDialog from './BackupDialog.tsx' import { BackupChip, TARGET_LABELS, backupRows, when } from './BackupSummary.tsx' import { Board, BoardButtons, useBoard, type BoardPanel } from './PanelBoard.tsx' import { Pairs, bytes } from './ServerPanel.tsx' import SearchField, { matches } from './SearchField.tsx' import Toolbar from './Toolbar.tsx' import { ApiError, BACKUP_KEEPS, BACKUP_SCHEDULES, BACKUP_SETTINGS_SHAPE, BACKUP_SHAPE, STARTED_SHAPE, request, type Backup } from './api.ts' /** The localStorage key of the tab's layout. */ const LAYOUT_KEY = 'linux-manager-ui.backup-layout' /** The note shown after a change, which the server applies when linux-manager-setup has run again. */ const APPLIED = 'The server applies the change in the background.' /** Seconds between reads while a backup runs or is starting. */ const WATCH_SECONDS = 3 /** * The server's backups: the settings, the last run and the local archives, searched by name and time and shown a page at * a time; edit the settings and start a backup now. * While a backup runs, or after Run now until it has ended, the tab reads every WATCH_SECONDS. The panels are on a * PanelBoard whose layout is kept in the browser. */ export default function BackupPanel() { const [backup, setBackup] = useState(null) const [error, setError] = useState(null) const [unavailable, setUnavailable] = useState(false) const [note, setNote] = useState(null) const [busy, setBusy] = useState(false) const [editing, setEditing] = useState(false) const board = useBoard(LAYOUT_KEY) const [watching, setWatching] = useState(false) const [search, setSearch] = useState('') const [page, setPage] = useState(0) const [rows, setRows] = useState(10) // The start of the last run when Run now was pressed; watching ends once a later run has ended. const startedBefore = useRef(null) const refresh = useCallback(async () => { try { const found = await request('GET', '/backup', BACKUP_SHAPE) setBackup(found) setError(null) setUnavailable(false) if (found.state && found.state.result !== 'running' && found.state.started !== startedBefore.current) { setWatching(false) } } catch (caught) { const failure = caught as ApiError setBackup(null) setUnavailable(failure.status === 404) setError(failure.message) setWatching(false) } }, []) useEffect(() => { void refresh() }, [refresh]) const running = backup?.state?.result === 'running' useEffect(() => { if (!watching && !running) { return } const timer = window.setInterval(() => void refresh(), WATCH_SECONDS * 1000) return () => window.clearInterval(timer) }, [refresh, watching, running]) async function runNow() { setBusy(true) try { startedBefore.current = backup?.state?.started ?? null await request('POST', '/backup/run', STARTED_SHAPE) setWatching(true) setError(null) } catch (caught) { setError((caught as Error).message) } finally { setBusy(false) } } const archives = (backup?.archives ?? []).filter((archive) => matches(search, [archive.name, when(archive.time)])) // The page shown stays within the archives found, which a refresh may make fewer. const shownPage = Math.min(page, Math.max(0, Math.ceil(archives.length / rows) - 1)) const settings = backup?.settings const local = settings?.target === 'local' const keeps = settings ? BACKUP_KEEPS.slice(BACKUP_SCHEDULES.indexOf(settings.schedule as (typeof BACKUP_SCHEDULES)[number])) : [] const panels: BoardPanel[] = backup && settings ? [ { id: 'settings', title: 'Settings', content: ( `${settings[keep]} ${keep.slice(5)}`).join(', ')], ['Cloud copy', settings.rclone_remote ? `${settings.rclone_mode} to ${settings.rclone_remote}` : 'none'], ] satisfies [string, string][] : [ ['Repository', settings.pbs_repository || '-'], ['Token secret', settings.pbs_password_set ? 'set' : 'missing'], ['Fingerprint', settings.pbs_fingerprint || '-'], ['Namespace', settings.pbs_namespace || '-'], ['Encryption key file', settings.pbs_keyfile || 'none'], ] satisfies [string, string][]), ]} /> ), }, { id: 'last-run', title: 'Last run', content: ( <> ), }, ...(local ? [{ id: 'archives', title: 'Local archives', wide: true, content: ( <> { setSearch(value); setPage(0) }} /> Archive Time Size {archives.length === 0 && ( None. )} {archives.slice(shownPage * rows, (shownPage + 1) * rows).map((archive) => ( {archive.name} {when(archive.time)} {bytes(archive.size)} ))}
setPage(next)} onRowsPerPageChange={(event) => { setRows(Number(event.target.value)); setPage(0) }} /> ), }] : []), ] : [] return ( {error && {error}} {note && setNote(null)}>{note}} {backup && settings && !local && !backup.client && ( proxmox-backup-client is not installed on the server; install proxmox-backup-client-static from Proxmox's client repository. )} {backup && settings && local && settings.rclone_remote && !(backup.rclone.installed && backup.rclone.config) && ( {backup.rclone.installed ? 'The server has no /etc/linux-manager/rclone.conf.' : 'rclone is not installed on the server.'} The cloud copy fails until it has. )} {backup && settings && } {editing && backup && ( setEditing(false)} onSave={async (next, password) => { await request('PUT', '/backup', BACKUP_SETTINGS_SHAPE, password === '' ? next : { ...next, pbs_password: password }) setEditing(false) setNote(APPLIED) await refresh() }} /> )} ) }