shubraVeil/test_backup.php
2024-12-25 13:05:50 +02:00

75 lines
2.3 KiB
PHP

<?php
require_once __DIR__ . '/includes/config.php';
echo "=== اختبار نظام النسخ الاحتياطي ===\n\n";
class DatabaseBackup {
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
private $backup_dir;
public function __construct() {
$this->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";
}