Skip to content

Add Google Drive integration for automated DB backups - #138

Merged
itsbkm merged 1 commit into
mainfrom
feature/google-drive-backups
Jul 21, 2026
Merged

itsbkm merged 1 commit into
mainfrom
feature/google-drive-backups

Conversation

@itsbkm

@itsbkm itsbkm commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds optional Google Drive integration so database backups can be automatically uploaded off-device on a schedule, on top of the local backups/ folder and the Backup Management & History UI from #120.

Follows the same explicit opt-in shape as the existing Cloud Services tab (see the offline-first audit): nothing in this PR talks to Google until the store owner explicitly clicks "Connect" in Settings > Integrations > Google Drive.

  • OAuth: standard "installed app" loopback flow (Google's recommended pattern for desktop apps) — opens the consent screen in the system browser via shell.openExternal and catches the redirect on a local HTTP server bound to a random port, rather than an embedded webview. Scope is drive.file only (the app only ever sees files it creates). Built with the googleapis package (its bundled google.auth.OAuth2, to avoid a duplicate-version type conflict with installing the standalone google-auth-library package alongside it).
  • Config: client credentials come from GOOGLE_DRIVE_CLIENT_ID / GOOGLE_DRIVE_CLIENT_SECRET environment variables (same pattern as the existing JWT_SECRET). If either is unset, Settings shows "Google Drive integration is not configured for this build" instead of a broken Connect button — no crash, no dead-end UI.
  • Token storage: OAuth tokens are encrypted at rest via Electron's safeStorage (identical pattern to main/services/master-pin.ts), in their own file (google-drive-token.enc) — never in the SQLite DB. Disconnecting revokes the token via Google's /revoke endpoint (not just clearing local UI state) before deleting the encrypted blob locally, and does so resiliently even if Google is unreachable.
  • Backups: reuses createBackup() from main/db.ts unmodified — no second export path that could skip the existing /api/db/export secret-redaction. Configurable daily/weekly schedule (hourly check timer, same cadence as the existing telemetry service) plus a manual "Back up to Drive now" button, both going through the same upload/retention path.
  • Retention: keeps the last N backups (default 10, configurable 1–100) in a dedicated "FloCafe Backups" Drive folder, deleting the oldest beyond that after each successful upload.
  • Backup History integration (Feature: Backup Management & History UI #120): the existing Backup History table now shows a "Synced to Drive" badge on the local backup file that was last uploaded, plus a dedicated status line (last backup time/success/failure) in the new Google Drive settings card.
  • Roles: connect/disconnect/backup-now are owner-only (mirrors how database.ts gates the raw backup/restore actions); status + frequency/retention preferences are owner+manager, matching the Cloud tab.

New dependency

  • googleapis (^173.0.0) — Drive API v3 client + OAuth2 client. (google-auth-library was evaluated but not added directly: googleapis bundles its own compatible copy, and installing both caused a TypeScript duplicate-declaration conflict between the two copies' OAuth2Client types.)

Maintainer setup required before this is usable

This integration ships disabled by default and needs real Google Cloud OAuth client credentials before "Connect" works in any build. I don't have a Google account / Cloud Console access in this environment, so I could not create real credentials — the full code path (loopback OAuth, token encryption, Drive upload, retention, revoke) is implemented and tested against the "not configured" and "configured but not yet connected" states, but the actual OAuth handshake against real Google infrastructure has not been exercised end-to-end.

A maintainer needs to:

  1. Follow docs/google-drive-setup.md to create an OAuth client in Google Cloud Console (enable Drive API, configure the consent screen with the drive.file scope, create a Desktop app OAuth client).
  2. Set GOOGLE_DRIVE_CLIENT_ID / GOOGLE_DRIVE_CLIENT_SECRET for the build (see .env.example / README "Environment Variables").
  3. Do one real manual connect/backup/disconnect pass against a test Google account before shipping.

Out of scope (per the issue)

  • Other providers (Dropbox, OneDrive, S3) — Drive only for v1.
  • Automatic restore-from-Drive on fresh install — manual download + restore via the existing restore flow is fine for v1.

Test plan

  • npm run build (backend tsc) — clean
  • npm --prefix frontend run build — clean
  • npm run lint (backend + frontend eslint) — 0 errors (pre-existing warnings elsewhere untouched; new files have 0 warnings)
  • npm test — full existing suite passes, including a new tests/google-drive.test.ts covering config detection, retention/scheduling math, settings persistence + validation, and the encrypted-token file lifecycle (connect/disconnect's real OAuth network path isn't covered — see above)
  • Manual end-to-end connect → scheduled/manual backup → retention rotation → disconnect, once real OAuth credentials are configured (maintainer follow-up)

Closes #129

Adds an opt-in Google Drive connection under Settings > Integrations,
following the same explicit opt-in shape as the existing Cloud Services
tab: nothing talks to Google until the owner clicks Connect.

- OAuth "installed app" loopback flow (system browser + local redirect
  server on a random port), scoped to drive.file only, using the
  googleapis package. Client credentials come from
  GOOGLE_DRIVE_CLIENT_ID/GOOGLE_DRIVE_CLIENT_SECRET env vars; when unset,
  Settings shows "Google Drive integration is not configured for this
  build" instead of a broken Connect button.
- Tokens are encrypted at rest via Electron's safeStorage (same pattern
  as master-pin.ts) in their own file, never in the SQLite DB.
  Disconnecting revokes the token with Google's /revoke endpoint before
  deleting the local blob.
- Reuses createBackup() from db.ts unchanged for the uploaded artifact —
  no second export path. Configurable daily/weekly schedule (hourly
  check, same cadence as telemetry) plus a manual "Back up to Drive now"
  action, both funneled through the same upload/retention code path.
- Retention: keeps the last N backups (default 10) in a dedicated Drive
  folder, deleting the oldest beyond that after each successful upload.
- Backup History table (#120) shows a "Synced to Drive" badge on the
  local backup that was last uploaded.
- docs/google-drive-setup.md documents the exact Google Cloud Console
  steps a maintainer must follow to create the OAuth client.

New dependency: googleapis (bundles its own google-auth-library, used
via google.auth.OAuth2 to avoid a duplicate-version type conflict with
a standalone google-auth-library install).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@itsbkm
itsbkm merged commit f32afb2 into main Jul 21, 2026
10 checks passed
@itsbkm
itsbkm deleted the feature/google-drive-backups branch July 21, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Google Drive integration for automated DB backups

1 participant