2019-12-06 07:05:40 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-07-08 08:07:01 +00:00
|
|
|
use anyhow::{Error};
|
2021-07-16 08:53:19 +00:00
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
use pbs_api_types::PruneOptions;
|
2021-07-08 08:07:01 +00:00
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
use super::BackupInfo;
|
2019-12-06 07:05:40 +00:00
|
|
|
|
2021-10-27 11:22:28 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum PruneMark { Protected, Keep, KeepPartial, Remove }
|
|
|
|
|
|
|
|
impl PruneMark {
|
|
|
|
pub fn keep(&self) -> bool {
|
|
|
|
*self != PruneMark::Remove
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn protected(&self) -> bool {
|
|
|
|
*self == PruneMark::Protected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for PruneMark {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let txt = match self {
|
|
|
|
PruneMark::Protected => "protected",
|
|
|
|
PruneMark::Keep => "keep",
|
|
|
|
PruneMark::KeepPartial => "keep-partial",
|
|
|
|
PruneMark::Remove => "remove",
|
|
|
|
};
|
|
|
|
write!(f, "{}", txt)
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 07:05:40 +00:00
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
fn mark_selections<F: Fn(&BackupInfo) -> Result<String, Error>> (
|
2019-12-06 07:05:40 +00:00
|
|
|
mark: &mut HashMap<PathBuf, PruneMark>,
|
2021-01-19 13:46:39 +00:00
|
|
|
list: &[BackupInfo],
|
2019-12-06 07:05:40 +00:00
|
|
|
keep: usize,
|
|
|
|
select_id: F,
|
2020-09-12 13:10:47 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-12-06 07:05:40 +00:00
|
|
|
|
|
|
|
let mut include_hash = HashSet::new();
|
|
|
|
|
|
|
|
let mut already_included = HashSet::new();
|
|
|
|
for info in list {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
|
|
|
if let Some(PruneMark::Keep) = mark.get(&backup_id) {
|
2020-09-12 13:10:47 +00:00
|
|
|
let sel_id: String = select_id(&info)?;
|
2019-12-06 07:05:40 +00:00
|
|
|
already_included.insert(sel_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for info in list {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
2021-01-19 09:27:59 +00:00
|
|
|
if mark.get(&backup_id).is_some() { continue; }
|
2021-10-27 11:22:27 +00:00
|
|
|
if info.protected {
|
|
|
|
mark.insert(backup_id, PruneMark::Protected);
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-12 13:10:47 +00:00
|
|
|
let sel_id: String = select_id(&info)?;
|
2019-12-06 07:05:40 +00:00
|
|
|
|
|
|
|
if already_included.contains(&sel_id) { continue; }
|
|
|
|
|
|
|
|
if !include_hash.contains(&sel_id) {
|
|
|
|
if include_hash.len() >= keep { break; }
|
|
|
|
include_hash.insert(sel_id);
|
|
|
|
mark.insert(backup_id, PruneMark::Keep);
|
|
|
|
} else {
|
|
|
|
mark.insert(backup_id, PruneMark::Remove);
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 13:10:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:12:08 +00:00
|
|
|
fn remove_incomplete_snapshots(
|
|
|
|
mark: &mut HashMap<PathBuf, PruneMark>,
|
2021-01-19 13:46:39 +00:00
|
|
|
list: &[BackupInfo],
|
2019-12-06 07:12:08 +00:00
|
|
|
) {
|
2019-12-06 07:05:40 +00:00
|
|
|
|
|
|
|
let mut keep_unfinished = true;
|
|
|
|
for info in list.iter() {
|
|
|
|
// backup is considered unfinished if there is no manifest
|
2020-07-29 12:33:11 +00:00
|
|
|
if info.is_finished() {
|
2019-12-06 07:05:40 +00:00
|
|
|
// There is a new finished backup, so there is no need
|
|
|
|
// to keep older unfinished backups.
|
|
|
|
keep_unfinished = false;
|
|
|
|
} else {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
|
|
|
if keep_unfinished { // keep first unfinished
|
2019-12-06 07:15:32 +00:00
|
|
|
mark.insert(backup_id, PruneMark::KeepPartial);
|
2019-12-06 07:05:40 +00:00
|
|
|
} else {
|
|
|
|
mark.insert(backup_id, PruneMark::Remove);
|
|
|
|
}
|
|
|
|
keep_unfinished = false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 07:12:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
pub fn keeps_something(options: &PruneOptions) -> bool {
|
|
|
|
let mut keep_something = false;
|
|
|
|
if let Some(count) = options.keep_last { if count > 0 { keep_something = true; } }
|
|
|
|
if let Some(count) = options.keep_hourly { if count > 0 { keep_something = true; } }
|
|
|
|
if let Some(count) = options.keep_daily { if count > 0 { keep_something = true; } }
|
|
|
|
if let Some(count) = options.keep_weekly { if count > 0 { keep_something = true; } }
|
|
|
|
if let Some(count) = options.keep_monthly { if count > 0 { keep_something = true; } }
|
|
|
|
if let Some(count) = options.keep_yearly { if count > 0 { keep_something = true; } }
|
|
|
|
keep_something
|
2019-12-06 07:56:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
pub fn cli_options_string(options: &PruneOptions) -> String {
|
|
|
|
let mut opts = Vec::new();
|
2019-12-06 07:56:27 +00:00
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
if let Some(count) = options.keep_last {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-last {}", count));
|
2019-12-06 07:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
if let Some(count) = options.keep_hourly {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-hourly {}", count));
|
2019-12-14 15:32:16 +00:00
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
}
|
|
|
|
if let Some(count) = options.keep_daily {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-daily {}", count));
|
2019-12-14 15:32:16 +00:00
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
}
|
|
|
|
if let Some(count) = options.keep_weekly {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-weekly {}", count));
|
2019-12-14 15:32:16 +00:00
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
}
|
|
|
|
if let Some(count) = options.keep_monthly {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-monthly {}", count));
|
2019-12-14 15:32:16 +00:00
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
}
|
|
|
|
if let Some(count) = options.keep_yearly {
|
|
|
|
if count > 0 {
|
|
|
|
opts.push(format!("--keep-yearly {}", count));
|
2019-12-14 15:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 07:21:27 +00:00
|
|
|
|
|
|
|
opts.join(" ")
|
2019-12-06 07:56:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:12:08 +00:00
|
|
|
pub fn compute_prune_info(
|
|
|
|
mut list: Vec<BackupInfo>,
|
2019-12-06 07:56:27 +00:00
|
|
|
options: &PruneOptions,
|
2021-10-27 11:22:28 +00:00
|
|
|
) -> Result<Vec<(BackupInfo, PruneMark)>, Error> {
|
2019-12-06 07:12:08 +00:00
|
|
|
|
|
|
|
let mut mark = HashMap::new();
|
|
|
|
|
|
|
|
BackupInfo::sort_list(&mut list, false);
|
|
|
|
|
|
|
|
remove_incomplete_snapshots(&mut mark, &list);
|
2019-12-06 07:05:40 +00:00
|
|
|
|
2019-12-06 07:56:27 +00:00
|
|
|
if let Some(keep_last) = options.keep_last {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_last as usize, |info| {
|
|
|
|
Ok(info.backup_dir.backup_time_string().to_owned())
|
|
|
|
})?;
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_time::strftime_local;
|
2020-09-12 13:10:47 +00:00
|
|
|
|
2019-12-07 10:23:33 +00:00
|
|
|
if let Some(keep_hourly) = options.keep_hourly {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_hourly as usize, |info| {
|
2021-10-08 09:19:37 +00:00
|
|
|
strftime_local("%Y/%m/%d/%H", info.backup_dir.backup_time()).map_err(Error::from)
|
2020-09-12 13:10:47 +00:00
|
|
|
})?;
|
2019-12-07 10:23:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:56:27 +00:00
|
|
|
if let Some(keep_daily) = options.keep_daily {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_daily as usize, |info| {
|
2021-10-08 09:19:37 +00:00
|
|
|
strftime_local("%Y/%m/%d", info.backup_dir.backup_time()).map_err(Error::from)
|
2020-09-12 13:10:47 +00:00
|
|
|
})?;
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:56:27 +00:00
|
|
|
if let Some(keep_weekly) = options.keep_weekly {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_weekly as usize, |info| {
|
|
|
|
// Note: Use iso-week year/week here. This year number
|
|
|
|
// might not match the calendar year number.
|
2021-10-08 09:19:37 +00:00
|
|
|
strftime_local("%G/%V", info.backup_dir.backup_time()).map_err(Error::from)
|
2020-09-12 13:10:47 +00:00
|
|
|
})?;
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:56:27 +00:00
|
|
|
if let Some(keep_monthly) = options.keep_monthly {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_monthly as usize, |info| {
|
2021-10-08 09:19:37 +00:00
|
|
|
strftime_local("%Y/%m", info.backup_dir.backup_time()).map_err(Error::from)
|
2020-09-12 13:10:47 +00:00
|
|
|
})?;
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:56:27 +00:00
|
|
|
if let Some(keep_yearly) = options.keep_yearly {
|
2020-09-12 13:10:47 +00:00
|
|
|
mark_selections(&mut mark, &list, keep_yearly as usize, |info| {
|
2021-10-08 09:19:37 +00:00
|
|
|
strftime_local("%Y", info.backup_dir.backup_time()).map_err(Error::from)
|
2020-09-12 13:10:47 +00:00
|
|
|
})?;
|
2019-12-06 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:22:28 +00:00
|
|
|
let prune_info: Vec<(BackupInfo, PruneMark)> = list.into_iter()
|
2019-12-06 07:05:40 +00:00
|
|
|
.map(|info| {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
2021-10-27 11:22:28 +00:00
|
|
|
let mark = if info.protected {
|
|
|
|
PruneMark::Protected
|
|
|
|
} else {
|
|
|
|
*mark.get(&backup_id).unwrap_or(&PruneMark::Remove)
|
2019-12-06 07:05:40 +00:00
|
|
|
};
|
2021-10-27 11:22:28 +00:00
|
|
|
|
|
|
|
(info, mark)
|
2019-12-06 07:05:40 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(prune_info)
|
|
|
|
}
|