Back up Claude Code and Codex sessions: rsync, git, or an encrypted archive

Published · Updated · Checked against Claude Code 2.1.319, Codex CLI 0.166.0, age 1.3.2, gitleaks 8.30.1

Both tools keep sessions as plain files, so any file backup works. The simplest safe one is rsync without --delete. Wrap it in tar | age if the destination is not yours, and think twice before putting transcripts in git.

What to copy

Claude Code writes transcripts to ~/.claude/projects/<project>/<session>.jsonl. Subagent transcripts and spilled tool output sit in a folder next to each file, and prompt history is ~/.claude/history.jsonl. The docs list all of it under application data, and Where Claude Code stores your sessions walks through the layout.

Codex writes one rollout file per session under ~/.codex/sessions/YYYY/MM/DD/rollout-<timestamp>-<id>.jsonl (see Where Codex stores sessions). The official Codex docs do not state that path; it is what Codex CLI 0.166.0 writes on disk. The same ~/.codex holds session_index.jsonl and several .sqlite files, and nothing documents which of them codex resume needs. For Codex, copy sessions/, session_index.jsonl and history.jsonl, then test a restore.

Leave out ~/.claude/.credentials.json and ~/.codex/auth.json. They are login credentials, and a shared backup that contains them is a bigger target for no gain. ~/.claude/plugins/ and the caches rebuild themselves.

Stop the deletion first

Claude Code deletes transcripts older than cleanupPeriodDays at startup, 30 days by default (settings reference, quoted in this issue). A backup that mirrors the source mirrors the deletion too, so raise the setting before anything else:

{
  "cleanupPeriodDays": 3650
}

That goes in ~/.claude/settings.json. A maintainer reproduced on v2.1.233 that 3650 keeps the file. The longer story is in Claude Code silently deletes transcripts after 30 days.

rsync

mkdir -p ~/agent-backup/claude ~/agent-backup/codex
rsync -a --ignore-existing ~/.claude/projects/ ~/agent-backup/claude/projects/
rsync -a ~/.claude/history.jsonl ~/agent-backup/claude/
rsync -a ~/.codex/sessions/ ~/agent-backup/codex/sessions/

There is no --delete on purpose. With it, a file that Claude Code swept from the source is swept from the backup on the next run, which is the exact failure you set out to avoid.

--ignore-existing on the transcripts makes the run fast, since a finished session never changes. The cost is that a session still being written gets copied once, half-finished, and never refreshed. Drop the flag, or run the backup when no agent is open, if you want live sessions updated; plain -a compares size and mtime and re-copies files that grew.

A transcript is JSONL that grows at the end, so a copy taken mid-write can end in a partial last line. The docs give no guarantee either way. jq -c . file > /dev/null will tell you if one parses.

For scheduling, cron or launchd is enough. A Claude Code SessionEnd hook also works: it receives transcript_path and session_id on stdin (hooks reference). The docs give SessionEnd hooks a shared 1.5-second budget, so the hook should be a plain cp, never an upload. They also warn that the transcript is written asynchronously and may lag, so a hook copy can miss the last turn. Use it on top of the scheduled sync, not instead of it.

git

Git gives you history and diffs, and it is a poor fit here.

Transcripts are append-only text, so each version of a growing file becomes a new delta-packed blob. That works, but the repository grows steadily. They also contain whatever tools printed. Issue #63593 describes a private dotfile-sync repo that mirrored ~/.claude/ and triggered 15 GitHub secret-scanning alerts across eight secret families; it was closed as a duplicate on 2 Aug 2026. And a hosted remote ends up holding your source, prompts and pasted secrets in plaintext.

If you still want git, use a local-only repo or a remote you run, and scan before every commit. Secrets in Claude Code transcripts covers the scanning.

Encrypted archive with age

For a cloud drive, a USB stick or a colleague's server, encrypt before the data leaves. age encrypts a stream to a public key or a passphrase. It does not handle directories, so pipe tar into it:

age-keygen -o ~/age-backup-key.txt          # keep this file somewhere else
RECIP=$(grep -o 'age1[a-z0-9]*' ~/age-backup-key.txt | head -1)
tar -C ~/.claude -cf - projects history.jsonl \
  | age -r "$RECIP" -o ~/agent-backup/claude-$(date +%F).tar.age

Restoring:

age -d -i ~/age-backup-key.txt claude-2026-11-10.tar.age | tar -xf - -C /tmp/restore

Run as written, that round trip lists the files back. Its weakness is that every run is a full copy with no deduplication, so a month of daily archives is a month of near-identical files. restic adds snapshots (restic backup, restic snapshots, restic restore) and restic check. Its encryption design is not verified here, so read its docs before relying on it.

Encrypting coding-agent transcripts at rest covers the threat model behind these choices.

The three side by side

rsyncgittar + age
SetupOne lineRepo + ignore rulesKey + script
Keeps deleted-at-source filesYes, without --deleteYesYes (old archives)
Space per runOnly new filesDeltasFull copy each time
Protects against a stolen backupNoNoYes
Easy to restore one sessionYesYesUnpack first
Secret riskSame as sourceHigh if pushedLow

Restore one session before you need to

A backup you have not restored is a guess. Copy a single session into a scratch directory and run claude --resume against it, or at least check that the JSONL parses. Claude Code finds sessions by project folder name, so a restore to a different path needs the folder renamed to match; Move Claude Code sessions to a new computer deals with that.

Doing it with Sealkeep

Sealkeep automates the same job. It encrypts each session with its own key, copies it to storage you pick (S3, R2, B2, GCS, Google Drive), reads the copy back to verify it, and only then, if you opt in, lets the local original go. sealkeep verify re-reads every stored backup and sealkeep recover <id> <dest> restores one.

It backs up agent sessions and nothing else, so it is not a general backup tool, and its encryption format has not been independently audited. Reclaiming local originals currently requires Sealkeep Cloud; with your own bucket the originals stay on the machine. If rsync and a cron line already cover you, skip it.

FAQ

Can I just back up the whole ~/.claude folder?

You can, but you will copy credentials and caches along with it. Back up projects/ and history.jsonl; .credentials.json is a login file that should not sit in a shared backup.

Does cleanupPeriodDays delete my backup too?

No, it only touches folders under ~/.claude on that machine. A backup disappears only when your sync tool propagates deletions, as rsync --delete does.

Does Codex have a retention setting like Claude Code?

OpenAI's official docs do not document one. The open issue openai/codex#34061 (opened 18 Jul 2026, still open at the last comment on 3 Nov) reports 755 GiB of session logs on one machine, so size is worth watching. See Codex sessions disk usage.