File Backup Hook

Erstellt: 2025-12-20 | Aktualisiert: 2025-12-31

Automatisches Backup-System, das Dateien in der Datenbank sichert, bevor Claude Code sie mit Edit/Write Tools ändert.

Script/var/www/tools/ki-protokoll/claude-hook/file_backup_hook.py
TriggerClaude PreToolUse (Edit, Write)
Datenbankki_dev.file_backup_history
Aktiviert2025-12-20

Funktionsweise

Claude Edit/Write → PreToolUse Hook → file_backup_hook.py
                                           │
                                           ▼
                                    Datei existiert?
                                    In BACKUP_DIRS?
                                    Hash geändert?
                                           │
                                           ▼
                              ki_dev.file_backup_history
                              (version++, changed_by='claude-code-hook')

Gesicherte Verzeichnisse

Ausgeschlossene Patterns

Datenbank-Schema

SpalteTypBeschreibung
idint(11) PKAuto-increment ID
file_pathvarchar(512)Vollständiger Dateipfad
file_contentlongtextDateiinhalt
content_hashchar(64)SHA256 Hash
file_sizeint(11)Größe in Bytes
versionint(11)Versionsnummer pro Datei (Default: 1)
change_typeenumcreated/modified/deleted (Default: modified)
changed_attimestampZeitstempel (Default: current_timestamp)
changed_byvarchar(100)claude-code-hook
reasontextBackup-Grund
diff_summarytextZusammenfassung der Änderungen (optional)
affected_entitieslongtextBetroffene Entities als JSON (optional)

Features

Hooks-Konfiguration

In /root/.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{
          "type": "command",
          "command": "/var/www/tools/ki-protokoll/claude-hook/file_backup_hook.py",
          "timeout": 10
        }]
      }
    ]
  }
}

Manueller Test

echo '{"hook_event_name": "PreToolUse", "tool_name": "Edit", "tool_input": {"file_path": "/var/www/dev.campus.systemische-tools.de/src/Controller/DocsController.php"}}' | /var/www/tools/ki-protokoll/claude-hook/file_backup_hook.py

Backups abfragen

Mit MCP-DB (empfohlen)

# Letzte Backups anzeigen (via MCP-DB Tool)
# Verwendet automatisch sichere Authentifizierung ohne Passwort-Eingabe

# Bestimmte Datei-History
SELECT version, file_size, changed_at
FROM file_backup_history
WHERE file_path = '/pfad/zur/datei.php'
ORDER BY version DESC;

# Inhalt einer Version
SELECT file_content
FROM file_backup_history
WHERE id = 1;

Mit direktem MySQL (Legacy, nicht empfohlen)

# Hinweis: Direkter mysql-Zugriff erfordert Passwort-Eingabe
# Besser: Verwende MCP-DB Tool für sichere Authentifizierung

mysql -u <username> -p ki_dev -e "SELECT id, file_path, version FROM file_backup_history LIMIT 10;"

Unterschied zu anderen Systemen

SystemTriggerSpeicherortZweck
File Backup HookClaude PreToolUseDB (ki_dev.file_backup_history)Automatisches Backup vor Code-Änderungen
System BackupCron (03:00)/backup/*.tar.gzVollständige System-Sicherung
KI-ProtokollAlle Claude HooksDB (ki_dev.protokoll)Claude Request/Response Logging

Verwandte Module