wal_backup/lib.rs
1//! # wal-backup
2//!
3//! Embedded SQLite WAL replication to S3-compatible object storage.
4//!
5//! **Not a sidecar — a library.** Your app links the crate, calls
6//! [`DurabilityLayer::open()`], and gets automatic S3 backup in the background.
7//!
8//! # Architecture
9//!
10//! ```text
11//! Your App → DurabilityLayer → SQLite DB (local, fast)
12//! └→ S3 Bucket (backup, source of truth)
13//! ```
14//!
15//! One process, one DB, one bucket prefix. Always.
16//! The bucket is a backup, not a replication bus.
17//!
18//! # Quick Start
19//!
20//! ```ignore
21//! use std::sync::Arc;
22//! use wal_backup::{Config, DurabilityLayer, ObjectStoreBucket, SyncMode};
23//!
24//! let store = wal_backup::build_s3_store_from_env("my-bucket")?;
25//! let bucket = Arc::new(
26//! ObjectStoreBucket::new(store, "my-app".to_string())?
27//! .with_writer_id("server-1"),
28//! );
29//!
30//! let config = Config::new("/data/my.db")
31//! .with_sync_mode(SyncMode::Immediate);
32//!
33//! let layer = DurabilityLayer::open(config, bucket).await?;
34//!
35//! // Write SQL — WAL frames ship to S3 automatically
36//! layer.with_conn(|conn| {
37//! conn.execute("INSERT INTO users (name) VALUES (?)", ["Alice"])
38//! }).await?;
39//!
40//! // Graceful shutdown: checkpoint + final sync + release locks
41//! layer.shutdown().await?;
42//! ```
43//!
44//! # Multi-writer protection
45//!
46//! Two processes writing to the same bucket prefix = silent data corruption.
47//! wal-backup prevents this at three levels:
48//!
49//! - **flock** (`.db.lock`): Two processes, same machine, same DB → [`Error::DatabaseLocked`]
50//! - **lock.json** (bucket): Two processes, anywhere, same prefix → [`Error::ReplicaLocked`]
51//! - **writer.json** (bucket): Wrong server opening the wrong prefix → [`Error::WrongWriter`]
52//!
53//! # Error handling
54//!
55//! ```ignore
56//! match DurabilityLayer::open(config, bucket).await {
57//! Err(e) if e.is_database_locked() => { /* another process on this machine */ }
58//! Err(e) if e.is_replica_locked() => { /* another process anywhere */ }
59//! Err(e) if e.is_wrong_writer() => { /* wrong bucket prefix */ }
60//! Err(e) => return Err(e.into()),
61//! Ok(layer) => { /* use it */ }
62//! }
63//! ```
64
65pub mod config;
66pub mod connection;
67pub mod error;
68pub mod metrics;
69pub mod replication;
70pub mod restore;
71pub mod storage;
72pub mod util;
73pub mod wal;
74
75pub use config::{Config, RestoreBehavior, SyncMode};
76pub use connection::DurabilityLayer;
77pub use connection::HealthReport;
78pub use error::{Error, Result};
79
80#[cfg(feature = "compression")]
81pub use storage::compressed::CompressedBucketStore;
82#[cfg(feature = "encryption")]
83pub use storage::encrypted::EncryptedBucketStore;
84pub use storage::object_store_util::build_s3_store;
85pub use storage::object_store_util::build_s3_store_from_env;
86pub use storage::BucketStore;
87pub use storage::ObjectStoreBucket;
88
89pub use metrics::{noop_metrics, Metrics, NoopMetrics};
90pub use replication::manifest::L1File;
91pub use replication::manifest::Manifest;
92
93pub use util::compute_sha256_file;
94pub use wal::WalFile;
95pub use wal::WalFrame;
96pub use wal::WalHeader;