- 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
164 lines
5.5 KiB
PHP
164 lines
5.5 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Sicherungen (Backup/Restore) von Datenbank und Dateien – CLI.
|
||
*
|
||
* php tools/backup.php create [--reason manuell]
|
||
* php tools/backup.php list
|
||
* php tools/backup.php restore <archiv> [--db-only|--files-only] [--force]
|
||
* php tools/backup.php rotate [--keep 14]
|
||
* php tools/backup.php delete <archiv>
|
||
*
|
||
* Die Archive liegen in app.backup_path (Standard: storage/backups) und
|
||
* enthalten personenbezogene Daten – Ablage mit 0600, Transfer verschlüsselt.
|
||
* Dieselbe Funktion gibt es in der Verwaltung (/admin/backups) und per
|
||
* Cron (/cron/backup?token=…).
|
||
*/
|
||
|
||
if (PHP_SAPI !== 'cli') {
|
||
http_response_code(403);
|
||
exit("Nur per CLI.\n");
|
||
}
|
||
|
||
require __DIR__ . '/../app/bootstrap.php';
|
||
|
||
/** @return list<string> */
|
||
function backup_cli_args(): array
|
||
{
|
||
global $argv;
|
||
return array_slice($argv, 1);
|
||
}
|
||
|
||
function backup_cli_option(array $args, string $name, ?string $default = null): ?string
|
||
{
|
||
foreach ($args as $index => $arg) {
|
||
if ($arg === '--' . $name) {
|
||
return $args[$index + 1] ?? $default;
|
||
}
|
||
if (str_starts_with($arg, '--' . $name . '=')) {
|
||
return substr($arg, strlen($name) + 3);
|
||
}
|
||
}
|
||
return $default;
|
||
}
|
||
|
||
function backup_cli_flag(array $args, string $name): bool
|
||
{
|
||
return in_array('--' . $name, $args, true);
|
||
}
|
||
|
||
/** @return list<string> */
|
||
function backup_cli_positional(array $args): array
|
||
{
|
||
$out = [];
|
||
foreach ($args as $arg) {
|
||
if (!str_starts_with($arg, '--')) {
|
||
$out[] = $arg;
|
||
}
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
$args = backup_cli_args();
|
||
$command = backup_cli_positional($args)[0] ?? 'help';
|
||
|
||
try {
|
||
switch ($command) {
|
||
case 'create':
|
||
$reason = (string) backup_cli_option($args, 'reason', 'cli');
|
||
$backup = backup_create($reason);
|
||
$rotation = backup_rotate();
|
||
echo sprintf(
|
||
"Sicherung %s erstellt: %d Zeilen aus %d Tabellen, %d Dateien, %s.\n",
|
||
$backup['name'],
|
||
$backup['rows'],
|
||
$backup['tables'],
|
||
$backup['files'],
|
||
backup_human_size($backup['size'])
|
||
);
|
||
echo 'Ablage: ' . $backup['path'] . "\n";
|
||
if ($rotation['deleted'] !== []) {
|
||
echo 'Aufgeräumt: ' . implode(', ', $rotation['deleted']) . "\n";
|
||
}
|
||
break;
|
||
|
||
case 'list':
|
||
$items = backup_list();
|
||
if ($items === []) {
|
||
echo "Keine Sicherungen vorhanden (" . backup_root() . ").\n";
|
||
break;
|
||
}
|
||
foreach ($items as $item) {
|
||
echo sprintf(
|
||
"%-46s %s %s %s\n",
|
||
$item['name'],
|
||
gmdate('d.m.Y H:i', strtotime($item['created_at'] . ' UTC') ?: time()),
|
||
backup_human_size($item['size']),
|
||
$item['rows'] === null ? '' : $item['rows'] . ' Zeilen / ' . (int) ($item['files'] ?? 0) . ' Dateien'
|
||
);
|
||
}
|
||
echo count($items) . " Sicherung(en) in " . backup_root() . "\n";
|
||
break;
|
||
|
||
case 'restore':
|
||
$archive = backup_cli_positional($args)[1] ?? '';
|
||
if ($archive === '') {
|
||
fwrite(STDERR, "Bitte ein Archiv angeben (siehe: php tools/backup.php list).\n");
|
||
exit(1);
|
||
}
|
||
$dbOnly = backup_cli_flag($args, 'db-only');
|
||
$filesOnly = backup_cli_flag($args, 'files-only');
|
||
if ($dbOnly && $filesOnly) {
|
||
fwrite(STDERR, "--db-only und --files-only schließen sich aus.\n");
|
||
exit(1);
|
||
}
|
||
$result = backup_restore($archive, [
|
||
'database' => !$filesOnly,
|
||
'files' => !$dbOnly,
|
||
'safety' => !backup_cli_flag($args, 'no-safety'),
|
||
]);
|
||
echo sprintf(
|
||
"Sicherung %s eingespielt: %d Anweisungen, %d Dateien.\n",
|
||
$result['archive'],
|
||
$result['statements'],
|
||
$result['files']
|
||
);
|
||
if ($result['safety'] !== null) {
|
||
echo 'Sicherheitskopie des alten Stands: ' . $result['safety'] . "\n";
|
||
}
|
||
foreach ($result['warnings'] as $warning) {
|
||
echo 'Hinweis: ' . $warning . "\n";
|
||
}
|
||
break;
|
||
|
||
case 'rotate':
|
||
$keep = backup_cli_option($args, 'keep');
|
||
$rotation = backup_rotate($keep === null ? null : (int) $keep);
|
||
echo $rotation['deleted'] === []
|
||
? "Nichts zu löschen.\n"
|
||
: 'Entfernt: ' . implode(', ', $rotation['deleted']) . "\n";
|
||
break;
|
||
|
||
case 'delete':
|
||
$archive = backup_cli_positional($args)[1] ?? '';
|
||
if ($archive === '') {
|
||
fwrite(STDERR, "Bitte ein Archiv angeben.\n");
|
||
exit(1);
|
||
}
|
||
echo backup_delete($archive) ? "Gelöscht: $archive\n" : "Nicht gefunden: $archive\n";
|
||
break;
|
||
|
||
default:
|
||
echo "Sicherungen von Drink it cold\n\n"
|
||
. " php tools/backup.php create [--reason text]\n"
|
||
. " php tools/backup.php list\n"
|
||
. " php tools/backup.php restore <archiv> [--db-only|--files-only] [--no-safety]\n"
|
||
. " php tools/backup.php rotate [--keep N]\n"
|
||
. " php tools/backup.php delete <archiv>\n";
|
||
break;
|
||
}
|
||
} catch (Throwable $error) {
|
||
fwrite(STDERR, 'Fehler: ' . $error->getMessage() . "\n");
|
||
exit(1);
|
||
}
|