Backup and restore
Back up with the database's own tools, then audit() the restored books before writing.
On this page
What to call
- After every restore, before the first write:
Ledger::audit→AuditReport, orError::Integrityat the first difference. - Then compare
Ledger::headwith the last head you published (aVerifierholds it). - SQLite file copies:
SqlStore::closefirst.
The crate has no backup function; the database's tools copy consistently.
Back up the whole database or schema, not only the log: the tables also
hold side data no hash covers (memo texts and salts, the draft fingerprints
idempotency compares) and the indexes and maintained values audit() checks.
SQLite
The store runs in WAL mode, so committed transactions may still sit in the
-wal file.
- Online:
VACUUM INTO(or thesqlite3shell's.backup) writes a consistent copy without blocking the writer. - Offline:
SqlStore::close()(or stop every process); the file alone is then complete. - Never copy just the
.dbfile of an open database.
use doubleentry::prelude::*;
use doubleentry::store::{SqlStore, Sqlite};
use sqlx::Connection;
let dir = tempfile::tempdir()?;
let (live, backup) = (dir.path().join("live.db"), dir.path().join("backup.db"));
let books = Ledger::new(SqlStore::<Sqlite>::sqlite(&format!("sqlite://{}", live.display())).await?, LedgerId::new("example.com/acme")?).await?;
books.declare_currency(EUR, 2, date!("2026-01-01")).await?;
books.define_months(2026, 3..=3).await?;
books.open("Assets:Cash", AccountKind::Asset, date!("2026-01-01")).await?;
books.open("Income:Sales", AccountKind::Income, date!("2026-01-01")).await?;
books
.record(
Entry::new("sale-1", date!("2026-03-05"))
.debit("Assets:Cash", amount!("10.00"), EUR)
.credit("Income:Sales", amount!("10.00"), EUR),
)
.await?;
// Online backup from a connection of your own, while the ledger stays open.
let mut connection = sqlx::SqliteConnection::connect(&format!("sqlite://{}", live.display())).await?;
sqlx::query("VACUUM INTO $1").bind(backup.display().to_string()).execute(&mut connection).await?;
// Restore: open the copy, audit it, compare it with the head you published.
let restored = Ledger::new(SqlStore::<Sqlite>::sqlite(&format!("sqlite://{}", backup.display())).await?, LedgerId::new("example.com/acme")?).await?;
let report = restored.audit().await?;
assert_eq!(report.entries, 1);
assert_eq!(restored.head().await?, books.head().await?);
The sqlx lines need sqlx with its sqlite feature in your application.
examples/restore_audit.rs
shows the offline path.
PostgreSQL
- Logical:
pg_dump --schema=ledger_acme, restored withpg_restoreorpsqlunder the same schema name (the trigger functions name their schema). The dump carries the grants; the roles must exist on the target. - Physical: base backups with WAL archiving (
pg_basebackup, or your provider's point-in-time recovery).
The triggers come back with the schema.
Restore, then audit, then compare heads
audit() replays every record through the rule core and compares every table
the store keeps with the replay, byte for byte (see Audit).
It proves the copy is internally consistent; it cannot know about bookings the
backup never had. For that, compare the restored head with the last head you
published:
- equal: the restore is complete;
- smaller but consistent: the backup predates published bookings. Their keys are unknown again, so a redelivered message is recorded as new;
- not a prefix: this is not the published ledger.
A rollback is visible to anyone holding a later head: a
Verifier refuses a smaller head, and a
witness cosigns only checkpoints that extend what it holds. Appending to a
rolled-back ledger forks the published history. So publish checkpoints only for
sizes you know are in a backup (with continuous WAL archiving, every committed
size is).
A journal rebuilt from the leaves alone (Journal::from_log) has no memo texts
and no fingerprints: a retry of an old key is then a KeyConflict instead of a
replay. Back up the database, not just the log.
Checking totals with SQL
audit() needs the library. For a check an operator runs with nothing but
SELECT — before the services start, or in a monitoring job — the crate
generates two read-only queries per dialect:
turnover_query: posting turnover per account path, currency and layer (settled,pending) —debit_units,credit_units,postingsandscale; the decimal isunits / 10^scale.balance_check_query: the balances whose maintained totals differ from that turnover; no rows when all agree.
Get them from an open store (store.turnover_query()), or without one from
Postgres::turnover_query(schema) and Sqlite::turnover_query(). They name
the schema identifier they were written for; regenerate them after an
upgrade. On PostgreSQL the totals are exact numeric; SQLite has no 128-bit
integer, so a total beyond 64 bits fails the query with "integer overflow"
instead of rounding.
For schema ledger on PostgreSQL (SQLite's texts decode amounts digit by
digit and are long: take them from Sqlite::turnover_query() and
Sqlite::balance_check_query()):
-- doubleentry/3: posting turnover per account, currency and layer (read-only)
SELECT a.path, t.currency,
CASE t.layer WHEN 0 THEN 'settled' ELSE 'pending' END AS layer,
t.debit_units, t.credit_units, t.postings,
(SELECT amount_scale FROM "ledger".meta) AS scale
FROM (SELECT handle, currency, layer,
SUM(CASE WHEN side = 0 THEN amount ELSE 0 END) AS debit_units,
SUM(CASE WHEN side = 1 THEN amount ELSE 0 END) AS credit_units,
COUNT(*) AS postings
FROM (SELECT handle, currency, layer, side,
('x' || encode(substr(amount, 1, 8), 'hex'))::bit(64)::bigint::numeric * 18446744073709551616
+ ('x' || encode(substr(amount, 9, 8), 'hex'))::bit(64)::bigint::numeric
+ CASE WHEN get_byte(amount, 8) >= 128 THEN 18446744073709551616 ELSE 0 END AS amount
FROM "ledger".postings) AS d
GROUP BY handle, currency, layer) AS t
JOIN "ledger".accounts a ON a.handle = t.handle
ORDER BY a.path COLLATE "C", t.currency, t.layer
-- doubleentry/3: balances that differ from their posting turnover (read-only; no rows: all agree)
SELECT a.path, COALESCE(t.currency, m.currency) AS currency,
CASE COALESCE(t.layer, m.layer) WHEN 0 THEN 'settled' ELSE 'pending' END AS layer,
t.debit_units AS posted_debit_units, m.debit AS balance_debit_units,
t.credit_units AS posted_credit_units, m.credit AS balance_credit_units,
t.postings AS posted_postings, m.postings AS balance_postings
FROM (SELECT handle, currency, layer,
SUM(CASE WHEN side = 0 THEN amount ELSE 0 END) AS debit_units,
SUM(CASE WHEN side = 1 THEN amount ELSE 0 END) AS credit_units,
COUNT(*) AS postings
FROM (SELECT handle, currency, layer, side,
('x' || encode(substr(amount, 1, 8), 'hex'))::bit(64)::bigint::numeric * 18446744073709551616
+ ('x' || encode(substr(amount, 9, 8), 'hex'))::bit(64)::bigint::numeric
+ CASE WHEN get_byte(amount, 8) >= 128 THEN 18446744073709551616 ELSE 0 END AS amount
FROM "ledger".postings) AS d
GROUP BY handle, currency, layer) AS t
FULL JOIN (SELECT handle, currency, layer, postings,
('x' || encode(substr(debit, 1, 8), 'hex'))::bit(64)::bigint::numeric * 18446744073709551616
+ ('x' || encode(substr(debit, 9, 8), 'hex'))::bit(64)::bigint::numeric
+ CASE WHEN get_byte(debit, 8) >= 128 THEN 18446744073709551616 ELSE 0 END AS debit,
('x' || encode(substr(credit, 1, 8), 'hex'))::bit(64)::bigint::numeric * 18446744073709551616
+ ('x' || encode(substr(credit, 9, 8), 'hex'))::bit(64)::bigint::numeric
+ CASE WHEN get_byte(credit, 8) >= 128 THEN 18446744073709551616 ELSE 0 END AS credit
FROM "ledger".balances) AS m
ON m.handle = t.handle AND m.currency = t.currency AND m.layer = t.layer
JOIN "ledger".accounts a ON a.handle = COALESCE(t.handle, m.handle)
WHERE t.debit_units IS DISTINCT FROM m.debit
OR t.credit_units IS DISTINCT FROM m.credit
OR t.postings IS DISTINCT FROM m.postings
ORDER BY a.path COLLATE "C", 2, 3Limits
- The crate does not schedule, store or encrypt backups, and does not re-create lost bookings.