WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script safe-sqlite-backup

Safe SQLite Backup

Use SQLite's online backup API to copy a live database consistently.

category Backup and Recovery shell Bash 4+ updated 2026-09-29
sqlitebackupdatabaseintegrity
Safety notes
  • Copies a database to a destination file; ensure the destination has space.
  • Do not copy a live SQLite file with cp — use the backup API as this script does.

Dependencies: sqlite3

Usage

  • Pass the database path and the destination directory.
  • The script runs integrity_check on the copy, not the live database.

Script

#!/usr/bin/env bash
set -euo pipefail

db="${1:-}"
out_dir="${2:-}"
[[ -f "$db" && -n "$out_dir" ]] || { echo "usage: $0 DB_PATH OUT_DIR" >&2; exit 1; }
command -v sqlite3 >/dev/null || { echo "sqlite3 not found" >&2; exit 1; }

mkdir -p "$out_dir"
stamp="$(date +%Y%m%d-%H%M%S)"
target="$out_dir/$(basename "$db").$stamp"

sqlite3 "$db" ".backup '$target'"
result="$(sqlite3 "$target" 'PRAGMA integrity_check;')"
if [[ "$result" == ok ]]; then
  echo "backup ok: $target"
else
  echo "integrity check failed: $result" >&2
  exit 1
fi

Example

./safe-sqlite-backup ./app.db /mnt/backups/sqlite

Notes

  • The online backup API is safe while the database is being written.
  • Copy the WAL and SHM files only if you also stop writers; a .backup already includes committed data.

Related scripts

Rsync Snapshot Backup

Mirror a directory to a backup target with dry-run, delete protection, and a log.

Backup and Recovery

Verify a Tar Backup

Check an archive's checksum, list its contents, and compare a sample safely.

Backup and Recovery