From 111c042bef016e42d801783bfac0417082229c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doma=C5=84ski?= Date: Thu, 2 Jul 2026 07:10:19 +0000 Subject: [PATCH] Jul 2, 2026, 9:10 AM --- toolbox/s3_backup_transfer.ps1 | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/toolbox/s3_backup_transfer.ps1 b/toolbox/s3_backup_transfer.ps1 index 0cb7c86..edfef0a 100644 --- a/toolbox/s3_backup_transfer.ps1 +++ b/toolbox/s3_backup_transfer.ps1 @@ -22,8 +22,10 @@ $ErrorActionPreference = "Stop" # ======================================================== $RcloneExePath = "C:\ProgramData\chocolatey\bin\rclone.exe" # Zmień na właściwą ścieżkę do rclone.exe $BaseSourceDir = "H:\month" # Katalog główny z backupami baz danych -$S3BaseDest = "s3-backup-remote:nazwa-twojego-bucketa/SQLBackups/$($env:COMPUTERNAME)" +$RcloneRemote = "s3-backup-remote:" # Nazwa skonfigurowanego remote (z rclone config) +$S3BasePath = "backup-s3-ovh-ch/Switzerland_SQLServer_DB_Backups/Month" $LogDir = "C:\DBAdmin\logs" +$CurrentYear = (Get-Date).Year.ToString() # Upewnij się, że katalog logów istnieje if (-not (Test-Path $LogDir)) { @@ -89,10 +91,24 @@ $GlobalExitCode = 0 foreach ($folder in $FoldersToProcess) { $SourceDir = Join-Path $BaseSourceDir $folder - $S3Destination = "$S3BaseDest/$folder" - $LogFile = Join-Path $LogDir "rclone_backup_$($folder)_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" + $S3DestinationParent = "$RcloneRemote$S3BasePath/$folder" + $S3Destination = "$S3DestinationParent/$CurrentYear" - Write-Host "`n[$(Get-Date -Format 'HH:mm:ss')] Rozpoczynam transfer: $folder -> S3..." -ForegroundColor Yellow + Write-Host "`n[$(Get-Date -Format 'HH:mm:ss')] Przetwarzanie: $folder ..." -ForegroundColor Yellow + Write-Host " Sprawdzanie istnienia docelowego folderu na S3: $S3DestinationParent/$CurrentYear/" -ForegroundColor Gray + + # Walidacja obecności folderu (roku) po stronie S3 za pomocą rclone lsf (wypisuje tylko foldery) + $s3Dirs = & $RcloneExePath lsf --dirs-only "$S3DestinationParent/" 2>$null + if ($s3Dirs -notcontains "$CurrentYear/") { + $errMsg = "Brak wymaganego folderu '$CurrentYear/' w lokalizacji '$S3DestinationParent/' na S3. Pomijam transfer." + Write-Host " Błąd: $errMsg" -ForegroundColor Red + Write-AppEventLog -Type Warning -Id 1002 -Message $errMsg + $GlobalExitCode = 1 + continue + } + + Write-Host " Folder '$CurrentYear/' znaleziony. Rozpoczynam transfer danych..." -ForegroundColor Green + $LogFile = Join-Path $LogDir "rclone_backup_$($folder)_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" $RcloneArgs = @( "move", $SourceDir, $S3Destination, @@ -122,6 +138,6 @@ foreach ($folder in $FoldersToProcess) { Write-Host "`nProces zakończony." if ($GlobalExitCode -ne 0) { - Write-Host "Wystąpiły błędy podczas transferu niektórych folderów. Sprawdź logi." -ForegroundColor Red + Write-Host "Wystąpiły błędy podczas transferu niektórych folderów lub brakowało docelowych katalogów na S3. Sprawdź logi." -ForegroundColor Red } exit $GlobalExitCode