66 lines
1.9 KiB
PowerShell
66 lines
1.9 KiB
PowerShell
$ErrorActionPreference = "Stop"
|
|
|
|
$ProjectDir = Get-Location
|
|
$Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
|
|
$BackupDir = Join-Path $ProjectDir "backups"
|
|
$BackupFile = Join-Path $BackupDir "kv_tiktok_backup_$Timestamp.zip"
|
|
|
|
# Create backup directory if it doesn't exist
|
|
if (-not (Test-Path $BackupDir)) {
|
|
New-Item -ItemType Directory -Path $BackupDir | Out-Null
|
|
Write-Host "Created backup directory: $BackupDir" -ForegroundColor Cyan
|
|
}
|
|
|
|
Write-Host "Starting backup of $ProjectDir..." -ForegroundColor Cyan
|
|
Write-Host "Target file: $BackupFile" -ForegroundColor Cyan
|
|
|
|
# Exclude list (Patterns to ignore)
|
|
$ExcludePatterns = @(
|
|
"^\.git",
|
|
"^\.venv",
|
|
"^node_modules",
|
|
"__pycache__",
|
|
"^backups",
|
|
"\.log$",
|
|
"backend\\downloads",
|
|
"backend\\cache",
|
|
"backend\\session",
|
|
"frontend\\dist"
|
|
)
|
|
|
|
# Get files to zip
|
|
$FilesToZip = Get-ChildItem -Path $ProjectDir -Recurse | Where-Object {
|
|
$relativePath = $_.FullName.Substring($ProjectDir.Path.Length + 1)
|
|
$shouldExclude = $false
|
|
|
|
foreach ($pattern in $ExcludePatterns) {
|
|
if ($relativePath -match $pattern) {
|
|
$shouldExclude = $true
|
|
break
|
|
}
|
|
}
|
|
|
|
# Also exclude the backup directory itself and any zip files inside root (if active)
|
|
if ($relativePath -like "backups\*") { $shouldExclude = $true }
|
|
|
|
return -not $shouldExclude
|
|
}
|
|
|
|
if ($FilesToZip.Count -eq 0) {
|
|
Write-Error "No files found to backup!"
|
|
}
|
|
|
|
# Compress
|
|
Write-Host "Compressing $($FilesToZip.Count) files..." -ForegroundColor Yellow
|
|
$FilesToZip | Compress-Archive -DestinationPath $BackupFile -Force
|
|
|
|
if (Test-Path $BackupFile) {
|
|
$Item = Get-Item $BackupFile
|
|
$SizeMB = [math]::Round($Item.Length / 1MB, 2)
|
|
Write-Host "Backup created successfully!" -ForegroundColor Green
|
|
Write-Host "Location: $BackupFile"
|
|
Write-Host "Size: $SizeMB MB"
|
|
}
|
|
else {
|
|
Write-Error "Backup failed!"
|
|
}
|