From 2f8c927bbe9f1a267ae0b371e32b268681da4c32 Mon Sep 17 00:00:00 2001 From: Marius Date: Sat, 11 Oct 2025 01:26:35 +0300 Subject: [PATCH] Oracle DR: Convert restore scripts to PowerShell for SSH compatibility - Add cleanup_database.ps1: PowerShell version without input redirection issues - Add rman_restore_from_zero.ps1: PowerShell version with inline SQL commands - Update weekly-dr-test-proxmox.sh: Call .ps1 scripts via PowerShell PowerShell scripts resolve SSH 'Input redirection not supported' errors All SQL commands are piped directly to sqlplus (no temp files needed) Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- .../cleanup_database.ps1 | 131 ++++++++++ .../rman_restore_from_zero.ps1 | 239 ++++++++++++++++++ .../weekly-dr-test-proxmox.sh | 2 +- 3 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 oracle/standby-server-scripts/cleanup_database.ps1 create mode 100644 oracle/standby-server-scripts/rman_restore_from_zero.ps1 diff --git a/oracle/standby-server-scripts/cleanup_database.ps1 b/oracle/standby-server-scripts/cleanup_database.ps1 new file mode 100644 index 0000000..d29592f --- /dev/null +++ b/oracle/standby-server-scripts/cleanup_database.ps1 @@ -0,0 +1,131 @@ +# Oracle Database Complete Cleanup Script (PowerShell) +# Purpose: Remove all database files and services to restore DR VM to clean state +# Run as: Administrator +# Location: D:\oracle\scripts\cleanup_database.ps1 + +$ErrorActionPreference = "Continue" + +$env:ORACLE_HOME = "C:\Users\Administrator\Downloads\WINDOWS.X64_193000_db_home" +$env:ORACLE_SID = "ROA" +$env:PATH = "$env:ORACLE_HOME\bin;$env:PATH" + +Write-Host "============================================" +Write-Host "Oracle Database Cleanup Script" +Write-Host "============================================" +Write-Host "" +Write-Host "This script will:" +Write-Host " 1. Stop and delete Oracle service" +Write-Host " 2. Delete all database files (datafiles, control files, redo logs)" +Write-Host " 3. Delete local FRA (backups are on F:\, safe to delete)" +Write-Host " 4. Delete trace files" +Write-Host " 5. Leave VM in completely clean state (no service, no DB files)" +Write-Host "" + +# Check if running in non-interactive mode +$silent = $args -contains "/SILENT" -or $args -contains "/AUTO" + +if (-not $silent) { + Write-Host "WARNING: This will DELETE the entire database!" -ForegroundColor Red + Write-Host "Starting cleanup in 3 seconds... (Press Ctrl+C to cancel)" + Start-Sleep -Seconds 3 +} + +# Create directories +New-Item -ItemType Directory -Path "D:\oracle\temp" -Force | Out-Null +New-Item -ItemType Directory -Path "D:\oracle\logs" -Force | Out-Null + +Write-Host "" +Write-Host "[1/6] Shutting down database (if running)..." + +# Check if Oracle service exists +$service = Get-Service -Name "OracleServiceROA" -ErrorAction SilentlyContinue +if ($service) { + Write-Host " Oracle service found, attempting shutdown..." + + # Try to shutdown database using SQL*Plus with inline command + $shutdownSQL = "SHUTDOWN ABORT;`nEXIT;" + try { + $shutdownSQL | & sqlplus -S / as sysdba 2>&1 | Out-Null + Start-Sleep -Seconds 2 + } catch { + Write-Host " Shutdown command sent (errors ignored)" + } + + # Force kill any Oracle processes + Get-Process -Name "sqlplus" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue + Get-Process -Name "oracle" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue +} else { + Write-Host " Oracle service not found, skipping shutdown" +} +Start-Sleep -Seconds 2 + +Write-Host "[2/6] Stopping and deleting Oracle service + registry + instance..." + +# Method 1: Use oradim to delete Oracle instance +$env:ORACLE_HOME = "C:\Users\Administrator\Downloads\WINDOWS.X64_193000_db_home" +$env:PATH = "$env:ORACLE_HOME\bin;$env:PATH" +& oradim -delete -sid ROA 2>&1 | Out-Null +Write-Host " Oracle instance deleted with oradim" + +# Method 2: Delete service directly (backup method) +$service = Get-Service -Name "OracleServiceROA" -ErrorAction SilentlyContinue +if ($service) { + Stop-Service -Name "OracleServiceROA" -Force -ErrorAction SilentlyContinue + Start-Sleep -Seconds 2 + & sc.exe delete OracleServiceROA 2>&1 | Out-Null + Write-Host " Oracle service deleted with sc" +} + +# Method 3: Delete Oracle registry keys +$regPath = "HKLM:\SOFTWARE\ORACLE\KEY_OraDB19Home1" +Remove-ItemProperty -Path $regPath -Name "ORA_ROA_PFILE" -ErrorAction SilentlyContinue +Remove-ItemProperty -Path $regPath -Name "ORA_ROA_AUTOSTART" -ErrorAction SilentlyContinue +Remove-ItemProperty -Path $regPath -Name "ORA_ROA_SHUTDOWN" -ErrorAction SilentlyContinue +Remove-ItemProperty -Path $regPath -Name "ORA_ROA_SHUTDOWNTYPE" -ErrorAction SilentlyContinue +Remove-ItemProperty -Path $regPath -Name "ORA_ROA_SHUTDOWN_TIMEOUT" -ErrorAction SilentlyContinue +Write-Host " Registry keys cleaned" +Start-Sleep -Seconds 2 + +Write-Host "[3/6] Deleting database files..." +Write-Host " Deleting datafiles..." +Remove-Item "C:\Users\oracle\oradata\ROA\*.dbf" -Force -ErrorAction SilentlyContinue +Write-Host " Deleting control files..." +Remove-Item "C:\Users\oracle\oradata\ROA\*.ctl" -Force -ErrorAction SilentlyContinue +Write-Host " Deleting redo logs..." +Remove-Item "C:\Users\oracle\oradata\ROA\*.log" -Force -ErrorAction SilentlyContinue + +Write-Host "[4/6] Deleting local FRA (backups are on F:\)..." +if (Test-Path "C:\Users\oracle\recovery_area\ROA") { + Remove-Item "C:\Users\oracle\recovery_area\ROA" -Recurse -Force -ErrorAction SilentlyContinue + New-Item -ItemType Directory -Path "C:\Users\oracle\recovery_area\ROA" -Force | Out-Null + Write-Host " FRA cleared" +} else { + New-Item -ItemType Directory -Path "C:\Users\oracle\recovery_area\ROA" -Force | Out-Null + Write-Host " FRA directory created" +} + +Write-Host "[5/6] Deleting trace files (to save space)..." +Remove-Item "C:\Users\oracle\diag\rdbms\roa\ROA\trace\*.trc" -Force -ErrorAction SilentlyContinue +Remove-Item "C:\Users\oracle\diag\rdbms\roa\ROA\trace\*.trm" -Force -ErrorAction SilentlyContinue +Write-Host " Trace files deleted" + +Write-Host "" +Write-Host "============================================" +Write-Host "Database Cleanup Complete!" +Write-Host "============================================" +Write-Host "" +Write-Host "Current state:" +Write-Host " [YES] Oracle software installed" +Write-Host " [YES] PFILE exists (C:\Users\oracle\admin\ROA\pfile\initROA.ora)" +Write-Host " [NO] Oracle service (will be created during restore)" +Write-Host " [NO] Database files (will be restored from backups)" +Write-Host " [NO] Control files (will be restored from backups)" +Write-Host " [NO] Datafiles (will be restored from backups)" +Write-Host "" +Write-Host "VM is now in COMPLETELY CLEAN STATE!" +Write-Host "" +Write-Host "Next step: Run D:\oracle\scripts\rman_restore_from_zero.ps1" +Write-Host " (It will create the Oracle service and restore the database)" +Write-Host "" + +exit 0 diff --git a/oracle/standby-server-scripts/rman_restore_from_zero.ps1 b/oracle/standby-server-scripts/rman_restore_from_zero.ps1 new file mode 100644 index 0000000..2f780c7 --- /dev/null +++ b/oracle/standby-server-scripts/rman_restore_from_zero.ps1 @@ -0,0 +1,239 @@ +# RMAN Restore Database FROM ZERO - Clean State (PowerShell) +# Backups are on F:\ (NFS mount from Proxmox host) +# Run as: Administrator +# Location: D:\oracle\scripts\rman_restore_from_zero.ps1 + +$ErrorActionPreference = "Continue" + +$env:ORACLE_HOME = "C:\Users\Administrator\Downloads\WINDOWS.X64_193000_db_home" +$env:ORACLE_SID = "ROA" +$env:PATH = "$env:ORACLE_HOME\bin;$env:PATH" + +Write-Host "============================================" +Write-Host "RMAN Database Restore FROM ZERO" +Write-Host "============================================" +Write-Host "" +Write-Host "Database: ROA" +Write-Host "DBID: 1363569330" +Write-Host "Backups: F:\ROA\autobackup (NFS mount from Proxmox)" +Write-Host "" +Write-Host "This script will:" +Write-Host " 1. CLEANUP: Delete any existing database files" +Write-Host " 2. RESTORE: Restore from F:\ backups" +Write-Host " 3. VERIFY: Check database is working" +Write-Host "" + +# Verify F:\ mount is accessible +if (-not (Test-Path "F:\ROA\autobackup")) { + Write-Host "ERROR: F:\ROA\autobackup not accessible!" -ForegroundColor Red + Write-Host "" + Write-Host "Please verify:" + Write-Host " 1. F:\ drive is mounted: dir F:\" + Write-Host " 2. NFS mount command: mount -o rw,nolock,mtype=hard,timeout=60 10.0.20.202:/mnt/pve/oracle-backups F:" + Write-Host " 3. Proxmox host is reachable: ping 10.0.20.202" + Write-Host "" + exit 1 +} + +Write-Host "[OK] F:\ROA\autobackup is accessible" +Write-Host "" + +# Create directories +New-Item -ItemType Directory -Path "D:\oracle\temp" -Force | Out-Null +New-Item -ItemType Directory -Path "D:\oracle\logs" -Force | Out-Null + +Write-Host "============================================" +Write-Host "STEP 1: CLEANUP - Delete existing database" +Write-Host "============================================" +Write-Host "" +Write-Host "Calling cleanup_database.ps1..." +Write-Host "" + +# Call cleanup script with /SILENT flag +& "D:\oracle\scripts\cleanup_database.ps1" /SILENT +if ($LASTEXITCODE -ne 0) { + Write-Host "" + Write-Host "ERROR: Cleanup failed!" -ForegroundColor Red + exit 1 +} + +Write-Host "" +Write-Host "[OK] Cleanup complete - VM is in clean state" +Write-Host "" +Write-Host "Starting restore in 2 seconds..." +Start-Sleep -Seconds 2 + +Write-Host "============================================" +Write-Host "STEP 2: RESTORE - Restore from F:\ backups" +Write-Host "============================================" +Write-Host "" + +# Step 2.1: Create Oracle service +Write-Host "[2.1] Creating Oracle service from PFILE..." +if (-not (Test-Path "C:\Users\oracle\admin\ROA\pfile\initROA.ora")) { + Write-Host "ERROR: PFILE not found at C:\Users\oracle\admin\ROA\pfile\initROA.ora" -ForegroundColor Red + Write-Host "Cannot create Oracle service without PFILE!" + exit 1 +} + +& oradim -new -sid ROA -startmode manual -pfile "C:\Users\oracle\admin\ROA\pfile\initROA.ora" 2>&1 | Out-Null +if ($LASTEXITCODE -ne 0) { + Write-Host "ERROR: Failed to create Oracle service" -ForegroundColor Red + exit 1 +} +Write-Host "[OK] Oracle service created successfully" +Start-Sleep -Seconds 2 + +# Step 2.2: Startup NOMOUNT +Write-Host "[2.2] Starting database in NOMOUNT mode..." +$nomountSQL = @" +STARTUP NOMOUNT PFILE='C:\Users\oracle\admin\ROA\pfile\initROA.ora'; +EXIT; +"@ + +$nomountSQL | & sqlplus -S / as sysdba 2>&1 | Out-Null +if ($LASTEXITCODE -ne 0) { + Write-Host "ERROR: Failed to startup NOMOUNT" -ForegroundColor Red + exit 1 +} +Write-Host "[OK] Database started in NOMOUNT mode" +Start-Sleep -Seconds 3 + +# Step 2.3: Copy backups and create RMAN script +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 +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..." +Copy-Item "F:\ROA\autobackup\*.BKP" "C:\Users\oracle\recovery_area\ROA\autobackup\" -Force -ErrorAction Stop +if ($LASTEXITCODE -ne 0) { + Write-Host "ERROR: Failed to copy backups from F:\" -ForegroundColor Red + exit 1 +} +Write-Host "[OK] All backups copied to recovery area" + +# Create RMAN script +$rmanContent = @" +SET DBID 1363569330; + +RUN { + ALLOCATE CHANNEL ch1 DEVICE TYPE DISK; + RESTORE CONTROLFILE FROM AUTOBACKUP; + RELEASE CHANNEL ch1; +} + +ALTER DATABASE MOUNT; + +CATALOG START WITH 'F:/ROA/autobackup' NOPROMPT; + +RUN { + ALLOCATE CHANNEL ch1 DEVICE TYPE DISK; + ALLOCATE CHANNEL ch2 DEVICE TYPE DISK; + RESTORE DATABASE; + RELEASE CHANNEL ch1; + RELEASE CHANNEL ch2; +} + +RUN { + ALLOCATE CHANNEL ch1 DEVICE TYPE DISK; + RECOVER DATABASE NOREDO; + RELEASE CHANNEL ch1; +} + +ALTER DATABASE OPEN RESETLOGS; + +EXIT; +"@ + +$rmanContent | Out-File -FilePath $rmanScript -Encoding ASCII +Write-Host "[OK] RMAN script created: $rmanScript" + +# Step 2.4: Run RMAN restore +Write-Host "[2.4] Running RMAN restore (this will take 10-20 minutes)..." +Write-Host " Log file: $logFile" +Write-Host "" + +& rman target / cmdfile=$rmanScript log=$logFile + +if ($LASTEXITCODE -ne 0) { + Write-Host "" + Write-Host "ERROR: RMAN restore failed!" -ForegroundColor Red + Write-Host "Check log: $logFile" + exit 1 +} + +Write-Host "" +Write-Host "[OK] RMAN restore completed successfully!" +Write-Host "" + +Write-Host "============================================" +Write-Host "STEP 3: VERIFY - Check database status" +Write-Host "============================================" +Write-Host "" +Write-Host "[3.1] Verifying database..." + +$verifySQL = @" +SET PAGESIZE 100 LINESIZE 200 +COLUMN info FORMAT A80 +SELECT 'DB_NAME: ' || NAME || ', OPEN_MODE: ' || OPEN_MODE AS info FROM V`$DATABASE; +SELECT 'INSTANCE: ' || INSTANCE_NAME || ', STATUS: ' || STATUS AS info FROM V`$INSTANCE; +SELECT 'TABLESPACES: ' || COUNT(*) AS info FROM DBA_TABLESPACES; +SELECT 'DATAFILES: ' || COUNT(*) AS info FROM DBA_DATA_FILES; +SELECT 'TABLES: ' || COUNT(*) AS info FROM DBA_TABLES WHERE OWNER NOT IN ('SYS','SYSTEM'); +EXIT; +"@ + +$verifySQL | & sqlplus -S / as sysdba + +Write-Host "" +Write-Host "[3.2] Creating SPFILE for database persistence..." +$spfileSQL = @" +CREATE SPFILE FROM PFILE='C:\Users\oracle\admin\ROA\pfile\initROA.ora'; +EXIT; +"@ + +$spfileSQL | & sqlplus -S / as sysdba 2>&1 | Out-Null +if ($LASTEXITCODE -ne 0) { + Write-Host "WARNING: Failed to create SPFILE - database may not persist after connections close" -ForegroundColor Yellow +} else { + Write-Host "[OK] SPFILE created successfully" + + # Recreate service with auto-start and SPFILE + Write-Host "[3.3] Recreating Oracle service with auto-start mode..." + & oradim -delete -sid ROA 2>&1 | Out-Null + Start-Sleep -Seconds 2 + & oradim -new -sid ROA -startmode auto -spfile 2>&1 | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Host "WARNING: Failed to recreate service with auto-start" -ForegroundColor Yellow + } else { + Write-Host "[OK] Service recreated with auto-start mode" + } + + # Register with listener + $registerSQL = @" +ALTER SYSTEM REGISTER; +EXIT; +"@ + $registerSQL | & sqlplus -S / as sysdba 2>&1 | Out-Null +} + +Write-Host "" +Write-Host "============================================" +Write-Host "Database Restore FROM ZERO Complete!" +Write-Host "============================================" +Write-Host "" +Write-Host "Restore log: $logFile" +Write-Host "" +Write-Host "Database is OPEN and ready for testing!" -ForegroundColor Green +Write-Host "" +Write-Host "Next steps:" +Write-Host " 1. Test application connectivity" +Write-Host " 2. Verify data integrity" +Write-Host " 3. Run cleanup_database.ps1 to remove database after test" +Write-Host " 4. Shutdown DR VM to conserve resources" +Write-Host "" + +exit 0 diff --git a/oracle/standby-server-scripts/weekly-dr-test-proxmox.sh b/oracle/standby-server-scripts/weekly-dr-test-proxmox.sh index be5cbe8..3819429 100644 --- a/oracle/standby-server-scripts/weekly-dr-test-proxmox.sh +++ b/oracle/standby-server-scripts/weekly-dr-test-proxmox.sh @@ -362,7 +362,7 @@ run_dr_test() { log "STEP 4: Running database restore" if ssh -p "$DR_VM_PORT" "$DR_VM_USER@$DR_VM_IP" \ - "D:\\oracle\\scripts\\rman_restore_from_zero.cmd" 2>&1 | tee -a "$LOG_FILE"; then + "powershell -ExecutionPolicy Bypass -File D:\\oracle\\scripts\\rman_restore_from_zero.ps1" 2>&1 | tee -a "$LOG_FILE"; then local restore_end=$(date +%s) restore_duration=$(( (restore_end - restore_start) / 60 ))