feat: use Restic for encrypted incremental backups
Keep legacy restores readable while using Restic for new captures, scoped Wormhole repositories, and chunk garbage collection.
This commit is contained in:
@@ -9,7 +9,13 @@ use std::sync::Arc;
|
||||
|
||||
pub fn post_json(url: &str, body: &serde_json::Value, headers: &[(&str, &str)]) -> Result<String> {
|
||||
let payload = serde_json::to_string(body)?;
|
||||
request("POST", url, Some(&payload), Some("application/json"), headers)
|
||||
request(
|
||||
"POST",
|
||||
url,
|
||||
Some(&payload),
|
||||
Some("application/json"),
|
||||
headers,
|
||||
)
|
||||
}
|
||||
|
||||
fn request(
|
||||
@@ -42,8 +48,15 @@ fn request(
|
||||
stream.write_all(req.as_bytes())?;
|
||||
let mut buf = String::new();
|
||||
stream.read_to_string(&mut buf)?;
|
||||
let body_start = buf.find("\r\n\r\n").map(|i| i + 4).unwrap_or(0);
|
||||
Ok(buf[body_start..].trim().to_string())
|
||||
let (response_headers, body) = buf.split_once("\r\n\r\n").context("invalid hub response")?;
|
||||
let status = response_headers
|
||||
.lines()
|
||||
.next()
|
||||
.and_then(|line| line.split_whitespace().nth(1))
|
||||
.context("missing hub response status")?
|
||||
.parse::<u16>()?;
|
||||
anyhow::ensure!((200..300).contains(&status), "hub returned HTTP {status}");
|
||||
Ok(body.trim().to_string())
|
||||
}
|
||||
|
||||
trait ReadWrite: Read + Write {}
|
||||
|
||||
@@ -81,6 +81,14 @@ async fn beam_once(cli: &Cli) -> Result<()> {
|
||||
let token = cli.upload_token.clone().context("TESSERA_UPLOAD_TOKEN")?;
|
||||
let orbit_id = cli.orbit_id.clone().context("TESSERA_ORBIT_ID")?;
|
||||
|
||||
if let Some(repository) = std::env::var_os("TESSERA_RESTIC_REPOSITORY") {
|
||||
let repository = repository
|
||||
.to_str()
|
||||
.context("TESSERA_RESTIC_REPOSITORY must be UTF-8")?;
|
||||
let id = uuid::Uuid::parse_str(&orbit_id).context("invalid TESSERA_ORBIT_ID")?;
|
||||
return beam_restic(repository, &hub, &token, id).await;
|
||||
}
|
||||
|
||||
let config = serde_json::json!({ "paths": ["./"] });
|
||||
let provider = provider_from_config(&ProviderKind::Filesystem, &config)?;
|
||||
let capture = provider.capture().await?;
|
||||
@@ -112,3 +120,55 @@ async fn beam_once(cli: &Cli) -> Result<()> {
|
||||
info!("beamed {} bytes via wormhole session {session_id}", capture.payload.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn beam_restic(repository: &str, hub: &str, token: &str, orbit_id: uuid::Uuid) -> Result<()> {
|
||||
let authority = repository
|
||||
.strip_prefix("rest:https://")
|
||||
.and_then(|url| url.split('/').next());
|
||||
anyhow::ensure!(
|
||||
authority.is_some_and(|host| !host.is_empty() && !host.contains('@')),
|
||||
"TESSERA_RESTIC_REPOSITORY must be a rest:https:// URL without embedded credentials"
|
||||
);
|
||||
let output = tokio::process::Command::new("restic")
|
||||
.env("RESTIC_REPOSITORY", repository)
|
||||
.env_remove("RESTIC_REPOSITORY_FILE")
|
||||
.args(["backup", "--json", "--quiet", "--force", "--", "."])
|
||||
.stdin(std::process::Stdio::null())
|
||||
.kill_on_drop(true)
|
||||
.output()
|
||||
.await
|
||||
.context("starting restic (install restic on PATH)")?;
|
||||
anyhow::ensure!(
|
||||
output.status.success(),
|
||||
"restic backup failed ({}): {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr).trim()
|
||||
);
|
||||
let summary: serde_json::Value = serde_json::from_str(
|
||||
std::str::from_utf8(&output.stdout)?
|
||||
.lines()
|
||||
.last()
|
||||
.context("missing restic summary")?,
|
||||
)?;
|
||||
let snapshot = summary["snapshot_id"]
|
||||
.as_str()
|
||||
.context("missing restic snapshot ID")?;
|
||||
let body = serde_json::json!({ "orbit_id": orbit_id, "snapshot_id": snapshot });
|
||||
let reply = http::post_json(
|
||||
&format!("{hub}/api/v1/wormhole/restic/commit"),
|
||||
&body,
|
||||
&[("x-tessera-upload-token", token)],
|
||||
)
|
||||
.with_context(|| format!("register Restic snapshot {snapshot}"))?;
|
||||
let reply: serde_json::Value = serde_json::from_str(&reply)?;
|
||||
let registered = reply["snapshot_id"]
|
||||
.as_str()
|
||||
.context("hub did not register the Restic snapshot")?;
|
||||
info!(
|
||||
snapshot,
|
||||
registered,
|
||||
stored_bytes = summary["data_added_packed"].as_u64(),
|
||||
"committed Restic backup"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user