Dokumentation » Server » File Backup Hook
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
Trigger Claude PreToolUse (Edit, Write)
Datenbank ki_protokoll.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_protokoll.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 Beschreibung
id Auto-increment ID
file_path Vollständiger Dateipfad
file_content Dateiinhalt (LONGTEXT)
content_hash SHA256 Hash
file_size Größe in Bytes
version Versionsnummer pro Datei
change_type created/modified/deleted
changed_at Timestamp
changed_by claude-code-hook
reason Backup-Grund
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 /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
System Trigger Speicherort Zweck
File Backup Hook Claude PreToolUse DB (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 (protokoll) Claude Request/Response Logging
Verwandte Module