public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v2 37/43] prune: skip snapshots with active retentions
Date: Fri,  6 Nov 2026 18:22:29 +0100	[thread overview]
Message-ID: <20261106172235.890542-38-c.ebner@proxmox.com> (raw)
In-Reply-To: <20261106172235.890542-1-c.ebner@proxmox.com>

Make sure that snapshots which have an active retention are not
tried to be pruned but rather skipped. This is analogous to the
checks performed for protected snapshots.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 pbs-datastore/src/prune.rs  | 54 +++++++++++++++++++++++++++++++++----
 src/api2/admin/datastore.rs |  7 +++--
 src/server/prune_job.rs     |  5 ++--
 tests/prune.rs              | 49 +++++++++++++++++++++++++++++++--
 www/datastore/Prune.js      |  4 +++
 5 files changed, 108 insertions(+), 11 deletions(-)

diff --git a/pbs-datastore/src/prune.rs b/pbs-datastore/src/prune.rs
index 6d8a215d7..5ece66e43 100644
--- a/pbs-datastore/src/prune.rs
+++ b/pbs-datastore/src/prune.rs
@@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
 use std::path::PathBuf;
 
 use anyhow::Error;
+use tracing::warn;
 
 use pbs_api_types::KeepOptions;
 
@@ -10,6 +11,7 @@ use super::BackupInfo;
 #[derive(Clone, Copy, PartialEq, Eq)]
 pub enum PruneMark {
     Protected,
+    Retained,
     Keep,
     KeepPartial,
     Remove,
@@ -24,12 +26,17 @@ impl PruneMark {
     pub fn protected(self) -> bool {
         self == PruneMark::Protected
     }
+
+    pub fn retained(self) -> bool {
+        self == PruneMark::Retained
+    }
 }
 
 impl std::fmt::Display for PruneMark {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.write_str(match self {
             PruneMark::Protected => "protected",
+            PruneMark::Retained => "retained",
             PruneMark::Keep => "keep",
             PruneMark::KeepPartial => "keep-partial",
             PruneMark::Remove => "remove",
@@ -115,10 +122,39 @@ fn remove_incomplete_and_ignore_future(
     }
 }
 
+/// Check if the snapshot is still protected by its retention timestamp.
+pub fn has_active_retention(info: &BackupInfo) -> bool {
+    match info.backup_dir.active_retention_lock() {
+        Ok(retain_until) => retain_until.is_some(),
+        Err(err) => {
+            warn!(
+                "failed to check retention of {:?} - {err:#}",
+                info.backup_dir.relative_path(),
+            );
+            true
+        }
+    }
+}
+
+/// Keep snapshots marked for removal which are still protected by their retention timestamp.
+///
+/// This must run after the keep options were applied by [`compute_prune_info`].
+pub fn mark_active_retained(
+    prune_info: &mut [(BackupInfo, PruneMark)],
+    has_active_retention: impl Fn(&BackupInfo) -> bool,
+) {
+    for (info, mark) in prune_info.iter_mut() {
+        if *mark == PruneMark::Remove && has_active_retention(info) {
+            *mark = PruneMark::Retained;
+        }
+    }
+}
+
 /// This filters incomplete and kept backups.
 pub fn compute_prune_info(
     mut list: Vec<BackupInfo>,
     options: &KeepOptions,
+    has_active_retention: impl Fn(&BackupInfo) -> bool,
 ) -> Result<Vec<(BackupInfo, PruneMark)>, Error> {
     let mut mark = HashMap::new();
 
@@ -126,6 +162,18 @@ pub fn compute_prune_info(
 
     remove_incomplete_and_ignore_future(&mut mark, &list);
 
+    for info in list.iter() {
+        let backup_id = info.backup_dir.relative_path();
+        if mark.contains_key(&backup_id) {
+            continue;
+        }
+        if info.protected {
+            mark.insert(backup_id, PruneMark::Protected);
+        } else if has_active_retention(info) {
+            mark.insert(backup_id, PruneMark::Retained);
+        }
+    }
+
     if let Some(keep_last) = options.keep_last {
         mark_selections(&mut mark, &list, keep_last as usize, |info| {
             Ok(info.backup_dir.backup_time_string().to_owned())
@@ -170,11 +218,7 @@ pub fn compute_prune_info(
         .into_iter()
         .map(|info| {
             let backup_id = info.backup_dir.relative_path();
-            let mark = if info.protected {
-                PruneMark::Protected
-            } else {
-                mark.get(&backup_id).copied().unwrap_or(PruneMark::Remove)
-            };
+            let mark = mark.get(&backup_id).copied().unwrap_or(PruneMark::Remove);
 
             (info, mark)
         })
diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 097d74dc1..563590430 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -58,7 +58,7 @@ use pbs_datastore::dynamic_index::{BufferedDynamicReader, DynamicIndexReader, Lo
 use pbs_datastore::fixed_index::FixedIndexReader;
 use pbs_datastore::index::IndexFile;
 use pbs_datastore::manifest::BackupManifest;
-use pbs_datastore::prune::compute_prune_info;
+use pbs_datastore::prune::{compute_prune_info, has_active_retention};
 use pbs_datastore::{
     BackupDir, DataStore, LocalChunkReader, StoreProgress, check_backup_owner,
     ensure_datastore_is_mounted, task_tracking,
@@ -1016,6 +1016,7 @@ pub fn prune(
         backup_time: i64,
         keep: bool,
         protected: bool,
+        retained: bool,
         #[serde(skip_serializing_if = "Option::is_none")]
         ns: Option<BackupNamespace>,
     }
@@ -1023,7 +1024,7 @@ pub fn prune(
 
     let list = group.list_backups()?;
 
-    let mut prune_info = compute_prune_info(list, &keep_options)?;
+    let mut prune_info = compute_prune_info(list, &keep_options, has_active_retention)?;
 
     prune_info.reverse(); // delete older snapshots first
 
@@ -1040,6 +1041,7 @@ pub fn prune(
                 backup_time: backup_dir.backup_time(),
                 keep,
                 protected: mark.protected(),
+                retained: mark.retained(),
                 ns: None,
             };
             let prune_ns = backup_dir.backup_ns();
@@ -1087,6 +1089,7 @@ pub fn prune(
                 backup_time,
                 keep,
                 protected: mark.protected(),
+                retained: mark.retained(),
                 ns: None,
             });
 
diff --git a/src/server/prune_job.rs b/src/server/prune_job.rs
index c94e34f43..79e0df804 100644
--- a/src/server/prune_job.rs
+++ b/src/server/prune_job.rs
@@ -8,7 +8,7 @@ use pbs_api_types::{
     PRIV_DATASTORE_PRUNE, PruneJobOptions, print_store_and_ns,
 };
 use pbs_datastore::DataStore;
-use pbs_datastore::prune::{PruneMark, compute_prune_info};
+use pbs_datastore::prune::{PruneMark, compute_prune_info, has_active_retention};
 use proxmox_rest_server::WorkerTask;
 
 use crate::backup::ListAccessibleBackupGroups;
@@ -56,7 +56,7 @@ pub fn prune_datastore(
         let ns = group.backup_ns();
         let list = group.list_backups()?;
 
-        let mut prune_info = compute_prune_info(list, &prune_options.keep)?;
+        let mut prune_info = compute_prune_info(list, &prune_options.keep, has_active_retention)?;
         prune_info.reverse(); // delete older snapshots first
 
         info!(
@@ -75,6 +75,7 @@ pub fn prune_datastore(
                 info.backup_dir.backup_time_string(),
                 mark = match mark {
                     PruneMark::Protected => "keep protected",
+                    PruneMark::Retained => "keep retained",
                     PruneMark::Keep => "keep",
                     PruneMark::KeepPartial => "keep partial",
                     PruneMark::Remove => "remove",
diff --git a/tests/prune.rs b/tests/prune.rs
index 612a915db..bb5184b76 100644
--- a/tests/prune.rs
+++ b/tests/prune.rs
@@ -3,7 +3,7 @@ use std::path::PathBuf;
 use anyhow::Error;
 
 use pbs_api_types::{MANIFEST_BLOB_NAME, PruneJobOptions};
-use pbs_datastore::prune::compute_prune_info;
+use pbs_datastore::prune::{PruneMark, compute_prune_info};
 use pbs_datastore::{BackupDir, BackupInfo};
 
 fn get_prune_list(
@@ -11,7 +11,7 @@ fn get_prune_list(
     return_kept: bool,
     options: &PruneJobOptions,
 ) -> Vec<PathBuf> {
-    let mut prune_info = compute_prune_info(list, &options.keep).unwrap();
+    let mut prune_info = compute_prune_info(list, &options.keep, |_| false).unwrap();
 
     prune_info.reverse();
 
@@ -74,6 +74,51 @@ fn test_prune_protected() -> Result<(), Error> {
     Ok(())
 }
 
+#[test]
+fn test_prune_retained() -> Result<(), Error> {
+    let orig_list = vec![
+        create_info("host/elsa/2019-11-15T09:39:15Z", false),
+        create_info("host/elsa/2019-11-15T10:39:15Z", false),
+        create_info("host/elsa/2019-11-15T10:49:15Z", false),
+        create_info("host/elsa/2019-11-15T10:59:15Z", false),
+    ];
+    let retained = [
+        PathBuf::from("host/elsa/2019-11-15T10:39:15Z"),
+        PathBuf::from("host/elsa/2019-11-15T10:59:15Z"),
+    ];
+
+    let mut options = PruneJobOptions::default();
+    options.keep.keep_last = Some(1);
+    let prune_info = compute_prune_info(orig_list, &options.keep, |info| {
+        retained.contains(&info.backup_dir.relative_path())
+    })?;
+
+    let marks: Vec<(PathBuf, PruneMark)> = prune_info
+        .iter()
+        .map(|(info, mark)| (info.backup_dir.relative_path(), *mark))
+        .collect();
+    let expect = vec![
+        (
+            PathBuf::from("host/elsa/2019-11-15T10:59:15Z"),
+            PruneMark::Retained,
+        ),
+        (
+            PathBuf::from("host/elsa/2019-11-15T10:49:15Z"),
+            PruneMark::Keep,
+        ),
+        (
+            PathBuf::from("host/elsa/2019-11-15T10:39:15Z"),
+            PruneMark::Retained,
+        ),
+        (
+            PathBuf::from("host/elsa/2019-11-15T09:39:15Z"),
+            PruneMark::Remove,
+        ),
+    ];
+    assert!(marks == expect);
+    Ok(())
+}
+
 #[test]
 fn test_prune_hourly() -> Result<(), Error> {
     let orig_list = vec![
diff --git a/www/datastore/Prune.js b/www/datastore/Prune.js
index 7722db2c7..abb732540 100644
--- a/www/datastore/Prune.js
+++ b/www/datastore/Prune.js
@@ -110,6 +110,10 @@ Ext.define('PBS.Datastore.PruneInputPanel', {
                             backup.keepReason = 'protected';
                             continue;
                         }
+                        if (backup.retained) {
+                            backup.keepReason = 'retained';
+                            continue;
+                        }
                         counter[rule]++;
                         if (rule !== 'keep-all') {
                             backup.keepReason = rule + ': ' + counter[rule];
-- 
2.47.3





  parent reply	other threads:[~2026-11-06 17:24 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 17:21 [PATCH proxmox{-widget-toolkit,,-backup} v2 00/43] append-only sync jobs and snapshot retention timespan Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox-widget-toolkit v2 01/43] form: date time field: init with now instead of epoch on empty fields Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox-widget-toolkit v2 02/43] form: date time field: set min/max value limits passed via config Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox-widget-toolkit v2 03/43] form: date time field: update to new limits when switching day/time Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox-widget-toolkit v2 04/43] form: date time field: submit combined datetime instead of item fields Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox-widget-toolkit v2 05/43] form: date time field: track value like other form fields Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox v2 06/43] pbs-api-types: add append only privilege and role Christian Ebner
2026-11-06 17:21 ` [PATCH proxmox v2 07/43] pbs-api-types: add remote datastore append " Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox v2 08/43] pbs-api-types: extend snapshot list items by retention timestamp Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox v2 09/43] pbs-api-types: extend sync job config by retention-timespan parameter Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox v2 10/43] pbs-api-types: add max/default retention timespan property to datastore Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox v2 11/43] pbs-api-types: store: expose max/default retention timespans in status Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 12/43] config/tools: add new fields for sync and snapshot/datastore list items Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 13/43] api: admin: improve code style for status endpoint Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 14/43] client: avoid error in status if user lacks permissions Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 15/43] server: allow iterating contents for Datastore.Append permissions Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 16/43] api: backup: fix possible information leak in multi-tenant datastores Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 17/43] datastore: s3: fix namespace level bulk remove not honoring group locks Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 18/43] api: backup: allow backup for user/token with append permission Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 19/43] api: allow namespace creation on append permissions Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 20/43] api: sync: allow pull to target for user/token with append permission Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 21/43] sync: pull: allow pulling " Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 22/43] sync: push: allow push and ns creation on Remote.DatastoreAppend Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 23/43] datastore: conditionally treat missing manifest as error or benign Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 24/43] api: backup: provide retain-until timestamp for extended prune protection Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 25/43] tools: include retain-until timestamp in snapshot list items Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 26/43] client: backup writer: allow to send retain-until timestamp on backup Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 27/43] sync: push: allow to set retention timestamp for synced snapshots Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 28/43] sync: pull: set retention timestamp from sync job for new snapshots Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 29/43] datastore/config: parse and enforce maximum retention timespan Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 30/43] api: config: allow to set or delete retention timespan for sync jobs Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 31/43] api: admin: allow to modify snapshot retention for root user Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 32/43] tape/s3-refresh: check for retentions outside datastore limits Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 33/43] ui: add retention timespan form and use it for sync job edit window Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 34/43] ui: allow datastore wide max retention timespan configuration Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 35/43] ui: show snapshot retention in datastore contents and allow extension Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 36/43] group: destroy: s3: skip snapshots with active retentions Christian Ebner
2026-11-06 17:22 ` Christian Ebner [this message]
2026-11-06 17:22 ` [PATCH proxmox-backup v2 38/43] api/ui: expose default retention setting as datastore config option Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 39/43] api: datastore: expose max/default retention timespans in status listing Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 40/43] backup/sync: fallback to datastore default-retention-timespan Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 41/43] client: add flags to set snapshot retention via timestamp/timespan Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 42/43] docs: describe the privileges and roles for append only backup/sync Christian Ebner
2026-11-06 17:22 ` [PATCH proxmox-backup v2 43/43] docs: document snapshot retention for new backup snapshots Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261106172235.890542-38-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal