commit 03c7eadf0a311536929970060baf8f27b27965f9 Author: scriptos Date: Wed Mar 26 14:33:33 2025 +0100 Initial diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..95dd9fe --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Patrick Asmus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..0b6d3ea --- /dev/null +++ b/Readme.md @@ -0,0 +1,46 @@ +# 🧹 Matrix Synapse Message Prune Script + +Dieses Bash-Script löscht automatisiert alte Nachrichten aus definierten Matrix-Räumen mithilfe der Synapse Admin API. +Je nach Konfiguration werden Nachrichten gelöscht, die älter als 7 oder 30 Tage sind. Die Ausführung erfolgt typischerweise per Cronjob. + +## 🔧 Funktionen + +- Löscht Nachrichten aus festgelegten Räumen über die Admin-API (`/purge_history`) +- Trennung nach Aufbewahrungsdauer (z. B. 7 Tage vs. 30 Tage) +- Zeitstempel werden automatisch berechnet +- Status und Ergebnisse werden in ein Logfile geschrieben (`/var/log/matrix_prune.log`) +- Optional mit `jq` für sauberes JSON-Parsing + +## 🚀 Verwendung + +1. Synapse Admin-API muss auf `localhost:8008` erreichbar sein +2. Admin-Token in der Variable `TOKEN` eintragen +3. Räume in den Arrays `ROOMS_7DAYS` und `ROOMS_30DAYS` definieren +4. Script mit `chmod +x` ausführbar machen + +```bash +chmod +x matrix_prune-messages.v1.sh +``` + +# 🕒 Cronjob einrichten + +Um das Script täglich um 23:00 Uhr auszuführen und alle Ausgaben zu unterdrücken: + +```bash +0 23 * * * bash /home/scripts/default/matrix_prune-messages.v1.sh >/dev/null 2>&1 +``` + +# 📝 Logfile + +Alle Löschvorgänge und API-Antworten werden mit Zeitstempel in folgendes Log geschrieben: + +```bash +/var/log/matrix_prune.log +``` + +# 🔒 Hinweis + +Autor: Patrick Asmus + +Lizenz: MIT + diff --git a/matrix_prune-messages.v1.sh b/matrix_prune-messages.v1.sh new file mode 100644 index 0000000..86ddc1c --- /dev/null +++ b/matrix_prune-messages.v1.sh @@ -0,0 +1,78 @@ +#!/bin/bash +# Autor: Patrick Asmus +# Web: https://www.techniverse.net +# Git-Reposit.: https://git.techniverse.net/Backups/backup_matrixsynapse +# Cron: 0 23 * * * bash /home/scripts/default/matrix_prune-messages.v1.sh >/dev/null 2>&1 +# Version: 1.0 +# Datum: 26.03.2025 +# Modifikation: Initial +##################################################### + +# 🔐 Token +TOKEN="" + +# 📄 Log-Datei +LOGFILE="/var/log/matrix_prune.log" + +# 🗂️ Räume nach Aufbewahrungsdauer +ROOMS_7DAYS=( + "!gHtY78vXsQwErTyUiO:matrixdomain.com" # System Alerts & Monitoring + "!zXc12vBnMaSdFgHjKl:matrixdomain.com" # DevOps Deployment Notifications + "!lPo98AsDfGhJkLmNqW:matrixdomain.com" # GitLab CI/CD Build Updates + "!xYu78vCzBnMqwErTyU:matrixdomain.com" # Webhook Events (external services) +) + +ROOMS_30DAYS=( + "!nMki87HgFdSaQwErTy:matrixdomain.com" # Company Announcements + "!vCxZrTuBnMwEyUiOpL:matrixdomain.com" # Community Chat (public) + "!aSdFgHjKlQwErTyUiO:matrixdomain.com" # Uptime & Status Reports +) + +# ⏱️ Timestamps +TS_7DAYS=$(date --date='7 days ago' +%s000) +TS_30DAYS=$(date --date='30 days ago' +%s000) +DATE_NOW=$(date '+%Y-%m-%d %H:%M:%S') + +# 🧩 Funktion: Prüfen & loggen +log_response() { + local room_id=$1 + local days=$2 + local response=$3 + local status="unbekannt" + + if command -v jq >/dev/null 2>&1; then + purge_id=$(echo "$response" | jq -r '.purge_id // empty') + error=$(echo "$response" | jq -r '.error // empty') + else + purge_id=$(echo "$response" | grep -o '"purge_id":"[^"]*"' | cut -d'"' -f4) + error=$(echo "$response" | grep -o '"error":"[^"]*"' | cut -d'"' -f4) + fi + + if [[ -n "$purge_id" ]]; then + status="✅ purge gestartet (purge_id: $purge_id)" + elif [[ -n "$error" ]]; then + status="⚠️ $error" + else + status="⚠️ Unbekannte Antwort: $response" + fi + + echo "[$DATE_NOW] [$days Tage] $room_id → $status" >> "$LOGFILE" +} + +# 🔁 Räume mit 7 Tagen +for ROOM_ID in "${ROOMS_7DAYS[@]}"; do + RESPONSE=$(curl -s -X POST "http://localhost:8008/_synapse/admin/v1/purge_history/$ROOM_ID" \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"purge_up_to_ts\": $TS_7DAYS}") + log_response "$ROOM_ID" "7" "$RESPONSE" +done + +# 🔁 Räume mit 30 Tagen +for ROOM_ID in "${ROOMS_30DAYS[@]}"; do + RESPONSE=$(curl -s -X POST "http://localhost:8008/_synapse/admin/v1/purge_history/$ROOM_ID" \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"purge_up_to_ts\": $TS_30DAYS}") + log_response "$ROOM_ID" "30" "$RESPONSE" +done \ No newline at end of file