_YYYYMMDD_HHMMSS.hosts * * @param string $zoneDir Base zone directory * @param string $zone Zone name (example.com) * @return string|null Path to backup file or null on failure */ function backup_zonefile(string $zoneDir, string $zone): ?string { $backupDir = $zoneDir . '/backups'; // Ensure backup directory exists if (!is_dir($backupDir)) { if (!mkdir($backupDir, 0755, true) && !is_dir($backupDir)) { return null; } } $timestamp = date('Ymd_His'); $src = "$zoneDir/$zone.hosts"; $dst = "$backupDir/{$zone}_{$timestamp}.hosts"; // Source must exist if (!file_exists($src)) { return null; } // Copy backup if (!copy($src, $dst)) { return null; } return $dst; } /** * Get the latest backup file for a zone. * * @param string $zoneDir * @param string $zone * @return string|null */ function get_latest_backup(string $zoneDir, string $zone): ?string { $backupDir = $zoneDir . '/backups'; $pattern = "{$backupDir}/{$zone}_*.hosts"; $files = glob($pattern); if (!$files) { return null; } sort($files); return end($files); }