# sqlite_backup
This exposes the python stdlib [`sqlite.backup`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.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
- Has the option (true by default) to first safely copy the database from disk to a temporary directory, which is:
- useful in case the source is in read-only mode (e.g. in some sort of docker container)
- safer if you're especially worried about corrupting or losing data
- Uses [`Cpython`s Connection.backup](https://github.com/python/cpython/blob/8fb36494501aad5b0c1d34311c9743c60bb9926c/Modules/_sqlite/connection.c#L1716), which directly uses the [underlying Sqlite C code](https://www.sqlite.org/c3ref/backup_finish.html)
- Performs a [`wal_checkpoint`](https://www.sqlite.org/pragma.html#pragma_wal_checkpoint) and sets `journal_mode=DELETE` after copying to the destination, to remove the WAL (write-ahead log; temporary database file). Typically the WAL is removed when the database is closed, but [particular builds of sqlite](https://sqlite.org/forum/forumpost/1fdfc1a0e7), the [default sqlite installed on mac](https://github.com/seanbreckenridge/sqlite_backup/issues/9), or sqlite compiled with [`SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE` enabled](https://www.sqlite.org/c3ref/c_dbconfig_enable_fkey.html) or [`SQLITE_FCNTL_PERSIST_WAL`](https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html#sqlitefcntlpersistwal) may prevent that -- so the checkpoint exists to ensure there are no temporary files leftover
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`](https://github.com/karlicoss/HPI/blob/a1f03f9c028df9d1898de2cc14f1df4fa6d8c471/my/core/sqlite.py#L33-L51) module
If other tools exist to do this, please [let me know!](https://github.com/seanbreckenridge/sqlite_backup/issues/new)
## Installation
Requires `python3.7+`
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](./docs/sqlite_backup/index.md)
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`](https://www.sqlite.org/uri.html#urimode) (readonly) or [`immutable`](https://www.sqlite.org/uri.html#uriimmutable) flags when connecting to the database. In python, like:
```python
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/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite', '/home/sean/.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/sean/.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/sean/.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/sean/Repos/sqlite_backup/firefox.sqlite'
Backed up /home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite to /home/sean/Repos/sqlite_backup/firefox.sqlite
```
### Tests
```bash
git clone 'https://github.com/seanbreckenridge/sqlite_backup'
cd ./sqlite_backup
pip install '.[testing]'
mypy ./sqlite_backup
pytest
```
Raw data
{
"_id": null,
"home_page": "https://github.com/seanbreckenridge/sqlite_backup",
"name": "sqlite-backup",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "database sqlite",
"author": "Sean Breckenridge",
"author_email": "seanbrecke@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/62/71/f474621612431e3e7ce35e24507fd3569b9b3fb89bb49ddc0ece22d88237/sqlite_backup-0.1.7.tar.gz",
"platform": null,
"description": "# sqlite_backup\n\nThis exposes the python stdlib [`sqlite.backup`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.backup) function as a library, with a couple extra steps.\n\nThe 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)\n\n### Features\n\n- Has the option (true by default) to first safely copy the database from disk to a temporary directory, which is:\n - useful in case the source is in read-only mode (e.g. in some sort of docker container)\n - safer if you're especially worried about corrupting or losing data\n- Uses [`Cpython`s Connection.backup](https://github.com/python/cpython/blob/8fb36494501aad5b0c1d34311c9743c60bb9926c/Modules/_sqlite/connection.c#L1716), which directly uses the [underlying Sqlite C code](https://www.sqlite.org/c3ref/backup_finish.html)\n- Performs a [`wal_checkpoint`](https://www.sqlite.org/pragma.html#pragma_wal_checkpoint) and sets `journal_mode=DELETE` after copying to the destination, to remove the WAL (write-ahead log; temporary database file). Typically the WAL is removed when the database is closed, but [particular builds of sqlite](https://sqlite.org/forum/forumpost/1fdfc1a0e7), the [default sqlite installed on mac](https://github.com/seanbreckenridge/sqlite_backup/issues/9), or sqlite compiled with [`SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE` enabled](https://www.sqlite.org/c3ref/c_dbconfig_enable_fkey.html) or [`SQLITE_FCNTL_PERSIST_WAL`](https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html#sqlitefcntlpersistwal) may prevent that -- so the checkpoint exists to ensure there are no temporary files leftover\n\nIn 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\n\nThe initial backup function and some tests were extracted out of the [`karlicoss/HPI` `core/sqlite`](https://github.com/karlicoss/HPI/blob/a1f03f9c028df9d1898de2cc14f1df4fa6d8c471/my/core/sqlite.py#L33-L51) module\n\nIf other tools exist to do this, please [let me know!](https://github.com/seanbreckenridge/sqlite_backup/issues/new)\n\n## Installation\n\nRequires `python3.7+`\n\nTo install with pip, run:\n\n pip install sqlite_backup\n\n## Usage\n\n```\nUsage: sqlite_backup [OPTIONS] SOURCE_DATABASE DESTINATION\n\n SOURCE_DATABASE is the database to copy\n\n DESTINATION is where to write the database. If a directory, uses\n the SOURCE_DATABASE name. If a file, the directory must exist,\n and the destination file must not already exist (to prevent\n possibly overwriting old data)\n\nOptions:\n --debug Increase log verbosity [default: False]\n --wal-checkpoint / --no-wal-checkpoint\n After writing to the destination, run a\n checkpoint to truncate the WAL to zero bytes\n [default: wal-checkpoint]\n --copy-use-tempdir / --no-copy-use-tempdir\n Copy the source database files to a\n temporary directory, then connect to the\n copied files [default: copy-use-tempdir]\n --copy-retry INTEGER If the files change while copying to the\n temporary directory, retry <n> times\n [default: 100]\n --copy-retry-strict / --no-copy-retry-strict\n Throws an error if this fails to safely copy\n the database files --copy-retry times\n [default: copy-retry-strict]\n --help Show this message and exit. [default:\n False]\n```\n\nFor usage in python, use the `sqlite_backup` function, see the [docs](./docs/sqlite_backup/index.md)\n\nIf 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`](https://www.sqlite.org/uri.html#urimode) (readonly) or [`immutable`](https://www.sqlite.org/uri.html#uriimmutable) flags when connecting to the database. In python, like:\n\n```python\nimport sqlite3\nfrom typing import Iterator\n\ndef sqlite_connect(database: str) -> Iterator[sqlite3.Connection]:\n try:\n # or for immutable, f\"file:{database}?immutable=1\"\n with sqlite3.connect(f\"file:{database}?mode=ro\", uri=True) as conn:\n yield conn\n finally:\n conn.close()\n\nwith sqlite_connect(\"/path/to/database\") as conn:\n conn.execute(\"...\")\n```\n\n### Example\n\n```\nsqlite_backup --debug ~/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite ./firefox.sqlite\n[D 220202 13:00:32 core:110] Source database files: '['/home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite', '/home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite-wal']'\n[D 220202 13:00:32 core:111] Temporary Destination database files: '['/tmp/tmpm2nhl1p3/places.sqlite', '/tmp/tmpm2nhl1p3/places.sqlite-wal']'\n[D 220202 13:00:32 core:64] Copied from '/home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite' to '/tmp/tmpm2nhl1p3/places.sqlite' successfully; copied without file changing: True\n[D 220202 13:00:32 core:64] Copied from '/home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite-wal' to '/tmp/tmpm2nhl1p3/places.sqlite-wal' successfully; copied without file changing: True\n[D 220202 13:00:32 core:240] Running backup, from '/tmp/tmpm2nhl1p3/places.sqlite' to '/home/sean/Repos/sqlite_backup/firefox.sqlite'\nBacked up /home/sean/.mozilla/firefox/ew9cqpqe.dev-edition-default/places.sqlite to /home/sean/Repos/sqlite_backup/firefox.sqlite\n```\n\n### Tests\n\n```bash\ngit clone 'https://github.com/seanbreckenridge/sqlite_backup'\ncd ./sqlite_backup\npip install '.[testing]'\nmypy ./sqlite_backup\npytest\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A tool to copy sqlite databases you don't own",
"version": "0.1.7",
"project_urls": {
"Homepage": "https://github.com/seanbreckenridge/sqlite_backup"
},
"split_keywords": [
"database",
"sqlite"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "80c4a6626e64db079f477ab7b52c9fc7412a673756d7ef4060315e15b555b0f0",
"md5": "0fd81176672b48a4c7ece6d7adfb4c04",
"sha256": "9ba41e3c1acaafe2ed5242e83adfee4e2e2aa9abd32fbf59c1106793e260f5e9"
},
"downloads": -1,
"filename": "sqlite_backup-0.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0fd81176672b48a4c7ece6d7adfb4c04",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 10784,
"upload_time": "2023-09-08T02:26:36",
"upload_time_iso_8601": "2023-09-08T02:26:36.563831Z",
"url": "https://files.pythonhosted.org/packages/80/c4/a6626e64db079f477ab7b52c9fc7412a673756d7ef4060315e15b555b0f0/sqlite_backup-0.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6271f474621612431e3e7ce35e24507fd3569b9b3fb89bb49ddc0ece22d88237",
"md5": "f81f02dfef9141c095616ecd46b80fe5",
"sha256": "81263978fe5ae5d0f389eef2976d7098ecf72bfb80702dc4ad2c59d53265ed6c"
},
"downloads": -1,
"filename": "sqlite_backup-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "f81f02dfef9141c095616ecd46b80fe5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 14413,
"upload_time": "2023-09-08T02:26:38",
"upload_time_iso_8601": "2023-09-08T02:26:38.458966Z",
"url": "https://files.pythonhosted.org/packages/62/71/f474621612431e3e7ce35e24507fd3569b9b3fb89bb49ddc0ece22d88237/sqlite_backup-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-08 02:26:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "seanbreckenridge",
"github_project": "sqlite_backup",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "sqlite-backup"
}