23 lines
840 B
Python
23 lines
840 B
Python
#!/usr/bin/env python3
|
|
"""Restore Mailu's SQLite snapshot without replacing the live database inode."""
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
backup_path = Path("/data/backup/main.db")
|
|
live_path = Path("/data/main.db")
|
|
|
|
# The source and destination must already exist. A missing snapshot or live
|
|
# database is a restore failure, not a reason to create an empty database.
|
|
with sqlite3.connect(
|
|
f"{backup_path.as_uri()}?mode=ro", uri=True, timeout=60
|
|
) as source:
|
|
if source.execute("PRAGMA integrity_check").fetchall() != [("ok",)]:
|
|
raise RuntimeError(f"Invalid SQLite snapshot: {backup_path}")
|
|
with sqlite3.connect(
|
|
f"{live_path.as_uri()}?mode=rw", uri=True, timeout=60
|
|
) as destination:
|
|
source.backup(destination, pages=256, sleep=0.1)
|
|
|
|
print(f"SQLite restore complete: {live_path}", flush=True)
|