Back up SQLite
SQLite is a single file, but you should not just copy that file while the app is running โ a copy taken mid-write can be corrupt. Use SQLite's online backup, which produces a consistent copy safely.
Prerequisitesโ
- The
sqlite3CLI:- macOS: ships with the OS, or
brew install sqlite - Debian/Ubuntu:
sudo apt-get install sqlite3
- macOS: ships with the OS, or
- Read access to the database file.
1. Sign in & create an API keyโ
You need a Lighthouse account, a workspace, and an API key before you can upload a backup.
-
Sign in to the portal and claim your free 5 GB workspace โ see Web Portal & Free Workspace.
-
Create an API key scoped
backup:write,backup:read,snapshots:readโ see API Keys. -
Export the credentials so the upload step can read them:
export LH_API_KEY="lh_xxxxxxxxxxxxxxxxxxxxxxxx"
export LH_WORKSPACE_ID="your-workspace-uuid"
Already have a key? Continue to the backup step below.
2. Create the backupโ
Create the local dump directory once:
mkdir -p ./db-dumps
Option A โ .backup (recommended)โ
Produces a consistent binary copy even while the database is in use:
sqlite3 /path/to/app.db ".backup './db-dumps/app.sqlite'"
This uses the online backup API, so it is safe against concurrent readers/writers.
Option B โ .dump (portable SQL text)โ
Emits a plain-SQL reconstruction โ useful if you want a human-readable, diffable, cross-version backup:
sqlite3 /path/to/app.db ".dump" > ./db-dumps/app.sql
If your database uses Write-Ahead Logging (journal_mode=WAL), .backup already produces a consistent result. Avoid copying app.db together with stray -wal/-shm files manually.
3. Verify the backupโ
ls -lh ./db-dumps/app.sqlite
sqlite3 ./db-dumps/app.sqlite "PRAGMA integrity_check;" # should print: ok
4. Restore (recovery test)โ
The .backup output is a database file โ just open it:
sqlite3 ./db-dumps/app.sqlite "SELECT count(*) FROM sqlite_master;"
For the .dump SQL form, recreate the database:
sqlite3 ./restore/app.db < ./db-dumps/app.sql
5. Upload to Lighthouseโ
Your backup is now in ./db-dumps. Construct the SDK client with the API key from step 1 and upload the directory as a snapshot. This reads LH_API_KEY and LH_WORKSPACE_ID from the environment (exported in step 1):
- Go SDK
- JS SDK
- CLI
package main
import (
"log"
"os"
sdkclient "github.com/lighthouse-web3/baas-go-sdk/client"
sdktypes "github.com/lighthouse-web3/baas-go-sdk/types"
)
func main() {
client, err := sdkclient.NewBackupClient(sdkclient.BackupClientOptions{
APIURL: "https://baas-api.lighthouse.storage", // API host, not the portal
APIKey: os.Getenv("LH_API_KEY"),
WorkspaceID: os.Getenv("LH_WORKSPACE_ID"),
})
if err != nil {
log.Fatalf("client init: %v", err)
}
snapshot, err := client.Backup([]string{"./db-dumps"}, &sdktypes.BackupOptions{
Description: "sqlite backup",
})
if err != nil {
log.Fatal(err)
}
log.Printf("snapshotId=%s", snapshot.SnapshotID)
}
Run it from the directory containing db-dumps:
go run .
import { BackupClient } from "@lighthouse-web3/baas-js-sdk";
const client = new BackupClient({
apiKey: process.env.LH_API_KEY,
workspaceId: process.env.LH_WORKSPACE_ID,
});
const snapshot = await client.backup(["./db-dumps"], {
description: "sqlite backup",
});
console.log(`snapshotId=${snapshot.snapshotId}`);
Save as upload.mjs and run it from the directory containing db-dumps:
node upload.mjs
baas backup ./db-dumps --description "SQLite backup" --tag database=sqlite
To run this on a schedule, use Automated backup with scheduling.
The backup command for this database, ready to drop into the scheduled job's make_dump():
sqlite3 /path/to/app.db ".backup './db-dumps/app.sqlite'"