Skip to content

Commit af17d0a

Browse files
Lemelsonclaude
andcommitted
MacBackup 1.0.0: verified, incremental disaster-recovery backup for developers' Macs
Engine (Python stdlib + zstd), SwiftUI app (English/Russian), scheduler, docs for people and AI agents on the backup drive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0 parents  commit af17d0a

42 files changed

Lines changed: 4424 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
state/
2+
logs/
3+
app/.build/
4+
app/build/
5+
__pycache__/
6+
*.pyc
7+
.DS_Store

‎AGENTS.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# MacBackup — notes for AI agents
2+
3+
Read this before restoring from this drive, or before debugging/changing MacBackup.
4+
Explain results to the owner in plain words; never print secret values (Keychain export, `.env`, keys).
5+
6+
## Where things are
7+
8+
| What | Path |
9+
|---|---|
10+
| Engine (one file, Python 3.9+ stdlib + `zstd` CLI) | on the Mac: `MacBackup.app/Contents/Resources/engine/macbackup.py`; copy here: `tools/macbackup.py` |
11+
| Settings | `~/Library/Application Support/MacBackup/config.json` |
12+
| Live status (updated every second while running) | `~/Library/Application Support/MacBackup/state/status.json` |
13+
| Index | `index.json` here (mirror: `…/MacBackup/state/index.json` on the Mac) |
14+
| Event log (JSON lines, all runs) | `logs/runs.jsonl` here and on the Mac in `…/MacBackup/logs/` |
15+
| Per-run log | `logs/run-<run_id>.log` |
16+
| Unfinished run marker | `…/MacBackup/state/pending.json` (`since`) |
17+
| Scheduler | `~/Library/LaunchAgents/com.macbackup.auto.plist` → `MacBackup --auto` → engine `auto` |
18+
19+
## Commands
20+
21+
```bash
22+
E="python3 <path>/macbackup.py"
23+
$E plan # scan only: sizes per part, what changed
24+
$E run # exit 0 ok · 1 errors · 2 no disk · 3 already running · 4 cancelled · 5 paused (disk gone)
25+
$E run --only projects/foo # one part (prefix)
26+
$E run --force --only ai/ # rebuild even if unchanged
27+
$E verify --all # re-verify every archive
28+
$E auto # scheduler logic
29+
$E cancel
30+
```
31+
32+
## Model
33+
34+
- **Part (unit)** = `archives/<layer>/<id>.tar.zst` + manifest `<id>.files.tsv`
35+
(`path, size, mtime_ns, sha256, kind`; kind f=file, s=SQLite snapshot, l=symlink, g=generated).
36+
Layers: `keys`, `ai`, `projects`, `system`, `media` (`media` = plain uncompressed folder copies, path relative to the disk root).
37+
- Parts are rebuilt from the home folder on every run, so new folders are picked up automatically.
38+
- Paths in archives are relative to `~` → restore is always `tar -xf X -C ~`.
39+
- **Incremental**: fingerprint over (path, kind, size, mtime_ns, link target) + engine version + zstd args.
40+
Same fingerprint + archive present + last verification ok → skipped. Bump `ENGINE_VERSION` to rebuild everything.
41+
- **Excluded**: dependency/build/cache directory names (`EXCLUDE_DIR_NAMES`), videos > `video_skip_mb`,
42+
`.gguf/.safetensors` > 200 MB, `.f32` render buffers (list: `inventory/skipped-big-media.tsv`).
43+
- **SQLite** files are snapshotted with the backup API (gives up after 20 s if locked, then copies raw).
44+
- **Verification**: decompress, hash every member, compare with the manifest, compare the archive's SHA-256 with
45+
the one computed while writing, then spot-check up to 25 unchanged files against the originals.
46+
- Writes go to `*.part` and are renamed only after success. Leftovers are removed at the start of the next run.
47+
- **Disk disconnected / access denied mid-run** → `run_interrupted` event, status `interrupted`, `pending.json`
48+
stays; the scheduler resumes on the next mount. No notification. Reminders only start
49+
`reminder_grace_days` after a backup became due or an unfinished run began, at most once per `reminder_every_hours`.
50+
51+
## Reading the logs
52+
53+
`runs.jsonl` events: `run_start`, `unit_start`, `unit_done` (bytes_raw, bytes_archive, seconds, note), `unit_skip`,
54+
`unit_fail` (error), `verify_ok` / `verify_fail`, `warn` (unreadable files; "No access" = grant Full Disk Access to
55+
MacBackup.app), `copy_done` (mirror folders), `run_done` / `run_fail` / `run_cancel` / `run_interrupted` with totals, `info`.
56+
57+
```bash
58+
python3 -c "import json;i=json.load(open('index.json'));u=i['units'];print(i.get('last_success_at'),len(u),sum(not v.get('verify_ok') for v in u.values()),'failed')"
59+
tail -5 logs/runs.jsonl
60+
```
61+
62+
When you change something by hand, append an `info` line to `runs.jsonl` saying what and why, so the next agent can follow.

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Lemelson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<p align="center">
2+
<img src="docs/images/icon.png" width="96" alt="MacBackup icon">
3+
</p>
4+
5+
<h1 align="center">MacBackup</h1>
6+
7+
<p align="center">
8+
<b>A disaster-recovery backup for people who build things on their Mac.</b><br>
9+
Keys, server access, Codex &amp; Claude Code history, every project with its git history —<br>
10+
compressed, verified byte by byte, and restorable on any Mac with one built-in command.
11+
</p>
12+
13+
<p align="center">
14+
<a href="../../releases/latest">Download</a> ·
15+
<a href="#quick-start">Quick start</a> ·
16+
<a href="#restore-on-a-new-mac">Restore</a> ·
17+
<a href="#macbackup-vs-time-machine">vs Time Machine</a> ·
18+
<a href="#how-it-works">How it works</a>
19+
</p>
20+
21+
<p align="center">
22+
<img src="docs/images/app-overview.png" width="820" alt="MacBackup main window">
23+
</p>
24+
25+
---
26+
27+
## Why
28+
29+
If your laptop dies today, what do you actually lose?
30+
31+
Not your photos — they are in iCloud. Not your apps — they reinstall. What hurts is the stuff that
32+
**only exists on this one machine**:
33+
34+
- the SSH key that gets you into your production server, and the root password you saved in Keychain;
35+
- `.env` files with API keys for twenty projects;
36+
- months of **Codex and Claude Code** sessions, skills, memory and settings;
37+
- commits you never pushed, branches with no upstream, folders that were never on GitHub at all;
38+
- the little scripts, login items and dotfiles that make your Mac *yours*.
39+
40+
MacBackup copies exactly that to an external drive — about **150 GB instead of a full 800 GB disk image** —
41+
and keeps it fresh automatically.
42+
43+
<p align="center"><img src="docs/images/whats-inside.svg" width="900" alt="What goes into the backup"></p>
44+
45+
## Features
46+
47+
- **Smart scope.** Every folder in your home and in `~/Documents` becomes its own archive. AI tools
48+
(Codex, Claude Code, Claude app, Gemini/Antigravity, Cursor, Grok) are detected and split sensibly —
49+
Codex sessions per month, Claude Code chats per project.
50+
- **Leaves out what comes back by itself:** `node_modules`, virtualenvs, build output, caches, large videos
51+
inside projects (listed in `inventory/skipped-big-media.tsv` so you know exactly what was skipped).
52+
- **Incremental.** A fingerprint of names, sizes and change dates decides what changed. The first run
53+
takes about an hour; a weekly run usually takes a few minutes.
54+
- **Verified, not hoped.** After writing, every archive is decompressed and **every file is SHA-256-checked**
55+
against the manifest, then up to 25 files per part are compared with the originals on your Mac.
56+
- **Live databases done right.** SQLite files (bots, apps, Codex history) are copied with SQLite's backup
57+
API, so you get a consistent snapshot even while the app is writing.
58+
- **Secrets collected in one place.** All `.env`, `.pem`, tokens and key folders from every project are also
59+
gathered into `keys/secret-files`; optionally chosen Keychain passwords are exported too.
60+
- **Restore needs nothing.** Archives are `tar` + `zstd`; macOS's built-in `tar` reads them:
61+
`tar -xf part.tar.zst -C ~`.
62+
- **Calm about real life.** Unplug the drive in the middle of a backup — nothing breaks. It pauses, cleans up
63+
the half-written file next time and continues where it stopped. No alarm notifications; a reminder comes
64+
only if a backup is overdue for days.
65+
- **Native Mac app** with a menu-bar icon: progress, speed, time left, what's inside, run history and logs.
66+
English and Russian.
67+
- **Agent-friendly.** The drive carries a `README.md` for people and an `AGENTS.md` for AI agents, plus a
68+
machine-readable `index.json`, per-file manifests and full logs — point Claude Code or Codex at the drive
69+
and ask it to restore your Mac.
70+
71+
<p align="center"><img src="docs/images/app-running.png" width="820" alt="A backup in progress"></p>
72+
73+
## MacBackup vs Time Machine
74+
75+
They solve different problems, and they work best **together**.
76+
77+
| | Time Machine | MacBackup |
78+
|---|---|---|
79+
| What it copies | The whole disk, apps and system included | The work that exists only on this Mac |
80+
| Size on the drive | ≈ everything you use (often 500 GB – 1 TB) + history | ≈ 15–20% of it (e.g. 150 GB of 813 GB) |
81+
| Shares a drive with your films | Wants its own volume and space for history | A folder next to anything else |
82+
| Restore | Whole Mac via Migration Assistant | Any single project, or everything, with `tar` — on any Mac |
83+
| Needs | Same macOS family, Migration Assistant | Nothing but the built-in `tar` |
84+
| Proof the copy is good | Trust | Every file re-hashed after every run |
85+
| Understands dev work | No — copies `node_modules` and caches too; a live database may be caught mid-write | Skips rebuildable junk, snapshots live DBs, gathers every secret, reports unpushed git work |
86+
| Readable without the app | Only through Time Machine | Plain archives + manifests + `README.md` / `AGENTS.md` |
87+
| History of old versions | Yes (hourly/daily/weekly) | Latest version of each part |
88+
89+
**Recommendation:** use Time Machine on a dedicated drive if you can, for the full-system safety net and file
90+
history. Use MacBackup for the irreplaceable work — it fits on the drive you already carry, and it's the one
91+
you'll actually be able to restore from on a borrowed Mac in an hour.
92+
93+
## How it works
94+
95+
<p align="center"><img src="docs/images/how-it-works.svg" width="900" alt="How a backup run works"></p>
96+
97+
<details>
98+
<summary><b>Why zstd -9 (measured)</b></summary>
99+
100+
700 MB samples on an M1 Max:
101+
102+
| codec | ratio on code / chats | speed |
103+
|---|---|---|
104+
| zstd -3 | ×1.49 | 1460 MB/s |
105+
| **zstd -9 --long=27** | **×1.59** | **360–440 MB/s** |
106+
| zstd -15 --long=27 | ×1.61 | 65–130 MB/s |
107+
| zstd -19 --long=27 | ×1.64 | 13–15 MB/s |
108+
| xz -6 | ×1.56 | 22 MB/s |
109+
| brotli -9 | ×1.58 | 3–11 MB/s |
110+
111+
Level 9 with a long window is within 1–3% of the strongest settings at 15–30× the speed.
112+
</details>
113+
114+
## Quick start
115+
116+
1. **Install the dependency** (once): `brew install zstd` — Python 3 ships with macOS / Xcode tools.
117+
2. **Download** `MacBackup.zip` from [Releases](../../releases/latest), unzip, move `MacBackup.app` to
118+
`/Applications` or `~/Applications`. The app is ad-hoc signed: the first time, right-click → **Open**.
119+
3. Open **Settings** (⌘,): choose your backup disk, language, and how often to back up (default: every 7 days).
120+
4. Give MacBackup access to your files: **System Settings → Privacy & Security → Full Disk Access → MacBackup**.
121+
5. Click **Back up now**. The first run takes a while; after that it's minutes.
122+
123+
To back up automatically, install the scheduler (runs hourly and whenever a disk is connected; it only does
124+
work when a backup is due):
125+
126+
```bash
127+
./scripts/install-scheduler.sh
128+
```
129+
130+
### Configuration
131+
132+
`~/Library/Application Support/MacBackup/config.json` — the app edits the common keys; the rest is for power users.
133+
See [`config.example.json`](config.example.json):
134+
135+
| key | meaning |
136+
|---|---|
137+
| `disk_volume`, `dest_folder` | where the backup lives, e.g. `/Volumes/Backup` + `MacBackup` |
138+
| `due_days`, `reminder_grace_days` | back up every N days; remind only after N more days without the disk |
139+
| `split_dirs` | big folders archived per child (e.g. `Documents/Codex`) |
140+
| `system_folders` | home folders that are "system" rather than projects (e.g. `Backups`) |
141+
| `home_skip`, `documents_skip`, `exclude_paths` | things to leave out |
142+
| `mirror_folders` | folders copied as-is, uncompressed (e.g. course videos you also want to watch from the drive) |
143+
| `secret_extra_paths` | extra key files/folders to gather into `keys/secret-files` |
144+
| `keychain_export_regex` | export these Keychain service passwords **with values** (empty = off). Only use on an encrypted drive. |
145+
| `video_skip_mb` | skip videos larger than this inside projects (default 20) |
146+
147+
### Command line
148+
149+
```bash
150+
E="python3 MacBackup.app/Contents/Resources/engine/macbackup.py"
151+
$E plan # what would be backed up, sizes, what changed — writes nothing
152+
$E run # back up now
153+
$E verify --all # re-check every archive on the drive
154+
$E auto # what the scheduler runs
155+
$E cancel # stop gracefully
156+
```
157+
158+
## Restore on a new Mac
159+
160+
Every archive restores into your home folder with the tar that comes with macOS:
161+
162+
```bash
163+
cd /Volumes/<YourDisk>/MacBackup
164+
for a in archives/keys/*.tar.zst; do tar -xf "$a" -C ~; done # keys first
165+
chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
166+
find archives -name '*.tar.zst' -print0 | xargs -0 -n1 tar -C ~ -xf # or everything
167+
```
168+
169+
- Need one file? `grep -l "name" archives/*/*.files.tsv archives/*/*/*.files.tsv` tells you which archive has it.
170+
- Apps: `inventory/apps.tsv` and `inventory/brew.txt` list what to reinstall.
171+
- The drive's own [`README.md`](engine/DISK-README.md) walks through it step by step, and
172+
[`AGENTS.md`](engine/DISK-AGENTS.md) lets an AI agent do it for you.
173+
174+
## Safety
175+
176+
- **Encrypt the backup drive** (Finder → right-click the disk → Encrypt, or format as APFS (Encrypted)).
177+
Archives themselves are not password-protected, by design: you should never be locked out of your own
178+
backup by a forgotten second password. The drive's encryption is the lock.
179+
- Keychain export is **off by default**. When enabled, values go only into the archive on the drive — never into
180+
logs, the app, or the terminal.
181+
- Nothing is uploaded anywhere. No network access at all.
182+
183+
## Project layout
184+
185+
```
186+
engine/macbackup.py the whole engine (Python 3.9+, stdlib only, + zstd CLI)
187+
engine/DISK-README.md copied to the drive as README.md (for people)
188+
engine/DISK-AGENTS.md copied to the drive as AGENTS.md (for AI agents)
189+
app/ SwiftUI app (Swift Package) — build with app/build.sh
190+
scripts/ scheduler install/uninstall
191+
docs/images/ pictures for this README
192+
```
193+
194+
Data on your Mac: `~/Library/Application Support/MacBackup/` (`config.json`, `state/`, `logs/`).
195+
Logs are JSON lines (`logs/runs.jsonl`) plus a human-readable log per run, mirrored to the drive.
196+
197+
## Build from source
198+
199+
```bash
200+
git clone https://github.com/Lemelson/MacBackup && cd MacBackup
201+
./app/build.sh # builds and installs ~/Applications/MacBackup.app (bundles the engine)
202+
```
203+
204+
Requires Xcode command line tools (Swift 5.10+), macOS 14+.
205+
206+
## License
207+
208+
MIT — see [LICENSE](LICENSE).

‎app/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.build/
2+
build/
3+
testdata/data/
4+
testdata/disk/

‎app/Package.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// swift-tools-version:6.2
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "MacBackup",
6+
defaultLocalization: "ru",
7+
platforms: [.macOS(.v14)],
8+
targets: [
9+
.executableTarget(
10+
name: "MacBackup",
11+
path: "Sources/MacBackup",
12+
swiftSettings: [.defaultIsolation(MainActor.self)]
13+
)
14+
]
15+
)

‎app/README.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# MacBackup app
2+
3+
A native macOS window + menu bar item for the MacBackup engine (`../engine/macbackup.py`).
4+
The app never copies files itself: it shows the engine's status and starts it.
5+
UI in English or Russian (switch in Settings ⌘, or the globe menu in the main window).
6+
7+
## Build
8+
./build.sh # builds, bundles ../engine/macbackup.py, installs ~/Applications/MacBackup.app
9+
`BUNDLE_ID=…` overrides the bundle identifier (default `app.macbackup.MacBackup`). Ad-hoc signed;
10+
every rebuild changes the signature, so macOS may ask again for Full Disk Access / notifications.
11+
12+
## Command-line modes
13+
- `MacBackup --auto` — no window or menu bar item: runs `macbackup.py auto` as a child process (file access
14+
is attributed to MacBackup.app, which gets Full Disk Access once), waits, returns its exit code,
15+
appends output to `<data>/logs/auto.log`. Meant for launchd (hourly and on disk mount).
16+
- `MacBackup --notify "<title>" "<body>"` — posts a notification as the app and exits. The engine calls it
17+
(the app passes its own path to the engine in `MACBACKUP_APP`).
18+
- Manual backups from the window also run as a child of the app. Closing the window keeps the app in the
19+
menu bar; quitting during a backup asks first.
20+
21+
## Where data lives
22+
`~/Library/Application Support/MacBackup/` (override: `MACBACKUP_DATA`):
23+
`config.json`, `state/status.json`, `state/index.json` (mirror), `logs/runs.jsonl`, `logs/run-<id>.log`,
24+
`logs/app-launch.log`, `logs/auto.log`. On the backup disk: `<disk_volume>/<dest_folder>/index.json` and `README.md`.
25+
Python: `MACBACKUP_PYTHON`, else the first of /opt/homebrew/bin, /usr/local/bin, /usr/bin `python3`.
26+
Engine: `MACBACKUP_ENGINE`, else `MacBackup.app/Contents/Resources/engine/macbackup.py`.
27+
28+
`config.json` (app reads and writes, engine reads; unknown keys are preserved):
29+
`{"language": "en"|"ru", "disk_volume": "/Volumes/Backup", "dest_folder": "MacBackup", "due_days": 7, "reminder_grace_days": 3}`.
30+
31+
## Engine contract (what the app reads)
32+
- `status.json`: `state` (idle | running | verifying | done | failed | cancelled | interrupted), `run_id`, `mode`,
33+
`started_at`, `updated_at`, `finished_at`, `pending_since`, `disk_mounted`, `disk_free_bytes`,
34+
`phase` (scan | archive | verify | copy | finish), `current_unit`, `units_total/done/skipped_unchanged/failed`,
35+
`bytes_total/done`, `speed_bps`, `eta_seconds`, `last_success_at`, `message`, `pid`.
36+
running/verifying with a dead pid or `updated_at` older than 2 min is shown as interrupted.
37+
- `index.json`: `{version, updated_at, units: {"<layer>/<name>": {layer (keys|ai|projects|system|media), title,
38+
source, archive, files, bytes_raw, bytes_archive, archive_sha256, created_at, verified_at, verify_ok, seconds,
39+
skipped_large_files}}}`. Media `archive` is a plain folder relative to the disk root (shown uncompressed).
40+
- `runs.jsonl`: one event per line `{ts, run_id, event, unit?, error?, message?, mode?}`; totals on
41+
run_done / run_fail / run_cancel (`units_done` = changed only, `units_skipped_unchanged`, `units_failed`,
42+
`bytes_raw`, `bytes_archive`, `seconds`). A `warn` mentioning "No access"/"Нет доступа" in the newest run shows
43+
the Full Disk Access banner. Broken lines are skipped. Dates are ISO 8601 (no zone = local time).
44+
- Engine exit code 3 = already running.
45+
46+
## Test mode (fake data, no real backups)
47+
python3 testdata/make_testdata.py running en # idle|running|verifying|stalled|interrupted|failed|due|empty|noaccess|nodisk, en|ru
48+
MACBACKUP_TESTDATA=$PWD/testdata ~/Applications/MacBackup.app/Contents/MacOS/MacBackup --test [--tab=history] [--settings]
49+
testdata/shot.sh running ../docs/images/app-running.png en # window-only screenshot (APPEARANCE=light, HIDE_BADGE=1)
50+
Test mode reads `testdata/data` and runs `testdata/fake_engine.py` (pretends for ~30 s).
51+
`MACBACKUP_APPEARANCE=light|dark` forces the look.

0 commit comments

Comments
 (0)