diff --git a/oracle/standby-server-scripts/rman_restore_from_zero.ps1 b/oracle/standby-server-scripts/rman_restore_from_zero.ps1 index 2786aad..f8b6579 100644 --- a/oracle/standby-server-scripts/rman_restore_from_zero.ps1 +++ b/oracle/standby-server-scripts/rman_restore_from_zero.ps1 @@ -178,28 +178,66 @@ Write-Host "[2.3] Preparing RMAN restore..." $rmanScript = "D:\oracle\temp\restore_from_zero.rman" $logFile = "D:\oracle\logs\restore_from_zero.log" -# Copy ALL backups from F:\ to recovery area +# Copy backups from F:\ to recovery area (mode-dependent) New-Item -ItemType Directory -Path "C:\Users\oracle\recovery_area\ROA\autobackup" -Force | Out-Null -Write-Host "[INFO] Copying all backups from F:\ROA\autobackup to recovery area..." -Write-Host " This may take 1-2 minutes for ~10 GB of backups..." -# Check backup files exist on F: drive before copying +if ($TestMode) { + Write-Host "[INFO] Copying selected backups from F:\ROA\autobackup to recovery area..." + Write-Host " TEST MODE: Only latest backup set (faster for weekly tests)" +} else { + Write-Host "[INFO] Copying all backups from F:\ROA\autobackup to recovery area..." + Write-Host " STANDALONE MODE: All backups for maximum DR safety" +} + +# Select backup files based on mode (TestMode vs Standalone) try { - $backupFiles = Get-ChildItem "F:\ROA\autobackup\*.BKP" -ErrorAction Continue + if ($TestMode) { + # TEST MODE: Copy only latest full backup + everything after it (optimal for weekly testing) + Write-Host "[INFO] TEST MODE: Selecting latest full backup + incrementals for faster restore..." -ForegroundColor Cyan + + # Find latest full backup (Level 0 - DAILY_FULL_COMPRESSED) + $fullBackups = Get-ChildItem "F:\ROA\autobackup\*DAILY_FULL*.BKP" -ErrorAction Continue | + Sort-Object LastWriteTime -Descending + + if ($fullBackups.Count -eq 0) { + Write-Host "ERROR: No full backup found on F: drive!" -ForegroundColor Red + Write-Host " Cannot proceed with restore - need at least one full backup" + exit 1 + } + + $latestFull = $fullBackups[0] + $cutoffTime = $latestFull.LastWriteTime + + # Get all backups >= latest full backup timestamp (includes incrementals, controlfiles, SPFILE backups) + $backupFiles = Get-ChildItem "F:\ROA\autobackup\*.BKP" -ErrorAction Continue | + Where-Object { $_.LastWriteTime -ge $cutoffTime } + + Write-Host "[INFO] Latest full backup: $($latestFull.Name)" -ForegroundColor Cyan + Write-Host " Timestamp: $($latestFull.LastWriteTime)" -ForegroundColor Cyan + Write-Host " Selected $($backupFiles.Count) files from latest backup set (saves ~60-70% copy time)" -ForegroundColor Cyan + + } else { + # STANDALONE MODE: Copy ALL backups (disaster recovery - maximum safety with fallback) + Write-Host "[INFO] STANDALONE MODE: Copying ALL backups for maximum DR safety..." -ForegroundColor Yellow + $backupFiles = Get-ChildItem "F:\ROA\autobackup\*.BKP" -ErrorAction Continue + Write-Host "[INFO] Full DR restore - will copy all available backups (includes redundancy)" -ForegroundColor Yellow + } } catch { Write-Host "WARNING: Cannot enumerate backup files on F: drive - $_" -ForegroundColor Yellow $backupFiles = @() } -if ($backupFiles.Count -lt 2) { +# Validate backup count +$minRequired = 2 +if ($backupFiles.Count -lt $minRequired) { Write-Host "ERROR: Insufficient backup files found on F: drive (found: $($backupFiles.Count))" -ForegroundColor Red - Write-Host " At least 2 backup files required for successful restore" + Write-Host " At least $minRequired backup files required for successful restore" Write-Host " Checking F:\ROA\autobackup directory..." try { $dirCheck = Get-ChildItem "F:\ROA\autobackup" -ErrorAction Continue Write-Host " Directory contents: $($dirCheck.Count) files" foreach ($file in $dirCheck) { - Write-Host " $($file.Name) - $($file.Length / 1GB) GB" -ForegroundColor Gray + Write-Host " $($file.Name) - $([math]::Round($file.Length / 1GB, 2)) GB" -ForegroundColor Gray } } catch { Write-Host " Cannot access directory: $_" -ForegroundColor Red