wal-backup 0.8.11

Embedded SQLite WAL replication to S3-compatible object storage. Not a sidecar — a library. Like Litestream, but embedded.
Documentation
use object_store::local::LocalFileSystem;
use std::sync::Arc;
use wal_backup::{Config, DurabilityLayer, ObjectStoreBucket, RestoreBehavior};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    let tmp = std::env::temp_dir().join("sqlite-restore-example");
    std::fs::create_dir_all(&tmp)?;

    let db_path = tmp.join("restored.db");
    let bucket_dir = tmp.join("bucket");
    std::fs::create_dir_all(&bucket_dir)?;

    let store = Arc::new(LocalFileSystem::new_with_prefix(&bucket_dir)?);
    let bucket = Arc::new(ObjectStoreBucket::new(store, "my-db".to_string())?);

    let config = Config::new(&db_path)
        .with_restore_behavior(RestoreBehavior::AlwaysRestoreFromBucket)
        .with_poll_interval(std::time::Duration::from_millis(100));

    println!("Opening DurabilityLayer — restore will happen automatically from bucket...");
    let layer = DurabilityLayer::open(config, bucket.clone()).await?;

    {
        let conn = layer.conn().await;
        match conn.query_row("SELECT COUNT(*) FROM items", [], |row| row.get::<_, i64>(0)) {
            Ok(count) => println!("Database restored! items table has {} rows.", count),
            Err(_) => println!(
                "No items table yet — database was restored but is empty (or still loading)."
            ),
        }
    }

    println!("\nRestore behavior options:");
    println!("  AlwaysRestoreFromBucket  — always restore, overwrites local");
    println!("  UploadLocalIfNewer       — upload local if newer than bucket");
    println!("  ErrorIfConflict          — error if local is newer");
    println!("  Recoverable              — restore, ignore checksum mismatches");

    layer.shutdown().await?;
    Ok(())
}