File Backup Hook

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_protokoll.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_protokoll.file_backup_history
                              (version++, changed_by='claude-code-hook')

Gesicherte Verzeichnisse

Ausgeschlossene Patterns

Datenbank-Schema

SpalteBeschreibung
idAuto-increment ID
file_pathVollständiger Dateipfad
file_contentDateiinhalt (LONGTEXT)
content_hashSHA256 Hash
file_sizeGröße in Bytes
versionVersionsnummer pro Datei
change_typecreated/modified/deleted
changed_atTimestamp
changed_byclaude-code-hook
reasonBackup-Grund

Features

Hooks-Konfiguration

In /var/www/dev.campus.systemische-tools.de/.claude/settings.local.json:

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

# Letzte Backups anzeigen
mysql -u claude_code -p ki_protokoll -e "SELECT id, file_path, version, file_size, changed_at FROM file_backup_history ORDER BY id DESC LIMIT 10;"

# Bestimmte Datei-History
mysql -u claude_code -p ki_protokoll -e "SELECT version, file_size, changed_at FROM file_backup_history WHERE file_path = '/pfad/zur/datei.php' ORDER BY version DESC;"

# Inhalt einer Version
mysql -u claude_code -p ki_protokoll -e "SELECT file_content FROM file_backup_history WHERE id = 1;" > restored_file.php

Unterschied zu anderen Systemen

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

Verwandte Module