Creato script di copia
This commit is contained in:
parent
9edbd0a91d
commit
22f6432535
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
logs/
|
logs/
|
||||||
stored*/
|
stored*/
|
||||||
|
extract/
|
22
README.md
22
README.md
@ -4,19 +4,24 @@ Questo progetto contiene gli script e le procedure necessarie per la creazione e
|
|||||||
|
|
||||||
## Struttura del Progetto
|
## Struttura del Progetto
|
||||||
|
|
||||||
- `/sql/` - Contiene gli script SQL per la creazione delle tabelle e delle stored procedures
|
- `/sql/` - Script SQL per la creazione del database
|
||||||
- `dbo.LogN.sql` - Script per la gestione dei log delle applicazioni
|
- `/script/` - Script PowerShell per l'automazione
|
||||||
|
|
||||||
- `/script/` - Contiene gli script PowerShell per l'automazione
|
|
||||||
- `creadb.ps1` - Script PowerShell per la creazione del database
|
|
||||||
|
|
||||||
- `/logs/` - Directory per i file di log (non tracciata in Git)
|
- `/logs/` - Directory per i file di log (non tracciata in Git)
|
||||||
|
- `/extract/` - Directory per i file CSV di esportazione (non tracciata in Git)
|
||||||
|
|
||||||
## Funzionalità
|
## Funzionalità
|
||||||
|
|
||||||
- Creazione automatizzata del database
|
- Creazione automatizzata del database
|
||||||
- Sistema di logging centralizzato
|
- Sistema di logging centralizzato
|
||||||
- Gestione delle stampe centralizzate
|
- Gestione delle stampe centralizzate
|
||||||
|
- Copia tabelle tra ambienti diversi
|
||||||
|
|
||||||
|
## Script Disponibili
|
||||||
|
|
||||||
|
- `creadb.ps1` - Crea il database e le tabelle
|
||||||
|
- `esporta.ps1` - Esporta una tabella in formato CSV
|
||||||
|
- `importa.ps1` - Importa una tabella da un file CSV
|
||||||
|
- `copia.ps1` - Copia una tabella da un ambiente all'altro (combina esporta e importa)
|
||||||
|
|
||||||
## Requisiti
|
## Requisiti
|
||||||
|
|
||||||
@ -26,9 +31,10 @@ Questo progetto contiene gli script e le procedure necessarie per la creazione e
|
|||||||
|
|
||||||
## Note Tecniche
|
## Note Tecniche
|
||||||
|
|
||||||
- I timestamp nel database sono gestiti utilizzando la funzione `CONVERT(datetime, ..., 121)` per garantire la compatibilità
|
|
||||||
- I log delle operazioni vengono salvati nella directory `/logs/`
|
- I log delle operazioni vengono salvati nella directory `/logs/`
|
||||||
|
- I file CSV di esportazione vengono salvati nella directory `/extract/`
|
||||||
|
- La configurazione degli ambienti (server, database, credenziali) è centralizzata in `conf.ps1`
|
||||||
|
|
||||||
## Ultimo Aggiornamento
|
## Ultimo Aggiornamento
|
||||||
|
|
||||||
Data: 23 Maggio 2025
|
Data: 6 Giugno 2025
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
# Configurazione database
|
# Configurazione database
|
||||||
$config = @{
|
$config = @{
|
||||||
# Credenziali database
|
# Credenziali database
|
||||||
DbUser = "sa"
|
DbUser = "F701264"
|
||||||
DbPassword = "_p1sap1a"
|
DbPassword = "contrsei"
|
||||||
# DbHost = "DATABASE_PDC_LOCALE"
|
# DbHostOutput = "DATABASE_PDC_LOCALE"
|
||||||
DbHost = "testbes.armundia.com"
|
# DbHostOutput = "testbes.armundia.com"
|
||||||
|
DbHostInput = "bfdskrepsei01c.sysfideuram.sysbancafideuram.it"
|
||||||
|
DbHostOutput = "testbes.armundia.com"
|
||||||
DbName = "C6StampeCentralizzate"
|
DbName = "C6StampeCentralizzate"
|
||||||
|
|
||||||
# Directory di log
|
# Directory di log
|
||||||
@ -14,13 +16,14 @@ $config = @{
|
|||||||
# Esporta le variabili come variabili d'ambiente
|
# Esporta le variabili come variabili d'ambiente
|
||||||
$env:dbUser = $config.DbUser
|
$env:dbUser = $config.DbUser
|
||||||
$env:dbPassword = $config.DbPassword
|
$env:dbPassword = $config.DbPassword
|
||||||
$env:dbHost = $config.DbHost
|
$env:dbHostInput = $config.DbHostInput
|
||||||
|
$env:dbHostOutput = $config.DbHostOutput
|
||||||
$env:dbName = $config.DbName
|
$env:dbName = $config.DbName
|
||||||
$env:logDir = $config.LogDir
|
$env:logDir = $config.LogDir
|
||||||
|
|
||||||
# Funzione per ottenere la connection string
|
# Funzione per ottenere la connection string
|
||||||
function Get-DatabaseConnectionString {
|
function Get-DatabaseConnectionString {
|
||||||
return "Server=$($config.DbHost);Database=$($config.DbName);User Id=$($config.DbUser);Password=$($config.DbPassword);"
|
return "Server=$($config.DbHostOutput);Database=$($config.DbName);User Id=$($config.DbUser);Password=$($config.DbPassword);"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Crea directory di log se non esiste
|
# Crea directory di log se non esiste
|
||||||
|
32
script/copia.ps1
Normal file
32
script/copia.ps1
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
param(
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$TableName
|
||||||
|
)
|
||||||
|
|
||||||
|
# Importa configurazione
|
||||||
|
. "$PSScriptRoot\conf.ps1"
|
||||||
|
|
||||||
|
try {
|
||||||
|
Write-Host "Inizio copia della tabella $TableName..."
|
||||||
|
Write-Host "Step 1: Esportazione dati dal server di origine ($env:dbHostInput)..."
|
||||||
|
|
||||||
|
# Esegui esportazione
|
||||||
|
& "$PSScriptRoot\esporta.ps1" -TableName $TableName
|
||||||
|
|
||||||
|
# Verifica che il file esista
|
||||||
|
$exportFile = Join-Path $PSScriptRoot "..\extract\$TableName.csv"
|
||||||
|
if (-not (Test-Path $exportFile)) {
|
||||||
|
throw "Errore: file di esportazione non trovato"
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "`nStep 2: Importazione dati nel server di destinazione ($env:dbHostOutput)..."
|
||||||
|
|
||||||
|
# Esegui importazione
|
||||||
|
& "$PSScriptRoot\importa.ps1" -TableName $TableName
|
||||||
|
|
||||||
|
Write-Host "`nCopia completata con successo!"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "Errore durante la copia dei dati: $_"
|
||||||
|
exit 1
|
||||||
|
}
|
@ -43,7 +43,7 @@ foreach ($fileName in $sqlFiles) {
|
|||||||
while (-not $success -and $retryCount -lt $maxRetries) {
|
while (-not $success -and $retryCount -lt $maxRetries) {
|
||||||
# try {
|
# try {
|
||||||
# Execute the SQL file using sqlcmd
|
# Execute the SQL file using sqlcmd
|
||||||
sqlcmd -S $env:dbHost -U $env:dbUser -P $env:dbPassword -i $fileSql >$fileLog 2>&1
|
sqlcmd -S $env:dbHostOutput -U $env:dbUser -P $env:dbPassword -i $fileSql >$fileLog 2>&1
|
||||||
$esitoComando = $LASTEXITCODE
|
$esitoComando = $LASTEXITCODE
|
||||||
# $risultato = $risultato.TrimEnd("`r", "`n")
|
# $risultato = $risultato.TrimEnd("`r", "`n")
|
||||||
"logSqlApp: $logSqlApp esitoComando: $esitoComando" | Out-File -FilePath $logFile -Append
|
"logSqlApp: $logSqlApp esitoComando: $esitoComando" | Out-File -FilePath $logFile -Append
|
||||||
|
57
script/esporta.ps1
Normal file
57
script/esporta.ps1
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
param(
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$TableName
|
||||||
|
)
|
||||||
|
|
||||||
|
function Install-RequiredModules {
|
||||||
|
$requiredModule = "SqlServer"
|
||||||
|
|
||||||
|
if (-not (Get-Module -ListAvailable -Name $requiredModule)) {
|
||||||
|
Write-Host "Installazione modulo $requiredModule..."
|
||||||
|
try {
|
||||||
|
Install-Module -Name $requiredModule -Force -AllowClobber -Scope CurrentUser
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "Errore durante l'installazione del modulo $requiredModule. Eseguire PowerShell come amministratore e riprovare."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verifica e installa moduli necessari
|
||||||
|
Install-RequiredModules
|
||||||
|
|
||||||
|
# Importa configurazione
|
||||||
|
. "$PSScriptRoot\conf.ps1"
|
||||||
|
|
||||||
|
$OutputFile = Join-Path $PSScriptRoot "..\extract\$TableName.csv"
|
||||||
|
|
||||||
|
$Query = "SELECT * FROM $TableName"
|
||||||
|
|
||||||
|
try {
|
||||||
|
# Import SQL Server module if not already loaded
|
||||||
|
if (-not (Get-Module -Name SqlServer)) {
|
||||||
|
Import-Module SqlServer
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Connessione a $env:dbHostInput, database $env:dbName..."
|
||||||
|
|
||||||
|
# Esegui la query e salva i risultati
|
||||||
|
$result = Invoke-Sqlcmd -ServerInstance $env:dbHostInput `
|
||||||
|
-Database $env:dbName `
|
||||||
|
-Username $env:dbUser `
|
||||||
|
-Password $env:dbPassword `
|
||||||
|
-Query $Query
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
$result | Export-Csv -Path $OutputFile -NoTypeInformation -Delimiter ";" -Encoding UTF8
|
||||||
|
} else {
|
||||||
|
throw "Nessun dato trovato nella tabella $TableName"
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Dati esportati con successo in: $OutputFile"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "Errore durante l'estrazione dei dati: $_"
|
||||||
|
exit 1
|
||||||
|
}
|
@ -26,7 +26,7 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Esegui la query per ottenere le stored procedures
|
# Esegui la query per ottenere le stored procedures
|
||||||
$storedProcedures = Invoke-Sqlcmd -ServerInstance $env:dbHost -Database $env:dbName -Query $query -Username $env:dbUser -Password $env:dbPassword
|
$storedProcedures = Invoke-Sqlcmd -ServerInstance $env:dbHostInput -Database $env:dbName -Query $query -Username $env:dbUser -Password $env:dbPassword
|
||||||
|
|
||||||
# Per ogni stored procedure trovata
|
# Per ogni stored procedure trovata
|
||||||
foreach ($sp in $storedProcedures) {
|
foreach ($sp in $storedProcedures) {
|
||||||
|
83
script/importa.ps1
Normal file
83
script/importa.ps1
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
param(
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$TableName
|
||||||
|
)
|
||||||
|
|
||||||
|
function Install-RequiredModules {
|
||||||
|
$requiredModule = "SqlServer"
|
||||||
|
|
||||||
|
if (-not (Get-Module -ListAvailable -Name $requiredModule)) {
|
||||||
|
Write-Host "Installazione modulo $requiredModule..."
|
||||||
|
try {
|
||||||
|
Install-Module -Name $requiredModule -Force -AllowClobber -Scope CurrentUser
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "Errore durante l'installazione del modulo $requiredModule. Eseguire PowerShell come amministratore e riprovare."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verifica e installa moduli necessari
|
||||||
|
Install-RequiredModules
|
||||||
|
|
||||||
|
# Importa configurazione
|
||||||
|
. "$PSScriptRoot\conf.ps1"
|
||||||
|
|
||||||
|
$InputFile = Join-Path $PSScriptRoot "..\extract\$TableName.csv"
|
||||||
|
|
||||||
|
if (-not (Test-Path $InputFile)) {
|
||||||
|
Write-Error "File non trovato: $InputFile"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
# Import SQL Server module if not already loaded
|
||||||
|
if (-not (Get-Module -Name SqlServer)) {
|
||||||
|
Import-Module SqlServer
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Lettura dati da: $InputFile"
|
||||||
|
$data = Import-Csv -Path $InputFile -Delimiter ";"
|
||||||
|
|
||||||
|
if ($data.Count -eq 0) {
|
||||||
|
Write-Error "Il file CSV è vuoto"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Connessione a $env:dbHostOutput, database $env:dbName..."
|
||||||
|
|
||||||
|
# Get column names from CSV
|
||||||
|
$columns = $data[0].PSObject.Properties.Name
|
||||||
|
$columnList = $columns -join ","
|
||||||
|
|
||||||
|
# Process each row
|
||||||
|
foreach ($row in $data) {
|
||||||
|
# Create value list for this row
|
||||||
|
$values = @()
|
||||||
|
foreach ($column in $columns) {
|
||||||
|
$value = $row.$column
|
||||||
|
if ([string]::IsNullOrEmpty($value)) {
|
||||||
|
$values += "NULL"
|
||||||
|
} else {
|
||||||
|
$values += "'" + $value.Replace("'", "''") + "'"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create and execute insert query for this row
|
||||||
|
$valuesList = $values -join ","
|
||||||
|
$query = "INSERT INTO $TableName ($columnList) VALUES ($valuesList)"
|
||||||
|
|
||||||
|
# Execute insert for each row
|
||||||
|
$connectionString = "Server=$env:dbHostOutput;Database=$env:dbName;User Id=$env:dbUser;Password=$env:dbPassword;Encrypt=False"
|
||||||
|
|
||||||
|
Invoke-Sqlcmd -ConnectionString $connectionString `
|
||||||
|
-Query $query
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Dati importati con successo nella tabella: $TableName"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "Errore durante l'importazione dei dati: $_"
|
||||||
|
exit 1
|
||||||
|
}
|
1452
script/pulisceDB.ps1
Normal file
1452
script/pulisceDB.ps1
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user