Loading...
Automatically creates timestamped backups of files before modification to prevent data loss
{
"hookConfig": {
"hooks": {
"preToolUse": {
"script": "./.claude/hooks/auto-save-backup.sh",
"matchers": [
"edit",
"write",
"multiedit"
]
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if file exists before backing up\nif [ -f \"$FILE_PATH\" ]; then\n echo \"💾 Creating backup for $FILE_PATH...\" >&2\n \n # Create backups directory\n mkdir -p .backups\n \n # Generate timestamped backup filename\n BASENAME=$(basename \"$FILE_PATH\")\n TIMESTAMP=$(date +%Y%m%d_%H%M%S)\n BACKUP_NAME=\"${BASENAME%.*}_${TIMESTAMP}.${BASENAME##*.}\"\n \n # Create backup\n cp \"$FILE_PATH\" \".backups/$BACKUP_NAME\" 2>/dev/null || true\n \n if [ $? -eq 0 ]; then\n echo \"✅ Backup created: .backups/$BACKUP_NAME\" >&2\n else\n echo \"⚠️ Backup failed for $FILE_PATH\" >&2\n fi\nelse\n echo \"📝 Creating new file $FILE_PATH (no backup needed)\" >&2\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"preToolUse": {
"script": "./.claude/hooks/auto-save-backup.sh",
"matchers": [
"edit",
"write",
"multiedit"
]
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if file exists before backing up
if [ -f "$FILE_PATH" ]; then
echo "💾 Creating backup for $FILE_PATH..." >&2
# Create backups directory
mkdir -p .backups
# Generate timestamped backup filename
BASENAME=$(basename "$FILE_PATH")
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="${BASENAME%.*}_${TIMESTAMP}.${BASENAME##*.}"
# Create backup
cp "$FILE_PATH" ".backups/$BACKUP_NAME" 2>/dev/null || true
if [ $? -eq 0 ]; then
echo "✅ Backup created: .backups/$BACKUP_NAME" >&2
else
echo "⚠️ Backup failed for $FILE_PATH" >&2
fi
else
echo "📝 Creating new file $FILE_PATH (no backup needed)" >&2
fi
exit 0PreToolUse hook runs but backup directory not created
Verify mkdir permissions in project root. Check disk space: df -h. Ensure script runs with correct CWD: pwd >&2 in hook. Create .backups manually if needed.
Backup created but original file modification fails after
PreToolUse only creates backup, doesn't block edits. Check subsequent tool execution logs. Verify hook exits with 0 (non-blocking). Review tool output for actual edit errors.
Timestamp collisions when editing same file rapidly
Add milliseconds to timestamp: date +%Y%m%d_%H%M%S_%N. Or use hash suffix: ${TIMESTAMP}_$(md5sum file | cut -c1-8). Implement collision detection and retry logic.
Hook backs up new files that don't exist yet on Write
Verify [ -f "$FILE_PATH" ] check works correctly. Check TOOL_NAME to distinguish edit vs write: if [[ "$TOOL_NAME" == "edit" ]]. Skip backup for new file creation.
Backup directory grows unbounded filling disk space
Add retention policy: find .backups -mtime +30 -delete. Implement backup rotation script. Use git for versioning instead. Add size limit checks before creating backups.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters