Error: stepping, database disk image is malformed (11). That came out of a backup made with plain cp while a program was writing to the database, and it was not a fluke: on a test container, 18 of 30 copies taken that way failed SQLite’s own integrity check. The command that makes the safe copy is one line long, ships with SQLite, and finished in a quarter of a second against a database taking writes. All tests ran on an Ubuntu 24.04 container using the sqlite3 command-line shell, version 3.45.1, plus a small Python writer script using Python’s built-in sqlite3 module.
Why a plain copy of a busy database breaks
The file is a building under construction, and cp photographs it floor by floor
cp reads a file from start to finish. If the program using the database writes to the middle of the file while cp is halfway through, the copy ends up with old pages from before the write and new pages from after it. SQLite’s own guide, How To Corrupt An SQLite Database File, describes exactly this: a background backup taken mid-transaction “might contain some old and some new content, and thus be corrupt.”
The test reproduced it. A Python script inserted rows in batches of 200 (about 1 KB of random data each) and updated older rows, committing in a loop for 12 seconds, in SQLite’s default rollback-journal mode. Meanwhile a shell loop copied the database file with cp every 0.2 seconds and ran PRAGMA integrity_check on each copy:
copy_2: Error: stepping, database disk image is malformed (11)
copy_4: *** in database main ***
copy_5: Error: stepping, database disk image is malformed (11)
...
copy_29: Error: stepping, database disk image is malformed (11)
writer transactions: 133
rollback-journal mode: 18 of 30 cp copies failed integrity_check
Twelve copies passed. That is the dangerous part. A backup script that uses cp will often produce good files, and a test restore on a quiet afternoon will probably succeed. The bad copies tend to come from the moments the database is busiest, which is when the data matters most.
The same SQLite page also says it is safe to copy the file “as long as there are no transactions in progress while the copy is taking place.” Stopping the application, copying and starting it again is fine; copying while it runs is a coin toss weighted against the operator, at 18 losses in 30 on the container.

WAL mode: the copy that is intact and empty
Committed data that lives in a second file nobody copied
Many applications switch SQLite to write-ahead logging. In that mode, per the SQLite WAL documentation, “the changes are appended into a separate WAL file,” and the main database file is only updated during a checkpoint. By default a checkpoint runs when the WAL reaches 1,000 pages, or when the last connection closes.
That changes how cp fails. A deterministic test: a Python program opened a database in WAL mode, created a table, committed 500 rows one at a time, and kept its connection open, the way a running app would. Then it copied the main file:
journal_mode: wal
committed rows: 500
{'app.db': 4096, 'app.db-shm': 32768, 'app.db-wal': 2080632}
cp copy error: no such table: orders
rows in VACUUM INTO copy: 500
rows in backup-API copy: 500
The main file was 4,096 bytes. All of the data, the table definition included, sat in the 2 MB -wal file. The copy of app.db alone opened cleanly and contained nothing. No corruption error, no warning; an empty database that looks healthy.
The WAL documentation is direct about this: the WAL file “is part of the persistent state of the database and should be kept with the database if the database is copied or moved,” and separating them means committed transactions “might be lost, or the database file might become corrupted.” A second test, with a busier writer and normal checkpointing, produced cp copies that passed the integrity check in 30 out of 30 tries. That run did not reliably measure how many committed rows each copy lacked, and the check could not have said either: passing integrity_check proves the file is internally consistent. It says nothing about whether the last hour of orders is in it.
The safe ways: .backup, VACUUM INTO, the backup API
Three tools that ask SQLite for a snapshot instead of photographing the file
The same corruption guide lists three safe approaches that work on a live database: the backup API, VACUUM INTO and sqlite3_rsync. The shell offers VACUUM INTO directly, along with a .backup command of its own.
.backup in the command-line shell. The sqlite3 CLI documentation lists it as “Backup DB (default “main”) to FILE.” On the container, against a WAL database receiving a continuous stream of writes:
$ time sqlite3 wal.db ".backup bk.db"
real 0m0.240s
bk rows: 19000 integrity: ok
VACUUM INTO. Added in SQLite 3.27.0, released 2019-02-07. The VACUUM documentation says the result is “a consistent snapshot of the original database” and that the new file is “fully vacuumed,” so freed space is not copied:
$ time sqlite3 wal.db "VACUUM INTO 'vi.db'"
real 0m0.111s
vi rows: 19200 integrity: ok
The backup API from code. Python’s standard library exposes it as Connection.backup(), and the Python sqlite3 documentation says it “Works even if the database is being accessed by other clients or concurrently by the same connection.” In the 500-row test above, it returned all 500 rows.
The two shell methods differ in useful ways. The VACUUM page says VACUUM INTO’s output “is minimal in size” and purges deleted content, “leaving behind no forensic traces,” while “the backup API uses fewer CPU cycles and can be executed incrementally.” The container showed a smaller one too: the .backup copy kept the source’s WAL journal mode, while the VACUUM INTO copy came out in rollback (delete) mode.
cli .backup journal_mode: wal rows: 500
VACUUM INTO journal_mode: delete
For copying across a network, the corruption guide also lists sqlite3_rsync, a separate utility available from SQLite 3.47.0. Its documentation says the replica becomes “a copy of a snapshot of ORIGIN as it existed when the sqlite3_rsync command started,” and contrasts it with ordinary rsync, which “does not understand SQLite transactions.” It needs to be installed on both ends of the SSH connection.

A minimal nightly backup
One shell line, one date stamp, one check
A script that runs from cron or a systemd timer:
#!/bin/bash
set -euo pipefail
src=/srv/app/data/app.db
dst=/backup/sqlite/app-$(date +%F).db
sqlite3 "$src" "VACUUM INTO '$dst'"
sqlite3 "$dst" "PRAGMA integrity_check;" | grep -qx okThe date stamp matters for a second reason besides history. The VACUUM documentation says the target “must not previously exist, or else it must be an empty file, or the VACUUM INTO command will fail with an error.” The container confirmed it:
$ sqlite3 app.db "VACUUM INTO 'vacuum_copy.db'"
Error: stepping, output file already exists
So a script that reuses a fixed filename will fail on its second night, every night, until someone reads the log. The script above was run twice in a row on the container, with paths pointed at a test directory; the first run exited 0, the second did not:
$ ./nightly.sh; echo "first run exit=$?"
first run exit=0
$ ./nightly.sh; echo "second run same day exit=$?"
Error: stepping, output file already exists
second run same day exit=1
A once-a-day stamp means a manual rerun on the same day fails loudly, which is the right kind of failure. A job that runs hourly needs the time in the name too, such as $(date +%F-%H%M). .backup has the opposite habit. The CLI help lists .save as an alias for .backup, the documentation warns that .save “will overwrite any preexisting database files having the same name without prompting,” and on the container .backup wrote over an existing file and exited 0. Neither behavior is wrong. Each one needs to be known in advance.
For durability, VACUUM INTO calls fsync() on the output when the source database’s synchronous setting is NORMAL or FULL, per the same page. If power fails mid-command, though, “the generated output database might be incomplete and corrupt.” That is what the integrity check on the last line is for.
Where people get burned
The backup that works, the restore that quietly lies
Copying only the .db file of a WAL database. Shown above: the copy can be perfectly valid and missing everything committed since the last checkpoint. Docker Compose stacks invite this, because a volume backup that tars or rsyncs a directory while the container runs is cp with extra steps. The Compose self-hosting walkthrough covers where those volumes live.
Restoring over a stale -wal file. The corruption guide lists “Overwriting a database file with another without also deleting any hot journal associated with the original database” as a corruption cause. On the container, a snapshot holding one row was copied over a database file whose leftover -wal from a crash still sat beside it. Opening the “restored” database:
rows: (49,) integrity: ('ok',)
The restore was supposed to roll the data back to one row. SQLite opened it, found a WAL with that name, and the result held 49. In this small test the file stayed consistent; the guide’s warning is about corruption, and one small test cannot rule that out. Delete or move the old -wal and -shm before putting a backup in place, with the application stopped.
Deleting a big -wal file to save space. A WAL can grow large when checkpoints cannot finish; with automatic checkpointing switched off in one test, it reached 535 MB beside a 4 KB main file. That kind of file shows up when hunting for what filled a disk. The WAL page says “The only safe way to remove a WAL file is to open the database file using one of the sqlite3_open() interfaces then immediately close the database.”
A backup that never finishes. The backup API documentation says that when another process writes during an incremental backup, SQLite “usually restarts the backup process,” and warns that if this happens often enough, the backup “may never run to completion.” On a database with constant writes, VACUUM INTO, which takes one consistent snapshot, can be the more reliable choice.
Treating one backup on the same disk as safe. A dated copy in the next directory protects against a bad migration and nothing else. The same rule as RAID not being a backup applies to a SQLite file.

The case for just stopping the app
Thirty seconds of downtime against a class of bugs
The strongest argument against everything above is the simplest one. SQLite’s own guide says copying is safe when no transaction is in progress. For a home-lab service that nobody uses at 3 a.m., stopping the container, copying the directory with its -wal and -shm files, and starting it again is boring, short and complete. It also catches files the application keeps outside the database, which no SQLite command knows about.
The weakness is the word “stopped.” Services that restart themselves, sidecar containers that keep a connection open, and cron jobs that run while a deploy is half done all turn a stopped app into a mostly stopped one. The online methods do not care whether the application is running. That is their entire value.
One number from the tests sticks. Twelve of the thirty cp copies passed. A backup job built on cp would have reported success on all thirty runs, the eighteen bad ones included, because cp did its job perfectly and copied what was there. When was the last time anyone actually opened one of the backups and counted the rows?
Sources
- SQLite, How To Corrupt An SQLite Database File: https://www.sqlite.org/howtocorrupt.html
- SQLite, Write-Ahead Logging: https://www.sqlite.org/wal.html
- SQLite, VACUUM: https://www.sqlite.org/lang_vacuum.html
- SQLite, Using the SQLite Online Backup API: https://www.sqlite.org/backup.html
- SQLite, Command Line Shell For SQLite: https://www.sqlite.org/cli.html
- SQLite Release 3.27.0: https://sqlite.org/releaselog/3_27_0.html
- SQLite, sqlite3_rsync: https://www.sqlite.org/rsync.html
- Python documentation, sqlite3: https://docs.python.org/3/library/sqlite3.html
