Skip to main content

sqlite_backup

This exposes the python stdlib sqlite.backup function as a library, with a couple extra steps.

The main purpose for writing this is to copy sqlite databases that you may not own -- perhaps it belongs to an application (e.g., your browser) and is locked since that's currently open, or the OS keeps it open while the computer is active (e.g. Mac with iMessage)

Features

In short, this prioritizes safety of the data over performance (temporarily copying data files to /tmp) - because we often don't know what the application may be doing while we're copying underlying sqlite databases

The initial backup function and some tests were extracted out of the karlicoss/HPI core/sqlite module

If other tools exist to do this, please let me know!

Installation

Requires python3.8+

To install with pip, run:

pip install sqlite_backup

Usage

Usage: sqlite_backup [OPTIONS] SOURCE_DATABASE DESTINATION

  SOURCE_DATABASE is the database to copy

  DESTINATION is where to write the database. If a directory, uses
  the SOURCE_DATABASE name. If a file, the directory must exist,
  and the destination file must not already exist (to prevent
  possibly overwriting old data)

Options:
  --debug                         Increase log verbosity  [default: False]
  --wal-checkpoint / --no-wal-checkpoint
                                  After writing to the destination, run a
                                  checkpoint to truncate the WAL to zero bytes
                                  [default: wal-checkpoint]
  --copy-use-tempdir / --no-copy-use-tempdir
                                  Copy the source database files to a
                                  temporary directory, then connect to the
                                  copied files  [default: copy-use-tempdir]
  --copy-retry INTEGER            If the files change while copying to the
                                  temporary directory, retry <n> times
                                  [default: 100]
  --copy-retry-strict / --no-copy-retry-strict
                                  Throws an error if this fails to safely copy
                                  the database files --copy-retry times
                                  [default: copy-retry-strict]
  --help                          Show this message and exit.  [default:
                                  False]

For usage in python, use the sqlite_backup function, see the docs

If you plan on reading from these backed up databases (and you're not planning on modifying these at all), I would recommend using the mode=ro (readonly) or immutable flags when connecting to the database. In python, like:

import sqlite3
from typing import Iterator

def sqlite_connect(database: str) -> Iterator[sqlite3.Connection]:
    try:
        # or for immutable, f"file:{database}?immutable=1"
        with sqlite3.connect(f"file:{database}?mode=ro", uri=True) as conn:
            yield conn
    finally:
        conn.close()

with sqlite_connect("/path/to/database") as conn:
    conn.execute("...")

Example

sqlite_backup --debug ~/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite ./firefox.sqlite
[D 220202 13:00:32 core:110] Source database files: '['/home/username/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite', '/home/username/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite-wal']'
[D 220202 13:00:32 core:111] Temporary Destination database files: '['/tmp/tmpm2nhl1p3/places.sqlite', '/tmp/tmpm2nhl1p3/places.sqlite-wal']'
[D 220202 13:00:32 core:64] Copied from '/home/username/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite' to '/tmp/tmpm2nhl1p3/places.sqlite' successfully; copied without file changing: True
[D 220202 13:00:32 core:64] Copied from '/home/username/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite-wal' to '/tmp/tmpm2nhl1p3/places.sqlite-wal' successfully; copied without file changing: True
[D 220202 13:00:32 core:240] Running backup, from '/tmp/tmpm2nhl1p3/places.sqlite' to '/home/username/Repos/sqlite_backup/firefox.sqlite'
Backed up /home/username/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite to /home/username/Repos/sqlite_backup/firefox.sqlite

Tests

git clone 'https://github.com/purarue/sqlite_backup'
cd ./sqlite_backup
pip install '.[testing]'
mypy ./sqlite_backup
pytest

Metadata

Release files for sqlite-backup 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlite-backup 0.1.8
File Size Uploaded Provenance Actions
sqlite_backup-0.1.8.tar.gz 14.3 kB Without provenance Details for sqlite_backup-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlite-backup 0.1.8
File Interpreter ABI Platform Actions
sqlite_backup-0.1.8-py3-none-any.whl Python 3 none any Details for sqlite_backup-0.1.8-py3-none-any.whl

Total release size: 25.2 kB

Release files / sqlite_backup-0.1.8.tar.gz

Download URL sqlite_backup-0.1.8.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
febd6249b9c137a2a4c0fb1ee024a1d0e6968590624cb0916ba068eb66179141
BLAKE2b-256 checksum
How to use checksums
8ec2108c65fe5abe42e23e71a1cca9ad3ffc1b14b3df8edce9d9e3edcfe81065
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / sqlite_backup-0.1.8-py3-none-any.whl

Download URL sqlite_backup-0.1.8-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be3704b849c87c2925cec85299c28b911aa107e448be59b83e8e38eb2dff0d1b
BLAKE2b-256 checksum
How to use checksums
96f0c180c80a3315a61db4c606e00d231a094462b7cf3156a6ef05296b4c4512
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for sqlite-backup 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page