[pbs-devel] [PATCH proxmox-backup 06/11] server/prune_job: factor out 'prune_datastore'

Dominik Csapak d.csapak at proxmox.com
Fri Jul 16 10:53:23 CEST 2021


we want to use that outside of a prune job

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/server/prune_job.rs | 114 +++++++++++++++++++++-------------------
 1 file changed, 61 insertions(+), 53 deletions(-)

diff --git a/src/server/prune_job.rs b/src/server/prune_job.rs
index 248068ea..7ea42fcb 100644
--- a/src/server/prune_job.rs
+++ b/src/server/prune_job.rs
@@ -1,6 +1,6 @@
-use anyhow::Error;
+use std::sync::Arc;
 
-use proxmox::try_block;
+use anyhow::Error;
 
 use pbs_datastore::{task_log, task_warn};
 
@@ -11,6 +11,61 @@ use crate::{
     server::WorkerTask,
 };
 
+pub fn prune_datastore(
+    worker: Arc<WorkerTask>,
+    prune_options: PruneOptions,
+    store: &str,
+    datastore: Arc<DataStore>,
+) -> Result<(), Error> {
+    task_log!(worker, "Starting datastore prune on store \"{}\"", store);
+
+    task_log!(
+        worker,
+        "retention options: {}",
+        prune_options.cli_options_string()
+    );
+
+    let base_path = datastore.base_path();
+
+    let groups = BackupInfo::list_backup_groups(&base_path)?;
+    for group in groups {
+        let list = group.list_backups(&base_path)?;
+        let mut prune_info = compute_prune_info(list, &prune_options)?;
+        prune_info.reverse(); // delete older snapshots first
+
+        task_log!(
+            worker,
+            "Starting prune on store \"{}\" group \"{}/{}\"",
+            store,
+            group.backup_type(),
+            group.backup_id()
+        );
+
+        for (info, keep) in prune_info {
+            task_log!(
+                worker,
+                "{} {}/{}/{}",
+                if keep { "keep" } else { "remove" },
+                group.backup_type(),
+                group.backup_id(),
+                info.backup_dir.backup_time_string()
+            );
+            if !keep {
+                if let Err(err) = datastore.remove_backup_dir(&info.backup_dir, false) {
+                    task_warn!(
+                        worker,
+                        "failed to remove dir {:?}: {}",
+                        info.backup_dir.relative_path(),
+                        err,
+                    );
+                }
+            }
+        }
+    }
+
+    Ok(())
+}
+
 pub fn do_prune_job(
     mut job: Job,
     prune_options: PruneOptions,
@@ -29,58 +84,11 @@ pub fn do_prune_job(
         move |worker| {
             job.start(&worker.upid().to_string())?;
 
-            let result = try_block!({
-                task_log!(worker, "Starting datastore prune on store \"{}\"", store);
-
-                if let Some(event_str) = schedule {
-                    task_log!(worker, "task triggered by schedule '{}'", event_str);
-                }
-
-                task_log!(
-                    worker,
-                    "retention options: {}",
-                    prune_options.cli_options_string()
-                );
-
-                let base_path = datastore.base_path();
-
-                let groups = BackupInfo::list_backup_groups(&base_path)?;
-                for group in groups {
-                    let list = group.list_backups(&base_path)?;
-                    let mut prune_info = compute_prune_info(list, &prune_options)?;
-                    prune_info.reverse(); // delete older snapshots first
-
-                    task_log!(
-                        worker,
-                        "Starting prune on store \"{}\" group \"{}/{}\"",
-                        store,
-                        group.backup_type(),
-                        group.backup_id()
-                    );
+            if let Some(event_str) = schedule {
+                task_log!(worker, "task triggered by schedule '{}'", event_str);
+            }
 
-                    for (info, keep) in prune_info {
-                        task_log!(
-                            worker,
-                            "{} {}/{}/{}",
-                            if keep { "keep" } else { "remove" },
-                            group.backup_type(),
-                            group.backup_id(),
-                            info.backup_dir.backup_time_string()
-                        );
-                        if !keep {
-                            if let Err(err) = datastore.remove_backup_dir(&info.backup_dir, false) {
-                                task_warn!(
-                                    worker,
-                                    "failed to remove dir {:?}: {}",
-                                    info.backup_dir.relative_path(),
-                                    err,
-                                );
-                            }
-                        }
-                    }
-                }
-                Ok(())
-            });
+            let result = prune_datastore(worker.clone(), prune_options, &store, datastore);
 
             let status = worker.create_state(&result);
 
-- 
2.30.2






More information about the pbs-devel mailing list