Skip to main content

Module backup

Module backup 

Source
Expand description

Backup, restore and export.

§A backup

One .tar.gz holding, under a manifest.json that comes first:

  • plm.sqlite — a snapshot of the metadata and the audit log: SQLite’s backup of one read transaction, which the server’s writes do not wait for (WAL);
  • models/<part>/<revision>.json — the document of every revision the snapshot records a hash for;
  • blobs/… — every attachment the snapshot references, and every version of every workspace file;
  • scripts/… — the administrator’s scripts directory (without .git).

The manifest lists every file with its size and SHA-256, and the snapshot’s row counts.

§Consistent while the server runs

The snapshot is one transaction, but documents and blobs are files that a running server keeps changing: a draft’s document is rewritten, a blob is removed with its last reference. So a backup is taken OPTIMISTICALLY and then proven: every document copied must hash to what the snapshot recorded, and every blob to its name. A mismatch or a missing file means a write landed in between, and the whole backup is taken again (up to ATTEMPTS times). A released revision’s document never changes and a blob is never rewritten, so only drafts written during the copy can cause a retry. This needs no lock on the server, which is why brep-plm backup works from another process while serve owns the directory.

A draft saved faster than a backup can be taken (an autosave loop) would make that retry forever, so after ADJUST_AFTER tries a draft’s document is taken as copied and the snapshot’s record of its hash and size set to match: a draft in work at the snapshot, with the document it had a moment later — a state the store passes through anyway between a CAD save and its next. The manifest lists each such file under adjusted.

The server’s own backups (the Backups page, --backup-dir) open that transaction, read what it references and copy the drafts’ documents under the store’s read lock instead: writes wait for that (not for the copy of the store, which follows from the same transaction), and nothing is adjusted.

§A restore

Into an empty (or absent) directory only. Every entry’s path is checked (no absolute paths, no ..), every file must be in the manifest with the size and hash it lists, and every file the manifest lists must be present. Anything wrong removes what was written and refuses.

§An export

Metadata only, for moving to another system: see export_json and the operator guide’s “Export format” (GUIDE.md).

Structs§

FileEntry
LastBackup
One backup this process took (or failed to).
Manifest
SavedBackup
Status

Enums§

Attempt
One attempt: snapshot, copy, prove. Retry when a write landed.

Constants§

ADJUST_AFTER
After this many tries a draft’s moving document is taken as copied (Manifest::adjusted) instead of retrying again.
ATTEMPTS
How many times a backup is retaken when a write lands during the copy.
DEFAULT_KEEP
How many saved backups are kept when --backup-keep is not given.
EXPORT_FORMAT
The export format’s format field.
EXPORT_VERSION
FORMAT
The manifest’s format field.
PREFIX
Saved backups are named brep-plm-<UTC time>.tar.gz.
SUFFIX
VERSION
Bumped when the archive’s layout changes.

Functions§

backup
Take a backup of data (with its scripts directory) into out, a .tar.gz, from outside the server: works while a server is running on data, retrying when a draft’s document is rewritten during the copy.
backup_held
backup, with the snapshot and the draft documents copied under hold.
export_json
Every piece of metadata a migration to another system needs, as one JSON value. Secrets and live state are left out: password verifiers, sessions, API tokens, sign-in throttles, locks. Documents and attachment bytes are not included (they are in a backup); each revision names its document_key and content_hash, and each attachment its sha256.
hash_file
The lower-case hex SHA-256 of a file, read in pieces.
is_backup_name
Whether name is a saved backup’s name — the only names ever joined to the backup directory from a request.
load_snapshot
Load a data directory’s metadata from a snapshot, without opening the store — what brep-plm export uses, so it works while a server runs.
parts_csv
One row per part: its identity, catalog, current state and preferred MPN.
read_manifest
Read a backup’s manifest without restoring it.
restore
Restore archive into data, which must be empty or absent. Returns the manifest. On any refusal, what was written is removed again.
saved
The saved backups in dir, newest first.
stamp
YYYYMMDD-HHMMSS in UTC, from Unix seconds.
structure_csv
One row per uses-list line in the store: the whole assembly structure, flat.
unheld
For a caller that cannot hold writes off: just run it.

Type Aliases§

Hold
Run f while writes to the store are held off, when the caller can hold them: the server passes its store’s read lock, a separate process cannot and passes unheld.