83 lines
3.7 KiB
PowerShell
83 lines
3.7 KiB
PowerShell
# Sriphat Data Platform - Daily Backup Script (Windows)
|
|
# ตั้งเวลารันใน Task Scheduler ทุกวันเวลา 02:00
|
|
|
|
$ProjectPath = "E:\git3\sriphat-dataplatform"
|
|
$BackupPath = "E:\backups\sriphat-data"
|
|
$Date = Get-Date -Format "yyyyMMdd-HHmmss"
|
|
$BackupFolder = "$BackupPath\$Date"
|
|
|
|
Write-Host "=== Sriphat Data Platform Backup Started ===" -ForegroundColor Green
|
|
Write-Host "Date: $Date"
|
|
Write-Host "Backup Location: $BackupFolder"
|
|
Write-Host ""
|
|
|
|
# สร้างโฟลเดอร์ backup
|
|
New-Item -ItemType Directory -Path $BackupFolder -Force | Out-Null
|
|
|
|
# Backup PostgreSQL
|
|
Write-Host "[1/5] Backing up PostgreSQL database..." -ForegroundColor Yellow
|
|
docker exec postgres pg_dumpall -U postgres | Out-File "$BackupFolder\postgres.sql" -Encoding UTF8
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "✓ PostgreSQL backup completed" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "✗ PostgreSQL backup failed" -ForegroundColor Red
|
|
}
|
|
|
|
# Backup 01-infra data
|
|
Write-Host "[2/5] Backing up Infrastructure data..." -ForegroundColor Yellow
|
|
robocopy "$ProjectPath\01-infra\data" "$BackupFolder\01-infra-data" /MIR /R:3 /W:5 /NFL /NDL /NJH /NJS | Out-Null
|
|
robocopy "$ProjectPath\01-infra\letsencrypt" "$BackupFolder\01-infra-letsencrypt" /MIR /R:3 /W:5 /NFL /NDL /NJH /NJS | Out-Null
|
|
Write-Host "✓ Infrastructure backup completed" -ForegroundColor Green
|
|
|
|
# Backup 04-ingestion data
|
|
Write-Host "[3/5] Backing up Airbyte data..." -ForegroundColor Yellow
|
|
robocopy "$ProjectPath\04-ingestion\data" "$BackupFolder\04-ingestion-data" /MIR /R:3 /W:5 /NFL /NDL /NJH /NJS | Out-Null
|
|
Write-Host "✓ Airbyte backup completed" -ForegroundColor Green
|
|
|
|
# Backup 06-analytics data
|
|
Write-Host "[4/5] Backing up Superset data..." -ForegroundColor Yellow
|
|
robocopy "$ProjectPath\06-analytics\data" "$BackupFolder\06-analytics-data" /MIR /R:3 /W:5 /NFL /NDL /NJH /NJS | Out-Null
|
|
Write-Host "✓ Superset backup completed" -ForegroundColor Green
|
|
|
|
# Backup config files
|
|
Write-Host "[5/5] Backing up configuration files..." -ForegroundColor Yellow
|
|
Copy-Item "$ProjectPath\.env.global" "$BackupFolder\.env.global" -Force
|
|
Copy-Item "$ProjectPath\06-analytics\superset_config.py" "$BackupFolder\superset_config.py" -Force
|
|
Write-Host "✓ Configuration backup completed" -ForegroundColor Green
|
|
|
|
# Compress backup
|
|
Write-Host ""
|
|
Write-Host "Compressing backup..." -ForegroundColor Yellow
|
|
Compress-Archive -Path $BackupFolder -DestinationPath "$BackupPath\backup-$Date.zip" -Force
|
|
$BackupSize = (Get-Item "$BackupPath\backup-$Date.zip").Length / 1MB
|
|
Write-Host "✓ Backup compressed: backup-$Date.zip ($([math]::Round($BackupSize, 2)) MB)" -ForegroundColor Green
|
|
|
|
# ลบโฟลเดอร์ที่ยังไม่ compress
|
|
Remove-Item -Path $BackupFolder -Recurse -Force
|
|
|
|
# ลบ backup เก่า (เก็บไว้ 30 วัน)
|
|
Write-Host ""
|
|
Write-Host "Cleaning old backups (keeping last 30 days)..." -ForegroundColor Yellow
|
|
$OldBackups = Get-ChildItem $BackupPath -Filter "backup-*.zip" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
|
|
if ($OldBackups) {
|
|
$OldBackups | ForEach-Object {
|
|
Write-Host " Removing: $($_.Name)" -ForegroundColor Gray
|
|
Remove-Item $_.FullName -Force
|
|
}
|
|
Write-Host "✓ Removed $($OldBackups.Count) old backup(s)" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "✓ No old backups to remove" -ForegroundColor Green
|
|
}
|
|
|
|
# Summary
|
|
Write-Host ""
|
|
Write-Host "=== Backup Completed Successfully ===" -ForegroundColor Green
|
|
Write-Host "Backup file: backup-$Date.zip"
|
|
Write-Host "Size: $([math]::Round($BackupSize, 2)) MB"
|
|
Write-Host "Location: $BackupPath"
|
|
Write-Host ""
|
|
|
|
# Log to file
|
|
$LogFile = "$BackupPath\backup.log"
|
|
"$Date - Backup completed successfully - Size: $([math]::Round($BackupSize, 2)) MB" | Out-File $LogFile -Append
|