diff --git a/AdHoc-Backup.ps1 b/AdHoc-Backup.ps1 index 3099359..f33311d 100644 --- a/AdHoc-Backup.ps1 +++ b/AdHoc-Backup.ps1 @@ -1,11 +1,21 @@ <# - Backup_AdHoc_rclone_mail.ps1 + Backup_AdHoc_rclone_mail_fixed_v3_fix26.ps1 + Fix: + - 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. + - Ordine operazioni: upload da \out, poi MOVE/DELETE, come fix25. #> #Requires -Version 5.1 Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' + +param( + [string]$Config, + [switch]$Quiet +) + try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 } catch { @@ -16,11 +26,25 @@ try { $ScriptPath = $MyInvocation.MyCommand.Path $ScriptDir = Split-Path -Parent $ScriptPath -$ConfPath = Join-Path $ScriptDir 'backup.conf' + +# Se passato da riga di comando (es. dalla GUI), usa quello. +# Altrimenti prova prima bin\conf\backup.conf vicino allo script, poi fallback a .\backup.conf +if ($PSBoundParameters.ContainsKey('Config') -and -not [string]::IsNullOrWhiteSpace($Config)) { + $ConfPath = $Config +} else { + $cand = @( + (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 ========================================================================== + #region ============================= Config loader ================================= function New-BackupConfTemplate { @@ -187,6 +211,15 @@ Load-Config -Path $ConfPath #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 ================================ function Now-IT { (Get-Date).ToString('dd-MM-yyyy HH:mm:ss') } @@ -194,11 +227,21 @@ function Now-IT { (Get-Date).ToString('dd-MM-yyyy HH:mm:ss') } $DateStamp = (Get-Date).ToString('yyyyMMdd_HHmmss') $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]@{ Root = $BackupRoot - Bin = Join-Path $BackupRoot 'Bin' - Bin7z = Join-Path $BackupRoot 'Bin\7zip' - BinRclone = Join-Path $BackupRoot 'Bin\RClone' + Bin = $prefBin + Bin7z = $prefBin7z + BinRclone = $prefRclone Logs = Join-Path $BackupRoot 'logs' Out = Join-Path $BackupRoot 'out' SqlStage = Join-Path $BackupRoot '_sql_stage' @@ -206,15 +249,44 @@ $Paths = [ordered]@{ 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]@{ Log = Join-Path $Paths.Logs ("backup_$DateStamp.log") - SevenZipExe = Join-Path $Paths.Bin7z '7z.exe' + SevenZipExe = Find-FirstExisting $sevenExeCand SevenZip7zr = Join-Path $Paths.Bin7z '7zr.exe' - RcloneExe = Join-Path $Paths.BinRclone 'rclone.exe' + RcloneExe = Find-FirstExisting $rcExeCand RcloneZip = Join-Path $Paths.BinRclone 'rclone-current-windows-amd64.zip' - RcloneConf = Join-Path $Paths.BinRclone 'rclone.conf' + RcloneConf = $null } +# Se rclone.exe esiste già, prova prima rclone.conf nella stessa cartella +if ($Files.RcloneExe) { + $rcFolder = Split-Path -Parent $Files.RcloneExe + $rcConfSide = Join-Path $rcFolder 'rclone.conf' + $rcConfCand[0] = $rcConfSide +} +$Files.RcloneConf = Find-FirstExisting $rcConfCand +if (-not $Files.RcloneConf) { + # 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 ==========================================================================