Oracle DR: Fix backup retention and monitoring for new naming convention
Problem: Backups accumulated on DR (73 files, 4 days) instead of keeping only 2 days - transfer_incremental.ps1 had no cleanup function (ran 2x/day without cleanup) - transfer_to_dr.ps1 cleanup had poor logging - oracle-backup-monitor-proxmox.sh couldn't detect new L0/L1 backup format Changes: - Add cleanup to transfer_incremental.ps1 (delete backups older than 2 days) - Improve cleanup logging in transfer_to_dr.ps1 (shows count before/after) - Update oracle-backup-monitor-proxmox.sh to detect both naming conventions: * Old: *FULL*.BKP, *INCR*.BKP * New: L0_*.BKP (Level 0), L1_*.BKP (Level 1) - Remove temporary files from /input/ directory Result: Monitor now correctly reports backup age, cleanup runs after each transfer 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
# Copy Existing SSH Key to Proxmox
|
||||
# Ruleaza acest script pe PRIMARY ca Administrator
|
||||
#
|
||||
# Acest script copiaza cheia publica SSH existenta din profilul SYSTEM pe Proxmox
|
||||
|
||||
param(
|
||||
[string]$ProxmoxHost = "10.0.20.202",
|
||||
[string]$ProxmoxUser = "root"
|
||||
)
|
||||
|
||||
Write-Host "=========================================" -ForegroundColor Cyan
|
||||
Write-Host "Copiere Cheie SSH Existenta -> Proxmox DR" -ForegroundColor Cyan
|
||||
Write-Host "=========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
$SystemSSHDir = "C:\Windows\System32\config\systemprofile\.ssh"
|
||||
$PublicKeyPath = "$SystemSSHDir\id_rsa.pub"
|
||||
$PrivateKeyPath = "$SystemSSHDir\id_rsa"
|
||||
|
||||
# Verifica daca cheia exista
|
||||
if (-not (Test-Path $PublicKeyPath)) {
|
||||
Write-Host "X Cheia publica nu exista: $PublicKeyPath" -ForegroundColor Red
|
||||
Write-Host " Scriptul trebuie rulat ca Administrator!" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "OK Cheie publica gasita: $PublicKeyPath" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "-> Copiez cheia publica pe Proxmox ($ProxmoxHost)..." -ForegroundColor Yellow
|
||||
Write-Host " IMPORTANT: Vei fi intrebat de parola root pentru Proxmox!" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# Metoda simpla: folosim SCP pentru a copia fisierul temporar, apoi cat
|
||||
$tempFile = "C:\Windows\Temp\temp_pubkey.pub"
|
||||
Copy-Item $PublicKeyPath $tempFile -Force
|
||||
|
||||
# Copiaza fisierul pe Proxmox
|
||||
& scp $tempFile "${ProxmoxUser}@${ProxmoxHost}:/tmp/temp_pubkey.pub"
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "X Eroare la copierea fisierului cu SCP" -ForegroundColor Red
|
||||
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Adauga cheia in authorized_keys
|
||||
& ssh "${ProxmoxUser}@${ProxmoxHost}" "mkdir -p /root/.ssh; chmod 700 /root/.ssh; cat /tmp/temp_pubkey.pub >> /root/.ssh/authorized_keys; chmod 600 /root/.ssh/authorized_keys; rm /tmp/temp_pubkey.pub; echo 'SSH key added'"
|
||||
|
||||
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "OK Cheie publica copiata pe Proxmox!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "X Eroare la adaugarea cheii in authorized_keys" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "-> Testez conexiunea SSH fara parola..." -ForegroundColor Yellow
|
||||
|
||||
# Testeaza conexiunea (cu cheia din profilul SYSTEM)
|
||||
$testResult = & ssh -o StrictHostKeyChecking=no -i $PrivateKeyPath "${ProxmoxUser}@${ProxmoxHost}" "echo 'SSH connection OK'" 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "OK Conexiune SSH functioneaza fara parola!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "X Conexiunea SSH nu functioneaza direct din acest cont" -ForegroundColor Yellow
|
||||
Write-Host " Dar cheia a fost adaugata - scheduled tasks (SYSTEM) ar trebui sa functioneze" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host "OK Setup complet!" -ForegroundColor Green
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "Cheia din profilul SYSTEM: $PrivateKeyPath" -ForegroundColor Cyan
|
||||
Write-Host "Scheduled tasks vor folosi aceasta cheie automat." -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 42 KiB |
@@ -1,89 +0,0 @@
|
||||
# Setup SSH Keys for PRIMARY → Proxmox DR Transfer
|
||||
# Rulează acest script MANUAL pe PRIMARY ca Administrator
|
||||
#
|
||||
# Acest script:
|
||||
# 1. Generează chei SSH în profilul curent ($env:USERPROFILE\.ssh)
|
||||
# 2. Copiază cheia publică pe Proxmox host (10.0.20.202)
|
||||
# 3. Testează conexiunea SSH
|
||||
|
||||
param(
|
||||
[string]$ProxmoxHost = "10.0.20.202",
|
||||
[string]$ProxmoxUser = "root"
|
||||
)
|
||||
|
||||
Write-Host "=========================================" -ForegroundColor Cyan
|
||||
Write-Host "SSH Key Setup pentru PRIMARY → Proxmox DR" -ForegroundColor Cyan
|
||||
Write-Host "=========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
$SSHDir = "$env:USERPROFILE\.ssh"
|
||||
$PrivateKeyPath = "$SSHDir\id_rsa"
|
||||
$PublicKeyPath = "$SSHDir\id_rsa.pub"
|
||||
|
||||
# Verifică dacă cheia există deja
|
||||
if (Test-Path $PrivateKeyPath) {
|
||||
Write-Host "✓ SSH key deja există: $PrivateKeyPath" -ForegroundColor Green
|
||||
Write-Host " Voi folosi cheia existentă." -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "→ Generez SSH key pair..." -ForegroundColor Yellow
|
||||
|
||||
# Creează directorul .ssh dacă nu există
|
||||
if (-not (Test-Path $SSHDir)) {
|
||||
New-Item -ItemType Directory -Path $SSHDir -Force | Out-Null
|
||||
}
|
||||
|
||||
# Generează cheia SSH (fără parolă pentru automatizare)
|
||||
& ssh-keygen.exe -t rsa -b 4096 -f $PrivateKeyPath -N '""' -C "PRIMARY-RMAN-to-Proxmox"
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ Cheie SSH generată cu succes!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ Eroare la generarea cheii SSH" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "→ Citesc cheia publică..." -ForegroundColor Yellow
|
||||
$PublicKey = Get-Content $PublicKeyPath -Raw
|
||||
|
||||
Write-Host "→ Copiez cheia publică pe Proxmox ($ProxmoxHost)..." -ForegroundColor Yellow
|
||||
Write-Host " IMPORTANT: Vei fi întrebat de parola root pentru Proxmox!" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# Copiază cheia publică pe Proxmox
|
||||
$command = "mkdir -p /root/.ssh && chmod 700 /root/.ssh && echo '$PublicKey' >> /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys && echo 'SSH key adăugat cu succes!'"
|
||||
|
||||
& ssh "${ProxmoxUser}@${ProxmoxHost}" $command
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ Cheie publică copiată pe Proxmox!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ Eroare la copierea cheii pe Proxmox" -ForegroundColor Red
|
||||
Write-Host " Verifică dacă ai acces SSH la Proxmox cu parolă" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "→ Testez conexiunea SSH fără parolă..." -ForegroundColor Yellow
|
||||
|
||||
# Testează conexiunea
|
||||
& ssh -o StrictHostKeyChecking=no -i $PrivateKeyPath "${ProxmoxUser}@${ProxmoxHost}" "echo 'SSH connection OK'"
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ Conexiune SSH funcționează fără parolă!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ Conexiunea SSH nu funcționează" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host "✓ Setup complet!" -ForegroundColor Green
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "Cheia privată: $PrivateKeyPath" -ForegroundColor Cyan
|
||||
Write-Host "Cheia publică: $PublicKeyPath" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host "Scripturile de transfer vor folosi această cheie automat." -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
@@ -1,130 +0,0 @@
|
||||
# Transfer Oracle INCREMENTAL Backup towards DR Server
|
||||
# Rulează după backup incremental (14:30)
|
||||
# Mai simplu decât scriptul full - doar transferă fișierele noi
|
||||
|
||||
param(
|
||||
[string]$SourceFRA = "C:\Users\Oracle\recovery_area\ROA",
|
||||
[string]$DRHost = "10.0.20.202",
|
||||
[int]$DRPort = 22,
|
||||
[string]$DRUser = "root",
|
||||
[string]$DRPath = "/mnt/pve/oracle-backups/ROA/autobackup",
|
||||
[string]$SSHKeyPath = "$env:USERPROFILE\.ssh\id_rsa",
|
||||
[string]$LogFile = "D:\rman_backup\logs\transfer_incr_$(Get-Date -Format 'yyyyMMdd_HHmm').log"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Continue"
|
||||
|
||||
function Write-Log {
|
||||
param([string]$Message, [string]$Level = "INFO")
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$logLine = "[$timestamp] [$Level] $Message"
|
||||
Write-Host $logLine
|
||||
Add-Content -Path $LogFile -Value $logLine -Encoding UTF8 -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
function Get-IncrementalBackups {
|
||||
Write-Log "Searching for incremental backup files created in last 2 hours..."
|
||||
|
||||
$cutoffTime = (Get-Date).AddHours(-2)
|
||||
$backupFiles = @()
|
||||
|
||||
# Caută în BACKUPSET, AUTOBACKUP
|
||||
$searchPaths = @(
|
||||
"$SourceFRA\BACKUPSET",
|
||||
"$SourceFRA\AUTOBACKUP"
|
||||
)
|
||||
|
||||
foreach ($path in $searchPaths) {
|
||||
if (Test-Path $path) {
|
||||
$files = Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue |
|
||||
Where-Object {
|
||||
$_.LastWriteTime -gt $cutoffTime -and
|
||||
$_.Name -match '\.(BKP|bkp)$'
|
||||
}
|
||||
$backupFiles += $files
|
||||
}
|
||||
}
|
||||
|
||||
return $backupFiles
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Log "========================================="
|
||||
Write-Log "Oracle INCREMENTAL Backup Transfer Started"
|
||||
Write-Log "========================================="
|
||||
Write-Log "Source FRA: $SourceFRA"
|
||||
Write-Log "DR Server: $DRHost"
|
||||
|
||||
# Test SSH connection
|
||||
Write-Log "Testing SSH connection to $DRHost`:$DRPort..."
|
||||
$null = & ssh -n -p $DRPort -i $SSHKeyPath -o StrictHostKeyChecking=no -o ConnectTimeout=10 "${DRUser}@${DRHost}" "exit 0" 2>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "SSH connection failed"
|
||||
}
|
||||
Write-Log "SSH connection OK" "SUCCESS"
|
||||
|
||||
# Găsește backup-uri incrementale
|
||||
$backupFiles = Get-IncrementalBackups
|
||||
|
||||
if ($backupFiles.Count -eq 0) {
|
||||
Write-Log "⚠️ No incremental backup files found (this might be normal if backup didn't run yet)" "WARNING"
|
||||
exit 0
|
||||
}
|
||||
|
||||
$totalSizeGB = ($backupFiles | Measure-Object -Property Length -Sum).Sum / 1GB
|
||||
Write-Log "Found $($backupFiles.Count) incremental files, total size: $([math]::Round($totalSizeGB, 2)) GB"
|
||||
|
||||
# Transfer fișiere
|
||||
Write-Log "Starting file transfer..."
|
||||
$successCount = 0
|
||||
$failCount = 0
|
||||
|
||||
foreach ($file in $backupFiles) {
|
||||
$fileName = $file.Name
|
||||
$fileSizeMB = [math]::Round($file.Length / 1MB, 2)
|
||||
|
||||
# Check dacă fișierul există deja pe DR (skip duplicates) - Linux bash command
|
||||
$checkCmd = "test -f '$DRPath/$fileName' && echo 'True' || echo 'False'"
|
||||
$checkResult = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" $checkCmd 2>&1
|
||||
|
||||
if ($checkResult -match "True") {
|
||||
Write-Log "Skipping (already on DR): $fileName" "INFO"
|
||||
$successCount++
|
||||
continue
|
||||
}
|
||||
|
||||
Write-Log "Transferring: $fileName ($fileSizeMB MB)"
|
||||
|
||||
# SCP optimized: no compression (already compressed), fast cipher
|
||||
$null = & scp -P $DRPort -i $SSHKeyPath -o StrictHostKeyChecking=no -o Compression=no -o Cipher=aes128-gcm@openssh.com `
|
||||
$file.FullName `
|
||||
"${DRUser}@${DRHost}:${DRPath}/$fileName" 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Log "Transferred: $fileName" "SUCCESS"
|
||||
$successCount++
|
||||
} else {
|
||||
Write-Log "Failed to transfer: $fileName" "ERROR"
|
||||
$failCount++
|
||||
}
|
||||
}
|
||||
|
||||
Write-Log "========================================="
|
||||
Write-Log "Transfer summary: $successCount succeeded, $failCount failed"
|
||||
|
||||
if ($failCount -gt 0) {
|
||||
Write-Log "⚠️ Some transfers failed!" "WARNING"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Log "========================================="
|
||||
Write-Log "INCREMENTAL Backup Transfer Completed Successfully"
|
||||
Write-Log "========================================="
|
||||
Write-Log "Files transferred: $successCount/$($backupFiles.Count)"
|
||||
|
||||
exit 0
|
||||
|
||||
} catch {
|
||||
Write-Log "CRITICAL ERROR: $($_.Exception.Message)" "ERROR"
|
||||
exit 1
|
||||
}
|
||||
@@ -1,201 +0,0 @@
|
||||
# Transfer Oracle RMAN Backup towards DR Server
|
||||
# Rulează după backup RMAN (03:00 AM)
|
||||
# Copiază backup-uri de pe PRIMARY (10.0.20.36) către DR (10.0.20.37)
|
||||
|
||||
param(
|
||||
[string]$SourceFRA = "C:\Users\Oracle\recovery_area\ROA",
|
||||
[string]$DRHost = "10.0.20.202",
|
||||
[int]$DRPort = 22,
|
||||
[string]$DRUser = "root",
|
||||
[string]$DRPath = "/mnt/pve/oracle-backups/ROA/autobackup",
|
||||
[string]$SSHKeyPath = "$env:USERPROFILE\.ssh\id_rsa",
|
||||
[int]$RetentionDays = 2,
|
||||
[string]$LogFile = "D:\rman_backup\logs\transfer_$(Get-Date -Format 'yyyyMMdd').log"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Continue"
|
||||
|
||||
function Write-Log {
|
||||
param([string]$Message, [string]$Level = "INFO")
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$logLine = "[$timestamp] [$Level] $Message"
|
||||
Write-Host $logLine
|
||||
Add-Content -Path $LogFile -Value $logLine -Encoding UTF8 -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
function Test-SSHConnection {
|
||||
Write-Log "Testing SSH connection to $DRHost`:$DRPort..."
|
||||
|
||||
try {
|
||||
# Folosește -n pentru a nu citi din stdin (fix pentru blocare)
|
||||
$null = & ssh -n -p $DRPort -i $SSHKeyPath -o StrictHostKeyChecking=no -o ConnectTimeout=10 "${DRUser}@${DRHost}" "exit 0" 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Log "SSH connection successful" "SUCCESS"
|
||||
return $true
|
||||
} else {
|
||||
Write-Log "SSH connection failed with exit code: $LASTEXITCODE" "ERROR"
|
||||
return $false
|
||||
}
|
||||
} catch {
|
||||
Write-Log "SSH connection error: $_" "ERROR"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Get-TodaysBackups {
|
||||
Write-Log "Searching for today's backup files..."
|
||||
|
||||
$today = Get-Date
|
||||
$cutoffDate = $today.Date # Only today (after midnight)
|
||||
$backupFiles = @()
|
||||
|
||||
$searchPaths = @(
|
||||
"$SourceFRA\BACKUPSET",
|
||||
"$SourceFRA\AUTOBACKUP"
|
||||
)
|
||||
|
||||
foreach ($path in $searchPaths) {
|
||||
if (Test-Path $path) {
|
||||
# Get files created TODAY only (exclude old backups)
|
||||
$files = Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue |
|
||||
Where-Object {
|
||||
$_.LastWriteTime -gt $cutoffDate -and
|
||||
$_.Name -notlike "*__TAG_*" # Exclude old uncompressed backups
|
||||
} |
|
||||
Sort-Object LastWriteTime -Descending
|
||||
|
||||
$backupFiles += $files
|
||||
}
|
||||
}
|
||||
|
||||
if ($backupFiles.Count -eq 0) {
|
||||
Write-Log "No backup files found for today!" "WARNING"
|
||||
return @()
|
||||
}
|
||||
|
||||
$totalSizeGB = ($backupFiles | Measure-Object -Property Length -Sum).Sum / 1GB
|
||||
Write-Log "Found $($backupFiles.Count) files, total size: $([math]::Round($totalSizeGB, 2)) GB"
|
||||
|
||||
return $backupFiles
|
||||
}
|
||||
|
||||
function Transfer-FileToDR {
|
||||
param([System.IO.FileInfo]$File, [string]$DestPath)
|
||||
|
||||
$fileName = $File.Name
|
||||
$fileSizeMB = [math]::Round($File.Length / 1MB, 2)
|
||||
|
||||
try {
|
||||
# Check dacă fișierul există deja pe DR (skip duplicates) - Linux bash command
|
||||
$checkCmd = "test -f '$DestPath/$fileName' && echo 'True' || echo 'False'"
|
||||
$checkResult = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" $checkCmd 2>&1
|
||||
|
||||
if ($checkResult -match "True") {
|
||||
Write-Log "Skipping (already on DR): $fileName" "INFO"
|
||||
return $true
|
||||
}
|
||||
|
||||
Write-Log "Transferring: $fileName ($fileSizeMB MB)"
|
||||
|
||||
# SCP transfer - NO compression (files already compressed by RMAN)
|
||||
# Use cipher aes128-gcm for better performance
|
||||
$null = & scp -P $DRPort -i $SSHKeyPath -o StrictHostKeyChecking=no -o Compression=no -o Cipher=aes128-gcm@openssh.com $File.FullName "${DRUser}@${DRHost}:${DestPath}/" 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Log "Transferred: $fileName" "SUCCESS"
|
||||
return $true
|
||||
} else {
|
||||
Write-Log "Failed to transfer: $fileName (exit code: $LASTEXITCODE)" "ERROR"
|
||||
return $false
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Transfer error for $fileName : $_" "ERROR"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Cleanup-OldBackupsOnDR {
|
||||
Write-Log "Cleaning up old backups on DR (keeping last $RetentionDays days)..."
|
||||
|
||||
try {
|
||||
# Cleanup: șterge fișiere mai vechi de $RetentionDays zile - Linux find command
|
||||
$cleanupCmd = "find '$DRPath' -type f -mtime +$RetentionDays -delete 2>/dev/null || true"
|
||||
$result = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" $cleanupCmd 2>&1
|
||||
|
||||
Write-Log "Cleanup completed on DR (removed files older than $RetentionDays days)"
|
||||
} catch {
|
||||
Write-Log "Cleanup warning: $_" "WARNING"
|
||||
}
|
||||
}
|
||||
|
||||
# ==================== MAIN ====================
|
||||
|
||||
try {
|
||||
Write-Log "========================================="
|
||||
Write-Log "Oracle DR Backup Transfer Started"
|
||||
Write-Log "========================================="
|
||||
Write-Log "Source FRA: $SourceFRA"
|
||||
Write-Log "DR Server: $DRHost"
|
||||
Write-Log "DR Path: $DRPath"
|
||||
|
||||
# Verificare prerequisite
|
||||
if (-not (Test-Path $SourceFRA)) {
|
||||
throw "Source FRA path not found: $SourceFRA"
|
||||
}
|
||||
|
||||
if (-not (Test-Path $SSHKeyPath)) {
|
||||
throw "SSH key not found: $SSHKeyPath"
|
||||
}
|
||||
|
||||
# Test SSH connection
|
||||
if (-not (Test-SSHConnection)) {
|
||||
throw "Cannot connect to DR server via SSH"
|
||||
}
|
||||
|
||||
# Creare director pe DR - Linux mkdir command
|
||||
Write-Log "Ensuring DR directory exists..."
|
||||
$null = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" "mkdir -p '$DRPath'" 2>&1
|
||||
|
||||
# Găsește backup-uri
|
||||
$backupFiles = Get-TodaysBackups
|
||||
|
||||
if ($backupFiles.Count -eq 0) {
|
||||
throw "No backup files to transfer!"
|
||||
}
|
||||
|
||||
# Transfer fișiere
|
||||
Write-Log "Starting file transfer..."
|
||||
$successCount = 0
|
||||
$failCount = 0
|
||||
|
||||
foreach ($file in $backupFiles) {
|
||||
if (Transfer-FileToDR -File $file -DestPath $DRPath) {
|
||||
$successCount++
|
||||
} else {
|
||||
$failCount++
|
||||
}
|
||||
}
|
||||
|
||||
Write-Log "Transfer summary: $successCount succeeded, $failCount failed"
|
||||
|
||||
if ($failCount -gt 0) {
|
||||
Write-Log "Some transfers failed!" "WARNING"
|
||||
}
|
||||
|
||||
# Cleanup old backups pe DR
|
||||
Cleanup-OldBackupsOnDR
|
||||
|
||||
Write-Log "========================================="
|
||||
Write-Log "DR Backup Transfer Completed Successfully"
|
||||
Write-Log "========================================="
|
||||
Write-Log "Files transferred: $successCount/$($backupFiles.Count)"
|
||||
Write-Log "DR Server: ${DRHost}:${DRPath}"
|
||||
|
||||
exit 0
|
||||
|
||||
} catch {
|
||||
Write-Log "CRITICAL ERROR: $($_.Exception.Message)" "ERROR"
|
||||
Write-Log "Stack trace: $($_.ScriptStackTrace)" "ERROR"
|
||||
exit 1
|
||||
}
|
||||
@@ -294,7 +294,9 @@ check_backups() {
|
||||
[ -z "$total_size" ] && total_size="0G"
|
||||
total_size_label="$total_size"
|
||||
|
||||
local latest_full=$(find "$BACKUP_PATH" -maxdepth 1 -type f -name '*FULL*.BKP' -printf '%T@ %p\n' | sort -nr | head -1 | cut -d' ' -f2-)
|
||||
# Search for FULL backups (both old and new naming conventions)
|
||||
# Old format: *FULL*.BKP, New format: L0_*.BKP
|
||||
local latest_full=$(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*FULL*.BKP' -o -name 'L0_*.BKP' \) -printf '%T@ %p\n' | sort -nr | head -1 | cut -d' ' -f2-)
|
||||
if [ -n "$latest_full" ]; then
|
||||
local full_timestamp=$(stat -c %Y "$latest_full")
|
||||
local current_timestamp=$(date +%s)
|
||||
@@ -310,7 +312,10 @@ check_backups() {
|
||||
errors+=("No FULL backup found")
|
||||
fi
|
||||
|
||||
local latest_cumulative=$(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*INCR*.BKP' -o -name '*INCREMENTAL*.BKP' -o -name '*CUMULATIVE*.BKP' \) -printf '%T@ %p\n' | sort -nr | head -1 | cut -d' ' -f2-)
|
||||
# Search for INCREMENTAL backups (both old and new naming conventions)
|
||||
# Old format: *INCR*.BKP, *INCREMENTAL*.BKP, *CUMULATIVE*.BKP
|
||||
# New format: L1_*.BKP
|
||||
local latest_cumulative=$(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*INCR*.BKP' -o -name '*INCREMENTAL*.BKP' -o -name '*CUMULATIVE*.BKP' -o -name 'L1_*.BKP' \) -printf '%T@ %p\n' | sort -nr | head -1 | cut -d' ' -f2-)
|
||||
if [ -n "$latest_cumulative" ]; then
|
||||
local cumulative_timestamp=$(stat -c %Y "$latest_cumulative")
|
||||
local current_timestamp=$(date +%s)
|
||||
@@ -323,10 +328,10 @@ check_backups() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# Collect ALL FULL backups
|
||||
# Collect ALL FULL backups (both old and new naming conventions)
|
||||
local -a full_backups=()
|
||||
local -a full_backup_entries=()
|
||||
if readarray -t full_backups < <(find "$BACKUP_PATH" -maxdepth 1 -type f -name '*FULL*.BKP' -printf '%T@ %p\n' | sort -nr | cut -d' ' -f2-); then
|
||||
if readarray -t full_backups < <(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*FULL*.BKP' -o -name 'L0_*.BKP' \) -printf '%T@ %p\n' | sort -nr | cut -d' ' -f2-); then
|
||||
for backup_file in "${full_backups[@]}"; do
|
||||
[ -z "$backup_file" ] && continue
|
||||
local backup_name=$(basename "$backup_file")
|
||||
@@ -337,10 +342,10 @@ check_backups() {
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect ALL INCREMENTAL backups
|
||||
# Collect ALL INCREMENTAL backups (both old and new naming conventions)
|
||||
local -a incr_backups=()
|
||||
local -a incr_backup_entries=()
|
||||
if readarray -t incr_backups < <(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*INCR*.BKP' -o -name '*INCREMENTAL*.BKP' -o -name '*CUMULATIVE*.BKP' \) -printf '%T@ %p\n' | sort -nr | cut -d' ' -f2-); then
|
||||
if readarray -t incr_backups < <(find "$BACKUP_PATH" -maxdepth 1 -type f \( -name '*INCR*.BKP' -o -name '*INCREMENTAL*.BKP' -o -name '*CUMULATIVE*.BKP' -o -name 'L1_*.BKP' \) -printf '%T@ %p\n' | sort -nr | cut -d' ' -f2-); then
|
||||
for backup_file in "${incr_backups[@]}"; do
|
||||
[ -z "$backup_file" ] && continue
|
||||
local backup_name=$(basename "$backup_file")
|
||||
|
||||
@@ -48,6 +48,34 @@ function Get-IncrementalBackups {
|
||||
return $backupFiles
|
||||
}
|
||||
|
||||
function Cleanup-OldBackupsOnDR {
|
||||
param([int]$RetentionDays = 2)
|
||||
|
||||
Write-Log "Cleaning up old backups on DR (keeping last $RetentionDays days)..."
|
||||
|
||||
try {
|
||||
# Count fișiere înainte de cleanup
|
||||
$countBefore = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" "find '$DRPath' -name '*.BKP' -type f | wc -l" 2>&1
|
||||
Write-Log "Backups before cleanup: $countBefore"
|
||||
|
||||
# Cleanup: șterge fișiere mai vechi de $RetentionDays zile
|
||||
$cleanupCmd = "find '$DRPath' -name '*.BKP' -type f -mtime +$RetentionDays -delete 2>&1"
|
||||
$result = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" $cleanupCmd 2>&1
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Log "Cleanup warning: $result" "WARNING"
|
||||
}
|
||||
|
||||
# Count fișiere după cleanup
|
||||
$countAfter = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" "find '$DRPath' -name '*.BKP' -type f | wc -l" 2>&1
|
||||
$deleted = [int]$countBefore - [int]$countAfter
|
||||
|
||||
Write-Log "Cleanup completed: Deleted $deleted old backup files, $countAfter remaining"
|
||||
} catch {
|
||||
Write-Log "Cleanup error: $_" "WARNING"
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Log "========================================="
|
||||
Write-Log "Oracle INCREMENTAL Backup Transfer Started"
|
||||
@@ -117,6 +145,9 @@ try {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Cleanup old backups pe DR (keep last 2 days)
|
||||
Cleanup-OldBackupsOnDR -RetentionDays 2
|
||||
|
||||
Write-Log "========================================="
|
||||
Write-Log "INCREMENTAL Backup Transfer Completed Successfully"
|
||||
Write-Log "========================================="
|
||||
|
||||
@@ -119,13 +119,25 @@ function Cleanup-OldBackupsOnDR {
|
||||
Write-Log "Cleaning up old backups on DR (keeping last $RetentionDays days)..."
|
||||
|
||||
try {
|
||||
# Count fișiere înainte de cleanup
|
||||
$countBefore = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" "find '$DRPath' -name '*.BKP' -type f | wc -l" 2>&1
|
||||
Write-Log "Backups before cleanup: $countBefore"
|
||||
|
||||
# Cleanup: șterge fișiere mai vechi de $RetentionDays zile - Linux find command
|
||||
$cleanupCmd = "find '$DRPath' -type f -mtime +$RetentionDays -delete 2>/dev/null || true"
|
||||
$cleanupCmd = "find '$DRPath' -name '*.BKP' -type f -mtime +$RetentionDays -delete 2>&1"
|
||||
$result = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" $cleanupCmd 2>&1
|
||||
|
||||
Write-Log "Cleanup completed on DR (removed files older than $RetentionDays days)"
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Log "Cleanup warning: $result" "WARNING"
|
||||
}
|
||||
|
||||
# Count fișiere după cleanup
|
||||
$countAfter = & ssh -n -p $DRPort -i $SSHKeyPath "${DRUser}@${DRHost}" "find '$DRPath' -name '*.BKP' -type f | wc -l" 2>&1
|
||||
$deleted = [int]$countBefore - [int]$countAfter
|
||||
|
||||
Write-Log "Cleanup completed: Deleted $deleted old backup files, $countAfter remaining"
|
||||
} catch {
|
||||
Write-Log "Cleanup warning: $_" "WARNING"
|
||||
Write-Log "Cleanup error: $_" "WARNING"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user