feat(backup): Sicherungen von Datenbank und Dateien mit Restore

- app/backup.php: ein tar.gz-Archiv aus manifest.json (Format, Zeilen- und
  Dateizahl, SHA-256 des Dumps), database.sql (DROP/CREATE/INSERT) und allen
  Dateien aus storage/ ohne backups/ und logs/; Aufbewahrung app.backup_keep,
  eigener SQL-Splitter fürs Einspielen, Rechte 0600/0700
- Verwaltung → Sicherungen (/admin/backups): anlegen, herunterladen, hochladen
  (mit Prüfsummen- und Formatprüfung), einspielen (Datenbank und/oder Dateien,
  Bestätigungswort, automatische Sicherheitskopie, alte Dateien beiseitegelegt),
  löschen
- tools/backup.php (create/list/restore/rotate/delete) und
  /cron/backup?token=… für Hosting ohne SSH
- Bedienungsanleitung um Admin-Abschnitt „Sicherungen“ erweitert, README,
  ROADMAP (Feature 19), DSGVO-Hinweis, Deploy-Manifest um tools/backup.php
This commit is contained in:
torsten 2026-11-30 16:11:36 +01:00
parent fb98f9a355
commit 952a2874b0
14 changed files with 1393 additions and 2 deletions

View file

@ -46,6 +46,14 @@ function admin_handle(string $path, array $user): void
admin_mailings($user);
return;
}
if ($path === '/admin/backups') {
admin_backups($user);
return;
}
if (preg_match('#^/admin/backups/(backup_[A-Za-z0-9_.-]+\.tar\.gz)/download$#', $path, $m) === 1) {
admin_backup_download($m[1]);
return;
}
if (preg_match('#^/admin/mailings/(\d+)$#', $path, $m) === 1) {
admin_mailing_detail((int) $m[1]);
return;
@ -718,3 +726,113 @@ function admin_mailings(array $user): void
'batchSize' => MAILING_BATCH_DEFAULT,
], 'Kundenrundmail');
}
/* ------------------------------------------------------------ Sicherungen */
function admin_backups(array $user): void
{
$errors = [];
if (is_post()) {
require_post();
$action = post_string('action');
try {
if ($action === 'create') {
$backup = backup_create('manuell');
$rotation = backup_rotate();
flash('success', sprintf(
'Sicherung %s erstellt: %d Zeilen, %d Dateien, %s%s.',
$backup['name'],
$backup['rows'],
$backup['files'],
backup_human_size($backup['size']),
$rotation['deleted'] !== [] ? ', ' . count($rotation['deleted']) . ' alte entfernt' : ''
));
redirect('/admin/backups');
}
if ($action === 'delete') {
$name = post_string('name');
if (backup_delete($name)) {
flash('success', 'Sicherung ' . $name . ' gelöscht.');
} else {
flash('error', 'Diese Sicherung gibt es nicht (mehr).');
}
redirect('/admin/backups');
}
if ($action === 'upload') {
$import = backup_import($_FILES['archive'] ?? []);
backup_rotate();
flash('success', sprintf(
'Sicherung übernommen: %s (Stand %s, %d Dateien, %s).',
$import['name'],
(string) fmt_datetime($import['created_at']),
(int) ($import['files'] ?? 0),
backup_human_size($import['size'])
));
redirect('/admin/backups');
}
if ($action === 'restore') {
admin_backup_restore($user);
}
} catch (Throwable $error) {
error_log('Sicherung: ' . $error->getMessage());
$errors['general'] = $error->getMessage();
}
}
$restoreResult = $_SESSION['backup_restore_result'] ?? null;
unset($_SESSION['backup_restore_result']);
render_page('admin_backups', [
'backups' => backup_list(),
'keep' => backup_keep(),
'errors' => $errors,
'restoreResult' => is_array($restoreResult) ? $restoreResult : null,
'directory' => backup_root(),
'maxMb' => (int) config('app.backup_max_mb', 200),
'available' => class_exists('PharData'),
], 'Sicherungen');
}
function admin_backup_restore(array $user): never
{
$name = post_string('name');
if (post_string('confirm') !== 'WIEDERHERSTELLEN') {
throw new RuntimeException('Bitte zur Bestätigung WIEDERHERSTELLEN in das Textfeld schreiben.');
}
$database = post_string('restore_database') === '1';
$files = post_string('restore_files') === '1';
if (!$database && !$files) {
throw new RuntimeException('Bitte auswählen, was eingespielt werden soll.');
}
$result = backup_restore($name, ['database' => $database, 'files' => $files, 'safety' => true]);
error_log(sprintf(
'Restore durch Benutzer %d (%s %s): %s – %d Anweisungen, %d Dateien, Sicherheitskopie %s',
(int) $user['id'],
(string) $user['first_name'],
(string) $user['last_name'],
$name,
$result['statements'],
$result['files'],
(string) $result['safety']
));
$_SESSION['backup_restore_result'] = $result;
flash('success', 'Sicherung ' . $name . ' wurde eingespielt.');
redirect('/admin/backups');
}
function admin_backup_download(string $name): void
{
$path = backup_path_for($name);
if ($path === null) {
abort_page(404, 'Diese Sicherung gibt es nicht (mehr).');
}
header('Content-Type: application/gzip');
header('Content-Length: ' . (string) filesize($path));
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Cache-Control: private, no-store');
readfile($path);
exit;
}