232 lines
9.2 KiB
TypeScript
232 lines
9.2 KiB
TypeScript
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 } from './BackupSummary.tsx'
|
|
import { backupRows, TARGET_LABELS } from './backup.ts'
|
|
import { when } from './format.ts'
|
|
import { Board, BoardButtons, type BoardPanel } from './PanelBoard.tsx'
|
|
import { useBoard } from './useBoard.ts'
|
|
import { Pairs } from './ServerPanel.tsx'
|
|
import { bytes } from './format.ts'
|
|
import SearchField from './SearchField.tsx'
|
|
import { matches } from './search.ts'
|
|
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<Backup | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [unavailable, setUnavailable] = useState(false)
|
|
const [note, setNote] = useState<string | null>(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<string | null>(null)
|
|
|
|
/** Reads the backups; the state changes only once the server answers. */
|
|
const refresh = useCallback(async () => {
|
|
await request<Backup>('GET', '/backup', BACKUP_SHAPE).then((found) => {
|
|
setBackup(found)
|
|
setError(null)
|
|
setUnavailable(false)
|
|
if (found.state && found.state.result !== 'running' && found.state.started !== startedBefore.current) {
|
|
setWatching(false)
|
|
}
|
|
}, (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: (
|
|
<Pairs
|
|
rows={[
|
|
['Scheduled backups', settings.enabled ? 'on' : 'off'],
|
|
['Schedule', settings.schedule],
|
|
['Target', TARGET_LABELS[settings.target] ?? settings.target],
|
|
['Metrics database', settings.metrics ? 'included' : 'not included'],
|
|
...(local
|
|
? [
|
|
['Folder', settings.path],
|
|
['Kept', keeps.map((keep) => `${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: (
|
|
<>
|
|
<Box sx={{ mb: 1 }}><BackupChip state={backup.state} /></Box>
|
|
<Pairs rows={backupRows(backup)} />
|
|
</>
|
|
),
|
|
},
|
|
...(local ? [{
|
|
id: 'archives',
|
|
title: 'Local archives',
|
|
wide: true,
|
|
content: (
|
|
<>
|
|
<Box sx={{ mb: 1 }}>
|
|
<SearchField value={search} onChange={(value) => { setSearch(value); setPage(0) }} />
|
|
</Box>
|
|
<Box sx={{ overflowX: 'auto' }}>
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell>Archive</TableCell>
|
|
<TableCell>Time</TableCell>
|
|
<TableCell align="right">Size</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{archives.length === 0 && (
|
|
<TableRow>
|
|
<TableCell colSpan={3} sx={{ color: 'text.secondary' }}>None.</TableCell>
|
|
</TableRow>
|
|
)}
|
|
{archives.slice(shownPage * rows, (shownPage + 1) * rows).map((archive) => (
|
|
<TableRow key={archive.name} hover>
|
|
<TableCell sx={{ fontFamily: 'monospace' }}>{archive.name}</TableCell>
|
|
<TableCell>{when(archive.time)}</TableCell>
|
|
<TableCell align="right">{bytes(archive.size)}</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</Box>
|
|
<TablePagination
|
|
component="div"
|
|
count={archives.length}
|
|
page={shownPage}
|
|
rowsPerPage={rows}
|
|
rowsPerPageOptions={[10, 25, 50, 100]}
|
|
onPageChange={(_event, next) => setPage(next)}
|
|
onRowsPerPageChange={(event) => { setRows(Number(event.target.value)); setPage(0) }}
|
|
/>
|
|
</>
|
|
),
|
|
}] : []),
|
|
] : []
|
|
|
|
return (
|
|
<Box component="section">
|
|
<Toolbar description="Backups of the server's configuration, keys, users and metrics.">
|
|
<Button variant="contained" startIcon={<Edit />} disabled={busy || backup === null} onClick={() => setEditing(true)}>
|
|
Edit
|
|
</Button>
|
|
<Button variant="outlined" startIcon={<PlayArrow />} disabled={busy || backup === null || running || watching} onClick={() => void runNow()}>
|
|
Run now
|
|
</Button>
|
|
<BoardButtons board={board} />
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>
|
|
Refresh
|
|
</Button>
|
|
</Toolbar>
|
|
|
|
{error && <Alert severity={unavailable ? 'info' : 'error'} sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
{note && <Alert severity="info" sx={{ mb: 2 }} onClose={() => setNote(null)}>{note}</Alert>}
|
|
{backup && settings && !local && !backup.client && (
|
|
<Alert severity="warning" sx={{ mb: 2 }}>
|
|
proxmox-backup-client is not installed on the server; install proxmox-backup-client-static from Proxmox's client repository.
|
|
</Alert>
|
|
)}
|
|
{backup && settings && local && settings.rclone_remote && !(backup.rclone.installed && backup.rclone.config) && (
|
|
<Alert severity="warning" sx={{ mb: 2 }}>
|
|
{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.
|
|
</Alert>
|
|
)}
|
|
|
|
{backup && settings && <Board board={board} panels={panels} />}
|
|
|
|
{editing && backup && (
|
|
<BackupDialog
|
|
backup={backup}
|
|
onCancel={() => 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()
|
|
}}
|
|
/>
|
|
)}
|
|
</Box>
|
|
)
|
|
}
|