<?php
declare(strict_types=1);
require __DIR__.'/app/bootstrap.php';require_permission('system.settings.manage');
set_time_limit(0);$pdo=db();$name='rakaez-backup-'.date('Ymd-His').'.sql';header('Content-Type: application/sql; charset=utf-8');header('Content-Disposition: attachment; filename="'.$name.'"');header('X-Content-Type-Options: nosniff');
echo "SET NAMES utf8mb4;\nSET FOREIGN_KEY_CHECKS=0;\n\n";
$tables=$pdo->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_TYPE='BASE TABLE' ORDER BY TABLE_NAME")->fetchAll(PDO::FETCH_COLUMN);
foreach($tables as $table){$safe=str_replace('`','``',(string)$table);$create=$pdo->query("SHOW CREATE TABLE `{$safe}`")->fetch(PDO::FETCH_NUM);echo "DROP TABLE IF EXISTS `{$safe}`;\n".$create[1].";\n";$rows=$pdo->query("SELECT * FROM `{$safe}`");while($row=$rows->fetch(PDO::FETCH_ASSOC)){$cols=array_map(static fn($c)=>'`'.str_replace('`','``',(string)$c).'`',array_keys($row));$vals=array_map(static fn($v)=>$v===null?'NULL':db()->quote((string)$v),array_values($row));echo "INSERT INTO `{$safe}` (".implode(',',$cols).") VALUES (".implode(',',$vals).");\n";}echo "\n";}
echo "SET FOREIGN_KEY_CHECKS=1;\n";audit('database.backup','system',null,['file'=>$name]);exit;
