73 lines
2.7 KiB
PHP
73 lines
2.7 KiB
PHP
<?php
|
|
require_once '../includes/db.php';
|
|
require_once '../includes/functions.php';
|
|
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', 'backup_debug.log');
|
|
|
|
set_time_limit(300); // 5 minutes
|
|
ini_set('memory_limit', '512M');
|
|
|
|
requireAdmin();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate_backup'])) {
|
|
$date = date("Y-m-d_H-i");
|
|
$backupDir = "backups/";
|
|
if (!is_dir($backupDir)) {
|
|
if (!mkdir($backupDir, 0755, true)) {
|
|
die("Could not create backup directory. Check permissions.");
|
|
}
|
|
}
|
|
|
|
if (!is_writable($backupDir)) {
|
|
die("Backup directory is not writable. Check permissions.");
|
|
}
|
|
|
|
// 1. Backup Audio Files
|
|
$audioTar = $backupDir . "backup_audio_$date.tar.gz";
|
|
$audioPath = realpath("../assets/uploads/audio");
|
|
shell_exec("tar -czf " . escapeshellarg($audioTar) . " -C " . escapeshellarg($audioPath) . " .");
|
|
|
|
// 2. Backup Database (PHP-based SQL Dump)
|
|
$dbSql = $backupDir . "backup_db_$date.sql";
|
|
$tables = ['episodes', 'plays', 'settings', 'admins', 'subscriptions'];
|
|
$sqlDump = "-- Database Backup\n-- Date: " . date("Y-m-d H:i:s") . "\n";
|
|
$sqlDump .= "SET FOREIGN_KEY_CHECKS = 0;\n\n";
|
|
|
|
foreach ($tables as $table) {
|
|
$stmt = $pdo->query("SELECT * FROM $table");
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$sqlDump .= "TRUNCATE TABLE `$table`;\n";
|
|
foreach ($rows as $row) {
|
|
$keys = array_keys($row);
|
|
$values = array_map(function($v) use ($pdo) {
|
|
return ($v === null) ? "NULL" : $pdo->quote($v);
|
|
}, array_values($row));
|
|
$sqlDump .= "INSERT INTO `$table` (`" . implode("`, `", $keys) . "`) VALUES (" . implode(", ", $values) . ");\n";
|
|
}
|
|
$sqlDump .= "\n";
|
|
}
|
|
|
|
file_put_contents($dbSql, $sqlDump . "\nSET FOREIGN_KEY_CHECKS = 1;");
|
|
|
|
// Tar the SQL
|
|
$dbTar = $backupDir . "backup_db_$date.tar.gz";
|
|
shell_exec("tar -czf " . escapeshellarg($dbTar) . " -C " . escapeshellarg($backupDir) . " " . escapeshellarg(basename($dbSql)));
|
|
unlink($dbSql);
|
|
|
|
// 3. Backup Site Files (Excluding uploads and backups)
|
|
$siteTar = $backupDir . "backup_site_$date.tar.gz";
|
|
$rootPath = realpath("../");
|
|
$exclude = " --exclude='./assets/uploads' --exclude='./admin/backups' --exclude='./.git'";
|
|
shell_exec("tar -czf " . escapeshellarg($siteTar) . " -C " . escapeshellarg($rootPath) . $exclude . " .");
|
|
|
|
logActivity($_SESSION['admin_id'], 'BACKUP_GENERATE', "Generated full system backup: Audio, DB, and Site files.");
|
|
header("Location: system.php?backup=success");
|
|
exit;
|
|
}
|
|
?>
|