server/prune_job: factor out 'prune_datastore'

we want to use that outside of a prune job

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-07-16 10:53:23 +02:00 committed by Dietmar Maurer
parent 9751ef4b36
commit 61f05679d2

View File

@ -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);
}
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()
);
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);