github_backup.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 logging
  6. from datetime import datetime, timedelta, timezone
  7. import httpx
  8. from sqlalchemy import desc, or_, select
  9. from sqlalchemy.ext.asyncio import AsyncSession
  10. from backend.app.core.database import async_session
  11. from backend.app.models.archive import PrintArchive
  12. from backend.app.models.github_backup import GitHubBackupConfig, GitHubBackupLog
  13. from backend.app.models.printer import Printer
  14. from backend.app.models.settings import Settings
  15. from backend.app.models.spool import Spool
  16. from backend.app.models.spool_usage_history import SpoolUsageHistory
  17. from backend.app.models.user import User
  18. from backend.app.services.git_providers.factory import get_provider_backend
  19. from backend.app.services.printer_manager import printer_manager
  20. logger = logging.getLogger(__name__)
  21. # Bambu's listing endpoint is keyed by preset type and calls process presets
  22. # "print". Same mapping as `routes/cloud.py` — kept in step with it, since a
  23. # divergence here silently drops a whole preset type from every backup.
  24. _BAMBU_PRESET_TYPES = {
  25. "filament": "filament",
  26. "printer": "printer",
  27. "print": "process",
  28. }
  29. def _bambu_preset_record(setting_id, our_type: str, entry: dict, detail: dict) -> dict:
  30. """One Bambu preset as stored in the backup: metadata plus the payload.
  31. ``base_id`` and ``setting`` are the two fields ``BambuCloudService.
  32. create_setting`` needs, so a restore can rebuild the preset rather than
  33. just list it.
  34. ``user_id`` from the listing is deliberately dropped. It identifies the
  35. account and adds nothing to a rebuild, and backup repositories can be
  36. public.
  37. """
  38. return {
  39. "setting_id": str(setting_id),
  40. "name": detail.get("name") or entry.get("name") or "Unknown",
  41. "type": our_type,
  42. "version": detail.get("version") or entry.get("version"),
  43. "updated_time": entry.get("updated_time"),
  44. "base_id": detail.get("base_id"),
  45. "filament_id": detail.get("filament_id"),
  46. "setting": detail.get("setting") or {},
  47. }
  48. def _orca_profile_record(entry: dict) -> dict:
  49. """One Orca profile as stored in the backup.
  50. ``content`` is kept whole rather than picked apart: it is the profile, the
  51. sync API hands it over inline, and Orca owns its shape. Narrowing it here
  52. would mean guessing which keys a future restore needs.
  53. """
  54. return {
  55. "id": str(entry.get("id")) if entry.get("id") is not None else None,
  56. "name": entry.get("name"),
  57. "updated_time": entry.get("updated_time"),
  58. "created_time": entry.get("created_time"),
  59. "content": entry.get("content"),
  60. }
  61. # Schedule intervals in seconds
  62. SCHEDULE_INTERVALS = {
  63. "hourly": 3600,
  64. "daily": 86400,
  65. "weekly": 604800,
  66. }
  67. _PROVIDER_DISPLAY_NAMES = {
  68. "github": "GitHub",
  69. "gitlab": "GitLab",
  70. "gitea": "Gitea",
  71. "forgejo": "Forgejo",
  72. }
  73. class GitHubBackupService:
  74. """Service for backing up profiles to GitHub."""
  75. def __init__(self):
  76. self._scheduler_task: asyncio.Task | None = None
  77. self._check_interval = 60 # Check every minute for scheduled runs
  78. self._running_backup: bool = False
  79. self._backup_progress: str | None = None
  80. self._http_client: httpx.AsyncClient | None = None
  81. async def _get_client(self) -> httpx.AsyncClient:
  82. """Get or create HTTP client."""
  83. if self._http_client is None or self._http_client.is_closed:
  84. self._http_client = httpx.AsyncClient(timeout=60.0)
  85. return self._http_client
  86. async def start_scheduler(self):
  87. """Start the background scheduler loop."""
  88. if self._scheduler_task is not None:
  89. return
  90. logger.info("Starting GitHub backup scheduler")
  91. self._scheduler_task = asyncio.create_task(self._scheduler_loop())
  92. def stop_scheduler(self):
  93. """Stop the scheduler."""
  94. if self._scheduler_task:
  95. self._scheduler_task.cancel()
  96. self._scheduler_task = None
  97. logger.info("Stopped GitHub backup scheduler")
  98. async def _scheduler_loop(self):
  99. """Main scheduler loop - checks for due backups."""
  100. while True:
  101. try:
  102. await asyncio.sleep(self._check_interval)
  103. await self._check_scheduled_backups()
  104. except asyncio.CancelledError:
  105. break
  106. except Exception:
  107. logger.exception("Error in GitHub backup scheduler")
  108. await asyncio.sleep(60)
  109. async def _check_scheduled_backups(self):
  110. """Check if any scheduled backups are due."""
  111. async with async_session() as db:
  112. result = await db.execute(
  113. select(GitHubBackupConfig).where(
  114. GitHubBackupConfig.enabled == True, # noqa: E712
  115. GitHubBackupConfig.schedule_enabled == True, # noqa: E712
  116. )
  117. )
  118. configs = result.scalars().all()
  119. now = datetime.now(timezone.utc)
  120. for config in configs:
  121. # Handle both naive (from DB) and aware datetimes
  122. next_run = config.next_scheduled_run
  123. if next_run and next_run.tzinfo is None:
  124. next_run = next_run.replace(tzinfo=timezone.utc)
  125. if next_run and next_run <= now:
  126. logger.info("Running scheduled backup for config %s", config.id)
  127. await self.run_backup(config.id, trigger="scheduled")
  128. def calculate_next_run(self, schedule_type: str, from_time: datetime | None = None) -> datetime:
  129. """Calculate the next scheduled run time."""
  130. now = from_time or datetime.now(timezone.utc)
  131. interval = SCHEDULE_INTERVALS.get(schedule_type, SCHEDULE_INTERVALS["daily"])
  132. return now + timedelta(seconds=interval)
  133. async def test_connection(self, repo_url: str, token: str, provider: str = "github") -> dict:
  134. """Test connection and permissions for the given provider."""
  135. backend = get_provider_backend(provider)
  136. client = await self._get_client()
  137. return await backend.test_connection(repo_url, token, client)
  138. async def run_backup(self, config_id: int, trigger: str = "manual") -> dict:
  139. """Run a backup operation.
  140. Args:
  141. config_id: ID of the backup configuration
  142. trigger: "manual" or "scheduled"
  143. Returns:
  144. dict with success, message, log_id, commit_sha, files_changed
  145. """
  146. # Everything from here to `self._running_backup = True` must stay
  147. # await-free. Both flags are plain bools and both callers are coroutines
  148. # on one event loop, so with no suspension point in between the loop
  149. # cannot run the restore service's mirror-image region (see
  150. # github_restore.run_restore) in the gap — whichever gets here first sets
  151. # its flag before the other can read it. Adding an `await` inside this
  152. # block reintroduces the check-then-set race and lets a backup and a
  153. # restore run at once.
  154. if self._running_backup:
  155. return {"success": False, "message": "A backup is already running", "log_id": None}
  156. # Imported locally to avoid a module-level import cycle — the restore
  157. # service imports this module's singleton to take the mirror-image lock.
  158. # A restore rewrites the same tables this collector reads and publishes
  159. # K-profiles to the same printers, so the two must not interleave.
  160. # (A local `import` of an already-loaded module is not a suspension
  161. # point, so it does not break the await-free rule above.)
  162. from backend.app.services.github_restore import github_restore_service
  163. if github_restore_service.is_running:
  164. return {
  165. "success": False,
  166. "message": "A restore is currently running. Wait for it to finish before backing up.",
  167. "log_id": None,
  168. }
  169. self._running_backup = True
  170. log_id = None
  171. try:
  172. async with async_session() as db:
  173. # Get config
  174. result = await db.execute(select(GitHubBackupConfig).where(GitHubBackupConfig.id == config_id))
  175. config = result.scalar_one_or_none()
  176. if not config:
  177. return {"success": False, "message": "Configuration not found", "log_id": None}
  178. if not config.enabled:
  179. return {"success": False, "message": "Backup is disabled", "log_id": None}
  180. # Defense in depth: re-verify the repo is private before each
  181. # push. The save endpoint already enforces this on every config
  182. # change, but a user can flip a repo from private to public in
  183. # GitHub's UI between configuration and the next scheduled run.
  184. test_result = await self.test_connection(
  185. config.repository_url, config.access_token, provider=config.provider
  186. )
  187. if not test_result.get("success") or test_result.get("is_private") is not True:
  188. visibility_note = (
  189. "the target repository is no longer private"
  190. if test_result.get("is_private") is False
  191. else "could not confirm the target repository is private"
  192. )
  193. abort_message = (
  194. f"Backup aborted: {visibility_note}. Bambuddy backups carry credentials "
  195. "and are refused for any non-private target. Make the repository private "
  196. "to resume scheduled backups."
  197. )
  198. log = GitHubBackupLog(
  199. config_id=config_id,
  200. status="failed",
  201. trigger=trigger,
  202. completed_at=datetime.now(timezone.utc),
  203. error_message=abort_message,
  204. )
  205. db.add(log)
  206. config.last_backup_at = datetime.now(timezone.utc)
  207. config.last_backup_status = "failed"
  208. config.last_backup_message = abort_message
  209. if config.schedule_enabled:
  210. config.next_scheduled_run = self.calculate_next_run(config.schedule_type)
  211. await db.commit()
  212. await db.refresh(log)
  213. logger.warning(
  214. "Backup aborted for config %s: repo not private (is_private=%r, success=%r)",
  215. config_id,
  216. test_result.get("is_private"),
  217. test_result.get("success"),
  218. )
  219. return {
  220. "success": False,
  221. "message": abort_message,
  222. "log_id": log.id,
  223. }
  224. # Create log entry
  225. log = GitHubBackupLog(config_id=config_id, status="running", trigger=trigger)
  226. db.add(log)
  227. await db.commit()
  228. await db.refresh(log)
  229. log_id = log.id
  230. try:
  231. # Collect backup data
  232. self._backup_progress = "Collecting profiles..."
  233. backup_data = await self._collect_backup_data(db, config)
  234. if not backup_data:
  235. # No data to backup
  236. log.status = "skipped"
  237. log.completed_at = datetime.now(timezone.utc)
  238. log.error_message = "No data to backup"
  239. config.last_backup_at = datetime.now(timezone.utc)
  240. config.last_backup_status = "skipped"
  241. config.last_backup_message = "No data to backup"
  242. if config.schedule_enabled:
  243. config.next_scheduled_run = self.calculate_next_run(config.schedule_type)
  244. await db.commit()
  245. return {
  246. "success": True,
  247. "message": "No data to backup",
  248. "log_id": log_id,
  249. "commit_sha": None,
  250. "files_changed": 0,
  251. }
  252. provider_name = _PROVIDER_DISPLAY_NAMES.get(config.provider, config.provider)
  253. self._backup_progress = f"Pushing to {provider_name}..."
  254. push_result = await self._push_to_provider(config, backup_data)
  255. # Update log and config
  256. log.status = push_result["status"]
  257. log.completed_at = datetime.now(timezone.utc)
  258. log.commit_sha = push_result.get("commit_sha")
  259. log.files_changed = push_result.get("files_changed", 0)
  260. log.error_message = push_result.get("error")
  261. config.last_backup_at = datetime.now(timezone.utc)
  262. config.last_backup_status = push_result["status"]
  263. config.last_backup_message = push_result.get("message", "")
  264. config.last_backup_commit_sha = push_result.get("commit_sha")
  265. if config.schedule_enabled:
  266. config.next_scheduled_run = self.calculate_next_run(config.schedule_type)
  267. await db.commit()
  268. return {
  269. "success": push_result["status"] in ("success", "skipped"),
  270. "message": push_result.get("message", "Backup completed"),
  271. "log_id": log_id,
  272. "commit_sha": push_result.get("commit_sha"),
  273. "files_changed": push_result.get("files_changed", 0),
  274. }
  275. except Exception as e:
  276. logger.exception("Backup failed")
  277. log.status = "failed"
  278. log.completed_at = datetime.now(timezone.utc)
  279. log.error_message = str(e)
  280. config.last_backup_at = datetime.now(timezone.utc)
  281. config.last_backup_status = "failed"
  282. config.last_backup_message = str(e)
  283. if config.schedule_enabled:
  284. config.next_scheduled_run = self.calculate_next_run(config.schedule_type)
  285. await db.commit()
  286. return {
  287. "success": False,
  288. "message": str(e),
  289. "log_id": log_id,
  290. "commit_sha": None,
  291. "files_changed": 0,
  292. }
  293. finally:
  294. self._running_backup = False
  295. self._backup_progress = None
  296. async def _collect_backup_data(self, db: AsyncSession, config: GitHubBackupConfig) -> dict:
  297. """Collect data to backup based on config settings.
  298. Returns dict with structure:
  299. {
  300. "backup_metadata.json": {...},
  301. "kprofiles/{serial}/{nozzle}.json": {...},
  302. "cloud_profiles/bambu/{account}/{filament,printer,process}.json": {...},
  303. "cloud_profiles/orca/{account}/{filament,printer,process}.json": {...},
  304. "settings/app_settings.json": {...},
  305. }
  306. ``{account}`` is ``global`` when auth is disabled, otherwise
  307. ``user-{id}`` — one directory per connected cloud account (#2717).
  308. """
  309. files: dict[str, dict | list] = {}
  310. # Metadata file (no timestamps - git tracks file history)
  311. metadata = {
  312. "version": "1.0",
  313. "backup_type": "bambuddy_profiles",
  314. "contents": {
  315. "kprofiles": config.backup_kprofiles,
  316. "cloud_profiles": config.backup_cloud_profiles,
  317. "settings": config.backup_settings,
  318. "spools": config.backup_spools,
  319. "archives": config.backup_archives,
  320. },
  321. }
  322. files["backup_metadata.json"] = metadata
  323. # Collect K-profiles from all connected printers
  324. if config.backup_kprofiles:
  325. self._backup_progress = "Collecting K-profiles from printers..."
  326. await self._collect_kprofiles(db, files)
  327. # Collect cloud profiles. `contents.cloud_profiles` is corrected below
  328. # from what was configured to what was actually written — it claimed
  329. # `true` on every backup, including the ones that collected nothing
  330. # (#2717), which is exactly the signal a restore needs to be able to
  331. # trust.
  332. if config.backup_cloud_profiles:
  333. self._backup_progress = "Collecting cloud profiles from Bambu Cloud and Orca Cloud..."
  334. cloud_summary = await self._collect_cloud_profiles(db, files)
  335. collected = bool(cloud_summary.get("bambu") or cloud_summary.get("orca"))
  336. metadata["contents"]["cloud_profiles"] = collected
  337. if collected:
  338. # Per-cloud, per-account counts, so a restore can tell an empty
  339. # account from one that failed to collect.
  340. metadata["cloud_profiles"] = cloud_summary
  341. # Collect app settings
  342. if config.backup_settings:
  343. self._backup_progress = "Collecting app settings..."
  344. await self._collect_settings(db, files)
  345. # Collect spool inventory
  346. if config.backup_spools:
  347. self._backup_progress = "Collecting spool inventory..."
  348. await self._collect_spools(db, files)
  349. # Collect print archives
  350. if config.backup_archives:
  351. self._backup_progress = "Collecting print archives..."
  352. await self._collect_archives(db, files)
  353. return files
  354. async def _collect_kprofiles(self, db: AsyncSession, files: dict):
  355. """Collect K-profiles from all connected printers."""
  356. result = await db.execute(select(Printer).where(Printer.is_active == True)) # noqa: E712
  357. printers = result.scalars().all()
  358. nozzle_diameters = ["0.2", "0.4", "0.6", "0.8"]
  359. for printer in printers:
  360. client = printer_manager.get_client(printer.id)
  361. if not client or not client.state.connected:
  362. continue
  363. serial = printer.serial_number
  364. printer_profiles = {}
  365. for nozzle in nozzle_diameters:
  366. try:
  367. profiles = await client.get_kprofiles(nozzle_diameter=nozzle)
  368. if profiles:
  369. profile_data = {
  370. "version": "1.0",
  371. "printer_name": printer.name,
  372. "printer_serial": serial,
  373. "nozzle_diameter": nozzle,
  374. "profiles": [
  375. {
  376. "slot_id": p.slot_id,
  377. "name": p.name,
  378. "k_value": p.k_value,
  379. "filament_id": p.filament_id,
  380. "nozzle_id": p.nozzle_id,
  381. "extruder_id": p.extruder_id,
  382. "setting_id": p.setting_id,
  383. "n_coef": p.n_coef,
  384. }
  385. for p in profiles
  386. ],
  387. }
  388. files[f"kprofiles/{serial}/{nozzle}.json"] = profile_data
  389. printer_profiles[nozzle] = len(profiles)
  390. except Exception as e:
  391. logger.warning("Failed to get K-profiles for printer %s nozzle %s: %s", serial, nozzle, e)
  392. if printer_profiles:
  393. logger.info("Collected K-profiles for %s: %s", serial, printer_profiles)
  394. async def _collect_cloud_profiles(self, db: AsyncSession, files: dict) -> dict:
  395. """Collect slicer presets from every connected cloud account.
  396. Two clouds, and on an auth-enabled install any number of accounts in
  397. each: Bambu Cloud tokens live on ``User.cloud_token`` and Orca Cloud
  398. tokens on ``User.orca_cloud_token``, falling back to the global
  399. ``Settings`` table only when auth is disabled. The previous version
  400. asked for the auth-disabled store unconditionally, so it collected
  401. nothing at all on any install with auth on (#2717).
  402. Layout is one directory per cloud per account, both clouds grouped the
  403. same way so a restore reads them identically::
  404. cloud_profiles/bambu/user-3/{filament,printer,process}.json
  405. cloud_profiles/orca/user-3/{filament,printer,process}.json
  406. Accounts are keyed by Bambuddy user id (``global`` when auth is off),
  407. never by email — a backup repository can be public.
  408. Returns a per-cloud summary for ``backup_metadata.json`` so the
  409. metadata records what was actually collected rather than what was
  410. merely enabled.
  411. """
  412. summary: dict = {"bambu": {}, "orca": {}}
  413. bambu_accounts, orca_accounts = await self.cloud_accounts(db)
  414. if not bambu_accounts and not orca_accounts:
  415. # Enabled but nothing to collect. Deliberately a warning: the INFO
  416. # line this replaces read as a successful collection of nothing,
  417. # which is how #2717 went unnoticed through every backup.
  418. logger.warning(
  419. "Cloud profiles are enabled for backup, but no Bambu Cloud or Orca Cloud "
  420. "account is connected — nothing to collect."
  421. )
  422. return summary
  423. for account_key, user in bambu_accounts:
  424. try:
  425. counts = await self._collect_bambu_profiles(db, files, account_key, user)
  426. except Exception:
  427. logger.warning("Failed to collect Bambu Cloud profiles for %s", account_key, exc_info=True)
  428. continue
  429. if counts:
  430. summary["bambu"][account_key] = counts
  431. for account_key, user in orca_accounts:
  432. try:
  433. counts = await self._collect_orca_profiles(db, files, account_key, user)
  434. except Exception:
  435. logger.warning("Failed to collect Orca Cloud profiles for %s", account_key, exc_info=True)
  436. continue
  437. if counts:
  438. summary["orca"][account_key] = counts
  439. if not summary["bambu"] and not summary["orca"]:
  440. logger.warning(
  441. "Cloud profiles are enabled and %d Bambu / %d Orca account(s) are connected, "
  442. "but no presets were collected — see the per-account warnings above.",
  443. len(bambu_accounts),
  444. len(orca_accounts),
  445. )
  446. else:
  447. logger.info("Collected cloud profiles: %s", summary)
  448. return summary
  449. async def cloud_accounts(self, db: AsyncSession) -> tuple[list, list]:
  450. """Enumerate connected accounts as ``(account_key, user_or_None)`` per cloud.
  451. With auth enabled every user holds their own credentials, so a backup
  452. that only looked at the global store saw none of them. With auth
  453. disabled there is a single global row and no ``User`` at all, which is
  454. what ``user=None`` means to both clouds' credential loaders.
  455. Both stores are read regardless: a ``Settings`` row survives enabling
  456. auth later, and dropping it silently would lose that account's presets.
  457. """
  458. from backend.app.api.routes.cloud import get_stored_token
  459. from backend.app.api.routes.orca_cloud import _load_credentials
  460. bambu: list = []
  461. orca: list = []
  462. global_token, _email, _region = await get_stored_token(db, None)
  463. if global_token:
  464. bambu.append(("global", None))
  465. global_orca = await _load_credentials(db, None)
  466. if global_orca.token:
  467. orca.append(("global", None))
  468. result = await db.execute(
  469. select(User).where(or_(User.cloud_token.isnot(None), User.orca_cloud_token.isnot(None)))
  470. )
  471. for user in result.scalars().all():
  472. if user.cloud_token:
  473. bambu.append((f"user-{user.id}", user))
  474. if user.orca_cloud_token:
  475. orca.append((f"user-{user.id}", user))
  476. return bambu, orca
  477. async def _collect_bambu_profiles(self, db: AsyncSession, files: dict, account_key: str, user) -> dict:
  478. """Collect one Bambu Cloud account's custom presets, with their payloads.
  479. The listing endpoint is keyed by preset type, each holding ``private``
  480. and ``public`` lists — there is no flat ``setting`` array, and the
  481. entries carry no ``type`` of their own, which is why the type comes
  482. from the outer key here exactly as it does in ``routes/cloud.py``.
  483. Bambu calls process presets ``print``.
  484. ``public`` is skipped: those are Bambu's own bundled catalogue, the
  485. same hundreds of entries for every user, re-downloadable at any time
  486. and not recreatable under your account anyway. Backing them up would
  487. churn the repository on every run for nothing.
  488. Each private preset then costs one ``get_setting_detail`` call, because
  489. the listing carries only metadata. Without ``base_id`` and ``setting``
  490. the backup is a list of names, not something a restore can rebuild
  491. from. Bounded by the number of *custom* presets, and the backup already
  492. makes a round-trip per printer for K-profiles.
  493. """
  494. from backend.app.api.routes.cloud import build_authenticated_cloud
  495. cloud = await build_authenticated_cloud(db, user=user)
  496. if cloud is None or not cloud.is_authenticated:
  497. logger.info("Bambu Cloud not authenticated for %s, skipping", account_key)
  498. return {}
  499. counts: dict = {}
  500. try:
  501. settings = await cloud.get_slicer_settings()
  502. if not isinstance(settings, dict) or not settings:
  503. logger.warning("Bambu Cloud returned no slicer settings for %s", account_key)
  504. return {}
  505. failed = 0
  506. for api_key, our_type in _BAMBU_PRESET_TYPES.items():
  507. type_data = settings.get(api_key)
  508. if not isinstance(type_data, dict):
  509. continue
  510. private = type_data.get("private")
  511. if not isinstance(private, list) or not private:
  512. continue
  513. profiles = []
  514. for entry in private:
  515. setting_id = entry.get("setting_id") or entry.get("id")
  516. if not setting_id:
  517. continue
  518. try:
  519. detail = await cloud.get_setting_detail(str(setting_id))
  520. except Exception as e:
  521. # One unreadable preset must not cost the rest of the
  522. # account, but it must not vanish quietly either.
  523. failed += 1
  524. logger.warning(
  525. "Failed to fetch Bambu Cloud preset %s (%s) for %s: %s",
  526. setting_id,
  527. entry.get("name", "unnamed"),
  528. account_key,
  529. e,
  530. )
  531. continue
  532. profiles.append(_bambu_preset_record(setting_id, our_type, entry, detail))
  533. if profiles:
  534. files[f"cloud_profiles/bambu/{account_key}/{our_type}.json"] = {
  535. "version": "2.0",
  536. "cloud": "bambu",
  537. "type": our_type,
  538. "profiles": profiles,
  539. }
  540. counts[our_type] = len(profiles)
  541. if failed:
  542. counts["failed"] = failed
  543. return counts
  544. finally:
  545. await cloud.close()
  546. async def _collect_orca_profiles(self, db: AsyncSession, files: dict, account_key: str, user) -> dict:
  547. """Collect one Orca Cloud account's profiles, grouped the same three ways.
  548. Cheaper than Bambu: the sync-pull listing already carries each
  549. profile's full ``content``, so there is no per-profile fetch.
  550. The type lives at ``content.type`` and is mapped through the same
  551. ``_ORCA_TYPE_TO_BAMBU`` table the Orca tab uses, so the backup groups
  552. exactly as the UI does. Where that route *drops* a profile whose type
  553. it can't map, this writes it to ``other.json`` instead — a backup that
  554. silently omits a profile because Orca added a type is the same class of
  555. bug as #2717 itself.
  556. Uses the route layer's ``_build_authenticated_service`` rather than
  557. re-implementing the refresh: the Orca refresh token is single-use and
  558. rotating, and that helper already persists the new pair atomically
  559. before returning.
  560. Passes ``clear_on_auth_failure=False``, so a rejected refresh skips the
  561. account instead of disconnecting it. A backup is an observer; it should
  562. not change anyone's sign-in state on a schedule, least of all on a
  563. rejection reason Orca does not disambiguate. The next time the user
  564. opens the Orca Profiles page that route clears the dead pairing anyway,
  565. with the user present to pair again.
  566. """
  567. from fastapi import HTTPException
  568. from backend.app.api.routes.orca_cloud import (
  569. _ORCA_TYPE_TO_BAMBU,
  570. _build_authenticated_service,
  571. )
  572. try:
  573. svc = await _build_authenticated_service(db, user, clear_on_auth_failure=False)
  574. except HTTPException as e:
  575. # Either way the stored credentials are untouched and this account
  576. # is skipped, not disconnected — but the two need different advice.
  577. # A rejected refresh will not fix itself and needs the user to pair
  578. # again; an unreachable Orca is very likely gone by the next run.
  579. if e.status_code == 401:
  580. logger.warning(
  581. "Orca Cloud rejected the stored session for %s, so its profiles are not in this "
  582. "backup. Later runs will skip it too until the account is paired again under "
  583. "Profiles > Orca Cloud Profiles — which is also where the dead credentials get "
  584. "cleared. Cause: %s",
  585. account_key,
  586. e.detail,
  587. )
  588. else:
  589. logger.warning(
  590. "Orca Cloud unreachable for %s, skipping its profiles this run: %s",
  591. account_key,
  592. e.detail,
  593. )
  594. return {}
  595. except Exception as e:
  596. logger.warning("Orca Cloud not usable for %s: %s", account_key, e, exc_info=True)
  597. return {}
  598. counts: dict = {}
  599. try:
  600. raw_profiles = await svc.list_profiles()
  601. grouped: dict[str, list] = {}
  602. unknown_types: dict[str, int] = {}
  603. for entry in raw_profiles:
  604. if not isinstance(entry, dict):
  605. continue
  606. content = entry.get("content")
  607. raw_type = content.get("type") if isinstance(content, dict) else None
  608. our_type = _ORCA_TYPE_TO_BAMBU.get(str(raw_type)) if raw_type is not None else None
  609. if our_type is None:
  610. unknown_types[str(raw_type) if raw_type is not None else "<missing>"] = (
  611. unknown_types.get(str(raw_type) if raw_type is not None else "<missing>", 0) + 1
  612. )
  613. our_type = "other"
  614. grouped.setdefault(our_type, []).append(_orca_profile_record(entry))
  615. for our_type, profiles in grouped.items():
  616. files[f"cloud_profiles/orca/{account_key}/{our_type}.json"] = {
  617. "version": "2.0",
  618. "cloud": "orca",
  619. "type": our_type,
  620. "profiles": profiles,
  621. }
  622. counts[our_type] = len(profiles)
  623. if unknown_types:
  624. logger.warning(
  625. "Orca Cloud sent %d profile(s) for %s with unmapped content.type values %s — "
  626. "backed up to other.json rather than dropped.",
  627. sum(unknown_types.values()),
  628. account_key,
  629. unknown_types,
  630. )
  631. return counts
  632. finally:
  633. await svc.close()
  634. async def _collect_settings(self, db: AsyncSession, files: dict):
  635. """Collect app settings."""
  636. result = await db.execute(select(Settings))
  637. settings = result.scalars().all()
  638. # Filter out sensitive settings
  639. sensitive_keys = {"bambu_cloud_token", "auth_secret_key"}
  640. settings_data = {s.key: s.value for s in settings if s.key not in sensitive_keys}
  641. files["settings/app_settings.json"] = {
  642. "version": "1.0",
  643. "settings": settings_data,
  644. }
  645. async def _collect_spools(self, db: AsyncSession, files: dict):
  646. """Collect spool inventory data."""
  647. result = await db.execute(select(Spool))
  648. spools = result.scalars().all()
  649. if not spools:
  650. return
  651. spool_list = []
  652. for s in spools:
  653. spool_data = {
  654. "id": s.id,
  655. "material": s.material,
  656. "subtype": s.subtype,
  657. "color_name": s.color_name,
  658. "rgba": s.rgba,
  659. "brand": s.brand,
  660. "label_weight": s.label_weight,
  661. "core_weight": s.core_weight,
  662. "weight_used": s.weight_used,
  663. "weight_locked": s.weight_locked,
  664. "slicer_filament": s.slicer_filament,
  665. "slicer_filament_name": s.slicer_filament_name,
  666. "nozzle_temp_min": s.nozzle_temp_min,
  667. "nozzle_temp_max": s.nozzle_temp_max,
  668. "note": s.note,
  669. "cost_per_kg": s.cost_per_kg,
  670. "tag_uid": s.tag_uid,
  671. "tray_uuid": s.tray_uuid,
  672. "data_origin": s.data_origin,
  673. "tag_type": s.tag_type,
  674. "archived_at": str(s.archived_at) if s.archived_at else None,
  675. "created_at": str(s.created_at) if s.created_at else None,
  676. }
  677. spool_list.append(spool_data)
  678. files["spools/inventory.json"] = {
  679. "version": "1.0",
  680. "spools": spool_list,
  681. }
  682. # Collect usage history
  683. usage_result = await db.execute(select(SpoolUsageHistory))
  684. usages = usage_result.scalars().all()
  685. if usages:
  686. usage_list = []
  687. for u in usages:
  688. usage_list.append(
  689. {
  690. "id": u.id,
  691. "spool_id": u.spool_id,
  692. "printer_id": u.printer_id,
  693. "print_name": u.print_name,
  694. "archive_id": u.archive_id,
  695. "weight_used": u.weight_used,
  696. "percent_used": u.percent_used,
  697. "status": u.status,
  698. "cost": u.cost,
  699. "created_at": str(u.created_at) if u.created_at else None,
  700. }
  701. )
  702. files["spools/usage_history.json"] = {
  703. "version": "1.0",
  704. "usage_history": usage_list,
  705. }
  706. logger.info("Collected %d spools and %d usage records", len(spool_list), len(usages))
  707. async def _collect_archives(self, db: AsyncSession, files: dict):
  708. """Collect print archive metadata (no binary files)."""
  709. result = await db.execute(select(PrintArchive))
  710. archives = result.scalars().all()
  711. if not archives:
  712. return
  713. archive_list = []
  714. for a in archives:
  715. archive_data = {
  716. "id": a.id,
  717. "printer_id": a.printer_id,
  718. "project_id": a.project_id,
  719. "filename": a.filename,
  720. "file_size": a.file_size,
  721. "content_hash": a.content_hash,
  722. "print_name": a.print_name,
  723. "print_time_seconds": a.print_time_seconds,
  724. "filament_used_grams": a.filament_used_grams,
  725. "filament_type": a.filament_type,
  726. "filament_color": a.filament_color,
  727. "layer_height": a.layer_height,
  728. "total_layers": a.total_layers,
  729. "nozzle_diameter": a.nozzle_diameter,
  730. "bed_temperature": a.bed_temperature,
  731. "nozzle_temperature": a.nozzle_temperature,
  732. "sliced_for_model": a.sliced_for_model,
  733. "status": a.status,
  734. "started_at": str(a.started_at) if a.started_at else None,
  735. "completed_at": str(a.completed_at) if a.completed_at else None,
  736. "makerworld_url": a.makerworld_url,
  737. "designer": a.designer,
  738. "external_url": a.external_url,
  739. "is_favorite": a.is_favorite,
  740. "tags": a.tags,
  741. "notes": a.notes,
  742. "cost": a.cost,
  743. "failure_reason": a.failure_reason,
  744. "quantity": a.quantity,
  745. "energy_kwh": a.energy_kwh,
  746. "energy_cost": a.energy_cost,
  747. "created_at": str(a.created_at) if a.created_at else None,
  748. # Soft-deleted archives are collected too — their row is kept on
  749. # purpose so the stats endpoint keeps counting their filament and
  750. # energy (see archive_service.soft_delete_archive). Recording
  751. # deleted_at is what lets a restore put them back the way they
  752. # were instead of resurrecting them as visible archives.
  753. "deleted_at": str(a.deleted_at) if a.deleted_at else None,
  754. # Who owns the archive, for the same reason deleted_at is here:
  755. # it is not decoration, it is what the access check runs on.
  756. # _ensure_archive_visible (api/routes/archives.py) fails closed on
  757. # a NULL created_by_id and the list paths filter on it, so a
  758. # restored row without it is invisible to everyone but an admin —
  759. # while the restore reports it restored.
  760. "created_by_id": a.created_by_id,
  761. }
  762. archive_list.append(archive_data)
  763. files["archives/print_history.json"] = {
  764. "version": "1.0",
  765. "archives": archive_list,
  766. }
  767. logger.info("Collected %d print archives", len(archive_list))
  768. async def _push_to_provider(self, config: GitHubBackupConfig, files: dict) -> dict:
  769. """Push files to the configured Git provider."""
  770. backend = get_provider_backend(config.provider)
  771. client = await self._get_client()
  772. return await backend.push_files(
  773. repo_url=config.repository_url,
  774. token=config.access_token,
  775. branch=config.branch,
  776. files=files,
  777. client=client,
  778. )
  779. @property
  780. def is_running(self) -> bool:
  781. """Check if a backup is currently running."""
  782. return self._running_backup
  783. @property
  784. def progress(self) -> str | None:
  785. """Get current backup progress message."""
  786. return self._backup_progress
  787. async def get_logs(self, config_id: int, limit: int = 50, offset: int = 0) -> list[GitHubBackupLog]:
  788. """Get backup logs for a configuration."""
  789. async with async_session() as db:
  790. result = await db.execute(
  791. select(GitHubBackupLog)
  792. .where(GitHubBackupLog.config_id == config_id)
  793. .order_by(desc(GitHubBackupLog.started_at))
  794. .offset(offset)
  795. .limit(limit)
  796. )
  797. return list(result.scalars().all())
  798. # Singleton instance
  799. github_backup_service = GitHubBackupService()