203 lines
9.8 KiB
TypeScript
203 lines
9.8 KiB
TypeScript
import {
|
|
Alert, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch,
|
|
TextField, Typography,
|
|
} from '@mui/material'
|
|
import { useState, type FormEvent } from 'react'
|
|
import { TARGET_LABELS } from './backup.ts'
|
|
import { BACKUP_KEEPS, BACKUP_SCHEDULES, BACKUP_TARGETS, RCLONE_MODES, type Backup, type BackupKeep, type BackupSettings } from './api.ts'
|
|
|
|
/** The labels of the retention settings. */
|
|
const KEEP_LABELS: Record<BackupKeep, string> = {
|
|
keep_hourly: 'Hourly',
|
|
keep_daily: 'Daily',
|
|
keep_weekly: 'Weekly',
|
|
keep_monthly: 'Monthly',
|
|
keep_yearly: 'Yearly',
|
|
}
|
|
|
|
/** The labels of the rclone modes. */
|
|
const MODE_LABELS: Record<string, string> = {
|
|
sync: 'Sync: the cloud mirrors the archives, retention applies to both',
|
|
copy: 'Copy: nothing is deleted in the cloud',
|
|
}
|
|
|
|
/** A PBS certificate fingerprint: 32 hexadecimal pairs joined by colons. */
|
|
const FINGERPRINT = /^([0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}$/
|
|
|
|
/**
|
|
* A dialog that edits the server's backup settings: whether backups run, the schedule and the target; for a local
|
|
* folder, its path, the retention settings that apply to the schedule and the cloud copy with a remote of the server's
|
|
* rclone.conf; for a Proxmox Backup Server, the repository, the token secret, the fingerprint, the namespace and the
|
|
* key file; and whether the metrics database is included. An empty token secret keeps the current one. onSave sends
|
|
* the settings and the secret; the dialog closes when it succeeds and shows the server's message when it fails.
|
|
*/
|
|
export default function BackupDialog(props: {
|
|
backup: Backup
|
|
onSave: (settings: BackupSettings, password: string) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const current = props.backup.settings
|
|
const [settings, setSettings] = useState<BackupSettings>(() => {
|
|
const { pbs_password_set: _set, ...rest } = current
|
|
return rest
|
|
})
|
|
const [remote, setRemote] = useState(current.rclone_remote.split(':')[0] ?? '')
|
|
const [remotePath, setRemotePath] = useState(current.rclone_remote.includes(':') ? current.rclone_remote.slice(current.rclone_remote.indexOf(':') + 1) : '')
|
|
const [password, setPassword] = useState('')
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const change = (values: Partial<BackupSettings>) => setSettings({ ...settings, ...values })
|
|
const local = settings.target === 'local'
|
|
const keeps = BACKUP_KEEPS.slice(BACKUP_SCHEDULES.indexOf(settings.schedule as (typeof BACKUP_SCHEDULES)[number]))
|
|
const remotes = [...new Set([...props.backup.rclone.remotes, ...(remote ? [remote] : [])])]
|
|
|
|
const pathProblem = local && !settings.path.startsWith('/') ? 'An absolute path.' : ''
|
|
const keepProblem = local && !keeps.some((keep) => settings[keep] > 0) ? 'At least one of them above 0.' : ''
|
|
const repositoryProblem = !local && settings.pbs_repository.trim() === '' ? 'Required.' : ''
|
|
const passwordProblem = !local && !current.pbs_password_set && password === '' ? 'Required.' : ''
|
|
const fingerprintProblem = settings.pbs_fingerprint !== '' && !FINGERPRINT.test(settings.pbs_fingerprint) ? '32 hexadecimal pairs joined by colons.' : ''
|
|
const keyfileProblem = settings.pbs_keyfile !== '' && !settings.pbs_keyfile.startsWith('/') ? 'An absolute path.' : ''
|
|
const blocked = [pathProblem, keepProblem, repositoryProblem, passwordProblem, fingerprintProblem, keyfileProblem].some((problem) => problem !== '')
|
|
|
|
async function submit(event: FormEvent) {
|
|
event.preventDefault()
|
|
if (blocked) {
|
|
return
|
|
}
|
|
setSaving(true)
|
|
setError(null)
|
|
try {
|
|
await props.onSave({ ...settings, rclone_remote: remote ? `${remote}:${remotePath.trim()}` : '' }, password)
|
|
} catch (caught) {
|
|
setError((caught as Error).message)
|
|
setSaving(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
|
|
<DialogTitle>Edit backup settings</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<FormControlLabel
|
|
control={<Switch checked={settings.enabled} onChange={(event) => change({ enabled: event.target.checked })} />}
|
|
label="Scheduled backups"
|
|
/>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField select fullWidth label="Schedule" value={settings.schedule} onChange={(event) => change({ schedule: event.target.value })}>
|
|
{BACKUP_SCHEDULES.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
<TextField select fullWidth label="Target" value={settings.target} onChange={(event) => change({ target: event.target.value })}>
|
|
{BACKUP_TARGETS.map((item) => <MenuItem key={item} value={item}>{TARGET_LABELS[item]}</MenuItem>)}
|
|
</TextField>
|
|
</Stack>
|
|
<FormControlLabel
|
|
control={<Checkbox checked={settings.metrics} onChange={(event) => change({ metrics: event.target.checked })} />}
|
|
label="Include the metrics database"
|
|
/>
|
|
{local ? (
|
|
<>
|
|
<TextField
|
|
label="Folder"
|
|
value={settings.path}
|
|
autoComplete="off"
|
|
error={pathProblem !== ''}
|
|
helperText={pathProblem || 'The folder of the archives on the server; it may be a mount.'}
|
|
onChange={(event) => change({ path: event.target.value })}
|
|
/>
|
|
<Typography variant="subtitle2">Archives kept</Typography>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
{keeps.map((keep) => (
|
|
<TextField
|
|
key={keep}
|
|
label={KEEP_LABELS[keep]}
|
|
type="number"
|
|
value={settings[keep]}
|
|
error={keepProblem !== ''}
|
|
slotProps={{ htmlInput: { min: 0 } }}
|
|
onChange={(event) => change({ [keep]: Math.max(0, Math.trunc(Number(event.target.value) || 0)) })}
|
|
/>
|
|
))}
|
|
</Stack>
|
|
{keepProblem && <Typography variant="caption" color="error">{keepProblem}</Typography>}
|
|
<Typography variant="subtitle2">Cloud copy</Typography>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField
|
|
select
|
|
fullWidth
|
|
label="rclone remote"
|
|
value={remote}
|
|
helperText={props.backup.rclone.installed ? (remotes.length === 0 ? 'No remotes in rclone.conf.' : '') : 'rclone is not installed.'}
|
|
onChange={(event) => setRemote(event.target.value)}
|
|
>
|
|
<MenuItem value="">None</MenuItem>
|
|
{remotes.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
<TextField
|
|
fullWidth
|
|
label="Remote path"
|
|
value={remotePath}
|
|
disabled={remote === ''}
|
|
autoComplete="off"
|
|
onChange={(event) => setRemotePath(event.target.value)}
|
|
/>
|
|
</Stack>
|
|
<TextField select label="Mode" value={settings.rclone_mode} disabled={remote === ''} onChange={(event) => change({ rclone_mode: event.target.value })}>
|
|
{RCLONE_MODES.map((item) => <MenuItem key={item} value={item}>{MODE_LABELS[item]}</MenuItem>)}
|
|
</TextField>
|
|
</>
|
|
) : (
|
|
<>
|
|
<TextField
|
|
label="Repository"
|
|
value={settings.pbs_repository}
|
|
autoComplete="off"
|
|
error={repositoryProblem !== ''}
|
|
helperText={repositoryProblem || 'user@realm!token@host:datastore'}
|
|
onChange={(event) => change({ pbs_repository: event.target.value.trim() })}
|
|
/>
|
|
<TextField
|
|
label="Token secret"
|
|
type="password"
|
|
value={password}
|
|
autoComplete="new-password"
|
|
error={passwordProblem !== ''}
|
|
helperText={passwordProblem || (current.pbs_password_set ? 'Leave empty to keep the current secret.' : '')}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
/>
|
|
<TextField
|
|
label="Fingerprint"
|
|
value={settings.pbs_fingerprint}
|
|
autoComplete="off"
|
|
error={fingerprintProblem !== ''}
|
|
helperText={fingerprintProblem || 'Of the server certificate, when the system CAs do not verify it.'}
|
|
onChange={(event) => change({ pbs_fingerprint: event.target.value.trim() })}
|
|
/>
|
|
<TextField
|
|
label="Namespace"
|
|
value={settings.pbs_namespace}
|
|
autoComplete="off"
|
|
onChange={(event) => change({ pbs_namespace: event.target.value.trim() })}
|
|
/>
|
|
<TextField
|
|
label="Encryption key file"
|
|
value={settings.pbs_keyfile}
|
|
autoComplete="off"
|
|
error={keyfileProblem !== ''}
|
|
helperText={keyfileProblem || 'A key of proxmox-backup-client key create on the server; empty for none.'}
|
|
onChange={(event) => change({ pbs_keyfile: event.target.value.trim() })}
|
|
/>
|
|
</>
|
|
)}
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|