25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(backup): remove emptied run directories when pruning old backups (#12902)
Jeff090da7eafff2
diff --git a/crates/dbx-core/src/scheduled_backup/service.rs b/crates/dbx-core/src/scheduled_backup/service.rsindex f5b936fef4..582664b5f0 100644--- a/crates/dbx-core/src/scheduled_backup/service.rs+++ b/crates/dbx-core/src/scheduled_backup/service.rs@@ -152,6 +152,7 @@ impl BackupService {let others = self.store.snapshot().await?.runs;let mut retained = Vec::new();let mut errors = Vec::new();+ let mut emptied = Vec::new();for file in &run.files {if others.iter().any(|r| r.id != run.id && r.files.iter().any(|f| f.file_path == file.file_path)) {continue;@@ -167,20 +168,24 @@ impl BackupService {continue;}match self.checked_file(run, path).await {- Ok(path) => {- if let Err(e) = tokio::fs::remove_file(path).await {- if e.kind() != std::io::ErrorKind::NotFound {- retained.push(file.clone());- errors.push(e.to_string());- }+ Ok(path) => match tokio::fs::remove_file(&path).await {+ Err(e) if e.kind() != std::io::ErrorKind::NotFound => {+ retained.push(file.clone());+ errors.push(e.to_string());}- }+ _ => emptied.extend(path.parent().map(Path::to_path_buf)),+ },Err(error) => {retained.push(file.clone());errors.push(error);}}}+ if let Ok(root) = self.validate_destination(&run.destination_directory) {+ for directory in emptied {+ remove_empty_directories(&directory, &root).await;+ }+ }run.files = retained;if errors.is_empty() {Ok(())@@ -210,3 +215,17 @@ impl BackupService {}}}++/// Removes the per-run directories left behind once their backup files are gone.+/// `remove_dir` refuses non-empty directories, so folders still holding other runs'+/// backups or the user's own files are kept, and the destination root is never removed.+async fn remove_empty_directories(mut directory: &Path, root: &Path) {+ while directory != root && directory.starts_with(root) {+ let is_directory = tokio::fs::symlink_metadata(directory).await.is_ok_and(|m| m.is_dir());+ if !is_directory || tokio::fs::remove_dir(directory).await.is_err() {+ break;+ }+ let Some(parent) = directory.parent() else { break };+ directory = parent;+ }+}diff --git a/crates/dbx-core/src/scheduled_backup/tests.rs b/crates/dbx-core/src/scheduled_backup/tests.rsindex 6674f2872b..6575a90d10 100644--- a/crates/dbx-core/src/scheduled_backup/tests.rs+++ b/crates/dbx-core/src/scheduled_backup/tests.rs@@ -209,6 +209,45 @@ async fn deletion_only_removes_recorded_files_within_the_backup_root() {assert!(service.store.snapshot().await.unwrap().runs.is_empty());}+#[tokio::test]+async fn deletion_removes_emptied_run_directories_but_keeps_shared_ones_and_the_root() {+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path().join("backups");+ let run_dir = root.join("dbx-backup__daily__20261020023000__abcd1234");+ let nested = root.join("2026").join("10").join("20");+ let shared = root.join("shared");+ for path in [&run_dir, &nested, &shared] {+ std::fs::create_dir_all(path).unwrap();+ }+ let user_file = shared.join("notes.txt");+ std::fs::write(&user_file, "keep").unwrap();+ let files = [run_dir.join("app.sql"), nested.join("app.sql.gz"), shared.join("app.sql"), root.join("flat.sql")];+ for file in &files {+ std::fs::write(file, "SELECT 1;").unwrap();+ }+ let service = service(dir.path(), Some(root.clone())).await;+ let mut run = service.store.enqueue(request(schedule(&root).config)).await.unwrap();+ service.store.claim().await.unwrap();+ run.status = "success".into();+ run.files = files+ .iter()+ .map(|file| BackupFile {+ database: "app".into(),+ schema: "app".into(),+ display_name: "app".into(),+ file_path: file.to_string_lossy().into_owned(),+ owned: true,+ })+ .collect();+ service.store.finish(run.clone()).await.unwrap();+ service.delete_runs(vec![run.id]).await.unwrap();+ assert!(files.iter().all(|file| !file.exists()));+ assert!(!run_dir.exists());+ assert!(!root.join("2026").exists());+ assert!(user_file.exists());+ assert!(root.is_dir());+}+#[tokio::test]async fn draining_transfers_leadership_without_cancelling_and_cancel_survives_reopen() {let dir = tempfile::tempdir().unwrap();@@ -405,7 +444,7 @@ async fn live_mysql_worker_scenario() {plan.config.table_filter_mode = "include".into();plan.config.table_patterns = vec!["chosen".into()];service.command(BackupCommand::Save { schedule: plan }).await?;- let mut previous = None;+ let mut previous: Option<std::path::PathBuf> = None;for _ in 0..2 {let run = service.store@@ -443,6 +482,10 @@ async fn live_mysql_worker_scenario() {}).await.map_err(|_| "Retention did not remove the old file")?;+ let previous_directory = previous.parent().unwrap();+ assert_ne!(previous_directory, path.parent().unwrap());+ assert!(!previous_directory.exists(), "Retention left the old run directory behind");+ assert!(dir.path().is_dir());}previous = Some(path);}