Skip to main content
hook

backup-before-edit — Claude Code hook: file backup before edits

A Claude hook that copies a file with a timestamp before every edit. Useful when the agent rewrites code and you want a way back.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook pre-tool/backup-before-edit
Open on GitHub

What backup-before-edit does

The hook binds to PreToolUse with the Edit matcher. Before Claude touches a file, the command checks that the file exists and copies it next to itself as filename.backup.timestamp, where the timestamp comes from date +%s. New files are skipped, since there is nothing to copy.

Copy errors are swallowed with 2>/dev/null || true, so the hook never blocks an edit even when a path is off. Handy, but it also means a failed backup stays silent. On large files or slow disks the copy adds a pause before each edit, so it is worth checking how much space the backups take in your working tree.

Install with npx claude-code-templates@latest --hook pre-tool/backup-before-edit; the config lands in .claude/settings.json. Restoring is manual: find the .backup.<timestamp> you want and put it back under the original name.

What it helps with

  • roll back a file after a bad agent edit
  • keep a version before a large refactor
  • guard configs while Claude edits them
  • recover the previous contents when an edit breaks the build

Example prompts for the agent

  • Refactor parser.py; the backup-before-edit hook will keep a copy before the edit
  • Update .claude/settings.json and let the backup happen automatically
  • Rewrite load_config, then list the backups that appeared

How to install backup-before-edit

1claude-code-templates

npx claude-code-templates@latest --hook pre-tool/backup-before-edit

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about backup-before-edit

How do I install backup-before-edit in Claude?

Run npx claude-code-templates@latest --hook pre-tool/backup-before-edit, then check the entry in .claude/settings.json.

What does backup-before-edit do?

Before each Edit it writes a timestamped copy next to the file. Existing files get copied, new ones are left alone.

How do I restore a file from the backup?

Find the copy with the timestamp you need next to the original and copy it back under the original name. The hook does not roll back on its own.

Source text

backup-before-edit.json · MIT
{
  "description": "Create automatic backup of files before any Edit operation for safety. This hook creates a timestamped backup copy (filename.backup.timestamp) of any existing file before Claude modifies it. Provides a safety net to recover previous versions if needed. Only backs up existing files, includes error suppression to handle edge cases gracefully.",
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "if [[ -f \"$CLAUDE_TOOL_FILE_PATH\" ]]; then cp \"$CLAUDE_TOOL_FILE_PATH\" \"$CLAUDE_TOOL_FILE_PATH.backup.$(date +%s)\" 2>/dev/null || true; fi"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

All hooks for Agent setup: memory, context, orchestration →

From the same set205

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor