Dokumentation » Betrieb » File Backup Hook
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
Trigger Claude PreToolUse (Edit, Write)
Datenbank ki_dev.file_backup_history
Aktiviert 2025-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
/var/www/dev.campus.systemische-tools.de/src/
/var/www/dev.campus.systemische-tools.de/public/
/var/www/dev.campus.systemische-tools.de/scripts/
/var/www/dev.campus.systemische-tools.de/includes/
/var/www/dev.campus.systemische-tools.de/config/
/var/www/prod.campus.systemische-tools.de/* (analog)
Ausgeschlossene Patterns
/vendor/, /node_modules/, /.git/
/backups/, /tmp/, /logs/, /cache/
.log, .cache, .tmp
Dateien > 10 MB
Datenbank-Schema
Spalte Typ Beschreibung
id int(11) PK Auto-increment ID
file_path varchar(512) Vollständiger Dateipfad
file_content longtext Dateiinhalt
content_hash char(64) SHA256 Hash
file_size int(11) Größe in Bytes
version int(11) Versionsnummer pro Datei (Default: 1)
change_type enum created/modified/deleted (Default: modified)
changed_at timestamp Zeitstempel (Default: current_timestamp)
changed_by varchar(100) claude-code-hook
reason text Backup-Grund
diff_summary text Zusammenfassung der Änderungen (optional)
affected_entities longtext Betroffene Entities als JSON (optional)
Features
Hash-Vergleich: Vor jedem Backup wird der aktuelle Hash mit dem letzten verglichen. Bei identischem Hash wird kein neues Backup erstellt.
Versionierung: Jede Datei hat eine eigene Versionsnummer (1, 2, 3, ...)
Non-blocking: Der Hook gibt immer {"continue": true} zurück und blockiert nie Claude Code.
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
System Trigger Speicherort Zweck
File Backup Hook Claude PreToolUse DB (ki_dev.file_backup_history) Automatisches Backup vor Code-Änderungen
System Backup Cron (03:00) /backup/*.tar.gz Vollständige System-Sicherung
KI-Protokoll Alle Claude Hooks DB (ki_dev.protokoll) Claude Request/Response Logging
Verwandte Module