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:
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.
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.
- In WAL mode a backup does not block writers. In rollback-journal mode, writers wait while
the copy is taken. Run
safegrd doctorto see which mode the database is in, andPRAGMA journal_mode=WAL;to switch it. - Before any of the copy is uploaded, it goes through
PRAGMA integrity_check. A damaged database is not stored, and the backup tells you why. Earlier snapshots are not touched. - The copy is written to an owner-only directory under the system temporary directory and
deleted after the upload. It needs about as much free space as the database. Set
TMPDIRto use another disk.
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:
- Stop the app.
- Move the old
app.db, and anyapp.db-walandapp.db-shm, aside. - Move
restored.dbtoapp.db. The restored file is in rollback-journal mode. If the app expects WAL, runPRAGMA journal_mode=WAL;on it first. - 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:
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 database has to be in WAL mode (
PRAGMA journal_mode=WAL;). - The daemon needs write access to the database and its directory. It adds one table,
_safegrd_wal. It also keeps a read transaction open, so the WAL cannot be reset before it is copied. - Only the resident daemon follows the WAL.
safegrd daemon run --oncefrom cron cannot, and tells you so.
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:
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.
--to-point <name>restores to a restore point instead.--to latestrestores every segment in the archive.- If a segment is missing before the moment you asked for, the restore is refused and names the segment.
safegrd wal status --surface app-dbshows the window.