# arelius-d/cerebro architecture

> Architecture diagram of the GitHub repository arelius-d/cerebro, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arelius-d/cerebro
- Repository: https://github.com/arelius-d/cerebro

## Overview

#### Purpose and entry points
Cerebro is a portable Linux backup suite centered on content-aware snapshot decisions rather than timestamp-only replication. The primary engine is `cerebro.sh`; it is configured by `cerebro.cfg` and runs manually or through a self-managed user cron entry. Recovery is provided by `recall.sh`, with an optional Python terminal UI in `recall-tui.py`. `install.sh` bootstraps a standalone installation.

#### Backup orchestration and isolation
`cerebro.sh` owns process lifecycle: dependency checks, per-script-name `/tmp` locking with stale-lock recovery, temporary workspace creation, `EXIT` cleanup, and guaranteed post-backup hook execution. The script directory is the instance boundary: local logs, temporary `backups/`, and `assets/` metadata are instance-scoped, while destinations can be shared safely through hostname subdirectories.
Paths: `cerebro.sh`, `cerebro.cfg`

#### Configuration and safety gate
The INI-style configuration defines included live paths, exclusions, destinations, classification rules, scheduling, hooks, transfers, and retention. Before any work, the engine validates that every `[CRUCIAL]` rule is covered by `[INCLUDE]`; later, deletion of a previously backed-up crucial path aborts the new snapshot rather than committing an incomplete state.
Paths: `cerebro.cfg`, `cerebro.sh`

#### Snapshot discovery, staging, and comparison
Each run probes all configured destination/hostname directories, verifies candidate archives with `tar -tzf` under `timeout`, removes corrupted files, and selects the newest valid archive. It extracts that baseline into `/tmp`, stages a new tarball from live include paths, and compares old versus new state. `[LOGDIFF]` files receive unified content diffs in the execution log; `[NOLOG]` changes remain detectable but suppress verbose diff content.
Paths: `cerebro.sh`, `cerebro.cfg`

#### Decision, metadata, and archive integrity
The comparison classifies a candidate as no-change, `RETAIN`, or `DISCARD:LATEST`; no-change archives are removed, and crucial deletion exits nonzero. Backup classifications are persisted in `assets/.tar_meta_data.txt` (created at runtime) and drive later pruning. Final archives are standard gzip-compressed tar files and must pass a post-creation `tar -tzf` integrity check before distribution.
Paths: `cerebro.sh`, `cerebro.cfg`

#### Distribution, retention, and scheduling
Validated archives are copied to every reachable destination using retrying, timeout-bounded `rsync`, with `cp -ru` fallback. Unavailable mounts are isolated through responsiveness checks so other destinations can proceed. Retention removes superseded DISCARD archives and optionally age/count-expired RETAIN archives while preserving at least one backup; metadata is updated synchronously. The engine installs or updates its cron invocation based on `[SCHEDULE]`.
Paths: `cerebro.sh`, `cerebro.cfg`

#### Recovery interfaces
`recall.sh` reads the same config, discovers the latest valid archive across destinations (or accepts `--backup`), searches archive members, and restores safely beside live paths using `.bak`/`_bak` suffixes. With no arguments or `--tui`, it delegates interactive archive browsing, previews/diffs, selection, and restoration to the curses-oriented Python UI. Recall keeps a separate local audit log.
Paths: `recall.sh`, `recall-tui.py`, `cerebro.cfg`

#### Runtime and external boundaries
The supported runtime is Bash 4+ on Linux plus GNU/POSIX utilities: `tar`, `rsync`, `diff`, `gawk`, `timeout`, `grep`, `sed`, and filesystem tools. Cron is the scheduling infrastructure; configured hooks may control Docker, databases, or other local services. Normal backup operation is offline and filesystem/mount based; network access is only material for optional installation or self-upgrade through GitHub.
Paths: `install.sh`, `cerebro.sh`, `recall-tui.py`

Main entry points: [`cerebro.sh`](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh) (Cerebro engine), [`recall.sh`](https://github.com/arelius-d/cerebro/blob/main/recall.sh) (Recall CLI), [`cerebro.cfg`](https://github.com/arelius-d/cerebro/blob/main/cerebro.cfg) (Backup configuration), [`install.sh`](https://github.com/arelius-d/cerebro/blob/main/install.sh) (Installer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backup["Backup engine"]
  node_config["Backup configuration<br/>INI config<br/>[cerebro.cfg]"]
  node_engine["Cerebro engine<br/>Bash orchestrator<br/>[cerebro.sh]"]
  node_safety{{"Safety gate<br/>rule validation<br/>[cerebro.sh]"}}
  node_baseline["Baseline discovery<br/>archive selection<br/>[cerebro.sh]"]
  node_staging["Snapshot staging<br/>tar workspace<br/>[cerebro.sh]"]
  node_comparison["Content comparison<br/>change classifier<br/>[cerebro.sh]"]
  node_metadata["Archive metadata<br/>classification index<br/>[cerebro.sh]"]
  node_integrity{{"Archive integrity check<br/>tar validation<br/>[cerebro.sh]"}}
  node_distribution["Distribution<br/>replicated transfer<br/>[cerebro.sh]"]
  node_retention["Retention pruning<br/>archive lifecycle<br/>[cerebro.sh]"]
  node_scheduler["User cron management<br/>scheduler integration<br/>[cerebro.sh]"]
end

subgraph group_recovery["Recovery"]
  node_recall["Recall CLI<br/>Bash recovery<br/>[recall.sh]"]
  node_tui["Recall terminal UI<br/>Python curses UI<br/>[recall-tui.py]"]
  node_audit["Recall audit log<br/>local log<br/>[recall.sh]"]
end

subgraph group_external["Runtime boundaries"]
  node_live_paths[("Live included paths<br/>filesystem source")]
  node_destinations[("Destination mounts<br/>archive storage")]
  node_hooks["Configured hooks<br/>local service control"]
  node_runtime["Linux shell toolchain"]
  node_cron(("User cron<br/>scheduler"))
  node_github["GitHub<br/>optional network source"]
end

node_install["Installer<br/>bootstrap script<br/>[install.sh]"]

node_install -->|"installs"| node_engine
node_install -.->|"optional download"| node_github
node_config -->|"configures"| node_engine
node_engine -->|"validates first"| node_safety
node_safety -->|"permits backup"| node_baseline
node_config -->|"destinations"| node_baseline
node_destinations -->|"candidate archives"| node_baseline
node_baseline -->|"valid baseline"| node_staging
node_live_paths -->|"included content"| node_staging
node_staging -->|"old and candidate state"| node_comparison
node_comparison -->|"retained candidate"| node_integrity
node_comparison -->|"classification"| node_metadata
node_integrity -->|"validated archive"| node_distribution
node_distribution -->|"replicates"| node_destinations
node_metadata -->|"pruning decisions"| node_retention
node_retention -->|"removes archives"| node_destinations
node_engine -->|"guaranteed post-backup"| node_hooks
node_engine -->|"updates schedule"| node_scheduler
node_scheduler -->|"manages invocation"| node_cron
node_cron -->|"runs"| node_engine
node_config -->|"configures"| node_recall
node_destinations -->|"archives"| node_recall
node_recall -->|"safe restore"| node_live_paths
node_recall -->|"delegates interactive mode"| node_tui
node_recall -->|"writes"| node_audit
node_runtime -.->|"executes"| node_engine
node_runtime -.->|"executes"| node_recall

click node_install "https://github.com/arelius-d/cerebro/blob/main/install.sh"
click node_config "https://github.com/arelius-d/cerebro/blob/main/cerebro.cfg"
click node_engine "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_safety "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_baseline "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_staging "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_comparison "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_metadata "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_integrity "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_distribution "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_retention "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_scheduler "https://github.com/arelius-d/cerebro/blob/main/cerebro.sh"
click node_recall "https://github.com/arelius-d/cerebro/blob/main/recall.sh"
click node_tui "https://github.com/arelius-d/cerebro/blob/main/recall-tui.py"
click node_audit "https://github.com/arelius-d/cerebro/blob/main/recall.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_config,node_engine,node_safety,node_baseline,node_staging,node_comparison,node_metadata,node_integrity,node_distribution,node_retention,node_scheduler toneBlue
class node_recall,node_tui,node_audit toneAmber
class node_live_paths,node_destinations,node_hooks,node_runtime,node_cron,node_github toneMint
class node_install toneNeutral
```

## Components

### Backup engine

Instance-scoped backup lifecycle.

- [Backup configuration](https://github.com/arelius-d/cerebro/blob/main/cerebro.cfg): INI config · Defines paths, rules, destinations, retention, hooks, and schedule. · `cerebro.cfg`
- [Cerebro engine](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): Bash orchestrator · Manages locking, workspaces, cleanup, hooks, and backup flow. · `cerebro.sh`
- [Safety gate](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): rule validation · Validates crucial coverage and aborts on crucial deletion. · `cerebro.sh`
- [Baseline discovery](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): archive selection · Finds and validates the newest usable destination archive. · `cerebro.sh`
- [Snapshot staging](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): tar workspace · Extracts the baseline and creates a candidate archive from live paths. · `cerebro.sh`
- [Content comparison](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): change classifier · Produces diffs and classifies no-change, RETAIN, or DISCARD:LATEST. · `cerebro.sh`
- [Archive metadata](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): classification index · Persists archive classifications for pruning. · `cerebro.sh`
- [Archive integrity check](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): tar validation · Verifies the final gzip tar before distribution. · `cerebro.sh`
- [Distribution](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): replicated transfer · Uses bounded rsync retries with cp fallback per reachable destination. · `cerebro.sh`
- [Retention pruning](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): archive lifecycle · Prunes superseded and expired archives while preserving one backup. · `cerebro.sh`
- [User cron management](https://github.com/arelius-d/cerebro/blob/main/cerebro.sh): scheduler integration · Installs or updates the configured cron invocation. · `cerebro.sh`

### Recovery

Archive discovery and safe restore.

- [Recall CLI](https://github.com/arelius-d/cerebro/blob/main/recall.sh): Bash recovery · Finds valid archives, searches members, and restores beside live paths. · `recall.sh`
- [Recall terminal UI](https://github.com/arelius-d/cerebro/blob/main/recall-tui.py): Python curses UI · Browses, previews, diffs, selects, and restores archive content. · `recall-tui.py`
- [Recall audit log](https://github.com/arelius-d/cerebro/blob/main/recall.sh): local log · Separate recovery activity log. · `recall.sh`

### Runtime boundaries

Local tools, storage, and optional external access.

- **Live included paths**: filesystem source
- **Destination mounts**: archive storage · Shared destinations are isolated by hostname subdirectories.
- **Configured hooks**: local service control · May coordinate Docker, databases, or other local services.
- **Linux shell toolchain**: Bash 4+ and GNU/POSIX tools · Provides tar, rsync, diff, timeout, and filesystem operations.
- **User cron**: scheduler
- **GitHub**: optional network source · Used only for optional installation or self-upgrade.

### Other components

- [Installer](https://github.com/arelius-d/cerebro/blob/main/install.sh): bootstrap script · Bootstraps a standalone installation. · `install.sh`

## Connections

- Installer → Cerebro engine: installs
- Installer → GitHub: optional download
- Backup configuration → Cerebro engine: configures
- Cerebro engine → Safety gate: validates first
- Safety gate → Baseline discovery: permits backup
- Backup configuration → Baseline discovery: destinations
- Destination mounts → Baseline discovery: candidate archives
- Baseline discovery → Snapshot staging: valid baseline
- Live included paths → Snapshot staging: included content
- Snapshot staging → Content comparison: old and candidate state
- Content comparison → Archive integrity check: retained candidate
- Content comparison → Archive metadata: classification
- Archive integrity check → Distribution: validated archive
- Distribution → Destination mounts: replicates
- Archive metadata → Retention pruning: pruning decisions
- Retention pruning → Destination mounts: removes archives
- Cerebro engine → Configured hooks: guaranteed post-backup
- Cerebro engine → User cron management: updates schedule
- User cron management → User cron: manages invocation
- User cron → Cerebro engine: runs
- Backup configuration → Recall CLI: configures
- Destination mounts → Recall CLI: archives
- Recall CLI → Live included paths: safe restore
- Recall CLI → Recall terminal UI: delegates interactive mode
- Recall CLI → Recall audit log: writes
- Linux shell toolchain → Cerebro engine: executes
- Linux shell toolchain → Recall CLI: executes
