Jul 2, 2026, 8:04 AM
This commit is contained in:
@@ -5,18 +5,25 @@
|
||||
Używa operacji 'rclone move', aby najpierw bezpiecznie skopiować pliki na S3,
|
||||
zweryfikować je, a następnie usunąć z lokalnego dysku serwera.
|
||||
W przypadku błędu raportuje zdarzenie do Windows Event Log.
|
||||
.PARAMETER FolderName
|
||||
Nazwa folderu z backupami do przesłania. Może to być nazwa konkretnego folderu
|
||||
(np. 'BISW12PFASTA02') lub słowo 'all', aby przesłać wszystkie foldery.
|
||||
Jeśli parametr nie zostanie podany, skrypt wyświetli listę dostępnych folderów.
|
||||
#>
|
||||
|
||||
param(
|
||||
[string]$FolderName
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# ========================================================
|
||||
# KONFIGURACJA
|
||||
# ========================================================
|
||||
$RcloneExePath = "C:\ProgramData\chocolatey\bin\rclone.exe" # Zmień na właściwą ścieżkę do rclone.exe
|
||||
$SourceDir = "C:\SQLBackups" # Katalog główny z backupami baz danych
|
||||
$S3Destination = "s3-backup-remote:nazwa-twojego-bucketa/SQLBackups/$(env:COMPUTERNAME)"
|
||||
$BaseSourceDir = "H:\month" # Katalog główny z backupami baz danych
|
||||
$S3BaseDest = "s3-backup-remote:nazwa-twojego-bucketa/SQLBackups/$(env:COMPUTERNAME)"
|
||||
$LogDir = "C:\DBAdmin\logs"
|
||||
$LogFile = Join-Path $LogDir "rclone_backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
|
||||
|
||||
# Upewnij się, że katalog logów istnieje
|
||||
if (-not (Test-Path $LogDir)) {
|
||||
@@ -24,54 +31,97 @@ if (-not (Test-Path $LogDir)) {
|
||||
}
|
||||
|
||||
# ========================================================
|
||||
# URUCHOMIENIE TRANSFERU
|
||||
# ========================================================
|
||||
Write-Host "Rozpoczynam proces bezpiecznego przenoszenia backupów z $SourceDir do $S3Destination..."
|
||||
|
||||
# Parametry zapewniające niezawodność:
|
||||
# move - Przenosi (kopiuje, weryfikuje, kasuje oryginał)
|
||||
# --checksum - Bezwzględnie weryfikuje sumy kontrolne po skopiowaniu
|
||||
# --retries 5 - Przy błędzie sieci ponawia do 5 razy
|
||||
# --retries-sleep 15s - Oczekiwanie między powtórzeniami
|
||||
# --delete-empty-src-dirs - Usuwa foldery, z których opróżniono wszystkie backupy
|
||||
$RcloneArgs = @(
|
||||
"move", $SourceDir, $S3Destination,
|
||||
"--checksum",
|
||||
"--retries", "5",
|
||||
"--retries-sleep", "15s",
|
||||
"--delete-empty-src-dirs",
|
||||
"--log-file=$LogFile",
|
||||
"--log-level=INFO"
|
||||
)
|
||||
|
||||
# Uruchomienie rclone
|
||||
& $RcloneExePath $RcloneArgs
|
||||
|
||||
$RcloneExitCode = $LASTEXITCODE
|
||||
|
||||
# ========================================================
|
||||
# WERYFIKACJA I ALERTOWANIE
|
||||
# WALIDACJA PARAMETRÓW I WYBÓR FOLDERÓW
|
||||
# ========================================================
|
||||
|
||||
# Rejestracja źródła w EventLog, jeśli nie istnieje (wymaga praw admina przy pierwszym uruchomieniu)
|
||||
$EventSource = "SQLBackupToS3"
|
||||
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
||||
try {
|
||||
New-EventLog -LogName Application -Source $EventSource
|
||||
} catch {
|
||||
Write-Warning "Nie udało się utworzyć źródła zdarzeń. Prawdopodobnie brak uprawnień administratora."
|
||||
if (-not (Test-Path $BaseSourceDir)) {
|
||||
Write-Host "BŁĄD: Katalog bazowy $BaseSourceDir nie istnieje." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
$AvailableFolders = Get-ChildItem -Path $BaseSourceDir -Directory | Select-Object -ExpandProperty Name
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($FolderName)) {
|
||||
Write-Host "Nie podano parametru z nazwą folderu." -ForegroundColor Yellow
|
||||
Write-Host "Dostępne opcje to:" -ForegroundColor Cyan
|
||||
Write-Host " all (aby przenieść wszystkie poniższe foldery)" -ForegroundColor Green
|
||||
foreach ($folder in $AvailableFolders) {
|
||||
Write-Host " $folder"
|
||||
}
|
||||
Write-Host "`nPrzykład użycia:"
|
||||
Write-Host " .\s3_backup_transfer.ps1 -FolderName BISW12PSQLDB01"
|
||||
Write-Host " .\s3_backup_transfer.ps1 -FolderName all"
|
||||
exit 0
|
||||
}
|
||||
|
||||
$FoldersToProcess = @()
|
||||
|
||||
if ($FolderName -eq "all") {
|
||||
$FoldersToProcess = $AvailableFolders
|
||||
Write-Host "Wybrano przeniesienie WSZYSTKICH folderów z $BaseSourceDir." -ForegroundColor Cyan
|
||||
} else {
|
||||
if ($AvailableFolders -contains $FolderName) {
|
||||
$FoldersToProcess = @($FolderName)
|
||||
Write-Host "Wybrano przeniesienie folderu: $FolderName." -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Host "BŁĄD: Folder '$FolderName' nie istnieje w $BaseSourceDir." -ForegroundColor Red
|
||||
Write-Host "Dostępne foldery:"
|
||||
$AvailableFolders | ForEach-Object { Write-Host " $_" }
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
if ($RcloneExitCode -eq 0) {
|
||||
$successMsg = "Pomyślnie przeniesiono backupy SQL na S3. Log: $LogFile"
|
||||
Write-Host $successMsg -ForegroundColor Green
|
||||
try { Write-EventLog -LogName Application -Source $EventSource -EntryType Information -EventId 1000 -Message $successMsg } catch {}
|
||||
} else {
|
||||
$errorMsg = "BŁĄD KRYTYCZNY: Nie udało się przenieść wszystkich backupów SQL na S3. Kod wyjścia rclone: $RcloneExitCode. Szczegóły sprawdź w logu: $LogFile"
|
||||
Write-Host $errorMsg -ForegroundColor Red
|
||||
try { Write-EventLog -LogName Application -Source $EventSource -EntryType Error -EventId 1001 -Message $errorMsg } catch {}
|
||||
|
||||
# [Opcjonalnie] Tutaj możesz dodać skrypt wysyłający maila lub webhook (Teams/Slack):
|
||||
# Invoke-RestMethod -Uri "WEBHOOK_URL" -Method Post -Body (@{text=$errorMsg} | ConvertTo-Json)
|
||||
# Funkcja pomocnicza do logowania zdarzeń
|
||||
function Write-AppEventLog {
|
||||
param([string]$Type, [int]$Id, [string]$Message)
|
||||
$EventSource = "SQLBackupToS3"
|
||||
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
||||
try { New-EventLog -LogName Application -Source $EventSource } catch { }
|
||||
}
|
||||
try { Write-EventLog -LogName Application -Source $EventSource -EntryType $Type -EventId $Id -Message $Message } catch { }
|
||||
}
|
||||
|
||||
# ========================================================
|
||||
# URUCHOMIENIE TRANSFERU DLA WYBRANYCH FOLDERÓW
|
||||
# ========================================================
|
||||
|
||||
$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"
|
||||
|
||||
Write-Host "`n[$(Get-Date -Format 'HH:mm:ss')] Rozpoczynam transfer: $folder -> S3..." -ForegroundColor Yellow
|
||||
|
||||
$RcloneArgs = @(
|
||||
"move", $SourceDir, $S3Destination,
|
||||
"--checksum",
|
||||
"--retries", "5",
|
||||
"--retries-sleep", "15s",
|
||||
"--delete-empty-src-dirs",
|
||||
"--log-file=$LogFile",
|
||||
"--log-level=INFO"
|
||||
)
|
||||
|
||||
# Uruchomienie rclone
|
||||
& $RcloneExePath $RcloneArgs
|
||||
$RcloneExitCode = $LASTEXITCODE
|
||||
|
||||
if ($RcloneExitCode -eq 0) {
|
||||
$successMsg = "Pomyślnie przeniesiono folder $folder na S3. Log: $LogFile"
|
||||
Write-Host " Sukces: $folder" -ForegroundColor Green
|
||||
Write-AppEventLog -Type Information -Id 1000 -Message $successMsg
|
||||
} else {
|
||||
$errorMsg = "Błąd przenoszenia folderu $folder. Kod wyjścia rclone: $RcloneExitCode. Log: $LogFile"
|
||||
Write-Host " Błąd: $folder (Kod $RcloneExitCode)" -ForegroundColor Red
|
||||
Write-AppEventLog -Type Error -Id 1001 -Message $errorMsg
|
||||
$GlobalExitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
exit $GlobalExitCode
|
||||
|
||||
Reference in New Issue
Block a user