<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } $aid = $_SESSION['admin_id'] ?? 0; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_backup_action'])) { while (ob_get_level()) { ob_end_clean(); } header('Content-Type: application/json'); CSRF::enforce(); $act = $_POST['_backup_action']; if ($act === 'list') { $backups = []; $backupDir = '/tmp/dashboard_backup_*'; $dirs = glob($backupDir); foreach ($dirs as $d) { $name = basename($d); $files = glob($d . '/*'); $totalSize = 0; foreach ($files as $f) { $totalSize += filesize($f); } $backups[] = ['name' => $name, 'date' => date('Y-m-d H:i', filemtime($d)), 'files' => count($files), 'size' => round($totalSize / 1024 / 1024, 2) . ' MB']; } exit(json_encode(['ok' => true, 'backups' => $backups])); } if ($act === 'create') { $ts = date('Ymd_His'); $dir = "/tmp/dashboard_backup_{$ts}"; @mkdir($dir, 0750, true); exec("sudo mariadb-dump --single-transaction store > {$dir}/store.sql 2>&1", $out1, $r1); exec("sudo mariadb-dump --single-transaction ghost_app > {$dir}/ghost_app.sql 2>&1", $out2, $r2); exec("cd /var/www && sudo tar czf {$dir}/dashboard.tar.gz dashboard/ 2>&1", $out3, $r3); exec("cd /var/www && sudo tar czf {$dir}/public.tar.gz public/ 2>&1", $out4, $r4); exec("cd /var/www && sudo tar czf {$dir}/private.tar.gz private/ 2>&1", $out5, $r5); $ok = ($r1 === 0 && $r2 === 0 && $r3 === 0 && $r4 === 0 && $r5 === 0); Events::record(Events::CAT_MODIFY, 'backup.created', 'system', 0, ['dir' => $dir, 'ok' => $ok], $aid); exit(json_encode(['ok' => $ok, 'dir' => $dir])); } exit(json_encode(['ok' => false, 'error' => 'Unknown action'])); } 