84 lines
2.6 KiB
PowerShell
84 lines
2.6 KiB
PowerShell
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
|
|
|
|
# Check if 7-Zip is installed
|
|
if (!(Test-Path "$env:ProgramFiles\7-Zip\7z.exe")) {
|
|
Write-Host "7-Zip is not installed. Please install it to use this script."
|
|
exit 1
|
|
Send-Notify "❌ 7-Zip is not installed. Backup aborted."
|
|
}
|
|
|
|
$BackupSource = @(
|
|
"$env:USERPROFILE\Documents",
|
|
"$env:USERPROFILE\Desktop",
|
|
"$env:USERPROFILE\Pictures",
|
|
"$env:USERPROFILE\.ssh",
|
|
"$env:USERPROFILE\.kube"
|
|
)
|
|
|
|
$NASDestination = "\\OMV\Backup\$env:COMPUTERNAME"
|
|
$TempDir = "$env:TEMP\BackupTemp"
|
|
$Date = Get-Date -Format "yyyy-MM-dd"
|
|
|
|
$NotifyUrl = "http://notify.haven/notify"
|
|
|
|
function Send-Notify {
|
|
param (
|
|
[string]$Message
|
|
)
|
|
if (-not $NotifyUrl) {
|
|
Write-Host "NOTIFY_URL environment variable is not set. Notification not sent."
|
|
return
|
|
}
|
|
$Title = "Backup - $env:COMPUTERNAME"
|
|
$Body = @{ title = $Title; message = $Message } | ConvertTo-Json
|
|
try {
|
|
Invoke-RestMethod -Uri $NotifyUrl -Method Post -ContentType 'application/json' -Body $Body | Out-Null
|
|
Write-Host "Notification sent: $Title - $Message"
|
|
} catch {
|
|
Write-Host "Failed to send notification: $_"
|
|
}
|
|
}
|
|
|
|
# Create temp directory
|
|
New-Item -ItemType Directory -Path $TempDir -Force
|
|
|
|
# Create NAS destination if it doesn't exist
|
|
if (!(Test-Path $NASDestination)) {
|
|
New-Item -ItemType Directory -Path $NASDestination -Force
|
|
}
|
|
|
|
foreach ($Folder in $BackupSource) {
|
|
if (Test-Path $Folder) {
|
|
$FolderName = Split-Path $Folder -Leaf
|
|
$ZipFile = "$TempDir\$FolderName-$Date.zip"
|
|
|
|
Write-Host "Compressing $Folder..."
|
|
$compressResult = & "$7zipPath" a -tzip "$ZipFile" "$Folder\*" -mx=9
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Compression failed for $Folder."
|
|
Send-Notify "❌ Compression failed for $Folder."
|
|
continue
|
|
}
|
|
|
|
Write-Host "Copying $ZipFile to NAS..."
|
|
Copy-Item $ZipFile $NASDestination -Force
|
|
|
|
Write-Host "Removing $ZipFile..."
|
|
Remove-Item $ZipFile
|
|
} else {
|
|
Write-Host "Source folder not found: $Folder"
|
|
Send-Notify "⚠️ Source folder not found: $Folder"
|
|
}
|
|
}
|
|
|
|
Write-Host "Removing Files older than 7 days from $NASDestination..."
|
|
$OldFiles = Get-ChildItem -Path $NASDestination -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) }
|
|
foreach ($OldFile in $OldFiles) {
|
|
Remove-Item $OldFile.FullName -Force
|
|
Write-Host "Removed: $($OldFile.FullName)"
|
|
}
|
|
|
|
# Cleanup
|
|
Remove-Item $TempDir -Recurse -Force
|
|
Write-Host "Backup completed!"
|
|
Send-Notify "✅ Backup completed successfully." |