db_host = DB_SERVER; $this->db_user = DB_USERNAME; $this->db_pass = DB_PASSWORD; $this->db_name = DB_NAME; $this->backup_dir = __DIR__ . '/backups'; if (!is_dir($this->backup_dir)) { mkdir($this->backup_dir, 0755, true); } } public function createBackup() { $date = date('Y-m-d_H-i-s'); $backup_file = $this->backup_dir . "/backup_" . $date . ".sql"; $command = sprintf( 'mysqldump --host=%s --user=%s --password=%s %s > %s', escapeshellarg($this->db_host), escapeshellarg($this->db_user), escapeshellarg($this->db_pass), escapeshellarg($this->db_name), escapeshellarg($backup_file) ); system($command, $return_var); if ($return_var === 0) { echo "تم إنشاء النسخة الاحتياطية بنجاح: " . basename($backup_file) . "\n"; return $backup_file; } else { throw new Exception("فشل إنشاء النسخة الاحتياطية"); } } public function listBackups() { $backups = glob($this->backup_dir . "/*.sql"); echo "\nالنسخ الاحتياطية المتوفرة:\n"; foreach ($backups as $backup) { echo "- " . basename($backup) . " (" . $this->formatSize(filesize($backup)) . ")\n"; } } private function formatSize($size) { $units = ['B', 'KB', 'MB', 'GB']; $power = $size > 0 ? floor(log($size, 1024)) : 0; return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power]; } } try { $backup = new DatabaseBackup(); // إنشاء نسخة احتياطية $backup_file = $backup->createBackup(); // عرض قائمة النسخ الاحتياطية $backup->listBackups(); } catch (Exception $e) { echo "خطأ: " . $e->getMessage() . "\n"; }