This backup solution prevents permanent loss of your Claude Code conversations by automatically committing them to a git repository every hour.
Claude Code stores conversation history in .jsonl
files but automatically purges files after 30 days to manage disk space. This means:
- All conversations older than 30 days are permanently deleted
- No built-in backup mechanism exists
- Years of valuable conversation history can be lost without warning
Through git log analysis, we discovered:
- June 17, 2025: Claude Code purchased and usage began
- June 25, 2025: Initial backup system created (smart move!)
- June 30, 2025: First file deletion detected (manual cleanup of fork experiments)
- July 17, 2025: Mass automatic deletions began (exactly 30 days after initial usage)
- Pattern: Claude Code automatically deletes files exactly 30 days after their git commit date
This backup system:
- Runs every hour via cron
- Commits any changes to conversation files with timestamps
- Preserves complete history in git even after Claude Code deletes files
- Logs all activity for monitoring
- Requires minimal setup and maintenance
backup-conversations.sh
- The backup scriptREADME.md
- This documentation- Installation and monitoring instructions below
# Navigate to your Claude Code projects directory
cd ~/.claude/projects
# Initialize git repository if not already done
git init
git add .
git commit -m "Initial backup of Claude Code conversations"
# Copy the backup script to your .claude directory
cp backup-conversations.sh ~/.claude/
chmod +x ~/.claude/backup-conversations.sh
# Edit your crontab
crontab -e
# Add this line to run backup every hour
0 * * * * /home/yourusername/.claude/backup-conversations.sh >> /home/yourusername/.claude/backup.log 2>&1
Replace yourusername
with your actual username!
# Check if cron job was added
crontab -l
# Test the script manually
~/.claude/backup-conversations.sh
# Check the log
tail -f ~/.claude/backup.log
# View recent backup log entries
tail -20 ~/.claude/backup.log
# See git commit history
cd ~/.claude/projects
git log --oneline -10
# Check if new conversation files are being committed
git status
# See what files were in recent commits
git show --name-only HEAD
# List all backed up conversations (even deleted ones)
git log --name-status | grep "\.jsonl$" | head -20
# Restore a deleted conversation file
git show COMMIT_HASH:path/to/file.jsonl > restored-conversation.jsonl
The script:
- Changes to
~/.claude/projects
directory - Checks if there are uncommitted changes using
git diff-index
- If changes exist:
- Stages all files with
git add -A
- Creates commit with timestamp and file count
- Logs successful backup
- Stages all files with
- If no changes, logs "no changes to backup"
2025-07-24 17:00:01: Conversations backed up
2025-07-24 18:00:01: No changes to backup
2025-07-24 19:00:01: Conversations backed up
# Check if cron service is running
systemctl status cron
# Check cron logs
journalctl -u cron
# Verify script permissions
ls -la ~/.claude/backup-conversations.sh
# Test script manually
bash -x ~/.claude/backup-conversations.sh
# Check git configuration
cd ~/.claude/projects
git config --list
# Initialize if needed
git init
git config user.email "[email protected]"
git config user.name "Your Name"
- Each conversation file is typically 1-50KB
- Git compresses history efficiently
- Expect ~1-10MB growth per month depending on usage
- Old commits can be cleaned up if storage becomes an issue
- Backup files contain your conversation history
- Ensure proper file permissions on backup directory
- Consider encrypting backups if sharing systems
- Never commit the backup repository to public repositories
Found this helpful? Please share with other Claude Code users to prevent conversation loss!
Feel free to use, modify, and share this backup solution.
Created by a Claude Code user who learned about the 30-day purge the hard way!