Collect read settings with settings.Load, which stands the defaults in for a file that is there but can't be read, or doesn't parse with no good .bak. A sync then pushed those defaults as "changed here", and every other computer brought them in over its own theme, language, text size, allow list and per-model settings; an export carried them too. Collect now reads through settings.LoadChecked and stops with its error, as it does for providers.json (9c54a560). The sync ends before anything is sealed or pushed, and its status shows the error; the CLI and the GUI's export show it. A missing or empty file is unchanged. Sibling: library.Collect read any failed stat of library.json as "no library"; only a missing file is that now, any other error stops the backup. plugins.json (provider.Carried -> plugin.list) still reads as no plugins when unreadable; bringing that in never removes a plugin, so it is filed separately rather than changed here. What was run: - TestCollectStopsOnUnreadableSettings (new, internal/backup) and TestSyncStopsOnUnreadableSettings (new, internal/davsync: two computers on a fake WebDAV, a's file cut short with no .bak; a's sync errs with no PUT, the status says so, the server keeps a's settings, b keeps its own). Both FAIL on the old backup.go ("Collect read a settings.json that can't be read as settings: <nil> &{Theme:system ..." and "a synced a broken settings.json") and PASS on the new, on macOS and on the Windows box (cross-compiled binaries, sandbox home). - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean. - go test -p 4 -timeout 40m ./... on macOS (origin/main69d7ee33aplus these three commits, load average 15-27 from other suites): every package ok but internal/gateway, where TestMaxRPMTooLongIs429With RetryAfter got 200 (the window had passed under load; #1791, open, gateway untouched here). -run TestMaxRPM -count=3 passes on this tree. - After rebasing on origin/main226222118(which touched internal/library): go vet darwin, GOOS=linux -tags nogui and GOOS=windows clean; go test ./internal/update ./internal/pluginrt ./internal/backup ./internal/davsync ./internal/library ./internal/settings ok (library under -timeout 40m, 550 s at load average ~60). Not tried: a settings.json held open by another Windows program, or a real WebDAV/S3/GitHub server.
117 lines
6.6 KiB
Markdown
117 lines
6.6 KiB
Markdown
# Sync and backup
|
|
|
|
`internal/backup` collects magpie's portable setup into a passphrase-sealed
|
|
bundle. `internal/davsync` keeps that bundle in a WebDAV folder, an S3 bucket
|
|
or a GitHub repository. It merges changes between computers; it does not
|
|
clone a working tree or synchronize arbitrary files.
|
|
|
|
## Sources of truth
|
|
|
|
- [`Config`, `Configure`, `Status` and `syncOnce`](../../internal/davsync/davsync.go)
|
|
keep the active server in `sync.json`, and the last local/remote hashes,
|
|
versions, notices and undo file in `sync-state.json`. These files live beside
|
|
magpie's settings. Secrets are local, never in the status response.
|
|
- The [`remote`](../../internal/davsync/dav.go) interface reads a version and
|
|
conditionally replaces it. WebDAV and S3 use ETags; GitHub uses the blob SHA
|
|
required by its Contents API. [`github.go`](../../internal/davsync/github.go)
|
|
interprets `github://owner/repo[/prefix]`, with `Config.Branch` selecting a
|
|
branch or using the repository's default.
|
|
- [`backup.go`](../../internal/backup/backup.go) owns bundle contents and
|
|
sealing. Sync divides the setup into providers, settings, profiles, agents
|
|
and library. Provider keys, agent models, library and usage have their own
|
|
inclusion switches.
|
|
- [`usage.go`](../../internal/davsync/usage.go) shares separately sealed usage
|
|
and quota files through a remote's `files` interface.
|
|
- [`restore.go`](../../internal/davsync/restore.go) replaces local parts from
|
|
the remote bundle, first keeping the local setup for Undo.
|
|
- [`backup.go`](../../internal/gui/backup.go), the Settings form in
|
|
[`app.js`](../../internal/gui/assets/app.js) and
|
|
[`webdav_cli.go`](../../webdav_cli.go) reach the same sync operations.
|
|
|
|
## Runtime and state
|
|
|
|
The gateway runs sync every three minutes by default. `SetAuto` can select a
|
|
longer interval or manual sync only. Saving in Settings syncs immediately.
|
|
Each operation holds the sync lock, so configuration changes cannot race a
|
|
sync's state write.
|
|
|
|
A first computer creates a remote backup. Another computer joining takes
|
|
the remote setup and keeps displaced local parts in its sync folder.
|
|
Subsequent syncs compare each part with the hashes from the previous sync:
|
|
local-only changes go up, remote-only changes come down, and a part changed
|
|
on both sides keeps the newer version while retaining the replaced copy.
|
|
A conditional write refused because another computer committed in between
|
|
is retried through this same merge.
|
|
|
|
When a remote file is empty, damaged or another app's format, sync stops and
|
|
describes it through `ServerFile`. Settings and the backend's `upload` command
|
|
can replace it with this computer's setup, keeping the old file first.
|
|
HTML and XML are not offered for replacement. GitHub diagnostics name the
|
|
repository and `magpie github upload`, rather than WebDAV's recovery path.
|
|
|
|
A local file that is there but can't be read is unknown, never empty or the
|
|
defaults. `backup.Collect` stops on an unreadable providers.json (with its groups
|
|
and order), settings.json (`settings.LoadChecked`: unreadable, or
|
|
not parsing with no good `.bak`) or library.json, and on a gateway key
|
|
store that fails to export. A backup, an export and a sync all end with that
|
|
error, nothing sealed or pushed; the sync's status shows it. A missing or
|
|
empty settings.json is still none (`TestCollectStopsOnUnreadableSettings`,
|
|
`TestSyncStopsOnUnreadableProviders`, `TestSyncStopsOnUnreadableSettings`,
|
|
#2269).
|
|
|
|
Switching services keeps all inactive kinds in `Config.Servers`. `Other`
|
|
remains the most recently left server for compatibility with older saved
|
|
configurations and clients. Only the active server is used. Empty credential
|
|
fields reuse a saved credential only for its matching account; GitHub
|
|
restricts reuse to the same owner/repo. Branch or prefix changes within that
|
|
repository can keep the token. The branch is part of the sync state key.
|
|
|
|
## GitHub constraints and errors
|
|
|
|
- The repository must exist and be private: `prepare` refuses one whose
|
|
repository read doesn't say `"private": true`, since a public history hands
|
|
every sealed version to anyone to guess the passphrase against
|
|
(`TestGitHubPublicRepositoryRefused`). In an empty repository, the first encrypted
|
|
backup initializes its default branch through the Contents API. A selected
|
|
branch other than the default must already exist. The token needs repository
|
|
Contents read and write.
|
|
- Backups are `<prefix>/magpie/magpie.magpie-backup`; optional usage files
|
|
live beside them under `usage/`. Changes create commits, and unchanged
|
|
setup does not create another backup commit.
|
|
- Tokens are separate from the library's GitHub token, sent only to
|
|
`https://api.github.com`, and must differ from the encryption passphrase.
|
|
Repository history retains older encrypted versions.
|
|
- Repository and branch access is checked before a file's 404 can mean
|
|
"no backup yet." Failed reads and malformed metadata fail sync.
|
|
- Only the ref endpoint's explicit `409: Git Repository is empty.` allows
|
|
initialization, after repository access and the default branch are checked.
|
|
The initial write omits `branch` so GitHub creates its default branch; other
|
|
conflicts and missing branches are not treated as an empty repository.
|
|
- Large files are downloaded by blob SHA, preserving the version that was
|
|
read. Sync files are limited to 64 MiB. Usage listing fails at the Contents
|
|
API's 1,000-file limit rather than treating a truncated list as complete.
|
|
- A stale SHA or a racing initial creation returns `errChanged`. Other 422
|
|
responses stay actionable errors. Authentication, permission and branch
|
|
failures remain distinct from missing files. Rate limits use sync backoff.
|
|
- Request archival remains an S3-only feature; its objects are separate from
|
|
the encrypted setup and usage files.
|
|
|
|
## Verification
|
|
|
|
```sh
|
|
go test -tags nogui ./internal/davsync
|
|
go test -tags nogui ./internal/cli -run 'TestGitHubCmd|TestWebdavCmd|TestS3Cmd'
|
|
node --test internal/gui/tests/github-sync.test.cjs internal/gui/tests/s3-sync.test.cjs internal/gui/tests/sync-other-kind.test.cjs
|
|
```
|
|
|
|
`TestGitHubEmptyRepository` covers the first encrypted backup initializing an
|
|
empty repository and an unchanged second sync. `TestGitHubEmptyRepositoryFailures`
|
|
covers missing branches, unrelated ref conflicts and another computer winning
|
|
the initial write. `TestGitHubNotBackup` covers backend-specific recovery
|
|
advice when the repository holds an empty or foreign file.
|
|
|
|
Go tests use `testenv` homes and HTTP fixtures. Browser tests use isolated
|
|
API fixtures in Chromium and WebKit. A live GitHub write requires a test
|
|
repository and token; fixture tests do not establish that a particular
|
|
account's permissions or repository rules allow commits.
|