garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /docs/backup-and-sync.md

raw

Backup and sync

The person's data lives in the vault (roadmap.md, encrypted storage), and there is no server. So far a lost or broken phone took everything with it. This is how Pinnacle keeps a copy: on the person's other devices, kept in step over the p2p layer, and on a backup disk, encrypted, for a person with one device. Both restore onto a new device.

Written 2026-11-29 (session s912) as the design for full-os.md's Need-1 item "Backup and restore of the vault, and sync between the person's own devices over p2p".

What a person sees

Settings → Devices lists the person's own devices: this one, and every device linked to it, each with when it was last in step.

The same page's Backup writes the vault's files to a backup disk (a USB stick, an SD card; in QEMU a third disk) whose file system is labelled backup, encrypted, and restores them from one.

What is kept in step

Every file in the vault (data:/), but what belongs to one device:

Not syncedWhy
data:/p2p/identityEach device keeps its own identity key, so a lost one can be dropped alone and its key never travels.
data:/bt/bondsA Bluetooth bond is the key of one radio with one other radio.
data:/bt/identityThis device's Bluetooth identity key: devices paired with know it by it, and a second device with it would pass for this one.
data:/sync/The sync service's own state: what each device last agreed on.
data:/display/The display server's: the past notifications this device showed, and how many were seen here.

So the apps' folders (data:/apps/notes, …), the address book and the contacts' cards, conversations, texts, Wi-Fi networks and their passwords, the agent's key and its log all go to every linked device. The system disk (system:/settings, installed apps) does not: that is the device's, and the Store reinstalls apps.

How it works

Own devices

A person's devices are not one identity: each keeps its own (p2p), and the person's devices are contacts of each other, marked own in data:/sync/devices (their keys, 32 bytes each). Only an own device may open a sync stream, and only to a device that has it as own too: the p2p service checks the list before it connects a stream tagged sync to the sync service. Being a contact or being offered services is not enough.

Linking, through the DHT

The two devices do not know each other yet, and the p2p layer opens sessions only between contacts. The code carries them there:

  1. Device A draws a random code c (60 bits: twelve characters of Crockford's base 32). From it: a key k = HKDF(c, "pinnacle link key") and two places in the DHT, offer = SHA-256("pinnacle link offer" ‖ c) and answer = SHA-256("pinnacle link answer" ‖ c).
  2. A stores a link record at offer: its card, sealed under k (ChaCha20-Poly1305), and keeps storing it every 30 s while the code shows.
  3. B, given c, looks up offer, opens the seal (a wrong code finds nothing, or a record that does not open), checks the card's signature, and stores its own card sealed under k at answer.
  4. A looks up answer every three seconds while the code shows; when it finds B's card, both have the other's card, add it as a contact and as own, and A's code is spent.

A link record is the DHT's second kind of record beside presence: a kind byte, the sealed card, valid ten minutes, at most 64 held by a node. The nodes that hold it cannot open it. Someone who learns the code in those ten minutes could link a device of theirs: the code is shown on the screen and nowhere else, as a Bluetooth pairing number is.

Sync, between two devices

The sync service (system-sync, a system program, so the kernel keeps installed apps from reaching it) walks the vault, and whenever an own device is reachable, every 30 s and after a change, runs a sync with it over a p2p stream. The logic is a pure state machine in the crate sync, like net and p2p: messages in, messages out, files read and written through a trait, so two devices are tested in memory.

  1. Manifests. Each side lists its files: path, size, time of change and SHA-256. The service keeps what it hashed (data:/sync/index) and hashes again only a file whose size or time changed, or whose time is this very second (a second write in the same second would otherwise be missed).
  2. The base. For every own device, each keeps the base: every path and its hash and size as the two last agreed (data:/sync/base/<key>).
  3. Three ways. The device with the lower key decides, for each path, from its own version L, the other's R and the base B:
CaseDone
L = Rnothing
L = B, R changed (or new)take R
R = B, L changed (or new)send L
L gone, R = Bremove it there
R gone, L = Bremove it here
one gone, the other changedthe changed one stays, and goes to both
both changed, each starting with Bmerged: B, then what was added here, then what was added there
both changed otherwiseboth kept: the newer under its name, the other beside it as name (from <device>).ext

The merge is what keeps logs whole: a conversation, the texts, the agent's log only ever grow at their end, so two devices that both added to one keep all of it. Nothing is lost in any case: a conflict leaves both versions for the person. 4. Transfer. Files go in pieces that fit one message (3 KiB), each file written whole when its last piece is in (one transaction of the file system, so a file is the old one or the new one, never half). 5. The new base. When every step is done the deciding device sends the paths and hashes they now share; both keep it as the new base. A sync cut off halfway keeps the old base, and the next one finds again what is left: what was already copied is equal on both sides and costs nothing.

A program that holds a file in memory has to hear that it changed. The p2p service (the address book, contacts, conversations) is told by the sync service after a sync changed data:/p2p/ and reads them again; an app sees a change the next time it reads the file.

Backup to a disk

A backup disk is a disk whose plain file system is labelled backup. The backup of a device is backup:/pinnacle/<its key>/:

A backup writes the blobs that are missing, then the index, then removes blobs no index names. A restore reads the index and writes each file into the vault, a file there already with the same hash left alone. The same sync crate lists the files, hashes them and decides what to copy.

Limits, and what is next