github_backup.py 35 KB

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