[pbs-devel] [PATCH v3 proxmox-backup 15/20] api: admin: implement endpoints to recover trashed contents

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


Implements the api endpoints to recover trashed contents contained
within backup groups or individual snapshots.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 src/api2/admin/datastore.rs | 143 +++++++++++++++++++++++++++++++++++-
 1 file changed, 142 insertions(+), 1 deletion(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 3f68edf24..3ea5b19f1 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -51,7 +51,7 @@ use pbs_api_types::{
 };
 use pbs_client::pxar::{create_tar, create_zip};
 use pbs_config::CachedUserInfo;
-use pbs_datastore::backup_info::BackupInfo;
+use pbs_datastore::backup_info::{BackupInfo, TRASH_MARKER_FILENAME};
 use pbs_datastore::cached_chunk_reader::CachedChunkReader;
 use pbs_datastore::catalog::{ArchiveEntry, CatalogReader};
 use pbs_datastore::data_blob::DataBlob;
@@ -2724,6 +2724,139 @@ pub async fn unmount(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<V
     Ok(json!(upid))
 }
 
+#[api(
+    input: {
+        properties: {
+            store: { schema: DATASTORE_SCHEMA },
+            group: {
+                type: pbs_api_types::BackupGroup,
+                flatten: true,
+            },
+            ns: {
+                type: BackupNamespace,
+                optional: true,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Anybody,
+        description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \
+            or DATASTORE_BACKUP and being the owner of the group",
+    },
+)]
+/// Recover trashed contents of a backup group.
+pub fn recover_group(
+    store: String,
+    group: pbs_api_types::BackupGroup,
+    ns: Option<BackupNamespace>,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+    let ns = ns.unwrap_or_default();
+    let datastore = check_privs_and_load_store(
+        &store,
+        &ns,
+        &auth_id,
+        PRIV_DATASTORE_MODIFY,
+        PRIV_DATASTORE_BACKUP,
+        Some(Operation::Write),
+        &group,
+    )?;
+
+    let backup_group = datastore.backup_group(ns, group);
+    do_recover_group(&backup_group)?;
+
+    Ok(())
+}
+
+fn do_recover_group(backup_group: &BackupGroup) -> Result<(), Error> {
+    let _exclusive_lock = backup_group
+        .lock()
+        .with_context(|| "while recovering group {backup_group:?}")?;
+    let trashed_snapshots = backup_group.list_backups(TrashStateFilter::OnlyTrash)?;
+    for snapshot in trashed_snapshots {
+        do_recover_snapshot(&snapshot.backup_dir)?;
+    }
+
+    let group_trash_path = backup_group.full_group_path().join(TRASH_MARKER_FILENAME);
+    if let Err(err) = std::fs::remove_file(&group_trash_path) {
+        if err.kind() != std::io::ErrorKind::NotFound {
+            bail!("failed to remove group trash file {group_trash_path:?} - {err}");
+        }
+    }
+    Ok(())
+}
+
+#[api(
+    input: {
+        properties: {
+            store: { schema: DATASTORE_SCHEMA },
+            backup_dir: {
+                type: pbs_api_types::BackupDir,
+                flatten: true,
+            },
+            ns: {
+                type: BackupNamespace,
+                optional: true,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Anybody,
+        description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \
+            or DATASTORE_BACKUP and being the owner of the group",
+    },
+)]
+/// Recover trashed contents of a backup snapshot.
+pub fn recover_snapshot(
+    store: String,
+    backup_dir: pbs_api_types::BackupDir,
+    ns: Option<BackupNamespace>,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+    let ns = ns.unwrap_or_default();
+    let datastore = check_privs_and_load_store(
+        &store,
+        &ns,
+        &auth_id,
+        PRIV_DATASTORE_MODIFY,
+        PRIV_DATASTORE_BACKUP,
+        Some(Operation::Write),
+        &backup_dir.group,
+    )?;
+
+    let backup_group = datastore.backup_group(ns.clone(), backup_dir.group.clone());
+    let snapshot = datastore.backup_dir(ns, backup_dir)?;
+    let _exclusive_group_lock = backup_group
+        .lock()
+        .with_context(|| "while recovering snapshot {snapshot_dir:?}")?;
+
+    do_recover_snapshot(&snapshot)?;
+
+    let group_trash_path = backup_group.full_group_path().join(TRASH_MARKER_FILENAME);
+    if let Err(err) = std::fs::remove_file(&group_trash_path) {
+        if err.kind() != std::io::ErrorKind::NotFound {
+            bail!("failed to remove group trash file {group_trash_path:?} - {err}");
+        }
+    }
+
+    Ok(())
+}
+
+fn do_recover_snapshot(snapshot_dir: &BackupDir) -> Result<(), Error> {
+    let _exclusive_lock = snapshot_dir
+        .lock()
+        .with_context(|| "while recovering snapshot {snapshot_dir:?}")?;
+    let trash_path = snapshot_dir.full_path().join(TRASH_MARKER_FILENAME);
+    if let Err(err) = std::fs::remove_file(&trash_path) {
+        if err.kind() != std::io::ErrorKind::NotFound {
+            bail!("failed to remove trash file {trash_path:?} - {err}");
+        }
+    }
+    Ok(())
+}
+
 #[sortable]
 const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
     (
@@ -2789,6 +2922,14 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
         "pxar-file-download",
         &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD),
     ),
+    (
+        "recover-group",
+        &Router::new().put(&API_METHOD_RECOVER_GROUP),
+    ),
+    (
+        "recover-snapshot",
+        &Router::new().put(&API_METHOD_RECOVER_SNAPSHOT),
+    ),
     ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)),
     (
         "snapshots",
-- 
2.39.5





More information about the pbs-devel mailing list