getAttribute(PDO::ATTR_DRIVER_NAME); $liveDb = null; if ($driver === 'sqlite') { $row = $pdo->query('PRAGMA database_list')->fetch(PDO::FETCH_ASSOC); $liveDb = !empty($row['file']) ? realpath($row['file']) : null; } // Alle Dateien sammeln $exclude = [realpath($backupDir) ?: $backupDir]; $iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($rootDir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); $files = []; foreach ($iter as $file) { if (!$file->isFile()) continue; $filePath = $file->getRealPath(); $norm = str_replace('\\', '/', $filePath); $skip = false; foreach ($exclude as $ex) { if ($ex && strpos($filePath, $ex) === 0) { $skip = true; break; } } foreach (backup_excluded_fragments() as $frag) { if (str_contains($norm, $frag)) { $skip = true; break; } } if ($liveDb && ($filePath === $liveDb || $filePath === $liveDb . '-wal' || $filePath === $liveDb . '-shm' || $filePath === $liveDb . '-journal')) $skip = true; if ($skip) continue; $files[$filePath] = str_replace(chr(92), '/', substr($filePath, strlen($rootDir) + 1)); // im ZIP immer mit / } // Abzug erzeugen $sqlDump = null; // MySQL: Text $sqlFile = null; // MySQL: Dateiname im ZIP $sqliteSnapshot = null; $tableCount = 0; $dbError = null; if ($driver === 'sqlite') { $snapshot = tempnam(sys_get_temp_dir(), 'dhbk'); @unlink($snapshot); try { $pdo->exec('VACUUM INTO ' . $pdo->quote($snapshot)); $sqliteSnapshot = $snapshot; } catch (Throwable $e) { // Ältere SQLite-Version ohne VACUUM INTO: Datei kopieren (weniger sicher, aber besser als nichts) if ($liveDb && @copy($liveDb, $snapshot)) $sqliteSnapshot = $snapshot; else $dbError = 'SQLite-Abzug fehlgeschlagen: ' . $e->getMessage(); } if ($sqliteSnapshot) { $tableCount = (int) $pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'")->fetchColumn(); } } elseif ($driver === 'mysql') { try { $dbName = (string) $pdo->query('SELECT DATABASE()')->fetchColumn(); $dump = "-- DeskHub MySQL Backup\n-- Erstellt: " . date('Y-m-d H:i:s') . "\n-- Datenbank: " . $dbName . "\n\nSET FOREIGN_KEY_CHECKS=0;\n\n"; // Alle Tabellen auslesen $tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN); $tableCount = count($tables); foreach ($tables as $table) { // CREATE TABLE $create = $pdo->query("SHOW CREATE TABLE `$table`")->fetch(); $dump .= "\n-- Tabelle: $table\n"; $dump .= "DROP TABLE IF EXISTS `$table`;\n"; $dump .= $create['Create Table'] . ";\n\n"; // Daten $rows = $pdo->query("SELECT * FROM `$table`")->fetchAll(PDO::FETCH_ASSOC); if ($rows) { $cols = '`' . implode('`, `', array_keys($rows[0])) . '`'; $dump .= "INSERT INTO `$table` ($cols) VALUES\n"; $vals = []; foreach ($rows as $row) { $escaped = array_map(fn($v) => $v === null ? 'NULL' : $pdo->quote((string)$v), $row); $vals[] = '(' . implode(', ', $escaped) . ')'; } $dump .= implode(",\n", $vals) . ";\n\n"; } } $dump .= "SET FOREIGN_KEY_CHECKS=1;\n"; $sqlFile = 'database_backup.sql'; $sqlDump = $dump; } catch (Throwable $e) { $dbError = 'MySQL-Abzug fehlgeschlagen: ' . $e->getMessage(); } } // Ohne Datenbank-Abzug ist es kein brauchbares Backup: lieber Fehler melden als ein Backup vortäuschen if ($dbError !== null) { if ($sqliteSnapshot) @unlink($sqliteSnapshot); return ['ok' => false, 'error' => $dbError]; } $manifest = json_encode([ 'created' => date('Y-m-d H:i:s'), 'driver' => $driver, 'version' => defined('DESKHUB_VERSION') ? DESKHUB_VERSION : null, 'database' => $sqliteSnapshot ? 'database_backup.sqlite' : $sqlFile, 'tables' => $tableCount, 'files' => count($files), ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); // ZIP erstellen – ZipArchive wenn verfügbar, sonst eigene Implementierung try { if (class_exists('ZipArchive')) { $zip = new ZipArchive(); if ($zip->open($backupPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { return ['ok' => false, 'error' => 'Backup-ZIP konnte nicht erstellt werden.']; } foreach ($files as $filePath => $relativePath) { $zip->addFile($filePath, $relativePath); } if ($sqlDump && $sqlFile) $zip->addFromString($sqlFile, $sqlDump); if ($sqliteSnapshot) $zip->addFile($sqliteSnapshot, 'database_backup.sqlite'); $zip->addFromString('backup_manifest.json', $manifest); $zip->close(); } else { // Eigene ZIP-Implementierung (kein php_zip nötig) $zip = new SimpleZip($backupPath); foreach ($files as $filePath => $relativePath) { $zip->addFile($filePath, $relativePath); } if ($sqlDump && $sqlFile) $zip->addFromString($sqlFile, $sqlDump); if ($sqliteSnapshot) $zip->addFromString('database_backup.sqlite', (string) file_get_contents($sqliteSnapshot)); $zip->addFromString('backup_manifest.json', $manifest); $zip->close(); } } catch (Exception $e) { if ($sqliteSnapshot) @unlink($sqliteSnapshot); return ['ok' => false, 'error' => 'Backup fehlgeschlagen: ' . $e->getMessage()]; } if ($sqliteSnapshot) @unlink($sqliteSnapshot); if (!file_exists($backupPath)) { return ['ok' => false, 'error' => 'Backup-Datei wurde nicht erstellt.']; } return [ 'ok' => true, 'filename' => $backupName, 'path' => $backupPath, 'files' => count($files), 'size' => round(filesize($backupPath) / 1024 / 1024, 2), ]; }