backup
Local backups of the pod background cache database.
Almost everything in the cache is derived — analysis output, file metadata,
fetched EHR data — and can be recomputed or re-fetched. The site's own input is
the exception: a site user's patient reviews and notes exist nowhere else, so a
deleted background_cache.db loses them for good. The file is a single file in
the user's home directory, easy to delete by accident (or on the advice of the
SDK's own "delete the cache file" recovery message).
CacheBackups manages the recent whole-file snapshots kept beside one cache
database, so a file lost that way can be recovered. It binds the cache path (and
the retention count), so a caller holds one configured object rather than
threading the path and the options through every call. Snapshots are taken with
SQLite's VACUUM INTO, which writes a compact, transactionally consistent copy
even while other processes hold the database open and are writing to it (the
cache runs in WAL mode). A raw file copy is not consistent and is never used.
Backups are taken automatically at two points — when the cache is opened (see
bitfount.runtimes.file_metadata.functions.open_background_cache) and by the
scheduled cache_backup_runtime flow — and can be taken and listed by hand
through the bitfount cache CLI (see bitfount.scripts.cache_backup). An open
that exists only to read the cache can opt out of the open-time snapshot
(take_backup=False), which is what keeps the periodic rerun sweep off the
backup lock; the scheduled flow still covers the pod.
Recovery is manual: with the app closed, copy a backup over the cache file and
remove any -wal/-shm siblings beside it.
Backups hold the same protected data as the cache, so they are written
owner-only (0600, in a 0700 directory) where the platform supports it.
_restrict_permissions documents the Windows caveat; restrict_cache_permissions
applies it to the cache file itself.
Module
Functions
backup_dir_for
def backup_dir_for(cache_path: str | Path) ‑> pathlib.Path:Return the directory holding backups of cache_path.
Arguments
cache_path: Path to abackground_cache.db.
Returns
The backups directory beside it (whether or not it exists yet).
restrict_cache_permissions
def restrict_cache_permissions(cache_path: str | Path) ‑> None:Best-effort owner-only permissions on the cache file and its directory.
The cache holds the site's own patient reviews and notes, which may contain PII, so it is kept readable only by its owner. Never raises: a filesystem that refuses the change (a read-only mount, a filesystem without POSIX modes) must not stop the cache opening.
Arguments
cache_path: Path to thebackground_cache.db.
Global variables
BACKUP_DIRNAME : str- Directory, beside the cache file, holding its backups.
DEFAULT_RETENTION : int- How many backups to keep when the caller does not say otherwise.
Classes
CacheBackup
class CacheBackup(path: Path, created_at: datetime, size_bytes: int):A backup snapshot of a cache database on disk.
Attributes
path: The backup file.created_at: When the snapshot was taken (from its filename).size_bytes: Its size on disk.
CacheBackups
class CacheBackups( cache_path: str | Path, *, backup_dir: str | Path | None = None, retain: int = 5,):The local snapshots of one pod's background cache database.
Construct one per cache file and call its methods; the cache path, the backups directory and the retention count are held on the instance rather than passed to each call.
Arguments
cache_path: Path to thebackground_cache.dbto back up.backup_dir: Override for the backups directory; defaults to thebackupsdirectory beside the cache.retain: How many backups to keep; older ones are pruned after each new snapshot.
Raises
CacheBackupError: If retain is less than 1.
Variables
cache_path : Path- The cache database these backups belong to.
directory : Path- The directory the backups are stored in.
latest : CacheBackup | None- The newest backup, orNonewhen there are none.
retain : int- How many backups are kept.
Methods
create
def create(self, *, now: datetime | None = None) ‑> CacheBackup:Snapshot this cache into a new backup file and prune older ones.
Unconditional: the caller has already decided a backup is wanted (the
bitfount cache backup CLI). For the automatic triggers, which should
snapshot only when one is due, use maybe_create instead.
The snapshot is consistent even while the cache is open and being written by this or another process. The new backup is the newest, so pruning never removes it.
Arguments
now: Override for the snapshot's timestamp (testing).
Returns The new backup.
Raises
CacheBackupError: If the database does not exist or the snapshot cannot be taken or verified.
maybe_create
def maybe_create( self, *, min_interval_seconds: float = 0.0, now: datetime | None = None,) ‑> CacheBackup | None:Take a backup if one is due, never raising.
Due-ness is checked before the backup lock and again under it, and the snapshot is taken only from the check under the lock: two processes opening the cache at the same time never both snapshot.
The safety net for callers on a path that must not fail — the cache open, the scheduled sweep. Every fault (nothing due, a backup that cannot be taken, another process holding the lock) is logged and swallowed.
Arguments
min_interval_seconds: Seeneeds_backup.now: Override for the current time (testing).
Returns
The new backup, or None when none was taken.
needs_backup
def needs_backup( self, *, min_interval_seconds: float = 0.0, now: datetime | None = None,) ‑> bool:Whether this cache is due a backup.
A backup is due when the database exists and has changed since the newest backup, and — when min_interval_seconds is positive — at least that long has passed since that newest backup. The interval is what keeps a cache opened repeatedly in a short window from being snapshotted every time; the scheduled sweep deliberately does not apply it.
Arguments
min_interval_seconds: Minimum age of the newest backup, in seconds.now: Override for the current time (testing).
Returns True when a backup should be taken.
prune
def prune(self) ‑> list[pathlib.Path]:Delete all but the newest retain backups.
Never raises: a backup that cannot be removed is left in place and logged.
Returns The paths that were deleted.
snapshots
def snapshots(self) ‑> list[CacheBackup]:The backups of this cache, newest first.
Returns
One CacheBackup per backup file; empty when there are none.