github_backup.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. """GitHub backup service for printer profiles.
  2. Handles scheduled and on-demand backups of K-profiles and cloud profiles to GitHub.
  3. """
  4. import asyncio
  5. import base64
  6. import hashlib
  7. import json
  8. import logging
  9. import re
  10. from datetime import datetime, timedelta, timezone
  11. import httpx
  12. from sqlalchemy import desc, select
  13. from sqlalchemy.ext.asyncio import AsyncSession
  14. from backend.app.core.database import async_session
  15. from backend.app.models.github_backup import GitHubBackupConfig, GitHubBackupLog
  16. from backend.app.models.printer import Printer
  17. from backend.app.models.settings import Settings
  18. from backend.app.services.bambu_cloud import get_cloud_service
  19. from backend.app.services.printer_manager import printer_manager
  20. logger = logging.getLogger(__name__)
  21. # Schedule intervals in seconds
  22. SCHEDULE_INTERVALS = {
  23. "hourly": 3600,
  24. "daily": 86400,
  25. "weekly": 604800,
  26. }
  27. class GitHubBackupService:
  28. """Service for backing up profiles to GitHub."""
  29. def __init__(self):
  30. self._scheduler_task: asyncio.Task | None = None
  31. self._check_interval = 60 # Check every minute for scheduled runs
  32. self._running_backup: bool = False
  33. self._backup_progress: str | None = None
  34. self._http_client: httpx.AsyncClient | None = None
  35. async def _get_client(self) -> httpx.AsyncClient:
  36. """Get or create HTTP client."""
  37. if self._http_client is None or self._http_client.is_closed:
  38. self._http_client = httpx.AsyncClient(timeout=60.0)
  39. return self._http_client
  40. async def start_scheduler(self):
  41. """Start the background scheduler loop."""
  42. if self._scheduler_task is not None:
  43. return
  44. logger.info("Starting GitHub backup scheduler")
  45. self._scheduler_task = asyncio.create_task(self._scheduler_loop())
  46. def stop_scheduler(self):
  47. """Stop the scheduler."""
  48. if self._scheduler_task:
  49. self._scheduler_task.cancel()
  50. self._scheduler_task = None
  51. logger.info("Stopped GitHub backup scheduler")
  52. async def _scheduler_loop(self):
  53. """Main scheduler loop - checks for due backups."""
  54. while True:
  55. try:
  56. await asyncio.sleep(self._check_interval)
  57. await self._check_scheduled_backups()
  58. except asyncio.CancelledError:
  59. break
  60. except Exception as e:
  61. logger.error("Error in GitHub backup scheduler: %s", e)
  62. await asyncio.sleep(60)
  63. async def _check_scheduled_backups(self):
  64. """Check if any scheduled backups are due."""
  65. async with async_session() as db:
  66. result = await db.execute(
  67. select(GitHubBackupConfig).where(
  68. GitHubBackupConfig.enabled == True, # noqa: E712
  69. GitHubBackupConfig.schedule_enabled == True, # noqa: E712
  70. )
  71. )
  72. configs = result.scalars().all()
  73. now = datetime.now(timezone.utc)
  74. for config in configs:
  75. # Handle both naive (from DB) and aware datetimes
  76. next_run = config.next_scheduled_run
  77. if next_run and next_run.tzinfo is None:
  78. next_run = next_run.replace(tzinfo=timezone.utc)
  79. if next_run and next_run <= now:
  80. logger.info("Running scheduled backup for config %s", config.id)
  81. await self.run_backup(config.id, trigger="scheduled")
  82. def _calculate_next_run(self, schedule_type: str, from_time: datetime | None = None) -> datetime:
  83. """Calculate the next scheduled run time."""
  84. now = from_time or datetime.now(timezone.utc)
  85. interval = SCHEDULE_INTERVALS.get(schedule_type, SCHEDULE_INTERVALS["daily"])
  86. return now + timedelta(seconds=interval)
  87. async def test_connection(self, repo_url: str, token: str) -> dict:
  88. """Test GitHub connection and permissions.
  89. Args:
  90. repo_url: GitHub repository URL
  91. token: Personal Access Token
  92. Returns:
  93. dict with success, message, repo_name, permissions
  94. """
  95. try:
  96. owner, repo = self._parse_repo_url(repo_url)
  97. client = await self._get_client()
  98. # Test API access
  99. response = await client.get(
  100. f"https://api.github.com/repos/{owner}/{repo}",
  101. headers={
  102. "Authorization": f"token {token}",
  103. "Accept": "application/vnd.github.v3+json",
  104. "User-Agent": "Bambuddy-Backup",
  105. },
  106. )
  107. if response.status_code == 401:
  108. return {"success": False, "message": "Invalid access token", "repo_name": None, "permissions": None}
  109. if response.status_code == 404:
  110. return {
  111. "success": False,
  112. "message": "Repository not found. Check URL and token permissions.",
  113. "repo_name": None,
  114. "permissions": None,
  115. }
  116. if response.status_code != 200:
  117. return {
  118. "success": False,
  119. "message": f"GitHub API error: {response.status_code}",
  120. "repo_name": None,
  121. "permissions": None,
  122. }
  123. data = response.json()
  124. permissions = data.get("permissions", {})
  125. # Check for push permission
  126. if not permissions.get("push", False):
  127. return {
  128. "success": False,
  129. "message": "Token does not have push permission to this repository",
  130. "repo_name": data.get("full_name"),
  131. "permissions": permissions,
  132. }
  133. return {
  134. "success": True,
  135. "message": "Connection successful",
  136. "repo_name": data.get("full_name"),
  137. "permissions": permissions,
  138. }
  139. except Exception as e:
  140. logger.error("GitHub connection test failed: %s", e)
  141. # Sanitize error - don't expose internal details
  142. error_type = type(e).__name__
  143. return {
  144. "success": False,
  145. "message": f"Connection failed: {error_type}",
  146. "repo_name": None,
  147. "permissions": None,
  148. }
  149. def _parse_repo_url(self, url: str) -> tuple[str, str]:
  150. """Parse owner and repo from GitHub URL."""
  151. # Limit URL length to prevent ReDoS attacks
  152. if not url or len(url) > 500:
  153. raise ValueError("Invalid GitHub URL: URL too long or empty")
  154. # Handle HTTPS URLs - use atomic groups via limited character classes
  155. # GitHub usernames: 1-39 chars, alphanumeric and hyphens
  156. # Repo names: 1-100 chars, alphanumeric, hyphens, underscores, dots
  157. match = re.match(r"https://github\.com/([\w-]{1,39})/([\w.\-]{1,100})(?:\.git)?/?$", url)
  158. if match:
  159. return match.group(1), match.group(2)
  160. # Handle SSH URLs
  161. match = re.match(r"git@github\.com:([\w-]{1,39})/([\w.\-]{1,100})(?:\.git)?$", url)
  162. if match:
  163. return match.group(1), match.group(2)
  164. raise ValueError(f"Invalid GitHub URL: {url}")
  165. async def run_backup(self, config_id: int, trigger: str = "manual") -> dict:
  166. """Run a backup operation.
  167. Args:
  168. config_id: ID of the backup configuration
  169. trigger: "manual" or "scheduled"
  170. Returns:
  171. dict with success, message, log_id, commit_sha, files_changed
  172. """
  173. if self._running_backup:
  174. return {"success": False, "message": "A backup is already running", "log_id": None}
  175. self._running_backup = True
  176. log_id = None
  177. try:
  178. async with async_session() as db:
  179. # Get config
  180. result = await db.execute(select(GitHubBackupConfig).where(GitHubBackupConfig.id == config_id))
  181. config = result.scalar_one_or_none()
  182. if not config:
  183. return {"success": False, "message": "Configuration not found", "log_id": None}
  184. if not config.enabled:
  185. return {"success": False, "message": "Backup is disabled", "log_id": None}
  186. # Create log entry
  187. log = GitHubBackupLog(config_id=config_id, status="running", trigger=trigger)
  188. db.add(log)
  189. await db.commit()
  190. await db.refresh(log)
  191. log_id = log.id
  192. try:
  193. # Collect backup data
  194. self._backup_progress = "Collecting profiles..."
  195. backup_data = await self._collect_backup_data(db, config)
  196. if not backup_data:
  197. # No data to backup
  198. log.status = "skipped"
  199. log.completed_at = datetime.now(timezone.utc)
  200. log.error_message = "No data to backup"
  201. config.last_backup_at = datetime.now(timezone.utc)
  202. config.last_backup_status = "skipped"
  203. config.last_backup_message = "No data to backup"
  204. if config.schedule_enabled:
  205. config.next_scheduled_run = self._calculate_next_run(config.schedule_type)
  206. await db.commit()
  207. return {
  208. "success": True,
  209. "message": "No data to backup",
  210. "log_id": log_id,
  211. "commit_sha": None,
  212. "files_changed": 0,
  213. }
  214. # Push to GitHub
  215. self._backup_progress = "Pushing to GitHub..."
  216. push_result = await self._push_to_github(config, backup_data)
  217. # Update log and config
  218. log.status = push_result["status"]
  219. log.completed_at = datetime.now(timezone.utc)
  220. log.commit_sha = push_result.get("commit_sha")
  221. log.files_changed = push_result.get("files_changed", 0)
  222. log.error_message = push_result.get("error")
  223. config.last_backup_at = datetime.now(timezone.utc)
  224. config.last_backup_status = push_result["status"]
  225. config.last_backup_message = push_result.get("message", "")
  226. config.last_backup_commit_sha = push_result.get("commit_sha")
  227. if config.schedule_enabled:
  228. config.next_scheduled_run = self._calculate_next_run(config.schedule_type)
  229. await db.commit()
  230. return {
  231. "success": push_result["status"] in ("success", "skipped"),
  232. "message": push_result.get("message", "Backup completed"),
  233. "log_id": log_id,
  234. "commit_sha": push_result.get("commit_sha"),
  235. "files_changed": push_result.get("files_changed", 0),
  236. }
  237. except Exception as e:
  238. logger.error("Backup failed: %s", e)
  239. log.status = "failed"
  240. log.completed_at = datetime.now(timezone.utc)
  241. log.error_message = str(e)
  242. config.last_backup_at = datetime.now(timezone.utc)
  243. config.last_backup_status = "failed"
  244. config.last_backup_message = str(e)
  245. if config.schedule_enabled:
  246. config.next_scheduled_run = self._calculate_next_run(config.schedule_type)
  247. await db.commit()
  248. return {
  249. "success": False,
  250. "message": str(e),
  251. "log_id": log_id,
  252. "commit_sha": None,
  253. "files_changed": 0,
  254. }
  255. finally:
  256. self._running_backup = False
  257. self._backup_progress = None
  258. async def _collect_backup_data(self, db: AsyncSession, config: GitHubBackupConfig) -> dict:
  259. """Collect data to backup based on config settings.
  260. Returns dict with structure:
  261. {
  262. "backup_metadata.json": {...},
  263. "kprofiles/{serial}/{nozzle}.json": {...},
  264. "cloud_profiles/filament.json": [...],
  265. "cloud_profiles/printer.json": [...],
  266. "cloud_profiles/process.json": [...],
  267. "settings/app_settings.json": {...},
  268. }
  269. """
  270. files: dict[str, dict | list] = {}
  271. # Metadata file (no timestamps - git tracks file history)
  272. metadata = {
  273. "version": "1.0",
  274. "backup_type": "bambuddy_profiles",
  275. "contents": {
  276. "kprofiles": config.backup_kprofiles,
  277. "cloud_profiles": config.backup_cloud_profiles,
  278. "settings": config.backup_settings,
  279. },
  280. }
  281. files["backup_metadata.json"] = metadata
  282. # Collect K-profiles from all connected printers
  283. if config.backup_kprofiles:
  284. self._backup_progress = "Collecting K-profiles from printers..."
  285. await self._collect_kprofiles(db, files)
  286. # Collect cloud profiles
  287. if config.backup_cloud_profiles:
  288. self._backup_progress = "Collecting cloud profiles from Bambu Cloud..."
  289. await self._collect_cloud_profiles(db, files)
  290. # Collect app settings
  291. if config.backup_settings:
  292. self._backup_progress = "Collecting app settings..."
  293. await self._collect_settings(db, files)
  294. return files
  295. async def _collect_kprofiles(self, db: AsyncSession, files: dict):
  296. """Collect K-profiles from all connected printers."""
  297. result = await db.execute(select(Printer).where(Printer.is_active == True)) # noqa: E712
  298. printers = result.scalars().all()
  299. nozzle_diameters = ["0.2", "0.4", "0.6", "0.8"]
  300. for printer in printers:
  301. client = printer_manager.get_client(printer.id)
  302. if not client or not client.state.connected:
  303. continue
  304. serial = printer.serial_number
  305. printer_profiles = {}
  306. for nozzle in nozzle_diameters:
  307. try:
  308. profiles = await client.get_kprofiles(nozzle_diameter=nozzle)
  309. if profiles:
  310. profile_data = {
  311. "version": "1.0",
  312. "printer_name": printer.name,
  313. "printer_serial": serial,
  314. "nozzle_diameter": nozzle,
  315. "profiles": [
  316. {
  317. "slot_id": p.slot_id,
  318. "name": p.name,
  319. "k_value": p.k_value,
  320. "filament_id": p.filament_id,
  321. "nozzle_id": p.nozzle_id,
  322. "extruder_id": p.extruder_id,
  323. "setting_id": p.setting_id,
  324. "n_coef": p.n_coef,
  325. }
  326. for p in profiles
  327. ],
  328. }
  329. files[f"kprofiles/{serial}/{nozzle}.json"] = profile_data
  330. printer_profiles[nozzle] = len(profiles)
  331. except Exception as e:
  332. logger.warning("Failed to get K-profiles for printer %s nozzle %s: %s", serial, nozzle, e)
  333. if printer_profiles:
  334. logger.info("Collected K-profiles for %s: %s", serial, printer_profiles)
  335. async def _collect_cloud_profiles(self, db: AsyncSession, files: dict):
  336. """Collect Bambu Cloud profiles if authenticated."""
  337. # Check if cloud is authenticated
  338. cloud = get_cloud_service()
  339. # Try to restore token from DB
  340. result = await db.execute(select(Settings).where(Settings.key == "bambu_cloud_token"))
  341. setting = result.scalar_one_or_none()
  342. if setting and setting.value:
  343. cloud.set_token(setting.value)
  344. if not cloud.is_authenticated:
  345. logger.info("Cloud not authenticated, skipping cloud profiles")
  346. return
  347. try:
  348. settings = await cloud.get_slicer_settings()
  349. if not settings:
  350. return
  351. # Separate by type
  352. filament_settings = []
  353. printer_settings = []
  354. process_settings = []
  355. for setting in settings.get("setting", []) if isinstance(settings.get("setting"), list) else []:
  356. setting_type = setting.get("type", "")
  357. if setting_type == "filament":
  358. filament_settings.append(setting)
  359. elif setting_type == "printer":
  360. printer_settings.append(setting)
  361. elif setting_type == "process":
  362. process_settings.append(setting)
  363. if filament_settings:
  364. files["cloud_profiles/filament.json"] = {
  365. "version": "1.0",
  366. "profiles": filament_settings,
  367. }
  368. if printer_settings:
  369. files["cloud_profiles/printer.json"] = {
  370. "version": "1.0",
  371. "profiles": printer_settings,
  372. }
  373. if process_settings:
  374. files["cloud_profiles/process.json"] = {
  375. "version": "1.0",
  376. "profiles": process_settings,
  377. }
  378. logger.info(
  379. f"Collected cloud profiles: {len(filament_settings)} filament, "
  380. f"{len(printer_settings)} printer, {len(process_settings)} process"
  381. )
  382. except Exception as e:
  383. logger.warning("Failed to collect cloud profiles: %s", e)
  384. async def _collect_settings(self, db: AsyncSession, files: dict):
  385. """Collect app settings."""
  386. result = await db.execute(select(Settings))
  387. settings = result.scalars().all()
  388. # Filter out sensitive settings
  389. sensitive_keys = {"bambu_cloud_token", "auth_secret_key"}
  390. settings_data = {s.key: s.value for s in settings if s.key not in sensitive_keys}
  391. files["settings/app_settings.json"] = {
  392. "version": "1.0",
  393. "settings": settings_data,
  394. }
  395. async def _push_to_github(self, config: GitHubBackupConfig, files: dict) -> dict:
  396. """Push files to GitHub using the GitHub API.
  397. Uses the Git Data API to create blobs, tree, and commit.
  398. Returns:
  399. dict with status, message, commit_sha, files_changed
  400. """
  401. try:
  402. owner, repo = self._parse_repo_url(config.repository_url)
  403. branch = config.branch
  404. client = await self._get_client()
  405. headers = {
  406. "Authorization": f"token {config.access_token}",
  407. "Accept": "application/vnd.github.v3+json",
  408. "User-Agent": "Bambuddy-Backup",
  409. }
  410. # Get current branch reference
  411. ref_response = await client.get(
  412. f"https://api.github.com/repos/{owner}/{repo}/git/refs/heads/{branch}", headers=headers
  413. )
  414. if ref_response.status_code == 404:
  415. # Branch doesn't exist, need to create it from default branch
  416. return await self._create_branch_and_push(client, headers, owner, repo, branch, files)
  417. if ref_response.status_code != 200:
  418. return {
  419. "status": "failed",
  420. "message": f"Failed to get branch ref: {ref_response.status_code}",
  421. "error": ref_response.text,
  422. }
  423. ref_data = ref_response.json()
  424. current_commit_sha = ref_data["object"]["sha"]
  425. # Get the current tree
  426. commit_response = await client.get(
  427. f"https://api.github.com/repos/{owner}/{repo}/git/commits/{current_commit_sha}", headers=headers
  428. )
  429. if commit_response.status_code != 200:
  430. return {"status": "failed", "message": "Failed to get current commit"}
  431. current_tree_sha = commit_response.json()["tree"]["sha"]
  432. # Get existing files to check for changes
  433. tree_response = await client.get(
  434. f"https://api.github.com/repos/{owner}/{repo}/git/trees/{current_tree_sha}?recursive=1", headers=headers
  435. )
  436. existing_files = {}
  437. if tree_response.status_code == 200:
  438. for item in tree_response.json().get("tree", []):
  439. if item["type"] == "blob":
  440. existing_files[item["path"]] = item["sha"]
  441. # Create blobs for changed files
  442. tree_items = []
  443. files_changed = 0
  444. for path, content in files.items():
  445. content_str = json.dumps(content, indent=2, default=str)
  446. content_bytes = content_str.encode("utf-8")
  447. content_sha = hashlib.sha1(
  448. f"blob {len(content_bytes)}\0".encode() + content_bytes, usedforsecurity=False
  449. ).hexdigest()
  450. # Skip if file hasn't changed
  451. if path in existing_files and existing_files[path] == content_sha:
  452. continue
  453. # Create blob
  454. blob_response = await client.post(
  455. f"https://api.github.com/repos/{owner}/{repo}/git/blobs",
  456. headers=headers,
  457. json={"content": base64.b64encode(content_bytes).decode(), "encoding": "base64"},
  458. )
  459. if blob_response.status_code != 201:
  460. logger.error("Failed to create blob for %s: %s", path, blob_response.text)
  461. continue
  462. blob_sha = blob_response.json()["sha"]
  463. tree_items.append({"path": path, "mode": "100644", "type": "blob", "sha": blob_sha})
  464. files_changed += 1
  465. if not tree_items:
  466. return {"status": "skipped", "message": "No changes to commit", "commit_sha": None, "files_changed": 0}
  467. # Create new tree
  468. tree_response = await client.post(
  469. f"https://api.github.com/repos/{owner}/{repo}/git/trees",
  470. headers=headers,
  471. json={"base_tree": current_tree_sha, "tree": tree_items},
  472. )
  473. if tree_response.status_code != 201:
  474. return {"status": "failed", "message": f"Failed to create tree: {tree_response.text}"}
  475. new_tree_sha = tree_response.json()["sha"]
  476. # Create commit
  477. commit_message = f"Bambuddy backup - {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}"
  478. commit_response = await client.post(
  479. f"https://api.github.com/repos/{owner}/{repo}/git/commits",
  480. headers=headers,
  481. json={"message": commit_message, "tree": new_tree_sha, "parents": [current_commit_sha]},
  482. )
  483. if commit_response.status_code != 201:
  484. return {"status": "failed", "message": f"Failed to create commit: {commit_response.text}"}
  485. new_commit_sha = commit_response.json()["sha"]
  486. # Update branch reference
  487. ref_update = await client.patch(
  488. f"https://api.github.com/repos/{owner}/{repo}/git/refs/heads/{branch}",
  489. headers=headers,
  490. json={"sha": new_commit_sha},
  491. )
  492. if ref_update.status_code != 200:
  493. return {"status": "failed", "message": f"Failed to update branch: {ref_update.text}"}
  494. return {
  495. "status": "success",
  496. "message": f"Backup successful - {files_changed} files updated",
  497. "commit_sha": new_commit_sha,
  498. "files_changed": files_changed,
  499. }
  500. except Exception as e:
  501. logger.error("Push to GitHub failed: %s", e)
  502. return {"status": "failed", "message": str(e), "error": str(e)}
  503. async def _create_branch_and_push(
  504. self, client: httpx.AsyncClient, headers: dict, owner: str, repo: str, branch: str, files: dict
  505. ) -> dict:
  506. """Create a new branch and push files when branch doesn't exist."""
  507. try:
  508. # Get default branch
  509. repo_response = await client.get(f"https://api.github.com/repos/{owner}/{repo}", headers=headers)
  510. if repo_response.status_code != 200:
  511. return {"status": "failed", "message": "Failed to get repo info"}
  512. default_branch = repo_response.json().get("default_branch", "main")
  513. # Get default branch ref
  514. ref_response = await client.get(
  515. f"https://api.github.com/repos/{owner}/{repo}/git/refs/heads/{default_branch}", headers=headers
  516. )
  517. if ref_response.status_code != 200:
  518. # Empty repo - create initial commit
  519. return await self._create_initial_commit(client, headers, owner, repo, branch, files)
  520. base_sha = ref_response.json()["object"]["sha"]
  521. # Create new branch
  522. create_ref = await client.post(
  523. f"https://api.github.com/repos/{owner}/{repo}/git/refs",
  524. headers=headers,
  525. json={"ref": f"refs/heads/{branch}", "sha": base_sha},
  526. )
  527. if create_ref.status_code != 201:
  528. return {"status": "failed", "message": f"Failed to create branch: {create_ref.text}"}
  529. # Now push to the new branch (recursive call will find the branch)
  530. return await self._push_to_github(
  531. type(
  532. "Config",
  533. (),
  534. {
  535. "repository_url": f"https://github.com/{owner}/{repo}",
  536. "access_token": headers["Authorization"].replace("token ", ""),
  537. "branch": branch,
  538. },
  539. )(),
  540. files,
  541. )
  542. except Exception as e:
  543. return {"status": "failed", "message": str(e)}
  544. async def _create_initial_commit(
  545. self, client: httpx.AsyncClient, headers: dict, owner: str, repo: str, branch: str, files: dict
  546. ) -> dict:
  547. """Create initial commit in an empty repository."""
  548. try:
  549. # Create blobs
  550. tree_items = []
  551. for path, content in files.items():
  552. content_str = json.dumps(content, indent=2, default=str)
  553. blob_response = await client.post(
  554. f"https://api.github.com/repos/{owner}/{repo}/git/blobs",
  555. headers=headers,
  556. json={"content": base64.b64encode(content_str.encode()).decode(), "encoding": "base64"},
  557. )
  558. if blob_response.status_code == 201:
  559. tree_items.append(
  560. {"path": path, "mode": "100644", "type": "blob", "sha": blob_response.json()["sha"]}
  561. )
  562. # Create tree
  563. tree_response = await client.post(
  564. f"https://api.github.com/repos/{owner}/{repo}/git/trees",
  565. headers=headers,
  566. json={"tree": tree_items},
  567. )
  568. if tree_response.status_code != 201:
  569. return {"status": "failed", "message": "Failed to create tree"}
  570. tree_sha = tree_response.json()["sha"]
  571. # Create commit (no parents for initial)
  572. commit_response = await client.post(
  573. f"https://api.github.com/repos/{owner}/{repo}/git/commits",
  574. headers=headers,
  575. json={
  576. "message": f"Initial Bambuddy backup - {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}",
  577. "tree": tree_sha,
  578. },
  579. )
  580. if commit_response.status_code != 201:
  581. return {"status": "failed", "message": "Failed to create commit"}
  582. commit_sha = commit_response.json()["sha"]
  583. # Create branch ref
  584. ref_response = await client.post(
  585. f"https://api.github.com/repos/{owner}/{repo}/git/refs",
  586. headers=headers,
  587. json={"ref": f"refs/heads/{branch}", "sha": commit_sha},
  588. )
  589. if ref_response.status_code != 201:
  590. return {"status": "failed", "message": "Failed to create branch ref"}
  591. return {
  592. "status": "success",
  593. "message": f"Initial backup created - {len(files)} files",
  594. "commit_sha": commit_sha,
  595. "files_changed": len(files),
  596. }
  597. except Exception as e:
  598. return {"status": "failed", "message": str(e)}
  599. @property
  600. def is_running(self) -> bool:
  601. """Check if a backup is currently running."""
  602. return self._running_backup
  603. @property
  604. def progress(self) -> str | None:
  605. """Get current backup progress message."""
  606. return self._backup_progress
  607. async def get_logs(self, config_id: int, limit: int = 50, offset: int = 0) -> list[GitHubBackupLog]:
  608. """Get backup logs for a configuration."""
  609. async with async_session() as db:
  610. result = await db.execute(
  611. select(GitHubBackupLog)
  612. .where(GitHubBackupLog.config_id == config_id)
  613. .order_by(desc(GitHubBackupLog.started_at))
  614. .offset(offset)
  615. .limit(limit)
  616. )
  617. return list(result.scalars().all())
  618. # Singleton instance
  619. github_backup_service = GitHubBackupService()