Docs / Surfaces / Databases / SQLite

SQLite

You can back up a SQLite file on the host with nothing else installed, including a database in WAL mode. This one command takes a backup now:

# a database file on this host; no client tools needed
safegrd backup --database-url sqlite:///var/lib/app/app.db --retention-days 14

To back up on a schedule and get Fire Drills, add the file as a surface in ~/.safegrd/config.yaml and run the daemon. Pick one way per database. A surface starts its own history, so any backups you took earlier with --database-url show in the console as a separate surface.

# ~/.safegrd/config.yaml
surfaces:
  - id: app-db
    type: sqlite
    database_url: sqlite:///var/lib/app/app.db
    schedule: 6h
# check it, then run it as a service
safegrd config validate && safegrd daemon install

If you copy the file off the disk while the app writes to it, the copy can be torn. In WAL mode the newest transactions are also still in the -wal file, and a copy of the database file misses them. SafeGrd copies the database through SQLite itself, inside one read transaction. The copy holds every committed transaction, including those still in the WAL, and your app keeps writing. SQLite WAL mode and backups shows the difference on a test database.

The SQLite engine is built into safegrd, so the host does not need sqlite3.

What each backup does

Each backup is a run of the surface's repository, as with PostgreSQL. The copy keeps every page where the database has it, so a run uploads and locks only the 64 KiB chunks whose pages changed. On a 228 MB database with 400 rows updated since the last run, a backup uploaded 6.8 MB. Inserts into an index on random keys, such as UUIDs or email addresses, touch pages all over that index and upload more.

For one archive per backup instead, use --format tar, or format: tar on a surface. The archive is compacted with VACUUM INTO. Once you decrypt and unpack it, any SQLite client can open the file.

Restore

A restore writes a new file and does not overwrite an existing one: safegrd restore --snapshot <id> --target sqlite:///var/lib/app/restored.db. Neither the file nor its -wal may exist yet. SafeGrd checks the copy with PRAGMA integrity_check and then renames it into place. --to-sql works for PostgreSQL only.

To put the restored file in place of the live one:

  1. Stop the app.
  2. Move the old app.db, and any app.db-wal and app.db-shm, aside.
  3. Move restored.db to app.db. The restored file is in rollback-journal mode. If the app expects WAL, run PRAGMA journal_mode=WAL; on it first.
  4. Start the app.

You can also restore one table into a database file that has other tables: safegrd restore --snapshot <id> --table accounts --target sqlite:///var/lib/app/app.db. Stop the app first, so nothing writes while the rows land. In the console, the Tables tab has Restore table for the same thing.

SafeGrd stages the backup beside the file and attaches it, so it needs about as much free space as the database. If the table is missing, SafeGrd creates it as the backup defines it, with its indexes and triggers. If it exists, it has to be empty and have the same columns. The rows are copied over in one transaction.

Restore to a point in time

A backup restores the database as it was when the run started. Add a wal: block to the surface and the resident daemon also follows the database's WAL. You can then restore to within a second of any moment in a window, 7 days by default:

surfaces:
  - id: app-db
    type: sqlite
    database_url: sqlite:///var/lib/app/app.db
    wal: {}
One week of point-in-time backup for a SQLite surface. The daemon takes a base copy when it starts and again every 7 days. Committed transactions are copied every second, unlocked, and joined once a day into a locked bundle. Each day has a restore point with row counts. You can restore to any second inside the window, 7 days by default, and the daemon checks a restore every 7 days. base copy base copy transactions, copied every second, unlocked joined once a day into a locked bundle day 0 1 2 3 4 5 6 day 7 dots: a restore point with row counts, daily Restore to any second in here 7 days by default; checked every 7 days
What the daemon keeps for a surface with wal: {}, over one 7-day window.

Every second, the daemon copies the transactions committed since the last copy to your bucket, compressed and encrypted on the host and unlocked. Once a day it joins them into one locked bundle and deletes the unlocked copies, as it does for PostgreSQL. On hosted storage, this comes with Growth (7 days) and Scale (30 days).

What it needs

The daemon checkpoints the database itself when the WAL passes 4 MB (checkpoint_mb). While it follows the WAL, your app's own wal_checkpoint(TRUNCATE) or RESTART, and VACUUM, wait for it and may return SQLITE_BUSY. Leave checkpointing to SQLite's default or to the daemon.

Each time the daemon starts, it takes a copy of the database and begins a new run of WAL from it. It does the same every 7 days (base_every_days).

How the daemon checks it

Once a day the daemon records a restore point with each table's row count. It counts under the write lock, so the counts are exact. Every 7 days it restores the archive on the host, to the newest restore point and to the end, checks the counts and runs PRAGMA integrity_check. To run this check now, use safegrd wal drill --surface app-db.

Restore to a moment

Restore into a new file:

safegrd wal restore --surface app-db --to "2026-10-17 14:52:00" --out /var/lib/app/restored.db

In the console, the surface's Point in time tab writes this command for a moment you pick.

The file holds every transaction committed up to the last copy at or before that moment (UTC). It passes PRAGMA integrity_check before it is renamed into place.