Update AdHoc-Backup.ps1
This commit is contained in:
parent
2966fdaefb
commit
64aed8c53d
112
AdHoc-Backup.ps1
112
AdHoc-Backup.ps1
@ -1,5 +1,5 @@
|
|||||||
<#
|
<#
|
||||||
Backup-AdHoc.ps1
|
Backup_AdHoc_rclone_mail_fixed_v3_fix26.ps1
|
||||||
Fix:
|
Fix:
|
||||||
- PowerShell è case-insensitive: le variabili locali "$files" sovrascrivevano "$Files" (hashtable globale).
|
- PowerShell è case-insensitive: le variabili locali "$files" sovrascrivevano "$Files" (hashtable globale).
|
||||||
Rinominati i locali in "$outFiles" e, in Write-Log, uso esplicito di $script:Files.Log per evitare collisioni.
|
Rinominati i locali in "$outFiles" e, in Write-Log, uso esplicito di $script:Files.Log per evitare collisioni.
|
||||||
@ -7,38 +7,44 @@
|
|||||||
#>
|
#>
|
||||||
|
|
||||||
#Requires -Version 5.1
|
#Requires -Version 5.1
|
||||||
Set-StrictMode -Version Latest
|
|
||||||
$ErrorActionPreference = 'Stop'
|
|
||||||
|
|
||||||
|
|
||||||
param(
|
param(
|
||||||
[string]$Config,
|
[string]$Config,
|
||||||
[switch]$Quiet
|
[switch]$Quiet
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Set-StrictMode -Version Latest
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
|
||||||
} catch {
|
} catch {
|
||||||
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {}
|
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region ============================= Percorsi script & conf =========================
|
#region ============================= Percorsi script & conf =========================
|
||||||
|
|
||||||
$ScriptPath = $MyInvocation.MyCommand.Path
|
$ScriptPath = $MyInvocation.MyCommand.Path
|
||||||
$ScriptDir = Split-Path -Parent $ScriptPath
|
$ScriptDir = Split-Path -Parent $ScriptPath
|
||||||
|
|
||||||
# Se passato da riga di comando (es. dalla GUI), usa quello.
|
# Layout dell'installer GUI (se presente): .\bin\{RClone,7Zip,conf}
|
||||||
# Altrimenti prova prima bin\conf\backup.conf vicino allo script, poi fallback a .\backup.conf
|
$InstallerBinRoot = Join-Path $ScriptDir 'bin'
|
||||||
|
$InstallerConfDir = Join-Path $InstallerBinRoot 'conf'
|
||||||
|
$InstallerRcloneExe = Join-Path $InstallerBinRoot 'RClone\rclone.exe'
|
||||||
|
$Installer7zExe = Join-Path $InstallerBinRoot '7Zip\7z.exe'
|
||||||
|
$Installer7zrExe = Join-Path $InstallerBinRoot '7Zip\7zr.exe'
|
||||||
|
$InstallerRcloneConf= Join-Path $InstallerConfDir 'rclone.conf'
|
||||||
|
|
||||||
|
# Se passato -Config usa quello, altrimenti priorità a .\bin\conf\backup.conf, poi .\backup.conf
|
||||||
if ($PSBoundParameters.ContainsKey('Config') -and -not [string]::IsNullOrWhiteSpace($Config)) {
|
if ($PSBoundParameters.ContainsKey('Config') -and -not [string]::IsNullOrWhiteSpace($Config)) {
|
||||||
$ConfPath = $Config
|
try {
|
||||||
|
$ConfPath = (Resolve-Path -LiteralPath $Config).Path
|
||||||
|
} catch {
|
||||||
|
throw "Il file di configurazione specificato con -Config non esiste: $Config"
|
||||||
|
}
|
||||||
|
} elseif (Test-Path -LiteralPath (Join-Path $InstallerConfDir 'backup.conf')) {
|
||||||
|
$ConfPath = Join-Path $InstallerConfDir 'backup.conf'
|
||||||
} else {
|
} else {
|
||||||
$cand = @(
|
$ConfPath = Join-Path $ScriptDir 'backup.conf'
|
||||||
(Join-Path $ScriptDir 'bin\conf\backup.conf'),
|
|
||||||
(Join-Path $ScriptDir 'backup.conf')
|
|
||||||
)
|
|
||||||
$found = $null
|
|
||||||
foreach ($c in $cand) { if (Test-Path -LiteralPath $c) { $found = $c; break } }
|
|
||||||
if ($found) { $ConfPath = $found } else { $ConfPath = Join-Path $ScriptDir 'backup.conf' }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion ==========================================================================
|
#endregion ==========================================================================
|
||||||
@ -211,37 +217,18 @@ Load-Config -Path $ConfPath
|
|||||||
#endregion ==========================================================================
|
#endregion ==========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Find-FirstExisting {
|
|
||||||
param([Parameter(Mandatory)][string[]]$Candidates)
|
|
||||||
foreach ($p in $Candidates) {
|
|
||||||
if (-not [string]::IsNullOrWhiteSpace($p) -and (Test-Path -LiteralPath $p)) { return $p }
|
|
||||||
}
|
|
||||||
return $null
|
|
||||||
}
|
|
||||||
|
|
||||||
#region ============================= Paths, Globals ================================
|
#region ============================= Paths, Globals ================================
|
||||||
|
|
||||||
function Now-IT { (Get-Date).ToString('dd-MM-yyyy HH:mm:ss') }
|
function Now-IT { (Get-Date).ToString('dd-MM-yyyy HH:mm:ss') }
|
||||||
|
|
||||||
$DateStamp = (Get-Date).ToString('yyyyMMdd_HHmmss')
|
$DateStamp = (Get-Date).ToString('dd-MM-yyyy_HHmmss')
|
||||||
$HostName = $env:COMPUTERNAME
|
$HostName = $env:COMPUTERNAME
|
||||||
|
|
||||||
|
|
||||||
$InstallBin = Join-Path $ScriptDir 'bin'
|
|
||||||
|
|
||||||
# Scegli il layout preferendo quello dell'Installer (.\bin\*) se presente
|
|
||||||
$prefBin = if (Test-Path -LiteralPath $InstallBin) { $InstallBin } else { Join-Path $BackupRoot 'Bin' }
|
|
||||||
$prefBin7z = if (Test-Path -LiteralPath (Join-Path $InstallBin '7Zip')) { Join-Path $InstallBin '7Zip' } else { Join-Path $BackupRoot 'Bin\7Zip' }
|
|
||||||
$prefRclone = if (Test-Path -LiteralPath (Join-Path $InstallBin 'RClone')) { Join-Path $InstallBin 'RClone' } else {
|
|
||||||
if (Test-Path -LiteralPath (Join-Path $BackupRoot 'Bin\RClone')) { Join-Path $BackupRoot 'Bin\RClone' } else { Join-Path $BackupRoot 'RClone' }
|
|
||||||
}
|
|
||||||
|
|
||||||
$Paths = [ordered]@{
|
$Paths = [ordered]@{
|
||||||
Root = $BackupRoot
|
Root = $BackupRoot
|
||||||
Bin = $prefBin
|
Bin = Join-Path $BackupRoot 'Bin'
|
||||||
Bin7z = $prefBin7z
|
Bin7z = Join-Path $BackupRoot 'Bin\7zip'
|
||||||
BinRclone = $prefRclone
|
BinRclone = Join-Path $BackupRoot 'Bin\RClone'
|
||||||
Logs = Join-Path $BackupRoot 'logs'
|
Logs = Join-Path $BackupRoot 'logs'
|
||||||
Out = Join-Path $BackupRoot 'out'
|
Out = Join-Path $BackupRoot 'out'
|
||||||
SqlStage = Join-Path $BackupRoot '_sql_stage'
|
SqlStage = Join-Path $BackupRoot '_sql_stage'
|
||||||
@ -249,45 +236,25 @@ $Paths = [ordered]@{
|
|||||||
StoreDb = Join-Path $BackupRoot 'Databases'
|
StoreDb = Join-Path $BackupRoot 'Databases'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Candidati per rclone.exe e rclone.conf
|
|
||||||
$rcExeCand = @(
|
|
||||||
(Join-Path $Paths.BinRclone 'rclone.exe'),
|
|
||||||
(Join-Path (Join-Path $BackupRoot 'RClone') 'rclone.exe')
|
|
||||||
)
|
|
||||||
$rcConfCand = @(
|
|
||||||
$null, # 1) accanto a rclone.exe (se già presente)
|
|
||||||
(Join-Path $InstallBin 'conf\rclone.conf'), # 2) conf vicino allo script (Installer GUI)
|
|
||||||
(Join-Path $Paths.Bin 'conf\rclone.conf'), # 3) conf sotto BackupRoot\Bin
|
|
||||||
(Join-Path (Join-Path $env:APPDATA 'rclone') 'rclone.conf') # 4) profilo utente standard
|
|
||||||
)
|
|
||||||
|
|
||||||
$sevenExeCand = @(
|
|
||||||
(Join-Path $Paths.Bin7z '7z.exe'),
|
|
||||||
(Join-Path $Paths.Bin7z '7zr.exe')
|
|
||||||
)
|
|
||||||
|
|
||||||
$Files = [ordered]@{
|
$Files = [ordered]@{
|
||||||
Log = Join-Path $Paths.Logs ("backup_$DateStamp.log")
|
Log = Join-Path $Paths.Logs ("backup_$DateStamp.log")
|
||||||
SevenZipExe = Find-FirstExisting $sevenExeCand
|
SevenZipExe = Join-Path $Paths.Bin7z '7z.exe'
|
||||||
SevenZip7zr = Join-Path $Paths.Bin7z '7zr.exe'
|
SevenZip7zr = Join-Path $Paths.Bin7z '7zr.exe'
|
||||||
RcloneExe = Find-FirstExisting $rcExeCand
|
RcloneExe = Join-Path $Paths.BinRclone 'rclone.exe'
|
||||||
RcloneZip = Join-Path $Paths.BinRclone 'rclone-current-windows-amd64.zip'
|
RcloneZip = Join-Path $Paths.BinRclone 'rclone-current-windows-amd64.zip'
|
||||||
RcloneConf = $null
|
RcloneConf = Join-Path $Paths.BinRclone 'rclone.conf'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Se rclone.exe esiste già, prova prima rclone.conf nella stessa cartella
|
#endregion ==========================================================================# Preferisci gli strumenti nel layout dell'installer GUI, se presenti
|
||||||
if ($Files.RcloneExe) {
|
if (Test-Path -LiteralPath $Installer7zExe) { $Files.SevenZipExe = $Installer7zExe }
|
||||||
$rcFolder = Split-Path -Parent $Files.RcloneExe
|
elseif (Test-Path -LiteralPath $Installer7zrExe) { $Files.SevenZip7zr = $Installer7zrExe }
|
||||||
$rcConfSide = Join-Path $rcFolder 'rclone.conf'
|
|
||||||
$rcConfCand[0] = $rcConfSide
|
if (Test-Path -LiteralPath $InstallerRcloneExe) { $Files.RcloneExe = $InstallerRcloneExe }
|
||||||
}
|
|
||||||
$Files.RcloneConf = Find-FirstExisting $rcConfCand
|
# rclone.conf: prima quello in .\bin\conf, altrimenti quello sotto BackupRoot\Bin
|
||||||
if (-not $Files.RcloneConf) {
|
if (Test-Path -LiteralPath $InstallerRcloneConf) { $Files.RcloneConf = $InstallerRcloneConf }
|
||||||
# Fallback: tienilo fianco a rclone.exe
|
|
||||||
if (-not $Files.RcloneExe) { $Files.RcloneExe = Join-Path $Paths.BinRclone 'rclone.exe' }
|
|
||||||
$Files.RcloneConf = Join-Path (Split-Path -Parent $Files.RcloneExe) 'rclone.conf'
|
|
||||||
}
|
|
||||||
#endregion ==========================================================================
|
|
||||||
|
|
||||||
|
|
||||||
#region ============================= Utils & Logging ===============================
|
#region ============================= Utils & Logging ===============================
|
||||||
@ -305,7 +272,8 @@ function Write-Log {
|
|||||||
[string]$Message
|
[string]$Message
|
||||||
)
|
)
|
||||||
$line = "[{0}] [{1}] {2}" -f (Now-IT), $Level, $Message
|
$line = "[{0}] [{1}] {2}" -f (Now-IT), $Level, $Message
|
||||||
Write-Host $line
|
if (-not $Quiet) { Write-Host $line }
|
||||||
|
|
||||||
# Usa SEMPRE lo scope di script per il file log
|
# Usa SEMPRE lo scope di script per il file log
|
||||||
Add-Content -LiteralPath $script:Files.Log -Value $line
|
Add-Content -LiteralPath $script:Files.Log -Value $line
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user