[pbs-devel] [PATCH v3 proxmox-backup 17/20] api: admin: add endpoint to clear trashed items from group

Christian Ebner c.ebner at proxmox.com
Tue May 13 15:52:44 CEST 2025


Allows to remove only the trashed snapshot items of a backup group,
including the backup group itself if all the contents have been
cleared. Instead of using the backup group delete stats to determine
whether the group directory should be cleaned up or not, use a local
variable instead, as the removed trash is otherwise not correctly
accounted for.

This allows to manually clear trashed groups from the UI for
convenience.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 pbs-datastore/src/backup_info.rs | 14 ++++++-
 pbs-datastore/src/datastore.rs   | 17 +++++++-
 src/api2/admin/datastore.rs      | 70 ++++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/pbs-datastore/src/backup_info.rs b/pbs-datastore/src/backup_info.rs
index f334600c7..85d856888 100644
--- a/pbs-datastore/src/backup_info.rs
+++ b/pbs-datastore/src/backup_info.rs
@@ -242,7 +242,11 @@ impl BackupGroup {
     ///
     /// Returns `BackupGroupDeleteStats`, containing the number of deleted snapshots
     /// and number of protected snaphsots, which therefore were not removed.
-    pub fn destroy(&self, skip_trash: bool) -> Result<BackupGroupDeleteStats, Error> {
+    pub fn destroy(
+        &self,
+        skip_trash: bool,
+        trash_only: bool,
+    ) -> Result<BackupGroupDeleteStats, Error> {
         let _guard = self
             .lock()
             .with_context(|| format!("while destroying group '{self:?}'"))?;
@@ -250,10 +254,16 @@ impl BackupGroup {
 
         log::info!("removing backup group {:?}", path);
         let mut delete_stats = BackupGroupDeleteStats::default();
+        let mut cleanup_group_dir = true;
         for snap in self.iter_snapshots()? {
             let snap = snap?;
             if snap.is_protected() {
                 delete_stats.increment_protected_snapshots();
+                cleanup_group_dir = false;
+                continue;
+            }
+            if trash_only && !snap.is_trash() {
+                cleanup_group_dir = false;
                 continue;
             }
             snap.destroy(false, skip_trash)?;
@@ -262,7 +272,7 @@ impl BackupGroup {
 
         // Note: make sure the old locking mechanism isn't used as `remove_dir_all` is not safe in
         // that case
-        if delete_stats.all_removed() && !*OLD_LOCKING {
+        if cleanup_group_dir && !*OLD_LOCKING {
             if skip_trash {
                 self.remove_group_dir()?;
             } else {
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index 574d6ec26..fde0096bf 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -581,7 +581,7 @@ impl DataStore {
         let mut stats = BackupGroupDeleteStats::default();
 
         for group in self.iter_backup_groups(ns.to_owned())? {
-            let delete_stats = group?.destroy(true)?;
+            let delete_stats = group?.destroy(true, false)?;
             stats.add(&delete_stats);
             removed_all_groups = removed_all_groups && delete_stats.all_removed();
         }
@@ -675,7 +675,20 @@ impl DataStore {
     ) -> Result<BackupGroupDeleteStats, Error> {
         let backup_group = self.backup_group(ns.clone(), backup_group.clone());
 
-        backup_group.destroy(skip_trash)
+        backup_group.destroy(skip_trash, false)
+    }
+
+    /// Remove snapshots marked as trash from a backup group, including the group if it is empty
+    /// afterwards.
+    ///
+    /// Returns `BackupGroupDeleteStats`, containing the number of deleted snapshots.
+    pub fn clear_backup_group(
+        self: &Arc<Self>,
+        ns: &BackupNamespace,
+        backup_group: &pbs_api_types::BackupGroup,
+    ) -> Result<BackupGroupDeleteStats, Error> {
+        let backup_group = self.backup_group(ns.clone(), backup_group.clone());
+        backup_group.destroy(true, true)
     }
 
     /// Remove a backup directory including all content
diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index bc2d51612..f97aeb5cb 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -2868,6 +2868,72 @@ fn do_recover_snapshot(snapshot_dir: &BackupDir) -> Result<(), Error> {
     Ok(())
 }
 
+#[api(
+    input: {
+        properties: {
+            store: { schema: DATASTORE_SCHEMA },
+            ns: {
+                type: BackupNamespace,
+                optional: true,
+            },
+            "backup-type": {
+                optional: true,
+                type: BackupType,
+            },
+            "backup-id": {
+                optional: true,
+                schema: BACKUP_ID_SCHEMA,
+            },
+        },
+    },
+    returns: {
+        type: BackupGroupDeleteStats,
+    },
+    access: {
+        permission: &Permission::Anybody,
+        description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \
+            or DATASTORE_PRUNE and being the owner of the group",
+    },
+)]
+/// Clear trash items in a namespace or backup group including the group itself it is marked as trash.
+pub async fn clear_trash(
+    store: String,
+    ns: Option<BackupNamespace>,
+    backup_type: Option<BackupType>,
+    backup_id: Option<String>,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<BackupGroupDeleteStats, Error> {
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
+    tokio::task::spawn_blocking(move || {
+        let ns = ns.unwrap_or_default();
+        let limited = check_ns_privs_full(
+            &store,
+            &ns,
+            &auth_id,
+            PRIV_DATASTORE_MODIFY,
+            PRIV_DATASTORE_PRUNE,
+        )?;
+        let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?;
+
+        let groups = groups_by_type_or_id(datastore.clone(), &ns, backup_type, backup_id)?;
+        let mut delete_stats = BackupGroupDeleteStats::default();
+        for group in groups {
+            if limited {
+                let owner = datastore.get_owner(&ns, group.group())?;
+                if check_backup_owner(&owner, &auth_id).is_err() {
+                    continue;
+                }
+            }
+            let stats = datastore.clear_backup_group(&ns, group.group())?;
+            delete_stats.add(&stats);
+        }
+
+        Ok(delete_stats)
+    })
+    .await?
+}
+
 #[sortable]
 const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
     (
@@ -2879,6 +2945,10 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
         "change-owner",
         &Router::new().post(&API_METHOD_SET_BACKUP_OWNER),
     ),
+    (
+        "clear-trash",
+        &Router::new().delete(&API_METHOD_CLEAR_TRASH),
+    ),
     (
         "download",
         &Router::new().download(&API_METHOD_DOWNLOAD_FILE),
-- 
2.39.5





More information about the pbs-devel mailing list