Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/backup.php

<?php
session_start();
require_once '../includes/functions.php';

// Check if user is logged in and has appropriate role
if (!isLoggedIn()) {
    header('Location: ../login.php');
    exit();
}

$user = getCurrentUser();
if (!hasRole(['superuser'])) {
    header('Location: index.php');
    exit();
}

// Handle backup/restore actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'create_backup') {
        $backup_name = 'backup_' . date('Y-m-d_H-i-s') . '.sql';
        $backup_path = '../database/backups/' . $backup_name;
        
        // Create backups directory if it doesn't exist
        if (!file_exists('../database/backups/')) {
            mkdir('../database/backups/', 0755, true);
        }
        
        // Database configuration - get from class instance
        $db = new CopMadinaDB();
        $host = 'localhost';
        $username = 'root';
        $password = '';
        $database = 'copmadinaconf';
        
        // Create mysqldump command with proper escaping
        $backup_path_full = realpath('../database/backups/') . DIRECTORY_SEPARATOR . $backup_name;
        
        // Try different mysqldump paths
        $mysqldump_paths = [
            "C:\\xampp\\mysql\\bin\\mysqldump.exe",
            "mysqldump"
        ];
        
        $mysqldump_cmd = null;
        foreach ($mysqldump_paths as $path) {
            if (file_exists($path) || $path === 'mysqldump') {
                $mysqldump_cmd = $path;
                break;
            }
        }
        
        if ($password === '') {
            $command = "\"$mysqldump_cmd\" --host=$host --user=$username --single-transaction --routines --triggers $database > \"$backup_path_full\"";
        } else {
            $command = "\"$mysqldump_cmd\" --host=$host --user=$username --password=$password --single-transaction --routines --triggers $database > \"$backup_path_full\"";
        }
        
        // Execute backup
        $result = exec($command, $output, $return_code);
        
        // Debug information
        error_log("Backup command: $command");
        error_log("Return code: $return_code");
        error_log("Output: " . implode("\n", $output));
        
        if ($return_code === 0 && file_exists($backup_path_full) && filesize($backup_path_full) > 0) {
            logAudit('backup', 'database', $backup_name);
            addNotification('success', "Database backup created successfully: $backup_name (" . round(filesize($backup_path_full)/1024, 2) . " KB)");
        } else {
            $error_msg = "Failed to create database backup. Return code: $return_code";
            if (!empty($output)) {
                $error_msg .= " Output: " . implode(", ", $output);
            }
            addNotification('error', $error_msg);
        }
        
        header('Location: backup.php');
        exit();
    } elseif ($action === 'delete_backup') {
        $backup_file = $_POST['backup_file'];
        $backup_path = '../database/backups/' . basename($backup_file);
        
        if (file_exists($backup_path) && unlink($backup_path)) {
            logAudit('delete', 'backup', $backup_file);
            addNotification('success', 'Backup file deleted successfully.');
        } else {
            addNotification('error', 'Failed to delete backup file.');
        }
        
        header('Location: backup.php');
        exit();
    }
}

// Get existing backup files
$backup_files = [];
$backup_dir = '../database/backups/';
if (is_dir($backup_dir)) {
    $files = scandir($backup_dir);
    foreach ($files as $file) {
        if (pathinfo($file, PATHINFO_EXTENSION) === 'sql') {
            $backup_files[] = [
                'name' => $file,
                'size' => filesize($backup_dir . $file),
                'date' => filemtime($backup_dir . $file)
            ];
        }
    }
    // Sort by date, newest first
    usort($backup_files, function($a, $b) {
        return $b['date'] - $a['date'];
    });
}

// Get database size
$db = new CopMadinaDB();
$conn = $db->getConnection();
$size_stmt = $conn->query("SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS db_size FROM information_schema.tables WHERE table_schema = DATABASE()");
$db_size = $size_stmt ? $size_stmt->fetch()['db_size'] : 0;

// Get table count
$table_stmt = $conn->query("SELECT COUNT(*) as table_count FROM information_schema.tables WHERE table_schema = DATABASE()");
$table_count = $table_stmt ? $table_stmt->fetch()['table_count'] : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Backup & Restore - COP Madina Conference</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gradient-to-br from-slate-50 to-blue-50 min-h-screen">
    <div class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/admin_sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden ml-72">
            <!-- Header -->
            <?php include 'includes/admin_header.php'; ?>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-8">
                <!-- Database Info Cards -->
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-emerald-100">
                                <i class="fas fa-database text-emerald-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo $db_size; ?> MB</p>
                                <p class="text-slate-600">Database Size</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-blue-100">
                                <i class="fas fa-table text-blue-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo $table_count; ?></p>
                                <p class="text-slate-600">Tables</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-purple-100">
                                <i class="fas fa-archive text-purple-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo count($backup_files); ?></p>
                                <p class="text-slate-600">Backups</p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Create Backup Section -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6 mb-8">
                    <div class="flex items-center justify-between">
                        <div>
                            <h2 class="text-xl font-bold text-slate-800">Create Database Backup</h2>
                            <p class="text-slate-600 mt-1">Generate a complete backup of your database</p>
                        </div>
                        <form method="POST" class="inline">
                            <input type="hidden" name="action" value="create_backup">
                            <button type="submit" class="px-6 py-3 bg-gradient-to-r from-emerald-600 to-green-600 hover:from-emerald-700 hover:to-green-700 text-white font-medium rounded-xl transition-all duration-200 flex items-center space-x-2">
                                <i class="fas fa-plus"></i>
                                <span>Create Backup</span>
                            </button>
                        </form>
                    </div>
                </div>

                <!-- Backup Files -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 overflow-hidden">
                    <div class="p-6 border-b border-slate-200/50">
                        <h2 class="text-xl font-bold text-slate-800">Backup Files</h2>
                        <p class="text-slate-600 mt-1">Manage your database backup files</p>
                    </div>
                    
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-slate-50">
                                <tr>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">File Name</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Size</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Created</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Actions</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200">
                                <?php if (empty($backup_files)): ?>
                                <tr>
                                    <td colspan="4" class="px-6 py-12 text-center text-slate-500">
                                        <i class="fas fa-archive text-4xl mb-4"></i>
                                        <p>No backup files found</p>
                                        <p class="text-sm mt-2">Create your first backup using the button above</p>
                                    </td>
                                </tr>
                                <?php else: ?>
                                <?php foreach ($backup_files as $backup): ?>
                                <tr class="hover:bg-slate-50">
                                    <td class="px-6 py-4">
                                        <div class="flex items-center">
                                            <i class="fas fa-file-archive text-emerald-600 mr-3"></i>
                                            <span class="text-sm font-medium text-slate-900">
                                                <?php echo htmlspecialchars($backup['name']); ?>
                                            </span>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo number_format($backup['size'] / 1024 / 1024, 2); ?> MB
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo date('M j, Y g:i A', $backup['date']); ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="flex space-x-2">
                                            <a href="../database/backups/<?php echo urlencode($backup['name']); ?>" 
                                               download
                                               class="px-3 py-2 bg-blue-100 hover:bg-blue-200 text-blue-700 font-medium rounded-lg transition-colors text-sm">
                                                <i class="fas fa-download mr-1"></i>
                                                Download
                                            </a>
                                            <form method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete this backup?')">
                                                <input type="hidden" name="action" value="delete_backup">
                                                <input type="hidden" name="backup_file" value="<?php echo htmlspecialchars($backup['name']); ?>">
                                                <button type="submit" class="px-3 py-2 bg-red-100 hover:bg-red-200 text-red-700 font-medium rounded-lg transition-colors text-sm">
                                                    <i class="fas fa-trash mr-1"></i>
                                                    Delete
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Restore Instructions -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6 mt-8">
                    <h3 class="text-lg font-bold text-slate-800 mb-4">
                        <i class="fas fa-info-circle text-blue-600 mr-2"></i>
                        Restore Instructions
                    </h3>
                    <div class="bg-blue-50 rounded-xl p-4">
                        <p class="text-slate-700 mb-3">To restore a database backup:</p>
                        <ol class="list-decimal list-inside space-y-2 text-sm text-slate-600">
                            <li>Download the backup file you want to restore</li>
                            <li>Access your database management tool (phpMyAdmin, MySQL Workbench, etc.)</li>
                            <li>Drop the existing database or create a new one</li>
                            <li>Import the downloaded SQL file</li>
                            <li>Update your database configuration if necessary</li>
                        </ol>
                        <div class="mt-4 p-3 bg-yellow-100 rounded-lg">
                            <p class="text-yellow-800 text-sm">
                                <i class="fas fa-exclamation-triangle mr-2"></i>
                                <strong>Warning:</strong> Always test database restores in a development environment first. 
                                Restoring a backup will overwrite all current data.
                            </p>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists