Restoredaybackup Documentation

Restore Day Backup Controller Documentation

File: /controllers/restoredaybackup.php

Purpose: Restores database from SQL backup files with execution tracking and error reporting

Last Updated: December 21, 2024

Total Functions: 1

Lines of Code: ~129

---

๐Ÿ“‹ Overview

The Restore Day Backup Controller is a specialized database management utility that handles the restoration of MySQL databases from SQL backup files. It provides comprehensive SQL execution tracking, error reporting, and transaction management for database recovery operations. The controller handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

No Direct Table Operations

This controller operates at the SQL execution level and doesn't directly manipulate specific tables. Instead, it:

Connection Requirements

ComponentPurposeDetails
**MySQL Connection**Direct database accessUses ConnectionProperty class for credentials
**UTF-8 Support**Character encodingSets proper charset for international data
**Transaction Support**Data consistencyHandles BEGIN/COMMIT/ROLLBACK commands
---

๐Ÿ”‘ Key Functions

1. Default Action - Upload Interface

Location: Line 13

Purpose: Display the backup file upload interface

Process Flow:

1. Display file upload form via getfile.html template

2. Allow users to select SQL backup files

3. Provide interface for restoration process

Template: restoredaybackupview/getfile.html

---

2. runsql() - Main Backup Restoration Function

Location: Line 41

Purpose: Execute SQL commands from uploaded backup file with comprehensive tracking

Function Signature:

function runsql()

Process Flow:

1. File Upload Validation:

- Check file upload errors

- Verify file was properly uploaded

- Validate file accessibility

2. File Processing:

- Read complete file contents

- Clean SQL content (remove comments and empty lines)

- Split into individual SQL commands

3. Database Connection:

- Establish MySQL connection using ConnectionProperty

- Set UTF-8 character encoding

- Initialize connection for SQL execution

4. SQL Execution Loop:

- Process each SQL command individually

- Handle transaction commands specially

- Track success/failure for each command

- Provide detailed output for monitoring

5. Results Reporting:

- Return success and error counts

- Display detailed execution log

- Show final statistics

File Validation Logic:

if ($_FILES['pfile']['error'] == UPLOAD_ERR_OK && 
    is_uploaded_file($_FILES['pfile']['tmp_name'])) {
    $handle = fopen($_FILES['pfile']['tmp_name'], 'rb');
    if ($handle) {
        $commands = file_get_contents($_FILES['pfile']['tmp_name']);
        fclose($handle);
    }
}

Connection Setup:

$con = mysqli_connect(
    ConnectionProperty::getHost(), 
    ConnectionProperty::getUser(), 
    ConnectionProperty::getPassword(), 
    ConnectionProperty::getDatabase()
);

// Set UTF-8 encoding
mysqli_query("SET NAMES 'utf8'");
mysqli_query('SET CHARACTER_SET utf8');

SQL Cleaning Process:

// Remove comments and empty lines
$lines = explode("\n", $commands);
$commands = '';
foreach ($lines as $line) {
    $line = trim($line);
    if ($line && !strpos($line, '--')) {  // Skip comments and empty lines
        $commands .= $line . "\n";
    }
}

Command Execution Logic:

$commands = explode(";", $commands);
foreach ($commands as $command) {
    $command = trim($command);
    if ($command) {
        if ($command == "BEGIN;") {
            $con->begin_transaction();
        } else if ($command == "COMMIT;") {
            $con->commit();
        } else if ($command == "ROLLBACK;") {
            $con->rollback();
        } else {
            $res = $con->query($command);
            if ($res === TRUE) {
                echo "SUCCESS: $command<br/>";
                $success++;
            } else {
                echo "ERROR: $command<br/>Error: " . $con->error . "<br/>";
                $errors++;
            }
        }
    }
}

Return Value:

return array($success, $errors);

---

๐Ÿ”„ Workflows

Workflow 1: Database Restoration Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Backup File Upload
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1File Upload Validation
- Check $_FILES['pfile']['error'] == UPLOAD_ERR_OK
- Verify is_uploaded_file() returns true
- Ensure file handle can be opened
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Read and Process File Contents
- Read complete file via file_get_contents()
- Split content into lines
- Remove comments (lines starting with '--')
- Remove empty lines and whitespace
- Rebuild clean SQL content
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Establish Database Connection
- Connect using ConnectionProperty credentials
- Check connection success
- Set UTF-8 character encoding:
โ”œโ”€ SET NAMES 'utf8'
โ”‚ โ””โ”€ SET CHARACTER_SET utf8 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Split Commands and Execute
- Split SQL content by semicolons
- Initialize success/error counters
โ”‚
FOR EACH SQL command:
โ”‚
โ†’ Trim whitespace
โ”‚
โ†’ Check for transaction commands:
โ”‚ โ”œโ”€ BEGIN: Start transaction
โ”‚ โ”œโ”€ COMMIT: Commit transaction
โ”‚ โ”‚ โ””โ”€ ROLLBACK: Rollback transaction โ”‚
โ”‚
โ†’ Execute regular SQL command
โ”‚
โ†’ Handle Results:
โ”‚ โ”œโ”€ SUCCESS: Increment success counter
โ”‚
โ”‚ โ”‚ โ””โ”€ ERROR: Increment error counter โ”‚
โ”‚ Show command and error message
โ”‚
โ”‚ โ””โ”€โ†’ Display real-time progress โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Display Final Results
- Show total successful commands executed
- Show total commands that failed
- Display "DONE" or "Error" status
- Return statistics array
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionDisplay file upload interface
`do=runsql``runsql()`Execute SQL from uploaded backup file
### Required Parameters by Action

File Upload Interface (do= empty):

SQL Execution (do=runsql):

File Upload Requirements

---

๐Ÿงฎ Processing Methods

File Content Cleaning

// Split into lines for processing
$lines = explode("\n", $commands);
$commands = '';

// Clean each line
foreach ($lines as $line) {
    $line = trim($line);
    if ($line && !strpos($line, '--')) {  // Skip comments and empty lines
        $commands .= $line . "\n";
    }
}

SQL Command Splitting

// Split by semicolon to get individual commands
$commands = explode(";", $commands);

// Process each command
foreach ($commands as $command) {
    $command = trim($command);
    if ($command) {  // Skip empty commands
        // Execute command
    }
}

Transaction Command Handling

if ($command == "BEGIN;") {
    $con->begin_transaction();
} else if ($command == "COMMIT;") {
    $con->commit();
} else if ($command == "ROLLBACK;") {
    $con->rollback();
} else {
    // Regular SQL command execution
    $res = $con->query($command);
}

Statistics Tracking

$success = 0;  // Count of successful commands
$errors = 0;   // Count of failed commands

// For each command execution
if ($res === TRUE) {
    $success++;
    echo "SUCCESS: " . mysqli_insert_id($con);
} else {
    $errors++;
    echo "ERROR: " . $con->error;
}

return array($success, $errors);

---

๐Ÿ”’ Security & Permissions

File Upload Security

Current Implementation:

Security Risks:

Recommended Improvements:

// Add file type validation
$allowedTypes = ['application/sql', 'text/plain'];
$fileType = $_FILES['pfile']['type'];
if (!in_array($fileType, $allowedTypes)) {
    throw new Exception("Invalid file type");
}

// Add file size validation
$maxSize = 10 * 1024 * 1024; // 10MB
if ($_FILES['pfile']['size'] > $maxSize) {
    throw new Exception("File too large");
}

// Add SQL content validation
$dangerousCommands = ['DROP DATABASE', 'DROP TABLE', 'TRUNCATE'];
foreach ($dangerousCommands as $dangerous) {
    if (stripos($commands, $dangerous) !== false) {
        throw new Exception("Dangerous command detected: $dangerous");
    }
}

Authentication & Authorization

Current State:

Critical Security Issues:

Required Improvements:

// Add authentication check
include_once("../public/authentication.php");

// Add permission check
if (!$_SESSION['usergroup']->canRestoreDatabase) {
    throw new Exception("Insufficient permissions");
}

// Add operation logging
$logEntry = "Database restoration by user " . $_SESSION['userid'] . 
           " at " . date('Y-m-d H:i:s');
error_log($logEntry, 3, "/path/to/restore.log");

---

๐Ÿ“Š Performance Considerations

File Processing Performance

1. Large File Handling:

- Current implementation loads entire file into memory

- May fail with very large backup files

- No streaming or chunked processing

2. Memory Usage:

- File contents loaded completely into PHP memory

- SQL commands split and stored in arrays

- Can cause memory exhaustion with large backups

Optimization Strategies:

// Stream large files instead of loading completely
function processLargeFile($filename) {
    $handle = fopen($filename, 'r');
    $buffer = '';
    
    while (!feof($handle)) {
        $line = fgets($handle);
        $buffer .= $line;
        
        // Process complete commands as found
        if (substr(trim($line), -1) === ';') {
            executeCommand($buffer);
            $buffer = '';
        }
    }
    fclose($handle);
}

Database Performance

1. Transaction Management:

- Supports explicit transaction control

- Can batch operations for better performance

- May hold locks for extended periods

2. Connection Efficiency:

- Uses single connection for all commands

- No connection pooling or optimization

- Character set configured properly

Performance Improvements:

// Add transaction batching
$batchSize = 100;
$commandCount = 0;

$con->begin_transaction();
foreach ($commands as $command) {
    $con->query($command);
    $commandCount++;
    
    if ($commandCount % $batchSize === 0) {
        $con->commit();
        $con->begin_transaction();
    }
}
$con->commit(); // Final batch

---

๐Ÿ› Common Issues & Troubleshooting

1. File Upload Failures

Issue: Files not uploading or processing

Cause: PHP upload settings, file permissions, or file size limits

Debug Steps:

// Check upload settings
echo "upload_max_filesize: " . ini_get('upload_max_filesize') . "<br>";
echo "post_max_size: " . ini_get('post_max_size') . "<br>";
echo "max_file_uploads: " . ini_get('max_file_uploads') . "<br>";

// Check file upload errors
switch ($_FILES['pfile']['error']) {
    case UPLOAD_ERR_OK:
        echo "File uploaded successfully";
        break;
    case UPLOAD_ERR_INI_SIZE:
        echo "File exceeds upload_max_filesize";
        break;
    case UPLOAD_ERR_FORM_SIZE:
        echo "File exceeds MAX_FILE_SIZE";
        break;
    case UPLOAD_ERR_PARTIAL:
        echo "File was only partially uploaded";
        break;
    default:
        echo "Upload error code: " . $_FILES['pfile']['error'];
}

2. SQL Execution Errors

Issue: SQL commands failing during restoration

Cause: Syntax errors, missing dependencies, or permission issues

Common Error Types:

-- Missing database selection
ERROR 1046 (3D000): No database selected

-- Table already exists
ERROR 1050 (42S01): Table 'tablename' already exists

-- Foreign key constraint fails
ERROR 1452 (23000): Cannot add or update a child row: foreign key constraint fails

Troubleshooting:

// Add detailed error reporting
if ($res === FALSE) {
    $errorInfo = array(
        'command' => $command,
        'error_code' => $con->errno,
        'error_message' => $con->error,
        'state' => $con->sqlstate
    );
    error_log("SQL Error: " . json_encode($errorInfo));
}

3. Character Encoding Issues

Issue: Foreign characters not displaying correctly

Cause: Character set mismatches or encoding problems

Solutions:

// Ensure proper encoding throughout
$con->set_charset("utf8mb4");
mysqli_query($con, "SET NAMES 'utf8mb4'");
mysqli_query($con, "SET CHARACTER_SET utf8mb4");
mysqli_query($con, "SET CHARACTER_SET_CONNECTION=utf8mb4");
mysqli_query($con, "SET CHARACTER_SET_DATABASE=utf8mb4");
mysqli_query($con, "SET CHARACTER_SET_RESULTS=utf8mb4");

4. Memory Exhaustion

Issue: PHP runs out of memory processing large files

Cause: Loading large backup files entirely into memory

Solutions:

// Increase memory limit temporarily
ini_set('memory_limit', '512M');

// Or implement streaming approach
function streamExecuteSQL($filename) {
    $handle = fopen($filename, 'r');
    $command = '';
    
    while (($line = fgets($handle)) !== false) {
        $command .= $line;
        if (substr(trim($line), -1) === ';') {
            // Execute complete command
            executeCommand($command);
            $command = '';
        }
    }
    fclose($handle);
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic SQL File Restoration

1. Create simple SQL file with CREATE TABLE and INSERT statements
2. Upload via interface
3. Verify all commands execute successfully
4. Check data was properly restored
5. Confirm success/error counts are accurate

Test Case 2: Transaction Handling

1. Create SQL file with explicit BEGIN/COMMIT statements
2. Include some valid and some invalid commands
3. Verify transaction commands are handled separately
4. Check that rollbacks work when errors occur

Test Case 3: Large File Processing

1. Create large SQL backup file (>10MB)
2. Upload and monitor memory usage
3. Verify all commands process correctly
4. Check performance timing
5. Ensure no memory exhaustion occurs

Test Case 4: Error Handling

1. Create SQL file with intentional syntax errors
2. Upload and process file
3. Verify errors are caught and reported
4. Check that execution continues after errors
5. Confirm final statistics are accurate

Test Case 5: Character Encoding

1. Create SQL file with international characters
2. Include various Unicode characters
3. Process file and verify proper encoding
4. Check that characters display correctly
5. Validate database storage is correct

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur