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:
2026-11-10 10:48:05 +07:00
parent 95778d54e4
commit 749c4cc828
15 changed files with 955 additions and 119 deletions

View File

@@ -49,6 +49,50 @@ impl PostgresProvider {
let config: PostgresConfig = serde_json::from_value(config.clone())?;
Ok(Self { config })
}
pub async fn dump_to(&self, file: fs::File) -> Result<serde_json::Value> {
self.dump(Stdio::from(file.try_clone()?)).await?;
anyhow::ensure!(file.metadata()?.len() > 0, "pg_dump produced an empty file");
Ok(self.restore_hints())
}
async fn dump(&self, stdout: Stdio) -> Result<std::process::Output> {
let target = parse_postgres_url(&self.config.connection_url)?;
let args = dump_args(&self.config.format)?;
let pgpass = write_pgpass(&target)?;
let mut cmd = Command::new("pg_dump");
let output = cmd
.args(args)
.env("PGPASSFILE", &pgpass.path)
.env("PGHOST", &target.host)
.env("PGPORT", &target.port)
.env("PGUSER", &target.user)
.env("PGDATABASE", &target.dbname)
.env_remove("PGPASSWORD")
.stdout(stdout)
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.context("pg_dump failed to start")?
.wait_with_output()
.await
.context("waiting for pg_dump")?;
anyhow::ensure!(
output.status.success(),
"pg_dump failed: {}",
String::from_utf8_lossy(&output.stderr)
);
Ok(output)
}
fn restore_hints(&self) -> serde_json::Value {
let command = if self.config.format == "plain" {
"psql -d \"$DATABASE_URL\" -f dump.file"
} else {
"pg_restore -d \"$DATABASE_URL\" dump.file"
};
json!({ "format": self.config.format, "restore_command_hint": command })
}
}
fn percent_decode(input: &str) -> Result<String> {
@@ -162,31 +206,11 @@ impl BackupProvider for PostgresProvider {
}
async fn capture(&self) -> Result<CaptureResult> {
let target = parse_postgres_url(&self.config.connection_url)?;
let args = dump_args(&self.config.format)?;
let pgpass = write_pgpass(&target)?;
let mut cmd = Command::new("pg_dump");
cmd.args(&args);
cmd.env("PGPASSFILE", &pgpass.path);
cmd.env("PGHOST", &target.host);
cmd.env("PGPORT", &target.port);
cmd.env("PGUSER", &target.user);
cmd.env("PGDATABASE", &target.dbname);
cmd.env_remove("PGPASSWORD");
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
let output = cmd.output().await.context("pg_dump failed to start")?;
drop(pgpass);
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("pg_dump failed: {stderr}");
}
let output = self.dump(Stdio::piped()).await?;
Ok(CaptureResult {
payload: output.stdout,
labels: vec!["postgres".into()],
restore_hints: json!({
"format": self.config.format,
"restore_command_hint": "pg_restore -d \"$DATABASE_URL\" dump.file"
}),
restore_hints: self.restore_hints(),
})
}
}