Coverage for downtify/backup_routes.py: 67%

196 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 22:18 +0000

1"""``/api/backups``: back up and restore a Downtify install (admins). 

2 

3* ``GET /api/backups`` - the schedule and the saved backups. 

4* ``POST /api/backups`` - back up now. 

5* ``PUT /api/backups/schedule`` - ``{interval_hours}`` (``0``: off). 

6* ``GET /api/backups/{name}`` - download one backup file. 

7* ``POST /api/backups/{name}/restore`` - restore a saved backup. 

8* ``POST /api/backups/restore`` - restore an uploaded backup file (the 

9 request body); both restores take ``settings``, ``watches`` and 

10 ``songs`` query flags saying what to bring back. 

11 

12See :mod:`downtify.backup` for what a backup holds. 

13""" 

14 

15from __future__ import annotations 

16 

17import asyncio 

18import copy 

19import uuid 

20from datetime import datetime, timezone 

21from pathlib import Path 

22from typing import Any, Optional 

23 

24from fastapi import APIRouter, HTTPException, Request 

25from fastapi.responses import FileResponse 

26from loguru import logger 

27from pydantic import BaseModel 

28 

29from . import __version__, api, backup 

30from .monitor import ALL_RELEASE_TYPES, KIND_PLAYLIST 

31 

32router = APIRouter(prefix='/api/backups') 

33 

34#: How often the schedule is looked at, in seconds. 

35SCHEDULE_CHECK_SECONDS = 600 

36 

37_lock = asyncio.Lock() 

38_last_error: dict[str, str] = {} 

39 

40 

41def folder() -> Optional[Path]: 

42 """Where backups are kept: ``backups`` beside ``settings.json``.""" 

43 

44 path = api.state.settings_path 

45 return None if path is None else Path(path).parent / 'backups' 

46 

47 

48def _folder_or_503() -> Path: 

49 found = folder() 

50 if found is None: 

51 raise HTTPException(status_code=503, detail='No data folder') 

52 return found 

53 

54 

55# ── Making one ────────────────────────────────────────────────────── 

56 

57 

58def _watches() -> list[dict[str, Any]]: 

59 db = api.state.monitor_db 

60 if db is None: 

61 return [] 

62 return [watch.to_dict() for watch in db.list_playlists()] 

63 

64 

65def _songs() -> list[dict[str, Any]]: 

66 rows = api.listing_for_request(api.library_context()) 

67 index = api.state.track_index 

68 ids = index.spotify_ids_by_filename() if index is not None else {} 

69 providers: dict[str, str] = {} 

70 if api.state.file_providers is not None: 

71 providers = api.state.file_providers.lookup({ 

72 str(row.get('file') or ''): '' for row in rows 

73 }) 

74 return [ 

75 backup.song_record( 

76 row, 

77 spotify_id=ids.get(str(row.get('file') or ''), ''), 

78 provider=providers.get(str(row.get('file') or ''), ''), 

79 ) 

80 for row in rows 

81 if row.get('file') 

82 ] 

83 

84 

85def _make() -> backup.BackupInfo: 

86 data = backup.build( 

87 settings=copy.deepcopy(api.state.settings), 

88 watches=_watches(), 

89 songs=_songs(), 

90 app_version=__version__, 

91 ) 

92 return backup.write_backup(_folder_or_503(), data) 

93 

94 

95async def back_up(trigger: str) -> backup.BackupInfo: 

96 """Make a backup now (one at a time).""" 

97 

98 async with _lock: 

99 try: 

100 info = await asyncio.to_thread(_make) 

101 except Exception as exc: 

102 _last_error.update( 

103 at=datetime.now(timezone.utc).isoformat(timespec='seconds'), 

104 error=str(exc) or type(exc).__name__, 

105 trigger=trigger, 

106 ) 

107 raise 

108 _last_error.clear() 

109 return info 

110 

111 

112def _interval() -> int: 

113 return backup.clamp_interval( 

114 api.state.settings.get('backup_interval_hours', 0) 

115 ) 

116 

117 

118async def maybe_back_up() -> bool: 

119 """Make a scheduled backup when one is due.""" 

120 

121 path = folder() 

122 if path is None or _lock.locked(): 

123 return False 

124 saved = await asyncio.to_thread(backup.list_backups, path) 

125 newest = saved[0].created_at if saved else None 

126 if not backup.due(newest, _interval()): 

127 return False 

128 try: 

129 await back_up('schedule') 

130 except Exception: 

131 logger.exception('Scheduled backup failed') 

132 return False 

133 return True 

134 

135 

136async def schedule_loop(*, first_delay: float = 120) -> None: 

137 """Look every few minutes whether a scheduled backup is due.""" 

138 

139 await asyncio.sleep(first_delay) 

140 while True: 

141 try: 

142 await maybe_back_up() 

143 except Exception: 

144 logger.exception('Backup schedule check failed') 

145 await asyncio.sleep(SCHEDULE_CHECK_SECONDS) 

146 

147 

148# ── Restoring ─────────────────────────────────────────────────────── 

149 

150 

151class _Body: 

152 """*request* with *payload* as its JSON body, for reusing the 

153 settings route (which also logs who changed what).""" 

154 

155 def __init__(self, request: Request, payload: dict[str, Any]) -> None: 

156 self._request = request 

157 self._payload = payload 

158 

159 async def json(self) -> dict[str, Any]: 

160 return self._payload 

161 

162 def __getattr__(self, name: str) -> Any: 

163 return getattr(self._request, name) 

164 

165 

166async def _restore_settings( 

167 request: Request, saved: dict[str, Any] 

168) -> list[str]: 

169 payload = { 

170 key: value 

171 for key, value in saved.items() 

172 if key in api.DEFAULT_SETTINGS 

173 } 

174 await api.update_settings_endpoint(_Body(request, payload), client_id='') # type: ignore[arg-type] 

175 return sorted(payload) 

176 

177 

178def _restore_watches(saved: list[Any]) -> tuple[int, int]: 

179 """Add the backup's watches this install doesn't have yet. Returns 

180 ``(added, already there)``.""" 

181 

182 db = api.state.monitor_db 

183 if db is None: 

184 raise HTTPException(status_code=503, detail='Monitor not ready') 

185 added = present = 0 

186 for raw in saved: 

187 if not isinstance(raw, dict): 

188 continue 

189 key = str(raw.get('spotify_id') or '') 

190 url = str(raw.get('url') or '') 

191 if not key or not url: 

192 continue 

193 user_id = int(raw.get('user_id') or 0) 

194 if db.get_by_spotify_id(key, user_id) is not None: 

195 present += 1 

196 continue 

197 types = raw.get('release_types') 

198 watch = db.add_playlist( 

199 key, 

200 str(raw.get('name') or key), 

201 url, 

202 interval_minutes=int(raw.get('interval_minutes') or 60), 

203 kind=str(raw.get('kind') or KIND_PLAYLIST), 

204 release_types=( 

205 ','.join(types) 

206 if isinstance(types, list) 

207 else str(types or '') 

208 ) 

209 or ALL_RELEASE_TYPES, 

210 new_only=bool(raw.get('new_only')), 

211 user_id=user_id, 

212 ) 

213 options = { 

214 name: raw[name] for name in backup.WATCH_OPTIONS if name in raw 

215 } 

216 if options: 

217 db.update_playlist(watch.id, **options) 

218 added += 1 

219 return added, present 

220 

221 

222def _on_disk(stored: str) -> bool: 

223 try: 

224 return ( 

225 api.resolve_library_file(stored, api.library_context()) is not None 

226 ) 

227 except Exception: 

228 return False 

229 

230 

231async def _restore_songs( 

232 request: Request, saved: list[Any] 

233) -> tuple[int, int]: 

234 """Queue the backup's songs whose file is missing. Returns 

235 ``(missing, queued)``.""" 

236 

237 missing = await asyncio.to_thread(backup.missing_songs, saved, _on_disk) 

238 if not missing: 

239 return 0, 0 

240 token = uuid.uuid4().hex[:12] 

241 for position, song in enumerate(missing): 

242 song.setdefault('song_id', f'backup:{token}:{position}') 

243 result = await api._submit_playlist_batch( # noqa: SLF001 

244 missing, 

245 '', 

246 generate_m3u=False, 

247 user_id=api.request_user_id(request), 

248 ) 

249 queued = len(result.get('job_ids') or []) or len(missing) 

250 return len(missing), queued 

251 

252 

253def _flag(request: Request, name: str) -> bool: 

254 return request.query_params.get(name, '1') not in {'0', 'false', ''} 

255 

256 

257async def _restore(request: Request, data: dict[str, Any]) -> dict[str, Any]: 

258 if api.state.downloader is None: 

259 raise HTTPException(status_code=503, detail='Downloader not ready') 

260 result: dict[str, Any] = { 

261 'created_at': data.get('created_at'), 

262 'settings': [], 

263 'watches_added': 0, 

264 'watches_present': 0, 

265 'songs_checked': False, 

266 'songs_missing': 0, 

267 'songs_queued': 0, 

268 } 

269 if _flag(request, 'settings'): 

270 result['settings'] = await _restore_settings(request, data['settings']) 

271 if _flag(request, 'watches'): 

272 added, present = await asyncio.to_thread( 

273 _restore_watches, data['watches'] 

274 ) 

275 result.update(watches_added=added, watches_present=present) 

276 if _flag(request, 'songs'): 

277 missing, queued = await _restore_songs(request, data['songs']) 

278 result.update( 

279 songs_checked=True, songs_missing=missing, songs_queued=queued 

280 ) 

281 await api.log_activity( 

282 request, 

283 'backup_restored', 

284 str(data.get('created_at') or ''), 

285 {k: v for k, v in result.items() if k != 'settings'}, 

286 ) 

287 logger.info('Backup restored: {}', result) 

288 return result 

289 

290 

291# ── Routes ────────────────────────────────────────────────────────── 

292 

293 

294@router.get('') 

295async def backups_status() -> dict[str, Any]: 

296 path = folder() 

297 saved = await asyncio.to_thread(backup.list_backups, path) if path else [] 

298 return { 

299 'interval_hours': _interval(), 

300 'intervals': list(backup.INTERVALS), 

301 'keep': backup.KEEP, 

302 'running': _lock.locked(), 

303 'folder': str(path) if path else '', 

304 'last_error': dict(_last_error) or None, 

305 'backups': [info.to_dict() for info in saved], 

306 } 

307 

308 

309@router.post('') 

310async def backups_create() -> dict[str, Any]: 

311 if _lock.locked(): 

312 raise HTTPException(status_code=409, detail='A backup is running') 

313 try: 

314 info = await back_up('manual') 

315 except HTTPException: 

316 raise 

317 except Exception as exc: 

318 logger.exception('Backup failed') 

319 raise HTTPException(status_code=500, detail='Backup failed') from exc 

320 return info.to_dict() 

321 

322 

323class ScheduleRequest(BaseModel): 

324 interval_hours: int 

325 

326 

327@router.put('/schedule') 

328async def backups_schedule(body: ScheduleRequest) -> dict[str, Any]: 

329 if body.interval_hours not in backup.INTERVALS: 

330 raise HTTPException(status_code=400, detail='Unknown interval') 

331 api.state.settings['backup_interval_hours'] = body.interval_hours 

332 if api.state.settings_path is not None: 

333 await asyncio.to_thread( 

334 api._save_settings, # noqa: SLF001 

335 api.state.settings_path, 

336 api.state.settings, 

337 ) 

338 return await backups_status() 

339 

340 

341@router.post('/restore') 

342async def backups_restore_upload(request: Request) -> dict[str, Any]: 

343 raw = await request.body() 

344 if not raw: 

345 raise HTTPException(status_code=400, detail='No backup file sent') 

346 if len(raw) > backup.MAX_UPLOAD_BYTES: 

347 raise HTTPException(status_code=413, detail='Backup file too large') 

348 try: 

349 data = await asyncio.to_thread(backup.parse, raw) 

350 except backup.BackupError as exc: 

351 raise HTTPException(status_code=400, detail=str(exc)) from None 

352 return await _restore(request, data) 

353 

354 

355@router.get('/{name}') 

356async def backups_download(name: str) -> FileResponse: 

357 path = backup.path_of(_folder_or_503(), name) 

358 if path is None: 

359 raise HTTPException(status_code=404, detail='No such backup') 

360 return FileResponse( 

361 path, media_type='application/gzip', filename=path.name 

362 ) 

363 

364 

365@router.post('/{name}/restore') 

366async def backups_restore_saved(name: str, request: Request) -> dict[str, Any]: 

367 path = backup.path_of(_folder_or_503(), name) 

368 if path is None: 

369 raise HTTPException(status_code=404, detail='No such backup') 

370 try: 

371 data = await asyncio.to_thread(backup.read, path) 

372 except backup.BackupError as exc: 

373 raise HTTPException(status_code=400, detail=str(exc)) from None 

374 return await _restore(request, data)