<?php
/**
 * Database Backup Script
 * Run via cron: 0 2 * * * php /var/www/nis-ams/scripts/backup.php
 */

require_once __DIR__ . '/../config/init.php';

class DatabaseBackup {
    
    private $backupDir;
    private $dbHost;
    private $dbName;
    private $dbUser;
    private $dbPass;
    private $retentionDays = 30;
    
    public function __construct() {
        $this->backupDir = Config::get('upload_path') . '../backups/';
        $this->dbHost = Config::get('db_host');
        $this->dbName = Config::get('db_name');
        $this->dbUser = Config::get('db_user');
        $this->dbPass = Config::get('db_pass');
        
        if (!is_dir($this->backupDir)) {
            mkdir($this->backupDir, 0755, true);
        }
    }
    
    public function run() {
        $this->log("Starting database backup...");
        
        $filename = $this->dbName . '_' . date('Y-m-d_H-i-s') . '.sql.gz';
        $filepath = $this->backupDir . $filename;
        
        // Create backup
        $command = sprintf(
            'mysqldump -h %s -u %s %s | gzip > %s',
            escapeshellarg($this->dbHost),
            escapeshellarg($this->dbUser),
            escapeshellarg($this->dbName),
            escapeshellarg($filepath)
        );
        
        if (!empty($this->dbPass)) {
            $command = 'MYSQL_PWD=' . escapeshellarg($this->dbPass) . ' ' . $command;
        }
        
        exec($command . ' 2>&1', $output, $returnCode);
        
        if ($returnCode === 0) {
            $this->log("Backup created successfully: $filename");
            $this->cleanOldBackups();
        } else {
            $this->log("Backup failed: " . implode("\n", $output), 'ERROR');
        }
    }
    
    private function cleanOldBackups() {
        $files = glob($this->backupDir . $this->dbName . '_*.sql.gz');
        $now = time();
        
        foreach ($files as $file) {
            if (is_file($file)) {
                $fileTime = filemtime($file);
                $ageDays = ($now - $fileTime) / (60 * 60 * 24);
                
                if ($ageDays > $this->retentionDays) {
                    unlink($file);
                    $this->log("Deleted old backup: " . basename($file));
                }
            }
        }
    }
    
    private function log($message, $level = 'INFO') {
        $timestamp = date('Y-m-d H:i:s');
        $logMessage = "[$timestamp] [$level] $message\n";
        
        echo $logMessage;
        
        $logFile = Config::get('upload_path') . '../logs/backup.log';
        file_put_contents($logFile, $logMessage, FILE_APPEND);
    }
}

// Run backup
$backup = new DatabaseBackup();
$backup->run();