Files
ROMFASTSQL/proxmox/lxc108-oracle/roa-windows-setup/scripts/09-copy-licenses.ps1
Marius 5a746990ce feat(oracle): instalare ROA in doua faze, licente, IIS si export zilnic
Adauga FAZA1.cmd/FAZA2.cmd, care despart migrarea in partea care se poate
face in timpul programului (contafin, obiecte SYS, sinonime, licente, IIS)
si partea de seara (schemele de firma). Scripturi noi: 09 pentru licentele
din SYS.AUTH_SERII/AUTH_DETALII, 10 pentru publicarea D:\ROAUPDATE in IIS,
11 pentru exportul zilnic cu backupora.exe, drop-contafin pentru reimport.

backupora.exe si sabloanele sale intra in repo ca sa fie kitul autonom.
Pasul 07 primeste verificarile corespunzatoare pentru licente, IIS si task.

Nerulat inca pe o baza reala - vezi docs/handoff_instalare-doua-faze.md.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SzF1hf4aFS1tmJWpPMiwGp
2026-08-25 20:57:23 +03:00

388 lines
14 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
Copiaza licentele ROA (SYS.AUTH_SERII + SYS.AUTH_DETALII) de pe serverul
vechi pe cel nou.
.DESCRIPTION
Fara acest pas, un client migrat ramane fara licente: sys-objects.sql
creeaza tabelele goale, iar 08-post-install-config.ps1 scrie doar
AUTH_DETALII (Customer ID). Seriile propriu-zise din AUTH_SERII, pe care
AUTH_PACK le decripteaza ca sa afle ce module si cate firme sunt permise,
nu se creeaza nicaieri.
Doua moduri:
1. Prin retea. Se conecteaza cu SQL*Plus la serverul VECHI (parametrii
-OldHost / -OldPort / -OldService / -OldSysPassword), ruleaza
sql\export-licente.sql si captureaza INSERT-urile generate.
Serverul vechi nu e modificat - se face doar SELECT.
2. Din fisier (-FromFile). Cand serverul vechi nu e accesibil din cel
nou, rulezi manual pe el:
sqlplus -S "sys/PAROLA@GAZDA:1521/SERVICIU as sysdba" @export-licente.sql > licente.sql
si aduci licente.sql la client.
Inainte de a scrie ceva, salveaza starea curenta a licentelor de pe
serverul NOU in logs\licente-backup-server-nou_<data>.sql, in acelasi
format. Daca ceva merge prost, ala e drumul inapoi.
Dupa import, recreeaza SYS.SEQ_AUTH_SERII peste MAX(ID_SERIE)+1, altfel
prima licenta adaugata ulterior din aplicatie ar incalca PK_AUTH_SERII.
.PARAMETER OldHost
Gazda serverului VECHI. Prezenta ei porneste modul prin retea.
.PARAMETER OldPort
Portul listener-ului de pe serverul vechi. Implicit 1521.
.PARAMETER OldService
Numele serviciului de pe serverul vechi (ex. XE, ORCL, XEPDB1).
Implicit XE - bazele vechi de client sunt de regula non-CDB.
.PARAMETER OldSysPassword
Parola SYS de pe serverul vechi. Obligatorie in modul prin retea.
.PARAMETER FromFile
Fisierul .sql produs de export-licente.sql. Ocoleste conexiunea la
serverul vechi.
.PARAMETER OracleHome
Oracle home local. Autodetectat daca lipseste.
.PARAMETER ServiceName
Serviciul bazei NOI. Implicit XEPDB1.
.PARAMETER SystemPassword
Parola SYS de pe serverul NOU. Implicit romfastsoft.
.PARAMETER Replace
Sterge licentele existente de pe serverul nou inainte de import.
Fara el, scriptul refuza sa scrie peste licente deja prezente.
.PARAMETER ExportOnly
Doar scoate licentele de pe serverul vechi intr-un fisier, fara sa
importe nimic. Util cand pregatesti kitul de pe alta statie.
.EXAMPLE
.\09-copy-licenses.ps1 -OldHost 192.168.1.10 -OldSysPassword "parolaveche"
Ia licentele prin retea si le importa.
.EXAMPLE
.\09-copy-licenses.ps1 -OldHost 192.168.1.10 -OldService ORCL -OldSysPassword "x" -ExportOnly
Doar salveaza licentele in logs\, pentru import ulterior.
.EXAMPLE
.\09-copy-licenses.ps1 -FromFile "C:\kit\licente.sql" -Replace
Importa dintr-un fisier adus manual, peste licentele existente.
.NOTES
File Name : 09-copy-licenses.ps1
Prerequisite : 02-create-sys-objects.ps1 (tabelele AUTH_* trebuie sa existe)
Copyright 2026 : ROMFAST
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$OldHost,
[Parameter(Mandatory = $false)]
[int]$OldPort = 1521,
[Parameter(Mandatory = $false)]
[string]$OldService = "XE",
[Parameter(Mandatory = $false)]
[string]$OldSysPassword,
[Parameter(Mandatory = $false)]
[string]$FromFile,
[Parameter(Mandatory = $false)]
[string]$OracleHome,
[Parameter(Mandatory = $false)]
[string]$ServiceName = "XEPDB1",
[Parameter(Mandatory = $false)]
[string]$SystemPassword = "romfastsoft",
[Parameter(Mandatory = $false)]
[switch]$Replace,
[Parameter(Mandatory = $false)]
[switch]$ExportOnly
)
$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "lib\logging-functions.ps1")
. (Join-Path $PSScriptRoot "lib\oracle-functions.ps1")
$configFile = Join-Path $PSScriptRoot "..\config.ps1"
if (Test-Path $configFile) {
. $configFile
if (-not $PSBoundParameters.ContainsKey('OracleHome') -and $Config.OracleHome) { $OracleHome = $Config.OracleHome }
if (-not $PSBoundParameters.ContainsKey('ServiceName') -and $Config.ServiceName) { $ServiceName = $Config.ServiceName }
if (-not $PSBoundParameters.ContainsKey('SystemPassword') -and $Config.SystemPassword) { $SystemPassword = $Config.SystemPassword }
}
$stamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$logDir = Join-Path $PSScriptRoot "..\logs"
if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
Initialize-LogFile -LogPath (Join-Path $logDir "09-copy-licenses_$stamp.log") -ScriptName "09-copy-licenses.ps1"
$MarkerStart = '-- ROA-LICENTE-EXPORT v1'
$MarkerEnd = '-- ROA-LICENTE-SFARSIT'
function Test-LicenseDump {
param([string]$Path)
if (-not (Test-Path $Path)) { throw "Fisierul de licente nu exista: $Path" }
$text = Get-Content -Path $Path -Raw
if ($null -eq $text) { $text = "" }
if ($text -notmatch [regex]::Escape($MarkerStart)) {
throw "Fisierul $Path nu contine marcajul '$MarkerStart'. Nu e o iesire valida a lui export-licente.sql."
}
if ($text -notmatch [regex]::Escape($MarkerEnd)) {
throw "Fisierul $Path nu contine marcajul de final '$MarkerEnd'. Exportul s-a intrerupt la mijloc - nu il import."
}
if ($text -match 'ORA-\d{5}:|SP2-\d{4}:') {
throw "Fisierul $Path contine erori Oracle. Corecteaza exportul inainte de import."
}
$nextval = 1
if ($text -match '-- ROA-LICENTE-NEXTVAL=(\d+)') { $nextval = [int]$Matches[1] }
return [PSCustomObject]@{
Serii = ([regex]::Matches($text, 'INSERT INTO SYS\.AUTH_SERII')).Count
Detalii = ([regex]::Matches($text, 'INSERT INTO SYS\.AUTH_DETALII')).Count
NextVal = $nextval
}
}
try {
Write-LogSection "Copiere licente ROA (AUTH_SERII + AUTH_DETALII)"
$oraHome = Get-OracleHome -OracleHome $OracleHome
Write-LogSuccess "Oracle Home: $oraHome"
$exportSql = Join-Path $PSScriptRoot "..\sql\export-licente.sql"
if (-not (Test-Path $exportSql)) {
throw "Lipseste sql\export-licente.sql din kit."
}
# ------------------------------------------------------------------
# Sursa licentelor: retea sau fisier
# ------------------------------------------------------------------
$dumpFile = $null
if ($FromFile) {
Write-LogSection "Sursa: fisier"
$dumpFile = (Resolve-Path $FromFile).Path
Write-Log "Fisier: $dumpFile"
}
elseif ($OldHost) {
Write-LogSection "Sursa: serverul vechi, prin retea"
if (-not $OldSysPassword) {
throw "-OldSysPassword e obligatoriu cand dai -OldHost."
}
Write-Log "Server vechi: ${OldHost}:${OldPort}/${OldService}"
Write-Log "Se testeaza conexiunea..."
$reachable = $false
try {
$reachable = Test-OracleConnection -OracleHome $oraHome -ServiceName $OldService `
-DbHost $OldHost -Port $OldPort -Password $OldSysPassword
}
catch {
Write-LogDebug $_.Exception.Message
}
if (-not $reachable) {
Write-LogError "Nu ma pot conecta la ${OldHost}:${OldPort}/${OldService}."
Write-LogError "Verifica: listener-ul pornit, firewall-ul, numele serviciului, parola SYS."
Write-LogError ""
Write-LogError "Varianta manuala - ruleaza PE SERVERUL VECHI:"
Write-LogError " sqlplus -S ""sys/PAROLA@localhost:$OldPort/$OldService as sysdba"" @export-licente.sql > licente.sql"
Write-LogError "apoi adu licente.sql aici si ruleaza:"
Write-LogError " .\09-copy-licenses.ps1 -FromFile licente.sql -Replace"
throw "Serverul vechi nu e accesibil."
}
Write-LogSuccess "Conexiune la serverul vechi: OK"
Write-Log "Se extrag licentele (doar SELECT, serverul vechi nu se modifica)..."
$raw = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $OldService `
-DbHost $OldHost -Port $OldPort `
-Username "sys" -Password $OldSysPassword -AsSysdba `
-SqlFile $exportSql -Silent
$dumpFile = Join-Path $logDir "licente-de-pe-server-vechi_$stamp.sql"
Set-Content -Path $dumpFile -Value $raw -Encoding ASCII
Write-LogSuccess "Salvat: $dumpFile"
}
else {
throw "Da fie -OldHost (+ -OldSysPassword), fie -FromFile. Vezi 'Get-Help .\09-copy-licenses.ps1 -Full'."
}
$info = Test-LicenseDump -Path $dumpFile
Write-LogSuccess "Export valid: $($info.Serii) serii, $($info.Detalii) inregistrari AUTH_DETALII"
Write-Log "SEQ_AUTH_SERII va porni de la $($info.NextVal)"
if ($info.Serii -eq 0) {
Write-LogWarning "Serverul vechi nu are nicio serie in AUTH_SERII."
Write-LogWarning "Ori clientul chiar nu are licente, ori te-ai conectat la baza gresita."
}
if ($ExportOnly) {
Write-LogSection "Gata (ExportOnly)"
Write-Log "Fisierul de licente: $dumpFile"
Write-Log "Il importi mai tarziu cu:"
Write-Log " .\09-copy-licenses.ps1 -FromFile ""$dumpFile"" -Replace"
Close-LogFile
exit 0
}
# ------------------------------------------------------------------
# Serverul nou: verificari si backup
# ------------------------------------------------------------------
Write-LogSection "Serverul nou"
if (-not (Test-OracleConnection -OracleHome $oraHome -ServiceName $ServiceName -Password $SystemPassword)) {
throw "Nu ma pot conecta la baza noua ($ServiceName)."
}
Write-LogSuccess "Conexiune: OK"
$check = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName `
-Username "sys" -Password $SystemPassword -AsSysdba -Silent -SqlCommand @"
SET PAGESIZE 0 FEEDBACK OFF HEADING OFF VERIFY OFF
SELECT 'TABELE=' || COUNT(*) FROM dba_tables
WHERE owner = 'SYS' AND table_name IN ('AUTH_SERII','AUTH_DETALII');
SELECT 'SERII=' || COUNT(*) FROM sys.auth_serii;
EXIT
"@
if ($check -notmatch 'TABELE=2') {
throw "SYS.AUTH_SERII / SYS.AUTH_DETALII nu exista pe serverul nou. Ruleaza intai 02-create-sys-objects.ps1."
}
$seriiExistente = 0
if ($check -match 'SERII=(\d+)') { $seriiExistente = [int]$Matches[1] }
Write-Log "Licente deja prezente pe serverul nou: $seriiExistente serii"
if ($seriiExistente -gt 0 -and -not $Replace) {
Write-LogError "Serverul nou are deja $seriiExistente serii de licenta."
Write-LogError "Ruleaza din nou cu -Replace daca vrei sa le inlocuiesti cu cele de pe serverul vechi."
throw "Import oprit ca sa nu dublez licentele."
}
# Backup in acelasi format, ca sa poata fi re-importat cu -FromFile.
$backupFile = Join-Path $logDir "licente-backup-server-nou_$stamp.sql"
Write-Log "Salvez licentele curente in $backupFile ..."
$backupRaw = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName `
-Username "sys" -Password $SystemPassword -AsSysdba `
-SqlFile $exportSql -Silent
Set-Content -Path $backupFile -Value $backupRaw -Encoding ASCII
Write-LogSuccess "Backup salvat."
Write-Log "Drumul inapoi: .\09-copy-licenses.ps1 -FromFile ""$backupFile"" -Replace"
# ------------------------------------------------------------------
# Import
# ------------------------------------------------------------------
Write-LogSection "Import licente"
$inserts = @(Get-Content -Path $dumpFile | Where-Object { $_ -match '^\s*INSERT INTO SYS\.AUTH_' })
$seqBlock = @"
DECLARE
v_next NUMBER := $($info.NextVal);
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE SYS.SEQ_AUTH_SERII';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -2289 THEN RAISE; END IF; -- -2289 = secventa nu exista
END;
EXECUTE IMMEDIATE 'CREATE SEQUENCE SYS.SEQ_AUTH_SERII ' ||
'MINVALUE 1 MAXVALUE 99999999999999999 INCREMENT BY 1 ' ||
'START WITH ' || v_next || ' NOCACHE NOORDER NOCYCLE';
DBMS_OUTPUT.PUT_LINE(' SEQ_AUTH_SERII recreata, START WITH ' || v_next);
END;
/
"@
$importLines = @()
$importLines += "SET FEEDBACK ON SERVEROUTPUT ON VERIFY OFF"
$importLines += "WHENEVER SQLERROR EXIT FAILURE ROLLBACK"
$importLines += "DELETE FROM sys.auth_serii;"
$importLines += "DELETE FROM sys.auth_detalii;"
$importLines += $inserts
$importLines += "COMMIT;"
$importLines += $seqBlock
$importLines += "SET PAGESIZE 0 HEADING OFF FEEDBACK OFF"
$importLines += "SELECT 'IMPORTAT_SERII=' || COUNT(*) FROM sys.auth_serii;"
$importLines += "SELECT 'IMPORTAT_DETALII=' || COUNT(*) FROM sys.auth_detalii;"
$importLines += "EXIT"
$importFile = Join-Path $logDir "licente-import_$stamp.sql"
Set-Content -Path $importFile -Value ($importLines -join "`r`n") -Encoding ASCII
$result = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName `
-Username "sys" -Password $SystemPassword -AsSysdba -SqlFile $importFile
Write-LogDebug $result
if ($result -match 'ORA-\d{5}:') {
Write-LogError "Importul a returnat erori Oracle. Vezi $importFile."
throw "Import esuat - tranzactia a fost anulata (ROLLBACK)."
}
$importateSerii = -1
if ($result -match 'IMPORTAT_SERII=(\d+)') { $importateSerii = [int]$Matches[1] }
if ($importateSerii -ne $info.Serii) {
Write-LogError "Am importat $importateSerii serii, dar exportul avea $($info.Serii)."
throw "Numarul de licente nu se potriveste."
}
Write-LogSuccess "$importateSerii serii de licenta importate"
# ------------------------------------------------------------------
# Verificare finala: ce vede AUTH_PACK prin VAUTH_SERII
# ------------------------------------------------------------------
Write-LogSection "Licente active"
$lista = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName `
-Username "sys" -Password $SystemPassword -AsSysdba -Silent -SqlCommand @"
SET PAGESIZE 200 LINESIZE 200 FEEDBACK OFF VERIFY OFF
COLUMN nume FORMAT A25
SELECT nume, nr_util, TO_CHAR(data_val,'YYYY-MM-DD') AS data_val
FROM sys.vauth_serii
ORDER BY nume;
EXIT
"@
Write-Host $lista
Write-LogSection "Gata"
Write-Log "Backup inainte de import: $backupFile"
Write-Log "Sursa importata: $dumpFile"
Close-LogFile
exit 0
}
catch {
Write-LogError $_.Exception.Message
Write-LogDebug $_.ScriptStackTrace
Close-LogFile
exit 1
}