From 332d02f585b03b9bae9e17dbd141c005d08f241e Mon Sep 17 00:00:00 2001 From: broisnees Date: Wed, 18 Nov 2026 05:38:51 +0000 Subject: [PATCH 1/2] Add scheduled backups (wip) --- package-lock.json | 14 +- package.json | 1 + src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 3 +- src-tauri/src/cloud_backup.rs | 443 +++++++++++++++++++++ src-tauri/src/db/backup.rs | 6 +- src-tauri/src/db/dynamodb.rs | 11 +- src-tauri/src/lib.rs | 8 + src/lib/components/BackupPage.svelte | 13 +- src/lib/components/LicenseGate.svelte | 2 + src/lib/components/ScheduledBackups.svelte | 382 ++++++++++++++++++ src/lib/components/StudioShell.svelte | 2 +- src/lib/stores/scheduled-backups.js | 209 ++++++++++ src/lib/stores/scheduled-backups.test.js | 43 ++ 14 files changed, 1127 insertions(+), 12 deletions(-) create mode 100644 src-tauri/src/cloud_backup.rs create mode 100644 src/lib/components/ScheduledBackups.svelte create mode 100644 src/lib/stores/scheduled-backups.js create mode 100644 src/lib/stores/scheduled-backups.test.js diff --git a/package-lock.json b/package-lock.json index ca2f2781..1bea7063 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "stroke", - "version": "2.17.9", + "version": "2.19.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "stroke", - "version": "2.17.9", + "version": "2.19.0", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@codemirror/autocomplete": "^6.20.3", @@ -52,6 +52,7 @@ "canvas-confetti": "^1.9.4", "chart.js": "^4.5.1", "clsx": "^2.1.1", + "croner": "^9.1.0", "echarts": "^6.1.0", "echarts-wordcloud": "^2.1.0", "elkjs": "^0.11.1", @@ -2564,6 +2565,15 @@ "integrity": "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==", "license": "MIT" }, + "node_modules/croner": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/croner/-/croner-9.1.0.tgz", + "integrity": "sha512-p9nwwR4qyT5W996vBZhdvBCnMhicY5ytZkR4D1Xj0wuTDEiMnjwR57Q3RXYY/s0EpX6Ay3vgIcfaR+ewGHsi+g==", + "license": "MIT", + "engines": { + "node": ">=18.0" + } + }, "node_modules/cross-env": { "version": "10.1.0", "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.1.0.tgz", diff --git a/package.json b/package.json index db6b661e..94ea1dfb 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "canvas-confetti": "^1.9.4", "chart.js": "^4.5.1", "clsx": "^2.1.1", + "croner": "^9.1.0", "echarts": "^6.1.0", "echarts-wordcloud": "^2.1.0", "elkjs": "^0.11.1", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 360bc8ff..0605fa7f 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -178,6 +178,7 @@ dependencies = [ "chrono", "duckdb", "ed25519-dalek", + "flate2", "futures", "gdk", "getrandom 0.2.17", @@ -3685,7 +3686,6 @@ version = "1.10506.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "113d1c26c9f518405a17b5729bb6ab6abf029fdc46626406922cf9a8dc9b4609" dependencies = [ - "cc", "flate2", "pkg-config", "serde", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e892f9e2..1b267a8e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -34,6 +34,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "net", axum = { version = "0.7", features = ["json", "macros", "ws"] } tower-http = { version = "0.5", features = ["cors"] } sqlx = { version = "0.8", features = ["runtime-tokio", "tls-rustls", "postgres", "sqlite", "mysql", "json", "chrono", "uuid", "rust_decimal"] } +flate2 = "1" reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls-webpki-roots", "stream"] } futures = "0.3" # Local agent CLIs (Claude Code, Codex, OpenCode), vendored from deck. @@ -77,7 +78,7 @@ oracle = "0.6.3" # startup (see `run`): tiberius 0.13 enables aws-lc-rs next to the ring the # other drivers use, and rustls then has no default of its own. rustls = { version = "0.23", default-features = false, features = ["ring", "std"] } -tokio-util = { version = "0.7.18", features = ["compat"] } +tokio-util = { version = "0.7.18", features = ["compat", "io"] } # The SQL terminal: the engine's own CLI (psql, mysql, sqlite3...) in a # pseudo-terminal. ConPTY on Windows, openpty everywhere else. portable-pty = "0.9" diff --git a/src-tauri/src/cloud_backup.rs b/src-tauri/src/cloud_backup.rs new file mode 100644 index 00000000..3e1e7786 --- /dev/null +++ b/src-tauri/src/cloud_backup.rs @@ -0,0 +1,443 @@ +//! Scheduled backups (Pro). The schedule and history live on stroke.click +//! (`/api/backups/*`); this side runs a job: open the saved connection, dump +//! it with the backup engine (`db::backup`), gzip it, and send it either to +//! Stroke Cloud (chunked upload through the API) or to the user's own +//! S3-compatible bucket (one signed PUT). Database credentials never leave the +//! device, and neither do the bucket's keys: they sit in the keychain vault. + +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use tauri::Manager; + +use crate::account::{base_url, http, load_token, read_error, ApiError}; +use crate::db::connection::{open_duckdb, open_mssql, open_mysql, open_pg, open_sqlite, ActiveConnection, AnyConnectionConfig}; + +/// Stroke Cloud upload part size. R2 wants at least 5 MB for every part but +/// the last, and the Worker reads each part into memory. +const PART_BYTES: usize = 16 * 1024 * 1024; +const VAULT_PREFIX: &str = "backup-s3:"; + +/// What the scheduler needs from a job, as the server lists it. +#[derive(Deserialize, Debug, Clone)] +pub struct JobSpec { + pub id: String, + pub label: String, + pub destination: String, + #[serde(default)] + pub s3_bucket: Option, + #[serde(default)] + pub s3_endpoint: Option, + #[serde(default)] + pub s3_prefix: Option, +} + +#[derive(Serialize, Debug)] +pub struct RunOutcome { + /// "success", "failed", or "skipped" (another device has this tick). + pub status: &'static str, + pub message: String, + pub bytes: u64, +} + +impl RunOutcome { + fn skipped(message: impl Into) -> Self { + Self { status: "skipped", message: message.into(), bytes: 0 } + } +} + +#[derive(Serialize, Deserialize, Default, Clone)] +struct S3Secret { + access_key_id: String, + secret_access_key: String, + #[serde(default)] + region: String, +} + +fn api_message(e: ApiError) -> String { + match e { + ApiError::Unauthorized => "Your Stroke session ended. Sign in again.".into(), + ApiError::Network(_) => "Couldn't reach stroke.click. Check your connection.".into(), + ApiError::Server(_, m) => m, + } +} + +async fn token(app: &tauri::AppHandle) -> Result { + load_token(app).await.ok_or_else(|| "Sign in to use scheduled backups.".to_string()) +} + +async fn send_json(req: reqwest::RequestBuilder) -> Result { + let resp = req.send().await.map_err(|e| ApiError::Network(e.to_string()))?; + if !resp.status().is_success() { + return Err(read_error(resp).await); + } + resp.json().await.map_err(|e| ApiError::Network(e.to_string())) +} + +// ── Job management (thin proxies; the server validates) ─────────────────────── + +#[tauri::command] +pub async fn backup_jobs_list(app: tauri::AppHandle) -> Result { + let t = token(&app).await?; + send_json(http().get(format!("{}/api/backups/jobs", base_url())).bearer_auth(t)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_save(app: tauri::AppHandle, job: Value) -> Result { + let t = token(&app).await?; + send_json(http().post(format!("{}/api/backups/jobs", base_url())).bearer_auth(t).json(&job)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_patch(app: tauri::AppHandle, id: String, patch: Value) -> Result { + let t = token(&app).await?; + send_json(http().patch(format!("{}/api/backups/jobs/{}", base_url(), enc(&id))).bearer_auth(t).json(&patch)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_delete(app: tauri::AppHandle, id: String) -> Result { + let t = token(&app).await?; + let out = send_json(http().delete(format!("{}/api/backups/jobs/{}", base_url(), enc(&id))).bearer_auth(t)) + .await + .map_err(api_message)?; + let key = format!("{VAULT_PREFIX}{id}"); + let _ = crate::secrets::update_async(&app, move |m| { + m.remove(&key); + }) + .await; + Ok(out) +} + +/// Keep a job's S3 keys in the keychain vault. Empty keys remove them. +#[tauri::command] +pub async fn backup_s3_save( + app: tauri::AppHandle, + job_id: String, + access_key_id: String, + secret_access_key: String, + region: String, +) -> Result<(), String> { + let key = format!("{VAULT_PREFIX}{job_id}"); + let secret = S3Secret { + access_key_id: access_key_id.trim().to_string(), + secret_access_key: secret_access_key.trim().to_string(), + region: region.trim().to_string(), + }; + let json = serde_json::to_string(&secret).map_err(|e| e.to_string())?; + crate::secrets::update_async(&app, move |m| { + if secret.access_key_id.is_empty() || secret.secret_access_key.is_empty() { + m.remove(&key); + } else { + m.insert(key, json); + } + }) + .await +} + +/// Whether this device has S3 keys for the job (the keys themselves stay put). +#[tauri::command] +pub async fn backup_s3_status(app: tauri::AppHandle, job_id: String) -> Result, String> { + Ok(load_s3(&app, &job_id).await.map(|s| s.region)) +} + +async fn load_s3(app: &tauri::AppHandle, job_id: &str) -> Option { + let raw = crate::secrets::read_all_async(app).await.get(&format!("{VAULT_PREFIX}{job_id}")).cloned()?; + serde_json::from_str(&raw).ok() +} + +// ── Running a job ───────────────────────────────────────────────────────────── + +/// Run one job now: claim `slot` on the server, dump, upload, report. +#[tauri::command] +pub async fn backup_job_run( + app: tauri::AppHandle, + job: JobSpec, + config: AnyConnectionConfig, + slot: String, +) -> Result { + let t = token(&app).await?; + let base = base_url(); + + // S3 keys first: without them there is nothing to claim the tick for. + let s3 = if job.destination == "s3" { + match load_s3(&app, &job.id).await { + Some(s) => Some(s), + None => return Err("This device has no S3 keys for this backup. Add them in the schedule's settings.".into()), + } + } else { + None + }; + + let claim = http() + .post(format!("{base}/api/backups/runs")) + .bearer_auth(&t) + .json(&serde_json::json!({ "job_id": job.id, "slot": slot })); + let run = match send_json(claim).await { + Ok(v) => v, + Err(ApiError::Server(code, msg)) if code == "claimed" => return Ok(RunOutcome::skipped(msg)), + Err(e) => return Err(api_message(e)), + }; + let run_id = run.get("id").and_then(Value::as_str).unwrap_or_default().to_string(); + if run_id.is_empty() { + return Err("stroke.click didn't start the run.".into()); + } + + let dir = app.path().app_cache_dir().map_err(|e| e.to_string())?.join("scheduled-backups"); + let result = dump_and_upload(&dir, &job, config, s3, &base, &t, &run_id).await; + + let body = match &result { + Ok(done) => serde_json::json!({ + "status": "success", "tables": done.tables, "rows": done.rows, + "bytes": done.bytes, "object_key": done.object_key, + }), + Err(e) => serde_json::json!({ "status": "failed", "error": e }), + }; + let finish = http().patch(format!("{base}/api/backups/runs/{}", enc(&run_id))).bearer_auth(&t).json(&body); + let reported = send_json(finish).await; + + match (result, reported) { + (Ok(done), Ok(_)) => Ok(RunOutcome { status: "success", message: format!("Backed up {} tables.", done.tables), bytes: done.bytes }), + (Ok(_), Err(e)) => Err(format!("The backup finished but couldn't be recorded: {}", api_message(e))), + (Err(e), _) => Ok(RunOutcome { status: "failed", message: e, bytes: 0 }), + } +} + +struct Done { + tables: usize, + rows: usize, + bytes: u64, + object_key: Option, +} + +async fn dump_and_upload( + dir: &Path, + job: &JobSpec, + config: AnyConnectionConfig, + s3: Option, + base: &str, + token: &str, + run_id: &str, +) -> Result { + std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; + let sql_path = dir.join(format!("{run_id}.sql")); + let gz_path = dir.join(format!("{run_id}.sql.gz")); + let cleanup = |paths: &[&PathBuf]| { + for p in paths { + let _ = std::fs::remove_file(p); + } + }; + + let result = async { + let conn = open(config).await?; + crate::db::backup::reset_cancel(); + let rep = crate::db::backup::Reporter::silent(); + let opts = crate::db::backup::ExportOptions::default(); + let export = crate::db::backup::run_export(&rep, conn.clone(), None, None, &opts, Some(sql_path.to_string_lossy().into_owned())).await; + crate::db::connection::close_in_background(conn); + let export = export?; + if export.cancelled { + return Err("The backup was cancelled.".to_string()); + } + let src = sql_path.clone(); + let dst = gz_path.clone(); + let bytes = tokio::task::spawn_blocking(move || gzip(&src, &dst)).await.map_err(|e| e.to_string())??; + let _ = std::fs::remove_file(&sql_path); + + let object_key = match s3 { + Some(s) => Some(upload_s3(&gz_path, job, &s, bytes).await?), + None => { + upload_stroke(&gz_path, base, token, run_id).await?; + None + } + }; + Ok(Done { tables: export.table_count, rows: export.row_count, bytes, object_key }) + } + .await; + cleanup(&[&sql_path, &gz_path]); + result +} + +/// A standalone connection for the dump, apart from whatever the page has open. +async fn open(config: AnyConnectionConfig) -> Result { + Ok(match config { + AnyConnectionConfig::Postgres(c) => ActiveConnection::Postgres(open_pg(&c).await?), + AnyConnectionConfig::Sqlite(c) => ActiveConnection::Sqlite(open_sqlite(&c).await?), + AnyConnectionConfig::Mysql(c) => ActiveConnection::Mysql(open_mysql(&c).await?), + AnyConnectionConfig::D1(c) => ActiveConnection::D1(c), + AnyConnectionConfig::Libsql(c) => ActiveConnection::LibSql(c), + AnyConnectionConfig::Duckdb(c) => ActiveConnection::Duckdb(open_duckdb(&c).await?), + AnyConnectionConfig::Mssql(c) => ActiveConnection::Mssql(open_mssql(&c).await?), + _ => return Err("Scheduled backups support PostgreSQL, MySQL, SQLite, D1, Turso/libSQL, DuckDB and SQL Server.".into()), + }) +} + +fn gzip(src: &Path, dst: &Path) -> Result { + let mut input = std::fs::File::open(src).map_err(|e| e.to_string())?; + let out = std::fs::File::create(dst).map_err(|e| e.to_string())?; + let mut enc = flate2::write::GzEncoder::new(std::io::BufWriter::new(out), flate2::Compression::default()); + std::io::copy(&mut input, &mut enc).map_err(|e| e.to_string())?; + enc.finish().map_err(|e| e.to_string())?.flush().map_err(|e| e.to_string())?; + Ok(std::fs::metadata(dst).map_err(|e| e.to_string())?.len()) +} + +/// Stroke Cloud: the file in PART_BYTES parts, each retried a few times. +async fn upload_stroke(path: &Path, base: &str, token: &str, run_id: &str) -> Result<(), String> { + let mut file = std::fs::File::open(path).map_err(|e| e.to_string())?; + let mut n = 1u32; + loop { + let mut buf = vec![0u8; PART_BYTES]; + let mut filled = 0; + while filled < PART_BYTES { + let read = file.read(&mut buf[filled..]).map_err(|e| e.to_string())?; + if read == 0 { + break; + } + filled += read; + } + if filled == 0 { + break; + } + buf.truncate(filled); + let url = format!("{base}/api/backups/runs/{}/parts/{n}", enc(run_id)); + let mut attempt = 0; + loop { + attempt += 1; + let sent = http() + .put(&url) + .bearer_auth(token) + .header("content-type", "application/octet-stream") + .timeout(Duration::from_secs(300)) + .body(buf.clone()) + .send() + .await; + match sent { + Ok(r) if r.status().is_success() => break, + Ok(r) if !crate::account::is_outage(r.status()) || attempt >= 3 => { + return Err(format!("Uploading to Stroke Cloud failed: {}", api_message(read_error(r).await))); + } + Err(e) if attempt >= 3 => return Err(format!("Uploading to Stroke Cloud failed: {e}")), + _ => tokio::time::sleep(Duration::from_secs(2 * attempt)).await, + } + } + if filled < PART_BYTES { + break; + } + n += 1; + } + Ok(()) +} + +/// Object keys use only these characters, so the path needs no escaping. +fn key_safe(s: &str) -> String { + let out: String = s + .chars() + .map(|c| if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_' | '/') { c } else { '-' }) + .collect(); + out.trim_matches('/').to_string() +} + +/// The user's bucket: one PUT, the payload streamed from disk and left +/// unsigned (`UNSIGNED-PAYLOAD`, accepted by S3, R2, MinIO and the rest). +async fn upload_s3(path: &Path, job: &JobSpec, s: &S3Secret, bytes: u64) -> Result { + let bucket = job.s3_bucket.as_deref().filter(|b| !b.is_empty()).ok_or("The schedule has no S3 bucket.")?; + let region = if s.region.is_empty() { "us-east-1" } else { s.region.as_str() }; + let stamp = chrono::Utc::now().format("%Y-%m-%dT%H-%M-%SZ"); + let prefix = job.s3_prefix.as_deref().map(key_safe).filter(|p| !p.is_empty()); + let name = format!("{}-{stamp}.sql.gz", key_safe(&job.label).replace('/', "-")); + let key = match prefix { + Some(p) => format!("{p}/{name}"), + None => name, + }; + + // A custom endpoint (R2, MinIO, B2, Wasabi) is addressed path-style; AWS + // itself virtual-host style. + let (scheme_host, host, path_part) = match job.s3_endpoint.as_deref().map(str::trim).filter(|e| !e.is_empty()) { + Some(ep) => { + let ep = ep.trim_end_matches('/'); + let with_scheme = if ep.contains("://") { ep.to_string() } else { format!("https://{ep}") }; + let host = with_scheme.split("://").nth(1).unwrap_or_default().split('/').next().unwrap_or_default().to_string(); + (with_scheme, host, format!("/{bucket}/{key}")) + } + None => { + let host = format!("{bucket}.s3.{region}.amazonaws.com"); + (format!("https://{host}"), host, format!("/{key}")) + } + }; + + let amz_date = chrono::Utc::now().format("%Y%m%dT%H%M%SZ").to_string(); + let headers = vec![ + ("host".to_string(), host), + ("x-amz-content-sha256".to_string(), "UNSIGNED-PAYLOAD".to_string()), + ("x-amz-date".to_string(), amz_date.clone()), + ]; + let creds = crate::db::dynamodb::Creds { + access_key_id: s.access_key_id.clone(), + secret_access_key: s.secret_access_key.clone(), + session_token: None, + }; + let auth = crate::db::dynamodb::sign("PUT", &path_part, "", &headers, b"", &creds, region, "s3", &amz_date); + + let file = tokio::fs::File::open(path).await.map_err(|e| e.to_string())?; + let body = reqwest::Body::wrap_stream(tokio_util::io::ReaderStream::new(file)); + let resp = http() + .put(format!("{scheme_host}{path_part}")) + .header("authorization", auth) + .header("x-amz-content-sha256", "UNSIGNED-PAYLOAD") + .header("x-amz-date", amz_date) + .header("content-type", "application/gzip") + .header("content-length", bytes.to_string()) + .timeout(Duration::from_secs(3600)) + .body(body) + .send() + .await + .map_err(|e| format!("Couldn't reach the S3 endpoint: {e}"))?; + if !resp.status().is_success() { + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + let code = text.split("").nth(1).and_then(|s| s.split("").next()).unwrap_or(""); + let msg = text.split("").nth(1).and_then(|s| s.split("").next()).unwrap_or(""); + return Err(format!("S3 refused the upload ({status}{}{}).", if code.is_empty() { "" } else { ", " }, [code, msg].iter().filter(|s| !s.is_empty()).copied().collect::>().join(": "))); + } + Ok(key) +} + +/// Ids are UUIDs from the app, but escape anything else defensively. +fn enc(s: &str) -> String { + s.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_').collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn object_keys_need_no_escaping() { + assert_eq!(key_safe("prod db (main)"), "prod-db--main-"); + assert_eq!(key_safe("/backups/stroke/"), "backups/stroke"); + assert_eq!(enc("a1-b2_c3/../x"), "a1-b2_c3x"); + } + + #[test] + fn gzip_round_trip() { + let dir = std::env::temp_dir().join(format!("stroke-gz-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let src = dir.join("a.sql"); + let dst = dir.join("a.sql.gz"); + std::fs::write(&src, "CREATE TABLE t (x int);\n".repeat(1000)).unwrap(); + let n = gzip(&src, &dst).unwrap(); + assert!(n > 0 && n < 24_000); + let mut out = String::new(); + flate2::read::GzDecoder::new(std::fs::File::open(&dst).unwrap()).read_to_string(&mut out).unwrap(); + assert_eq!(out.len(), 24_000); + let _ = std::fs::remove_dir_all(dir); + } +} diff --git a/src-tauri/src/db/backup.rs b/src-tauri/src/db/backup.rs index ef0ce3ca..8ee75faa 100644 --- a/src-tauri/src/db/backup.rs +++ b/src-tauri/src/db/backup.rs @@ -39,7 +39,7 @@ use super::connection::{require_conn, ActiveConnection}; /// Only one backup/restore runs at a time in the UI, so a single flag suffices. static BACKUP_CANCELLED: AtomicBool = AtomicBool::new(false); -fn reset_cancel() { BACKUP_CANCELLED.store(false, Ordering::SeqCst); } +pub(crate) fn reset_cancel() { BACKUP_CANCELLED.store(false, Ordering::SeqCst); } fn is_cancelled() -> bool { BACKUP_CANCELLED.load(Ordering::SeqCst) } /// Request cancellation of the running backup/restore. The loops stop at the @@ -213,7 +213,7 @@ impl Reporter { fn restore(app: &AppHandle) -> Self { Self { app: Some(app.clone()), log_event: "restore-log", progress_event: "restore-progress" } } - #[cfg(test)] + /// No events: scheduled backups run in the background, away from the page's log. pub(crate) fn silent() -> Self { Self { app: None, log_event: "", progress_event: "" } } @@ -460,7 +460,7 @@ pub async fn backup_export( run_export(&rep, conn, schema, tables, &opts, path).await } -async fn run_export( +pub(crate) async fn run_export( rep: &Reporter, conn: ActiveConnection, schema: Option, diff --git a/src-tauri/src/db/dynamodb.rs b/src-tauri/src/db/dynamodb.rs index fa85dd71..36d9010f 100644 --- a/src-tauri/src/db/dynamodb.rs +++ b/src-tauri/src/db/dynamodb.rs @@ -236,7 +236,9 @@ fn sha_hex(data: &[u8]) -> String { } /// The `Authorization` header for one request. `headers` must already hold -/// every header to sign (host and x-amz-date included), names lowercase. +/// every header to sign (host and x-amz-date included), names lowercase. An +/// `x-amz-content-sha256` header (S3's `UNSIGNED-PAYLOAD`, say) is used as the +/// payload hash instead of hashing `body`. #[allow(clippy::too_many_arguments)] pub fn sign( method: &str, @@ -256,7 +258,12 @@ pub fn sign( hs.sort(); let canonical_headers: String = hs.iter().map(|(k, v)| format!("{k}:{v}\n")).collect(); let signed: String = hs.iter().map(|(k, _)| k.as_str()).collect::>().join(";"); - let canonical = format!("{method}\n{path}\n{query}\n{canonical_headers}\n{signed}\n{}", sha_hex(body)); + let payload_hash = hs + .iter() + .find(|(k, _)| k == "x-amz-content-sha256") + .map(|(_, v)| v.clone()) + .unwrap_or_else(|| sha_hex(body)); + let canonical = format!("{method}\n{path}\n{query}\n{canonical_headers}\n{signed}\n{payload_hash}"); let date = &amz_date[..8]; let scope = format!("{date}/{region}/{service}/aws4_request"); let to_sign = format!("AWS4-HMAC-SHA256\n{amz_date}\n{scope}\n{}", sha_hex(canonical.as_bytes())); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e620ba5a..6a96ba07 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -10,6 +10,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; mod account; +mod cloud_backup; mod shares; mod nightly; mod agents; @@ -935,6 +936,13 @@ pub fn run() { db::parked::close_parked, db::parked::parked_connections, commands::export_parquet, + cloud_backup::backup_jobs_list, + cloud_backup::backup_job_save, + cloud_backup::backup_job_patch, + cloud_backup::backup_job_delete, + cloud_backup::backup_s3_save, + cloud_backup::backup_s3_status, + cloud_backup::backup_job_run, ]) .build(tauri::generate_context!()) .expect("error while running tauri application") diff --git a/src/lib/components/BackupPage.svelte b/src/lib/components/BackupPage.svelte index 0ccb57b2..34899f17 100644 --- a/src/lib/components/BackupPage.svelte +++ b/src/lib/components/BackupPage.svelte @@ -1,6 +1,8 @@ + +{#snippet message(/** @type {string} */ title, /** @type {string} */ body)} +
+ +

{title}

+

{body}

+
+{/snippet} + +{#if !$isSignedIn} + {@render message('Sign in for scheduled backups', 'Scheduled backups come with Stroke Pro on your account.')} +{:else if !$hasPro || overview?.pro === false} +
+ +

Scheduled backups are part of Stroke Pro

+

Back up up to 10 databases on a schedule, to Stroke Cloud or your own S3 bucket.

+ +
+{:else if !connection} + {@render message('Save this connection first', 'Scheduled backups run on saved connections, so Stroke can open them again later.')} +{:else if !supported} + {@render message('Not available for this database', 'Scheduled backups support PostgreSQL, MySQL, SQLite, Cloudflare D1, Turso/libSQL, DuckDB and SQL Server.')} +{:else} +
+
+
+
+

Scheduled backups

+

+ Stroke runs them while it's open on a computer with this connection. A run missed while it was closed happens the next time it opens. +

+ {#if overview} +

+ {overview.jobs.length} of {overview.max_jobs} schedules · {formatBytes(overview.used_bytes)} of {formatBytes(overview.quota_bytes)} on Stroke Cloud +

+ {/if} +
+ +
+ + {#if $backupLoadError && !overview} +

{$backupLoadError}

+ {/if} + +
+

This database

+ {#if !overview} +
+ {:else if jobs.length === 0 && !formOpen} +

No schedule yet.

+ {/if} + {#each jobs as job (job.id)} + {@const last = job.runs[0]} +
+ {#if job.destination === 'stroke'}{:else}{/if} +
+

+ {describeCron(job.cron)} + {#if !job.enabled}Paused{/if} +

+

+ {job.destination === 'stroke' ? `Stroke Cloud, keeps ${job.keep}` : `S3 bucket ${job.s3_bucket}`} + {#if $runningJobId === job.id} · Running now + {:else if last} · Last {last.status === 'failed' ? 'failed' : last.status === 'running' ? 'started' : 'ran'} {when(last.started_at)} + {/if} + {#if job.enabled && job.next_run_at} · Next {when(job.next_run_at)}{/if} +

+ {#if last?.status === 'failed' && last.error} +

{last.error}

+ {/if} +
+
+ {#if $runningJobId === job.id} + + {:else} + + {/if} + + + {#if confirmDelete === job.id} + + + {:else} + + {/if} +
+
+ {/each} + + {#if overview && !formOpen} +
+ + {#if atLimit}

You have {overview.max_jobs} schedules, the most Pro allows. Delete one to add another.

{/if} +
+ {/if} +
+ + {#if formOpen} +
+
+
+ + +
+ {#if preset === 'daily' || preset === 'weekly'} +
+ + +
+ {/if} + {#if preset === 'weekly'} +
+ + ({ value: String(i), label: d }))} /> +
+ {/if} + {#if preset === 'custom'} +
+ + +
+ {/if} +
+

{describeCron(cron)}, {timezone} time. At most once an hour.

+ +
+ Save to + {#each [{ v: 'stroke', icon: Cloud, title: 'Stroke Cloud', body: 'Stored for you, 5 GB per account. Download from stroke.click.' }, { v: 's3', icon: HardDrive, title: 'My S3 bucket', body: 'AWS S3, Cloudflare R2, MinIO, Backblaze B2 or any S3-compatible storage.' }] as opt (opt.v)} + + {/each} +
+ + {#if destination === 'stroke'} +
+ + ({ value: String(n), label: `${n} backups` }))} /> +
+ {:else} +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+

The keys stay in this computer's keychain. stroke.click only sees the bucket name.

+
+ {/if} + + {#if formError}{/if} +
+ + +
+
+ {/if} + + {#if otherJobs.length} +
+

Other databases

+
    + {#each otherJobs as job (job.id)} +
  • + {job.label} + {describeCron(job.cron)}{job.enabled ? '' : ' · Paused'} +
  • + {/each} +
+
+ {/if} +
+
+{/if} diff --git a/src/lib/components/StudioShell.svelte b/src/lib/components/StudioShell.svelte index f1d4fa88..863b3fed 100644 --- a/src/lib/components/StudioShell.svelte +++ b/src/lib/components/StudioShell.svelte @@ -10654,7 +10654,7 @@ let rowSearch = $state('') > {#await import('./BackupPage.svelte')}{:then { default: BackupPage }} - ({ name: t.name, rowCount: t.rowCount }))} onrestored={async () => { await loadSchemas(); await loadTables({ force: true }); bumpObjects() }} /> + c.id === activeConnectionId) ?? null} tables={tables.map((t) => ({ name: t.name, rowCount: t.rowCount }))} onrestored={async () => { await loadSchemas(); await loadTables({ force: true }); bumpObjects() }} /> {/await} diff --git a/src/lib/stores/scheduled-backups.js b/src/lib/stores/scheduled-backups.js new file mode 100644 index 00000000..29a09040 --- /dev/null +++ b/src/lib/stores/scheduled-backups.js @@ -0,0 +1,209 @@ +import { invoke } from '@tauri-apps/api/core' +import { writable, get } from 'svelte/store' +import { Cron } from 'croner' +import { loadSavedConnections } from './connections.js' +import { hasPro } from './license.js' +import { isSignedIn } from './account.js' + +/** + * Scheduled backups (Pro). Jobs live on stroke.click; this app runs them, + * because only it has the database credentials. Every minute the scheduler + * looks for a job whose schedule tick has passed (or a "Run now" from the + * website) on a connection saved on this device, claims the tick on the + * server (so two devices never both run it), and runs it in Rust + * (src-tauri/src/cloud_backup.rs). + * + * @typedef {{ id: string, slot: string, status: 'running'|'success'|'failed', destination: 'stroke'|'s3', + * object_key: string|null, bytes: number, tables: number, rows: number, error: string|null, + * device_name: string|null, started_at: number, finished_at: number|null }} BackupRun + * @typedef {{ id: string, connection_id: string, label: string, driver: string, cron: string, timezone: string, + * destination: 'stroke'|'s3', s3_bucket: string|null, s3_endpoint: string|null, s3_prefix: string|null, + * keep: number, enabled: boolean, run_requested_at: number|null, created_at: number, + * next_run_at: number|null, runs: BackupRun[] }} BackupJob + * @typedef {{ jobs: BackupJob[], max_jobs: number, used_bytes: number, quota_bytes: number, pro: boolean }} BackupOverview + */ + +/** Engines the backup engine can dump. Keep in sync with stroke-web's DRIVERS. */ +export const SCHEDULE_DRIVERS = new Set(['postgres', 'mysql', 'sqlite', 'd1', 'libsql', 'duckdb', 'mssql']) + +/** @type {import('svelte/store').Writable} */ +export const backupOverview = writable(null) +/** @type {import('svelte/store').Writable} */ +export const backupLoadError = writable(null) +/** The job running on this device right now, if any. */ +export const runningJobId = writable(/** @type {string|null} */ (null)) + +/** @param {BackupOverview} o */ +const setOverview = (o) => { + backupOverview.set(o) + backupLoadError.set(null) + return o +} + +export async function refreshBackupJobs() { + try { + return setOverview(/** @type {BackupOverview} */ (await invoke('backup_jobs_list'))) + } catch (e) { + backupLoadError.set(String(e)) + return null + } +} + +/** @param {Record} job */ +export async function saveBackupJob(job) { + return setOverview(/** @type {BackupOverview} */ (await invoke('backup_job_save', { job }))) +} + +/** @param {string} id @param {{ enabled?: boolean, run_now?: boolean }} patch */ +export async function patchBackupJob(id, patch) { + const o = setOverview(/** @type {BackupOverview} */ (await invoke('backup_job_patch', { id, patch }))) + if (patch.run_now) void tick() + return o +} + +/** @param {string} id */ +export async function deleteBackupJob(id) { + return setOverview(/** @type {BackupOverview} */ (await invoke('backup_job_delete', { id }))) +} + +/** + * @param {string} jobId + * @param {{ accessKeyId: string, secretAccessKey: string, region: string }} keys + */ +export async function saveS3Keys(jobId, keys) { + await invoke('backup_s3_save', { jobId, accessKeyId: keys.accessKeyId, secretAccessKey: keys.secretAccessKey, region: keys.region }) +} + +/** The region saved with this device's S3 keys for the job, or null when it has none. */ +export async function s3KeysRegion(/** @type {string} */ jobId) { + try { + return /** @type {string|null} */ (await invoke('backup_s3_status', { jobId })) + } catch { + return null + } +} + +/** + * The tick a job should run for now, or null. A "Run now" request wins. + * Missed ticks (the app was closed) collapse into one run for the latest. + * @param {BackupJob} job + * @param {number} [now] + */ +export function dueSlot(job, now = Date.now()) { + // "Run now" works on a paused job too: it's an explicit ask. + if (job.run_requested_at) return `manual:${job.run_requested_at}` + if (!job.enabled) return null + /** @type {Cron} */ + let cron + try { + cron = new Cron(job.cron, { timezone: job.timezone || 'UTC', paused: true }) + } catch { + return null + } + const lastTick = Math.max(0, ...job.runs.filter((r) => !r.slot.startsWith('manual:')).map((r) => Date.parse(r.slot) || 0)) + const baseline = Math.max(job.created_at, lastTick) + let tick = cron.nextRun(new Date(baseline)) + if (!tick || tick.getTime() > now) return null + for (let i = 0; i < 5000; i++) { + const next = cron.nextRun(tick) + if (!next || next.getTime() > now) break + tick = next + } + return tick.toISOString() +} + +/** Plain words for the common schedules; the cron itself otherwise. @param {string} cron */ +export function describeCron(cron) { + const [min, hour, dom, mon, dow] = String(cron).trim().split(/\s+/) + const num = /^\d+$/ + const at = (/** @type {string} */ h, /** @type {string} */ m) => `${h.padStart(2, '0')}:${m.padStart(2, '0')}` + const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] + if (num.test(min) && hour === '*' && dom === '*' && mon === '*' && dow === '*') return `Every hour at :${min.padStart(2, '0')}` + if (num.test(min) && /^\*\/\d+$/.test(hour ?? '') && dom === '*' && mon === '*' && dow === '*') return `Every ${hour.slice(2)} hours` + if (num.test(min) && num.test(hour) && mon === '*') { + if (dom === '*' && dow === '*') return `Every day at ${at(hour, min)}` + if (dom === '*' && num.test(dow) && days[Number(dow) % 7]) return `Every ${days[Number(dow) % 7]} at ${at(hour, min)}` + if (num.test(dom) && dow === '*') return `Day ${dom} of every month at ${at(hour, min)}` + } + return cron +} + +/** @param {number} n */ +export function formatBytes(n) { + if (n < 1024) return `${n} B` + const units = ['KB', 'MB', 'GB', 'TB'] + let v = n / 1024 + let i = 0 + while (v >= 1024 && i < units.length - 1) { v /= 1024; i++ } + return `${v < 10 ? v.toFixed(1) : Math.round(v)} ${units[i]}` +} + +// ── Scheduler ──────────────────────────────────────────────────────────────── + +const TICK_MS = 60_000 +/** Re-read the jobs at most this often (a "Run now" from the website shows up within it). */ +const REFRESH_MS = 2 * 60_000 +/** Ticks this device already tried (ran, failed, or lost to another device). */ +const handled = new Set() +let started = false +let busy = false +let lastRefresh = 0 + +async function tick() { + if (busy || !get(isSignedIn) || !get(hasPro)) return + busy = true + try { + if (Date.now() - lastRefresh > REFRESH_MS || !get(backupOverview)) { + lastRefresh = Date.now() + await refreshBackupJobs() + } + const overview = get(backupOverview) + if (!overview?.pro) return + const saved = new Map(loadSavedConnections().map((c) => [c.id, c])) + for (const job of overview.jobs) { + const conn = saved.get(job.connection_id) + // Another device's connection: that device runs it. + if (!conn) continue + const slot = dueSlot(job) + if (!slot || handled.has(`${job.id}|${slot}`)) continue + handled.add(`${job.id}|${slot}`) + await runJob(job, conn, slot) + lastRefresh = Date.now() + await refreshBackupJobs() + } + } finally { + busy = false + } +} + +/** + * @param {BackupJob} job + * @param {object} conn the saved connection, secrets included + * @param {string} slot + */ +async function runJob(job, conn, slot) { + const { toast } = await import('$lib/components/ui/sonner/toast.svelte.js') + runningJobId.set(job.id) + try { + const out = /** @type {{ status: string, message: string, bytes: number }} */ ( + await invoke('backup_job_run', { job, config: conn, slot }) + ) + if (out.status === 'success') { + toast.success(`Backed up ${job.label}`, { description: `${out.message} ${formatBytes(out.bytes)} compressed.` }) + } else if (out.status === 'failed') { + toast.error(`Scheduled backup of ${job.label} failed`, { description: out.message, duration: 15000 }) + } + } catch (e) { + toast.error(`Scheduled backup of ${job.label} failed`, { description: String(e), duration: 15000 }) + } finally { + runningJobId.set(null) + } +} + +/** Start the scheduler once, after sign-in has been settled. */ +export function startBackupScheduler() { + if (started) return + started = true + setTimeout(() => void tick(), 15_000) + setInterval(() => void tick(), TICK_MS) +} diff --git a/src/lib/stores/scheduled-backups.test.js b/src/lib/stores/scheduled-backups.test.js new file mode 100644 index 00000000..3d31e41f --- /dev/null +++ b/src/lib/stores/scheduled-backups.test.js @@ -0,0 +1,43 @@ +import { describe, it, expect, vi } from 'vitest' + +vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn() })) + +const { dueSlot, describeCron } = await import('./scheduled-backups.js') + +/** @param {object} over */ +const job = (over = {}) => /** @type {any} */ ({ + id: 'j', cron: '0 3 * * *', timezone: 'UTC', enabled: true, run_requested_at: null, + created_at: Date.parse('2026-11-01T00:00:00Z'), runs: [], ...over, +}) + +describe('dueSlot', () => { + const now = Date.parse('2026-11-18T05:00:00Z') + it('missed ticks collapse into the latest one', () => { + expect(dueSlot(job(), now)).toBe('2026-11-18T03:00:00.000Z') + }) + it('a tick already run is not due again', () => { + const runs = [{ slot: '2026-11-18T03:00:00.000Z' }] + expect(dueSlot(job({ runs }), now)).toBeNull() + }) + it('nothing is due before the first tick after creation', () => { + expect(dueSlot(job({ created_at: Date.parse('2026-11-18T04:00:00Z') }), now)).toBeNull() + }) + it('paused jobs only run when asked', () => { + expect(dueSlot(job({ enabled: false }), now)).toBeNull() + expect(dueSlot(job({ enabled: false, run_requested_at: 42 }), now)).toBe('manual:42') + }) + it('reads the schedule in its time zone', () => { + // 03:00 in Kolkata is 21:30 UTC the day before. + expect(dueSlot(job({ timezone: 'Asia/Kolkata' }), now)).toBe('2026-11-17T21:30:00.000Z') + }) +}) + +describe('describeCron', () => { + it('names the common schedules', () => { + expect(describeCron('0 3 * * *')).toBe('Every day at 03:00') + expect(describeCron('30 22 * * 1')).toBe('Every Monday at 22:30') + expect(describeCron('0 */6 * * *')).toBe('Every 6 hours') + expect(describeCron('0 * * * *')).toBe('Every hour at :00') + expect(describeCron('5 4 1-7 * *')).toBe('5 4 1-7 * *') + }) +}) From f2378ed9c24732b662272872d2e9ac9aeee953e9 Mon Sep 17 00:00:00 2001 From: broisnees Date: Wed, 18 Nov 2026 05:45:10 +0000 Subject: [PATCH 2/2] Report the app version on account checks; fix lockfile --- src-tauri/Cargo.lock | 1 + src-tauri/src/account.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 0605fa7f..7290c566 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3686,6 +3686,7 @@ version = "1.10506.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "113d1c26c9f518405a17b5729bb6ab6abf029fdc46626406922cf9a8dc9b4609" dependencies = [ + "cc", "flate2", "pkg-config", "serde", diff --git a/src-tauri/src/account.rs b/src-tauri/src/account.rs index 514bd532..f7d7be4e 100644 --- a/src-tauri/src/account.rs +++ b/src-tauri/src/account.rs @@ -479,8 +479,16 @@ async fn me_text(req: reqwest::RequestBuilder) -> Result { /// Asks for the v3 account trial: one fresh trial per account, separate from 2.x. const TRIAL_SCHEME: (&str, &str) = ("X-Stroke-Trial", "account-v3"); +/// This app's version, sent with every `/api/me` so the account's device list +/// shows what each computer runs now. Set on the first refresh. +static APP_VERSION: OnceLock = OnceLock::new(); + pub async fn fetch_me(base: &str, token: &str) -> Result { - me_text(http().get(format!("{base}/api/me")).bearer_auth(token).header(TRIAL_SCHEME.0, TRIAL_SCHEME.1)).await + let mut req = http().get(format!("{base}/api/me")).bearer_auth(token).header(TRIAL_SCHEME.0, TRIAL_SCHEME.1); + if let Some(v) = APP_VERSION.get() { + req = req.header("X-Stroke-Version", v.as_str()); + } + me_text(req).await } pub async fn remote_sign_out(base: &str, token: &str) { @@ -774,6 +782,7 @@ fn data_dir(app: &tauri::AppHandle) -> Result { /// failure keeps the cache (trusted until `valid_until`). async fn refresh(app: &tauri::AppHandle, dir: &Path, token: &str) -> AccountStatus { let base = base_url(); + APP_VERSION.get_or_init(|| app.package_info().version.to_string()); match fetch_me(&base, token).await { Ok(raw) => match save_me(dir, raw) { Ok(_) => build_status(dir, true),