From 7520d70ce9c491a881493937abcdbccdc37db684 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sun, 17 Aug 2025 12:40:37 -0300 Subject: [PATCH] things --- haven-notify/README.md | 45 ++++++++++++++++++++++++++++++++++++++++++ windows-backup.ps1 | 42 ++++++++++++++++++++++++++++++++++----- 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 haven-notify/README.md diff --git a/haven-notify/README.md b/haven-notify/README.md new file mode 100644 index 0000000..4b7ce1d --- /dev/null +++ b/haven-notify/README.md @@ -0,0 +1,45 @@ +# Haven Notify + +## Overview +Haven Notify is an internal service designed to send notifications to a specified Discord channel. + +It is built in Go and can be deployed as a container or managed service. + +## Prerequisites +- Go 1.18 or newer (for local development) +- Docker (for containerized deployment) +- Discord Webhook URL + +## API Specification + +### Send Notification +- **Endpoint**: `/notify` +- **Method**: `POST` +- **Request Body**: +```json +{ + "title": "Notification Title", + "message": "Notification Message" +} +``` + +## Setup & Usage + +### Docker +1. Build the Docker image: + ```sh + docker build -t haven-notify . + ``` +2. Run the container: + ```sh + docker run -e WEBHOOK_URL=your_webhook_url haven-notify + ``` + +### Kubernetes +Deployment manifest is available at `deploy/haven-notify.yaml`. +1. Edit the manifest to set your environment variables. +2. Create a generic secret named `WEBHOOK_URL` with `discord-webhook=your_webhook_url` +3. Apply deployment: + ```sh + kubectl apply -f deploy/haven-notify.yaml + ``` diff --git a/windows-backup.ps1 b/windows-backup.ps1 index ea3a11a..0e9e082 100644 --- a/windows-backup.ps1 +++ b/windows-backup.ps1 @@ -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!" \ No newline at end of file +Write-Host "Backup completed!" +Send-Notify "✅ Backup completed successfully." \ No newline at end of file