Skip to main content

Git-backed Claude Code session backup with timeline view, folder analysis, deletion detection, and session restore.

Project description

Claude-Session-Backup

PyPI Release Date Python 3.10+ License: GPL v3 Installs Platform

Git-backed Claude Code session backup with timeline view, folder analysis, deletion detection, and session restore.

The Problem

Claude Code stores session data in ~/.claude/projects/ as JSONL files. These can be silently deleted during upgrades, lossy-compacted via /compact, or lost when session compatibility breaks between versions. Once gone, your conversation history -- including debugging sessions, architectural decisions, and code review context -- is unrecoverable.

csb preserves every session in your existing ~/.claude git repository, builds a searchable metadata index, detects deletions, and can restore lost sessions from git history.

[!WARNING] Prealpha software. csb is functional and all tests pass, but it is not yet feature-complete and has not been broadly tested outside of active dogfooding. Expect bugs, rough edges, and breaking changes until the first alpha/beta releases. Three items gate the next milestone: distilled conversation backup (#12), end-to-end restore verification (#13), and a CLI launcher for claude-code-history-viewer (#14). By all means use it -- and please file issues -- but don't rely on csb as your only backup just yet.

Quick Start

# 1. Install the CLI (PyPI release coming shortly)
pip install git+https://github.com/DazzleML/Claude-Session-Backup.git

# 2. (recommended) Install the Claude Code plugin so pre-compact backups fire
#    automatically. Full commands in the "Claude Code Plugin" section below.

# 3. Scan all sessions and build the index (no git commits)
csb backup --no-commit

# 4. See your session timeline
csb list

# 5. Full backup with git commits (noise + user, separate commits, unsigned)
csb backup

Features

  • Full session preservation: Every byte of JSONL, subagent data, tool results backed up via git
  • Timeline view: Sessions sorted by last use with relative dates, start folder, and top N working directories
  • Folder analysis: See where work actually happened -- the most-used folder is highlighted
  • Deletion detection: Know when Claude Code removes a session you previously tracked
  • Session restore: Recover deleted sessions from git history with csb restore
  • Two-commit model: Noise (transient state) and user (configs, skills) committed separately
  • Unattended operation: --no-gpg-sign, --quiet, lock file -- designed for cron and Task Scheduler
  • Cross-platform: Works on Windows, Linux, macOS, BSD

Commands

csb backup                            # Scan, index, git commit (noise + user)
csb backup --no-commit                # Scan and index only
csb list [-n 20]                      # Timeline view (default sort: last-used)
csb list [keyword]                    # Filter by keyword in name/project/folders
csb list --sort expiration            # Sort by soonest-to-purge first
csb list --sort {last-used|expiration|started|oldest|messages|size}
csb list --deleted                    # Show deleted sessions
csb scan [path]                       # Find sessions touching this directory
csb scan [path] -NU                   # Prefix-only match (skip folder-usage search)
csb status                            # Summary stats
csb show <session-id>                 # Detailed session info with folder analysis
csb search "query"                    # Search by session name, project, or folder
csb restore <session-id>              # Restore deleted session from git history
csb resume <session-id>               # Launch claude --resume with full UUID
csb rebuild-index                     # Reconstruct SQLite from scratch
csb config [key] [value]              # View/edit configuration

Finding sessions at risk of purge

Claude Code auto-deletes sessions after cleanupPeriodDays (default 30). To see which of your sessions are closest to being purged:

csb list --sort expiration -n 20

Sessions are sorted by the JSONL file's modification time, so active sessions (which refresh their mtime on every interaction) stay safe while dormant sessions surface to the top of the expiration list.

How It Works

flowchart LR
    subgraph GitRepo["~/.claude/ (your git repo)"]
        direction TB
        Data["projects/*.jsonl<br>session-states/<br>file-history/"]
    end

    subgraph CSB["csb Tool"]
        direction TB
        Scripts["scanner.py<br>metadata.py<br><i>(extract names, dates, folders)</i>"]
        Restore["restore.py"]
    end

    DB[("session-backup.db<br>(rebuildable metadata cache)")]

    Data -- "scan & read" --> Scripts
    Scripts -- "upsert" --> DB
    Scripts -- "git add + commit" --> Data
    Data -- "git show {commit}:path" --> Restore

Key principle: Git is the source of truth. The SQLite database is a rebuildable index for fast queries. If the DB is lost, csb rebuild-index reconstructs it from git history.

Automation

Claude Code Plugin (recommended)

The repository ships as a Claude Code plugin that registers PreCompact and SessionEnd hooks automatically. You can install it straight from GitHub -- no clone required:

# Add the DazzleML marketplace (one-time)
claude plugin marketplace add "DazzleML/Claude-Session-Backup"

# Install the plugin
claude plugin install claude-session-backup@dazzle-claude-session-backup

Alternatively, if you already have a clone for development:

# From a clone of this repo
claude plugin marketplace add ./
claude plugin install claude-session-backup@dazzle-claude-session-backup

The plugin uses a Node.js bootstrapper (run-hook.mjs) to find the correct Python binary on each platform, so it works reliably on Windows, Linux, and macOS without any shell quoting concerns. PreCompact fires synchronously before /compact to preserve full conversation detail; SessionEnd fires on exit to catch any remaining changes.

Manual hook installation

If you prefer to manage hooks yourself, add this to ~/.claude/settings.json:

{
  "hooks": {
    "PreCompact": [{"hooks": [{"type": "command", "command": "csb backup --quiet"}]}],
    "SessionEnd": [{"hooks": [{"type": "command", "command": "csb backup --quiet &"}]}]
  }
}

Or use python install.py in the repo to copy the hook script and print the snippet.

Cron (Linux/Mac)

Belt-and-suspenders periodic backup as a safety net:

*/15 * * * * /usr/local/bin/csb backup --quiet 2>/dev/null

Task Scheduler (Windows)

schtasks /create /tn "Claude Session Backup" /tr "csb backup --quiet" /sc minute /mo 15

Requirements

  • Python 3.10+
  • Git (for backup storage)
  • ~/.claude/ initialized as a git repository (git -C ~/.claude init)

Installation

# From GitHub (recommended until PyPI publish lands)
pip install git+https://github.com/DazzleML/Claude-Session-Backup.git

# From source (development / contributing)
git clone https://github.com/DazzleML/Claude-Session-Backup.git
cd Claude-Session-Backup
pip install -e ".[dev]"

# From PyPI (once published)
pip install claude-session-backup

Contributing

Contributions welcome! See CONTRIBUTING.md.

Acknowledgements

This project draws inspiration and patterns from:

License

Claude-Session-Backup, Copyright (C) 2026 Dustin Darcy

Licensed under the GNU General Public License v3.0 (GPL-3.0) -- see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_session_backup-0.2.1.tar.gz (48.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_session_backup-0.2.1-py3-none-any.whl (43.8 kB view details)

Uploaded Python 3

File details

Details for the file claude_session_backup-0.2.1.tar.gz.

File metadata

  • Download URL: claude_session_backup-0.2.1.tar.gz
  • Upload date:
  • Size: 48.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for claude_session_backup-0.2.1.tar.gz
Algorithm Hash digest
SHA256 79cc3434fa57a2650331b52b92f1ead85d1c1bf97de46ec8164eca8ea5cfa873
MD5 34ae448a21cc49466d53f25c358181e6
BLAKE2b-256 be2464c4408835ac67615fce31f95b4f3d2323b659e888f762bb124df4510829

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_session_backup-0.2.1.tar.gz:

Publisher: release.yml on DazzleML/Claude-Session-Backup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file claude_session_backup-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for claude_session_backup-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5b5a68b09d914d396558f9b55b31fae2a92b6c3fe95d7deb914954bc36fddee
MD5 f2cad2945b894a72ae8b9200de45295f
BLAKE2b-256 3df8657670d31ddf5fa36b908c99a7b89288fa99e338c2ecfcf74781afb000ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_session_backup-0.2.1-py3-none-any.whl:

Publisher: release.yml on DazzleML/Claude-Session-Backup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page