← Blog
A lavender index-card drawer pulled open to reveal an ivory card with an orange tab.
Published on
2026-11-24

Back up SQLite with Litestream and verify the restore

When a SQLite server goes down, the question for the person running the application is often simple: did the last order make it into the backup? Litestream replicates changes from a live SQLite database. A transaction can finish on the application’s disk before the replica receives it. To find your recovery point, restore the replica into a new file and look for a transaction you can identify.

We tested this with a small order ledger. The restored database passed SQLite’s integrity check and contained orders 101 through 103, worth 4,200 cents in total. The source also contained order 105. We had deliberately stopped replication before committing it. Both databases were structurally sound, but only one contained that last sale.

Choose what the replica needs to survive

Start with a loss you can describe. If an application VM disappears, a copy on the same disk can disappear with it. A local file replica is convenient for learning the restore procedure. For host recovery, use a destination that remains accessible without the original VM, and make sure a recovery operator can obtain the configuration and credentials from elsewhere.

Decide how much recent work you can reconstruct and how long the application can stay offline. The first limit is your recovery point objective. The second is your recovery time objective. Measure both with a representative database. An upload notification alone gives you neither answer. If you’re still choosing where the live database should run, the SQLite vs PostgreSQL guide covers the trade-offs around application ownership and concurrent writes.

Create a small ledger with known transactions

We used Windows, Litestream 0.5.18 and Python 3.12.14 with SQLite 3.53.1. Start in an empty working directory. Put the Litestream executable on your path and check that litestream version prints 0.5.18. The example uses Python’s standard library. Tested Litestream release

Save the following as orders.py. It creates a disposable ledger, writes known amounts in cents and can inspect a database read-only. The seed step refuses an existing filename. Order 104 deliberately rolls back, which gives you a second way to check that the recovered contents make sense.

import argparse
import json
import sqlite3
from contextlib import closing
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["seed", "sales", "late", "inspect"])
parser.add_argument("database", type=Path)
args = parser.parse_args()
path = args.database.resolve()

if args.action == "seed":
    path.touch(exist_ok=False)  # Refuse to replace an existing database.

mode = "ro" if args.action == "inspect" else "rw"
with closing(sqlite3.connect(path.as_uri() + "?mode=" + mode, uri=True)) as db:
    if args.action == "seed":
        db.execute("PRAGMA journal_mode=WAL")
        db.execute("CREATE TABLE orders (id INTEGER PRIMARY KEY, cents INTEGER NOT NULL)")
        with db:
            db.execute("INSERT INTO orders VALUES (101, 1250)")
    elif args.action == "sales":
        for row in [(102, 850), (103, 2100)]:
            with db:
                db.execute("INSERT INTO orders VALUES (?, ?)", row)
        db.execute("INSERT INTO orders VALUES (104, 9999)")
        db.rollback()
    elif args.action == "late":
        with db:
            db.execute("INSERT INTO orders VALUES (105, 500)")
    else:
        rows = db.execute("SELECT id, cents FROM orders ORDER BY id").fetchall()
        integrity = db.execute("PRAGMA integrity_check").fetchall()
        print(json.dumps({"orders": rows, "integrity": integrity}))

Replicate the live database to a local directory

Save this configuration as litestream.yml. The replica directory holds Litestream’s recovery data. Use the restore command to turn that data back into a SQLite database. The one-second sync interval is a setting for this exercise. Actual recovery depends on the replicator running and the destination accepting writes. Litestream file replica guide

dbs:
  - path: drill-orders.db
    replica:
      type: file
      path: replica
      sync-interval: 1s

Run these commands in the first terminal and leave the replicator running.

python orders.py seed drill-orders.db
litestream replicate -config litestream.yml

Open a second terminal in the same directory. Verify that the initial order has reached the replica before making more sales.

litestream restore -config litestream.yml -integrity-check full -o baseline.db drill-orders.db
python orders.py inspect baseline.db

Look for order 101 with 1,250 cents and ok from the integrity check. On a first start the replica may have nothing to restore yet. If necessary, wait briefly and repeat the restore and inspection using a fresh output filename. Keep each successful restore as a separate observation.

Find the newest order that can actually be restored

Run the sales command once. It commits orders 102 and 103 in separate transactions, then inserts and rolls back 104. Take another restore while the replicator is still running.

python orders.py sales drill-orders.db
litestream restore -config litestream.yml -integrity-check full -o after-sales.db drill-orders.db
python orders.py inspect after-sales.db

The expected rows are [[101, 1250], [102, 850], [103, 2100]]. If the new restore still contains only 101 or 102 is the latest order, repeat the restore and inspection to a fresh filename. Leave the original ledger alone. In our run, a restore containing order 103 passed inspection about one second after the sales command returned. That observation includes polling, restoration and inspection of a tiny local database. A remote bucket or a larger database needs its own measurement.

Restore with the source database out of reach

Once you have confirmed order 103 in a restore, stop the replicator in the first terminal with Ctrl+C and wait for it to exit. In the second terminal, use the PowerShell commands below to record one more sale on the source and copy the settled replica into a new recovery directory. Only the configuration and replica go into that directory. The source database and its sidecar files stay behind.

python orders.py late drill-orders.db
python orders.py inspect drill-orders.db
New-Item -ItemType Directory -Path recovery
Copy-Item -LiteralPath litestream.yml -Destination recovery
Copy-Item -LiteralPath replica -Destination recovery/replica -Recurse
Set-Location recovery
litestream restore -config litestream.yml -integrity-check full -o recovered.db drill-orders.db
python ../orders.py inspect recovered.db

The final drill-orders.db argument tells Litestream which database entry to look up in the configuration. The destination is recovered.db. With these relative paths, running the command inside recovery makes the copied replica available even though the original database is absent from that directory. We also repeated the restore to an existing output filename. Litestream refused to overwrite it and its contents stayed intact. Litestream restore options

  • Orders 101–103 matched the expected identifiers and amounts. Order 103 was the last verified recovery point.
  • Order 104 appeared in neither database. Its transaction had rolled back.
  • Order 105 appeared on the source for 500 cents. It was absent from the restore because replication had already stopped.

Before putting a restored file into service, check both its structure and a business record that matters. Here, PRAGMA integrity_check returned ok for the source and the restore. Only the order comparison exposed the gap. In production, you might reconcile that last order against a payment receipt or another system that recorded it independently.

Repeat the drill with an off-host replica

For a remote replica, follow Litestream’s S3 documentation and verify the endpoint, bucket, region, permissions and retention settings for your provider. Write a known order, restore to a clean machine using the recovery credentials, and compare the result with an independent record. Measure the elapsed time through reopening the application and reading that order. That is the interval the operator needs to plan for.

If you’re evaluating Object Storage with Hivenet, request access to a test bucket and run that same round trip before moving a production ledger. The exercise above verified a file replica on one host. Compatibility with your bucket and the time to retrieve your data need a test against that destination.

Finally, inventory what lives outside SQLite. Uploaded files, configuration and other application state may need separate backups. The Restic restore guide shows how to verify a file snapshot. A recovered order that references an unavailable attachment can still leave the application unusable.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.