things
This commit is contained in:
@@ -4,18 +4,41 @@ $7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
|
||||
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\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
|
||||
|
||||
@@ -30,18 +53,26 @@ foreach ($Folder in $BackupSource) {
|
||||
$ZipFile = "$TempDir\$FolderName-$Date.zip"
|
||||
|
||||
Write-Host "Compressing $Folder..."
|
||||
& "$7zipPath" a -tzip "$ZipFile" "$Folder\*" -mx=9
|
||||
$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 15 days from $NASDestination..."
|
||||
$OldFiles = Get-ChildItem -Path $NASDestination -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-15) }
|
||||
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)"
|
||||
@@ -49,4 +80,5 @@ foreach ($OldFile in $OldFiles) {
|
||||
|
||||
# Cleanup
|
||||
Remove-Item $TempDir -Recurse -Force
|
||||
Write-Host "Backup completed!"
|
||||
Write-Host "Backup completed!"
|
||||
Send-Notify "✅ Backup completed successfully."
|
Reference in New Issue
Block a user