|
|
|
|
|
|
|
|
| import sqlite3, zipfile, os, hashlib, json
|
| from filelock import FileLock
|
| from .github_client import get_or_create_release, upload_asset
|
|
|
|
|
| SQLITE_DB = "data_enterprise/cache_store.db"
|
| LOCK = FileLock("/tmp/sqlite.lock")
|
|
|
| def get_file_hash(path):
|
| """Calculate SHA256 hash of the SQLite file."""
|
| h = hashlib.sha256()
|
| with open(path, "rb") as f:
|
| while chunk := f.read(8192):
|
| h.update(chunk)
|
| return h.hexdigest()
|
|
|
| def backup_sqlite():
|
| print(f"[Backup] backup_sqlite() called for {SQLITE_DB}")
|
| if not os.path.exists(SQLITE_DB):
|
| print("[Backup] SQLite DB file not found. Skipping backup.")
|
| return
|
|
|
| with LOCK:
|
| current_hash = get_file_hash(SQLITE_DB)
|
| zip_path = "/tmp/sqlite_backup.zip"
|
|
|
|
|
| with zipfile.ZipFile(zip_path, "w") as z:
|
|
|
| z.write(SQLITE_DB, arcname="cache_store.db")
|
|
|
| release = get_or_create_release()
|
|
|
|
|
| print(f"[Backup] Uploading SQLite backup to GitHub. Hash: {current_hash[:10]}")
|
| upload_asset(release, zip_path, "sqlite_backup.zip")
|
|
|
|
|
| hash_data = json.dumps({"hash": current_hash}).encode()
|
| upload_asset(release, hash_data, "sqlite_hash.json")
|
| print("[Backup] SQLite backup and hash successfully updated in GitHub.")
|
|
|
|
|
| |