[Blog](https://deploywithox.com/blog) [Guides](https://deploywithox.com/blog#c-guides) # Back Up a Live SQLite Database Before Deploy, and Prove It Back up a live SQLite database before a deploy with .backup or VACUUM INTO, not cp, which can miss WAL commits. Then prove the copy in an empty folder. Saurav Sharma Dec 2, 2026 15 min read On this page - [Why a copy of app.db can miss a commit](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-why) - [Check whether your database uses WAL](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-check) - [Back up the live database before the deploy](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-backup) - [What the check proves, and how to use it in a deploy](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-verify) - [Restore a backup into a new folder](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-restore) - [Try it: the lab](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-lab) - [How ox backs up SQLite](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-ox) - [Key takeaways](https://deploywithox.com/blog/sqlite-backup-before-deploy#key-takeaways) - [Frequently asked questions](https://deploywithox.com/blog/sqlite-backup-before-deploy#faq) - [Next step](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-next) To back up a live SQLite database before a deploy, let SQLite make the copy with `.backup` or `VACUUM INTO`. Do not `cp` the database file while the app runs. In WAL mode, SQLite writes new commits to a second file, `app.db-wal`, and moves them into `app.db` later, so a copy of `app.db` alone can open cleanly, pass `PRAGMA integrity_check`, and still lack the last things your users saved. Then prove the backup before the deploy goes on: open a copy of it in an empty folder, run `integrity_check`, and check that it holds at least the newest row the live database had. The script below does all of that and stops the deploy if any step fails. How this was tested: on 2 December 2026 (UTC) I ran the lab at the end of this post and both scripts below as an ordinary user, in temporary folders, with two builds of the `sqlite3` command line tool. One was 3.46.1 from Ubuntu 26.04's `sqlite3` package (3.46.1-9ubuntu0.5), the other 3.54.1, the newest release on sqlite.org that day. A test harness ran the scripts' exact text against a `sqlite3` process standing in for the running app. It also gave them a mistyped database path, a missing, damaged or out-of-date backup, and a stop signal halfway through. Each failed run removed its own backup or verification directory if it had created one. A newly created backup root and its parent directories can remain. I did not run the scripts on a real server or with a service manager. I build ox, which deploys apps to your own server. You need no ox for any of this. ## Why a copy of app.db can miss a commit SQLite's [write-ahead log page](https://sqlite.org/wal.html) explains it. In WAL mode, "the original content is preserved in the database file and the changes are appended into a separate WAL file", and "a COMMIT occurs when a special record indicating a commit is appended to the WAL." Moving those changes into the main file is a separate step called a checkpoint. By default it runs when the WAL reaches 1000 pages, and the last connection to close does one more. While your app has the database open, a commit can sit in `app.db-wal` for a long time. The same page warns: "If a database file is separated from its WAL file, then transactions that were previously committed to the database might be lost, or the database file might become corrupted." The lab below shows the quiet version of that. The bare copy opens, `integrity_check` says `ok`, and the committed row is not there. Nothing tells you something is missing. SQLite's [page on how files get corrupted](https://sqlite.org/howtocorrupt.html) names three safe ways to copy a live database: the online backup API, `VACUUM INTO`, and the `sqlite3_rsync` program. It adds that a plain file copy is safe from corruption only when no transaction is in progress during the copy. The lab's bare copy was taken with no transaction in progress and was not corrupt, yet it still missed a commit that had not been checkpointed. `sqlite3_rsync` needs SQLite 3.47.0 or later, newer than Ubuntu 26.04's 3.46.1, so this post uses the other two. ## Check whether your database uses WAL The paths and the table name in this post are examples. Use your own, and run every command as the user your app runs as, for example in a shell opened with `sudo -u app -s`: Shell ```bash ls -l /srv/app/shared/app.db* sqlite3 -readonly /srv/app/shared/app.db 'PRAGMA journal_mode;' ``` If it prints `wal`, a bare copy can miss commits as shown here. In the default rollback journal mode, a copy taken while a write is half done can hold some old pages and some new ones. The scripts below work in both modes. `-readonly` matters here and in the scripts: without it, a mistyped path makes `sqlite3` create a new, empty database, and a backup of that would look fine. If `ls` shows an `app.db-journal` file after the database closes, keep it with the database. Some journal modes leave this file after a clean commit. SQLite may need it for recovery if a write was interrupted, so keep it beside `app.db`. Also check where the file lives. If it sits inside the release folder, such as `current/db/app.db`, a deploy that builds a fresh folder can leave the app opening a new, empty database. Keep it in a folder outside the releases, and point the app at it. ## Back up the live database before the deploy Save this as `/srv/app/bin/sqlite-backup.sh`. It takes the database, a backup folder and a query that prints a whole number that never goes down, such as the highest order id: Shell ```bash #!/usr/bin/env bash # sqlite-backup.sh DB BACKUP_DIR CHECK_SQL # Backs up a live SQLite database into a new private folder, proves the # copy, then prints its path. CHECK_SQL must print one whole number that # never goes down, such as 'SELECT max(id) FROM orders;'. set -euo pipefail umask 077 db=$1 dir=$2 sql=$3 out= t= proven= cleanup() { if [ -n "$t" ]; then rm -rf -- "$t"; fi if [ -n "$out" ] && [ -z "$proven" ]; then rm -rf -- "$out"; fi } trap cleanup EXIT trap 'exit 130' INT TERM [ -f "$db" ] || { echo "no database file at $db" >&2; exit 1; } before=$(sqlite3 -readonly "$db" "$sql") mkdir -p -- "$dir" out=$(mktemp -d "$dir/$(date -u +%Y%m%dT%H%M%SZ)-XXXXXX") sqlite3 -readonly "$db" ".backup '$out/app.db'" # Check a copy in an empty temp folder, so no -wal from the live # database sits beside it and nothing is added next to the backup. t=$(mktemp -d) cp -- "$out/app.db" "$t/check.db" ok=$(sqlite3 -readonly "$t/check.db" 'PRAGMA integrity_check;') after=$(sqlite3 -readonly "$t/check.db" "$sql") echo "integrity_check=$ok live=$before backup=$after" >&2 [ "$ok" = ok ] || { echo "the backup failed integrity_check" >&2; exit 1; } [ "$after" -ge "$before" ] 2>/dev/null || { echo "the backup is older than the live database, or the query did not print a whole number" >&2; exit 1; } proven=1 echo "$out/app.db" ``` Run it as the app's user while the app keeps running: Shell ```bash bash /srv/app/bin/sqlite-backup.sh /srv/app/shared/app.db /srv/app/backups 'SELECT max(id) FROM orders;' ``` It prints the check on one line and the backup's path on the last line, something like this: Code ```text integrity_check=ok live=4182 backup=4182 /srv/app/backups/20261203T091500Z-k3J9qL/app.db ``` The `.backup` command uses SQLite's [online backup API](https://sqlite.org/backup.html). The app keeps running, and the result is, in the API docs' words, "a consistent and up-to-date snapshot of the original." Each run writes into a new folder that `mktemp` creates with a random name, so a retry or two runs in the same second can never overwrite an earlier backup. `umask 077` makes that folder and the file in it readable only by the app's user, whatever the backup folder above them allows. The script never deletes old backups, so remove the ones you no longer need. The same docs note that when another process writes during the copy, the backup usually starts over, and a database written to often enough may never finish. If your app writes constantly, change the `.backup` line to `VACUUM INTO`, which [its docs](https://sqlite.org/lang_vacuum.html) describe as "an alternative to the backup API for generating backup copies of a live database": Shell ```bash sqlite3 -readonly "$db" "VACUUM INTO '$out/app.db'" ``` Its copy leaves out deleted content, so it can be smaller than the original. The same page says `VACUUM` may change the ROWIDs of tables that have no explicit `INTEGER PRIMARY KEY`, and that `VACUUM INTO` works the same way. In my test its copy was also in rollback journal mode although the original used WAL. WAL mode is stored in the file, so if your app does not turn it on itself, run `PRAGMA journal_mode=WAL;` on a restored copy. ## What the check proves, and how to use it in a deploy A backup you have never opened is a guess. The script copies the backup into an empty temporary folder, so no `-wal` file from the live database sits beside it, and opens that copy read-only. `integrity_check` must print exactly `ok`. That alone is not enough, as the lab shows, so the script also reads your number from the live database before the backup and from the copy after it. The copy was made later, so its number must be at least as high. If it is lower, the backup missed recent commits and the script fails. Each failure deletes the new backup folder and the temporary folder, and exits with a non-zero status, so a bad backup is never left looking like a good one. Put it in your deploy script before the step that migrates, with `set -e` so a failed backup stops the deploy: Shell ```bash set -euo pipefail backup=$(bash /srv/app/bin/sqlite-backup.sh /srv/app/shared/app.db /srv/app/backups 'SELECT max(id) FROM orders;') echo "proven backup: $backup" ``` Write down the `backup=` number it printed. You need it to check a restore. ## Restore a backup into a new folder A restore is where a backup can do damage, so this one changes no file your app uses. Save it as `/srv/app/bin/sqlite-restore.sh`. It copies the backup into a new private folder, checks a copy of it, and requires the number the backup script printed: Shell ```bash #!/usr/bin/env bash # sqlite-restore.sh BACKUP PARENT CHECK_SQL EXPECTED # Copies a backup into a new private folder under PARENT, proves that # CHECK_SQL prints EXPECTED there, then prints the restored file's path. # It changes no file your app uses now. set -euo pipefail umask 077 b=$1 parent=$2 sql=$3 want=$4 r= t= proven= cleanup() { if [ -n "$t" ]; then rm -rf -- "$t"; fi if [ -n "$r" ] && [ -z "$proven" ]; then rm -rf -- "$r"; fi } trap cleanup EXIT trap 'exit 130' INT TERM [ -f "$b" ] || { echo "no backup file at $b" >&2; exit 1; } r=$(mktemp -d "$parent/restored-$(date -u +%Y%m%dT%H%M%SZ)-XXXXXX") cp -- "$b" "$r/app.db" t=$(mktemp -d) cp -- "$r/app.db" "$t/check.db" ok=$(sqlite3 -readonly "$t/check.db" 'PRAGMA integrity_check;') got=$(sqlite3 -readonly "$t/check.db" "$sql") echo "integrity_check=$ok check=$got" >&2 [ "$ok" = ok ] || { echo "the backup failed integrity_check" >&2; exit 1; } [ -n "$got" ] && [ "$got" = "$want" ] || { echo "wanted $want, the backup has $got" >&2; exit 1; } proven=1 echo "$r/app.db" ``` Shell ```bash bash /srv/app/bin/sqlite-restore.sh /srv/app/backups/20261203T091500Z-k3J9qL/app.db /srv/app/shared 'SELECT max(id) FROM orders;' 4182 ``` It prints something like this: Code ```text integrity_check=ok check=4182 /srv/app/shared/restored-20261203T101200Z-Qw81zT/app.db ``` If the backup is missing, damaged or holds a different number, the script stops with an error and removes the folder it made, so nothing half restored is left behind. When it succeeds, you have a verified file at the printed path, beside the old database rather than over it. To hand it to the app, stop the app and everything else that opens the database, such as workers and timers. Change the app's database path setting to the printed path, start it, and check in the app that recent data is there. The old `app.db` stays where it was, with any `app.db-wal`, `app.db-shm` or `app.db-journal` beside it, so going back means changing the setting back. Do not copy the restored file over the old `app.db`. SQLite's [corruption page](https://sqlite.org/howtocorrupt.html#_mispairing_database_files_and_hot_journals_) counts both `-journal` and `-wal` files as hot journals, and lists "overwriting a database file with another without also deleting any hot journal associated with the original database" as likely to corrupt it. If your app cannot open a database at another path, use a restore tool that stops every process and moves the old file with its `-wal`, `-shm` and `-journal` files. The scripts create a private directory and file for the user who runs them. Before switching paths, check access for each service account that opens the database. It needs search permission on every parent directory. Writers also need write access to the database and permission to create and remove journal files in its directory. If workers use different accounts, grant the intended group or named accounts access through group ownership and permissions or an ACL. Keep backups private. If the app also stores files on disk, such as uploads, restore them from the same moment. The [PostgreSQL backups post](https://deploywithox.com/blog/postgres-backups-on-a-vps) covers the same idea for a database server. ## Try it: the lab This script needs only bash and `sqlite3`. It runs as an ordinary user, makes a new temporary folder, and deletes it at the end, also when it fails or you press Ctrl-C. A second `sqlite3` process plays the running app: it keeps its connection open, turns off automatic checkpoints, and commits a row named `committed-marker`. The script then makes a bare copy of `app.db`, makes an online backup and a `VACUUM INTO` copy, restores the backup into an empty folder, and checks each copy. Every check must open the file and print exactly the expected `integrity_check` result and number of marker rows, or the script stops with an error. Shell ```bash #!/usr/bin/env bash # A bare copy of a live SQLite file misses a commit; an online backup keeps it. # Runs as an ordinary user, in a new temporary folder it deletes at the end. set -euo pipefail command -v sqlite3 >/dev/null || { echo "sqlite3 is not installed" >&2; exit 1; } lab=$(mktemp -d) app= cleanup() { exec 3>&- 2>/dev/null || true if [ -n "$app" ]; then kill "$app" 2>/dev/null || true; wait "$app" 2>/dev/null || true; fi rm -rf -- "$lab" } trap cleanup EXIT trap 'exit 130' INT TERM cd "$lab" mkdir live copy backup restore vacuum # expect FILE INTEGRITY ROWS: stops the lab unless the file opens, and # integrity_check and the count of marker rows print exactly these. expect() { local ok marker ok=$(sqlite3 -readonly "$1" 'PRAGMA integrity_check;') marker=$(sqlite3 -readonly "$1" "SELECT count(*) FROM notes WHERE body = 'committed-marker';") echo "$1: integrity_check=$ok marker_rows=$marker" [ "$ok" = "$2" ] && [ "$marker" = "$3" ] || { echo "UNEXPECTED: wanted integrity_check=$2 marker_rows=$3" >&2; exit 1; } } # 1. A database in WAL mode. Its table and first row reach the main file # when this command closes the last connection. sqlite3 live/app.db "PRAGMA journal_mode=WAL; CREATE TABLE notes(id INTEGER PRIMARY KEY, body TEXT NOT NULL); INSERT INTO notes(body) VALUES ('before-deploy');" >/dev/null # 2. The running app: one connection that stays open and never # checkpoints, so its commit stays in app.db-wal. mkfifo app.in sqlite3 live/app.db app.out 2>&1 & app=$! exec 3>app.in printf '%s\n' 'PRAGMA wal_autocheckpoint=0;' \ "INSERT INTO notes(body) VALUES ('committed-marker');" '.print committed' >&3 for _ in $(seq 100); do grep -qx committed app.out && break; sleep 0.1; done grep -qx committed app.out || { echo "the app did not commit" >&2; exit 1; } echo "live files: $(ls live | paste -sd " ")" # 3. The bare copy: only the main file, while the app is running. # It opens and passes integrity_check, but has no marker row. cp live/app.db copy/app.db expect copy/app.db ok 0 # 4. Two safe backups, still while the app is running. The main file # still lacks the marker afterwards: it is only in the WAL. sqlite3 -readonly live/app.db ".backup backup/app.db" sqlite3 -readonly live/app.db "VACUUM INTO 'vacuum/app.db'" cp live/app.db copy/after.db expect copy/after.db ok 0 # 5. Restore the online backup into an empty folder and check it there. cp backup/app.db restore/app.db expect restore/app.db ok 1 expect vacuum/app.db ok 1 echo "all checks passed" ``` This is the output from both builds: Code ```text live files: app.db app.db-shm app.db-wal copy/app.db: integrity_check=ok marker_rows=0 copy/after.db: integrity_check=ok marker_rows=0 restore/app.db: integrity_check=ok marker_rows=1 vacuum/app.db: integrity_check=ok marker_rows=1 all checks passed ``` The bare copy passed `integrity_check` and had no `committed-marker` row. The restored backup and the `VACUUM INTO` copy, both taken while the app still had the row only in its WAL, had it. To be sure the checks catch a bad copy, I also ran changed versions of the script, and each one stopped with an error. With `cp` in place of `.backup` or of `VACUUM INTO`, that copy had `marker_rows=0`. When the bare copy took `app.db-wal` along, it had `marker_rows=1`. When the bare copy was a file of junk, or the backup was missing, `sqlite3` could not open it. Every run, and one stopped with `kill -TERM` while the app connection was open, left no folder and no process behind. Copying the WAL file too is still not a backup method you should rely on: it is not one of the safe ways SQLite lists. ## How ox backs up SQLite ox looks for the SQLite files your app keeps in the project's data folder (`OX_DATA_DIR`) and backs each one up every day soon after 03:00 UTC. The search has limits. It goes at most 3 folders down, stops after 20 databases or 2,000 files and folders, and skips symlinks, the `services` folder and folders left by a restore. So before you rely on it, open the project's **Backups** tab and check that your database's path is listed. That tab shows each file it found with a **Back up now** button, so you can take one right before a deploy. Each backup runs `sqlite3 .backup` as the project's own user while the app keeps running, then stores the copy gzipped. Unlike the script above, an ox restore puts the file back in place. It stops the app, its workers and its cron jobs, backs up the file as it is first, replaces it, removes the old `-wal`, `-shm` and `-journal` files, and starts them again. The automatic copy ox makes right before a deploy's migrate step covers PostgreSQL only, so before a deploy that changes a SQLite schema, press Back up now. The [backups guide](https://deploywithox.com/docs/operate/backups) has the details, and [how a new release takes over traffic](https://deploywithox.com/blog/zero-downtime-deploys-systemd-caddy) covers the deploy itself. ## Key takeaways - In WAL mode, recent commits can live in `app.db-wal`, so a copy of `app.db` alone can open, pass `integrity_check`, and miss them. - Back up a live database with `.backup` or `VACUUM INTO`, opened with `-readonly` so a typo cannot create an empty database, into a new private folder, and stop the deploy on any error. - Prove each backup on a copy in an empty folder: `integrity_check` must print `ok`, and a number that never goes down must be at least the live value. - Restore into a new folder, check it there, then point the stopped app at it. Never put a backup over `app.db` while `app.db-wal` or `app.db-journal` sits beside it. ## Frequently asked questions ### Is it safe to cp a SQLite database while the app is running? Not in general. In WAL mode, recent commits can sit in the -wal file, so a copy of the main file alone can open cleanly and still miss them. In rollback mode, a copy taken during a write can mix old and new pages. Use the sqlite3 .backup command or VACUUM INTO instead, which are built for live databases. ### Do I need to back up the -wal file too? Not when you use .backup or VACUUM INTO: each writes one complete database file that includes the committed changes from the WAL. If you copy files by hand with the app stopped and a -wal or -journal file remains, SQLite's docs say it must be kept together with the database file. ### How do I know a SQLite backup is good? Copy it into an empty folder and open it there read-only. PRAGMA integrity_check must print ok, and a number that never goes down, such as the highest order id, must be at least what the live database showed just before the backup. Integrity alone does not prove the backup holds your latest commits, so check both before you deploy. ## Next step Run [the lab](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-lab) once to watch the bare copy miss the marker, then add the [backup script](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-backup) to your deploy script [before the step that migrates](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-verify). New to running apps on your own server? [How ox runs your apps](https://deploywithox.com/self-hosting) shows what ox sets up on your server. On this page - [Why a copy of app.db can miss a commit](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-why) - [Check whether your database uses WAL](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-check) - [Back up the live database before the deploy](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-backup) - [What the check proves, and how to use it in a deploy](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-verify) - [Restore a backup into a new folder](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-restore) - [Try it: the lab](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-lab) - [How ox backs up SQLite](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-ox) - [Key takeaways](https://deploywithox.com/blog/sqlite-backup-before-deploy#key-takeaways) - [Frequently asked questions](https://deploywithox.com/blog/sqlite-backup-before-deploy#faq) - [Is it safe to cp a SQLite database while the app is running?](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-faq-cp) - [Do I need to back up the -wal file too?](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-faq-wal) - [How do I know a SQLite backup is good?](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-faq-verify) - [Next step](https://deploywithox.com/blog/sqlite-backup-before-deploy#heading-next) Written by Saurav Sharma Builds ox. Writes about running apps on servers you own, without Docker and without a platform bill. [Older post Strapi Uploads Gone After Redeploy? Restore and Keep Them](https://deploywithox.com/blog/strapi-uploads-disappear-after-redeploy) [Newer post PostgreSQL Connection String Password Special Characters](https://deploywithox.com/blog/postgres-connection-string-special-characters) ## Keep reading - Guides [PostgreSQL Connection String Password Special Characters](https://deploywithox.com/blog/postgres-connection-string-special-characters) Fix a PostgreSQL connection string whose password has special characters: percent-encode it once, check how libpq reads it, keep it off the command line. Saurav Sharma Dec 3, 2026 16 min read - Guides [Strapi Uploads Gone After Redeploy? Restore and Keep Them](https://deploywithox.com/blog/strapi-uploads-disappear-after-redeploy) Recover Strapi uploads that return 404 after a redeploy. Compare files across releases, resolve conflicting copies, and keep uploads in one shared folder. Saurav Sharma Dec 2, 2026 22 min read - Guides [Caddy Reverse Proxy 502: Fix dial tcp Connection Refused](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused) Fix a Caddy reverse proxy 502 with dial tcp connection refused: read the failing request's log line, compare where Caddy dials with where the app listens. Saurav Sharma Dec 1, 2026 14 min read ## Deploy to a server you own ox sets up systemd, Caddy and PostgreSQL on your Ubuntu server and deploys on every push. Free for your first project. [Sign up with GitHub](https://deploywithox.com/login/github) [Read the docs](https://deploywithox.com/docs)