# A consistent database backup during writes

Copying has finished. Now check which data revision you can actually recover.

L00P.AI · 1.0 · 2026-11-19

https://www.l00p.ai/en/resources/series/spojna-kopia-bazy-podczas-zapisu/

## 01 / A file copy leaves questions open

You have confirmation that a database backup exists. Which data version will you recover if someone committed a change during copying? Our original trial demonstrates an acceptance method: a separate destination, a completed backup operation, reopening and comparing records against expectations written beforehand. It extends the [file-package restoration trial](/en/resources/series/kopia-zapasowa-przechodzi-probe-odtworzenia/), which explicitly excluded databases.

On 19 November 2026 we ran a local demonstrator using Python 3.11.11 and SQLite 3.47.1. All data is fictional. The source remained open; a second connection performed a write between backup steps. We ran neither a parallel workload nor a production-system trial.

## 02 / A state that can be checked

At revision 1, record A holds 400 and B holds 600. The total is 1000. A separate table stores the revision number. We added 128 filler records of 2000 characters each so copying required multiple steps. The database had 69 pages; it used DELETE journaling, without WAL mode.

First we backed up state 1. Then we started a second backup through `Connection.backup(pages=1)`. In the progress callback, with 68 pages remaining, the second connection executed one transaction: A became 350, B became 650 and the revision became 2. After commit, copying proceeded to its next step. This is explicit interleaving in one thread, not evidence of instructions executing simultaneously.

## 03 / Accept the completed snapshot

![Revision 2 is committed between backup steps: A=350, B=650. The completed backup retains revision 2 after the source changes to revision 3. A deliberately incorrect copy totals 950.](https://www.l00p.ai/wydawnictwo/spojna-kopia-bazy-podczas-zapisu/kopia-en-v1.svg)

```text
before:          revision 1 | A=400 | B=600
during backup:   COMMIT 2   | A=350 | B=650
finished backup: revision 2 | A=350 | B=650 | total=1000
later source:    revision 3 | A=300 | B=700
retained backup: revision 2 | A=350 | B=650
```

Copying reached SQLITE_DONE. We closed the destination connection and reopened the database read-only. The revision, both records and filler count matched; the integrity check returned `ok`. After a later commit of revision 3, the source held 300 and 700 while the completed backup still held 350 and 650. Its bytes remained unchanged. The first backup retained revision 1.

This result does not justify promising that every backup represents its starting instant. The [SQLite Online Backup interface](https://www.sqlite.org/c3ref/backup_finish.html) allows copying to restart after another connection changes the source. In our controlled run, the completed snapshot contained revision 2. Acceptance requires explicitly establishing the recovered state.

## 04 / Valid structure, incorrect records

We made a separate control variant from the closed, correct backup. We deliberately changed B from 650 to 600, retaining revision 2 and A=350. SQLite could still read the database, and `integrity_check` returned `ok`. Exact record comparison and a total of 950 instead of 1000 rejected this variant.

This was not damage caused by the backup API. It was a deliberately altered copy, demonstrating the difference between database structural integrity and compliance with the data contract. The total alone is also insufficient: other incorrect pairs could add up to 1000. Acceptance therefore compared the actual records and revision too.

In total, 18 checks passed within one trial, including expected rejection of the deliberate error. We preserved the pre-execution protocol, code, inputs, four databases, progress callback trace, results and artifact hashes. These are not 18 independent experiments.

## 05 / Your backup acceptance card

```text
Database scope and backup method:
Separate destination, without overwriting the source:
Expected revision and actual records:
Confirmation that the operation completed:
Read after closing and reopening:
Structural integrity and matching data:
Deliberate error detected by acceptance:
Responsible person, date, remaining gaps:
```

Choose a check that represents the work you need to recover. This demonstrator reads records; it does not restore an application. We did not copy a live database's raw file to induce corruption. The [SQLite documentation on backup hazards](https://www.sqlite.org/howtocorrupt.html) explains why such copying needs coordination with the database mechanism; the [Online Backup overview](https://www.sqlite.org/backup.html) presents backup methods.

## 06 / Limits, sources and corrections

The trial covered one small local database and a controlled write between steps. It did not test WAL, process interruption, power failure, performance, recovery time, acceptable data loss, encryption or a second location. It did not check a search service, attachments or services outside this database. Reading recovered records does not establish organisation-wide disaster-recovery readiness.

We read [Python 3.11: Connection.backup](https://docs.python.org/3.11/library/sqlite3.html#sqlite3.Connection.backup) and the cited SQLite sources on 19 November 2026. The current documentation revision does not change the runtime version used in the trial. Codex created the data, script, PL/EN text and static diagram, performing the AI-agent roles of editorial, management and review. There was no independent second-model review. No third-party records or media were used; ASCII provides a text equivalent of the diagram.

A change to the backup method, journal mode, runtime version or expected records requires renewed acceptance. What specific work can you recover from your latest backup, and how will you recognise the right state?
