Open a separate destination database and call source.backup(destination) from Python’s sqlite3. To test the backup, open the copy from a fresh connection, run an integrity check, and query records you expect the application to find. Practice the restore at a separate path before you need to replace a live database.
Why an ordinary copy can be misleading
An active SQLite database may use journal or write-ahead log files alongside its main file. While changes are occurring, copying only the main file can omit recent committed data or capture an unusable combination of state. A SQLite-aware backup uses the database engine to produce a consistent copy. The destination should be a separate database connection and a private path reserved for the backup.
If you make ordinary file copies instead, follow a documented method that accounts for journal mode and ensures a stable source. An application window closing does not prove there are no other database connections. Identify which process owns the data and use a backup method appropriate to its operating state.
Create a backup and open it as a new database
This example creates synthetic data in a temporary directory. It demonstrates the API and reopens the backup as a new connection.
import sqlite3
from contextlib import closing
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
live = Path(folder) / "inventory.sqlite"
backup = Path(folder) / "inventory-backup.sqlite"
with closing(sqlite3.connect(live)) as source:
with source:
source.execute("CREATE TABLE tools (name TEXT PRIMARY KEY)")
source.execute("INSERT INTO tools VALUES (?)", ("pliers",))
with closing(sqlite3.connect(backup)) as destination:
source.backup(destination)
with closing(sqlite3.connect(backup)) as restored:
assert restored.execute("PRAGMA integrity_check").fetchone() == ("ok",)
assert restored.execute("SELECT name FROM tools").fetchall() == [("pliers",)]
The source commits its insert before the backup. The integrity check verifies SQLite structure, while the query confirms that the application’s tools table contains the expected item. On a staging restore, test recognizable records, key totals and required relationships. If the schema uses foreign keys, also run PRAGMA foreign_key_check.
Name and store verified backups
Choose a unique output name or a temporary destination so a failed copy cannot replace the only known good backup. Record which database was copied, the backup time and the application schema version. Do not treat the backup time as the timestamp of every record inside. The database may contain data across many periods.
Determine where backups are stored and who is permitted to access them. A database backup can include the same sensitive contents as the active file, plus records that the current app no longer displays. Store it under the protection and retention rules that apply to the data. A second copy on the same disk may be convenient for testing and still fail along with the original disk.
Practice a restore as a separate operation
Restore to a different path first. Open it using the application version that understands its schema, run your checks, and confirm that it contains data from the expected recovery period. Record what would happen to changes made after that point. If a restore is eventually needed in production, coordinate how the app stops writing to the old destination and how the validated replacement becomes the one it reads.
If you have multiple databases or related file stores, decide whether they need matching snapshots. A backup of one database can be internally consistent while still referencing files that were removed after the snapshot. Know how those dependencies are restored together or reconciled after a partial recovery.
Verify failure behavior before depending on it
Test an unavailable destination and an interrupted backup where the environment allows it. Verify that the previous usable backup remains available. Confirm that a restore can be performed by someone who did not write the script. The backup workflow is complete when that person can recover the expected data and describe which interval of work would need to be reconstructed.
If a backup raises an error, treat the destination file as failed until it has been verified. A name ending in .sqlite does not establish that every page was copied. For critical backups, record what ran, where the verified copy is stored, and when a separate restore last succeeded. Read how transactions define committed changes before writing application-specific restore checks.