Files
agent-call/deploy/backup_sqlite.py
T

39 lines
1.0 KiB
Python

from __future__ import annotations
import argparse
import sqlite3
from pathlib import Path
def copy_database(source: str, destination: str) -> None:
source_path = Path(source)
destination_path = Path(destination)
if not source_path.is_file():
raise FileNotFoundError(source)
destination_path.parent.mkdir(parents=True, exist_ok=True)
source_db = sqlite3.connect(source_path)
destination_db = sqlite3.connect(destination_path)
try:
source_db.backup(destination_db)
destination_db.commit()
finally:
destination_db.close()
source_db.close()
def main() -> int:
parser = argparse.ArgumentParser(
description="offline SQLite backup/restore for agent-call"
)
parser.add_argument("action", choices=("backup", "restore"))
parser.add_argument("source")
parser.add_argument("destination")
args = parser.parse_args()
copy_database(args.source, args.destination)
print(f"{args.action} complete")
return 0
if __name__ == "__main__":
raise SystemExit(main())