CClaude Code Catalog
All Hooks

Session Backup on Stop

StopIntermediateHook Type: stop

Session Backup on Stop hooks into the stop event to create a comprehensive backup before a session terminates. It captures the list of all files modified during the session, creates a git stash if there are uncommitted changes, generates a brief summary of what was accomplished, and saves everything to a timestamped directory. This ensures no work is lost between sessions and makes it easy to resume where you left off. Backups can be pruned after a configurable retention period.

backupsessionrecoverygit-stashpersistence

Hook Code

#!/bin/bash # Session Backup on Stop Hook # Saves session context when Claude Code stops BACKUP_DIR="${HOME}/.claude/backups" TIMESTAMP=$(date +%Y%m%d_%H%M%S) SESSION_DIR="$BACKUP_DIR/session_$TIMESTAMP" RETENTION_DAYS=14 mkdir -p "$SESSION_DIR" # Record modified files echo "=== Session Backup: $TIMESTAMP ===" > "$SESSION_DIR/summary.md" echo "" >> "$SESSION_DIR/summary.md" # List recently modified files (last 2 hours) echo "## Modified Files" >> "$SESSION_DIR/summary.md" find . -name '*.ts' -o -name '*.tsx' -o -name '*.js' -o -name '*.py' | xargs stat -f "%m %N" 2>/dev/null | sort -rn | head -20 | awk '{print "- " $2}' >> "$SESSION_DIR/summary.md" # Save git status and diff echo "" >> "$SESSION_DIR/summary.md" echo "## Git Status" >> "$SESSION_DIR/summary.md" git status --short >> "$SESSION_DIR/summary.md" 2>/dev/null # Save full diff of uncommitted changes git diff > "$SESSION_DIR/uncommitted.diff" 2>/dev/null git diff --cached > "$SESSION_DIR/staged.diff" 2>/dev/null # Create stash if there are uncommitted changes if [ -n "$(git status --porcelain 2>/dev/null)" ]; then STASH_MSG="claude-session-backup-$TIMESTAMP" git stash push -m "$STASH_MSG" --keep-index 2>/dev/null echo "Stashed uncommitted changes: $STASH_MSG" git stash pop --quiet 2>/dev/null fi # Prune old backups find "$BACKUP_DIR" -maxdepth 1 -name "session_*" -type d -mtime +$RETENTION_DAYS -exec rm -rf {} \; 2>/dev/null echo "Session backup saved to $SESSION_DIR" exit 0

Add this hook to your Claude Code settings or .claude/settings.json to activate.

Terminal Preview

Session Backup on Stop

About Session Backup on Stop

Claude Code hooks let you run custom shell commands automatically in response to specific events during Claude's operation. Session Backup on Stop is a Stop hook at the Intermediate level that automates tasks at key moments in your development workflow, reducing manual steps and enforcing consistency across your team.