src/backup/prune.rs: define new struct PruneOptions
This commit is contained in:
parent
92acbd69f7
commit
9b78352188
@ -314,14 +314,15 @@ fn prune(
|
||||
|
||||
let list = group.list_backups(&datastore.base_path())?;
|
||||
|
||||
let mut prune_info = compute_prune_info(
|
||||
list,
|
||||
param["keep-last"].as_u64(),
|
||||
param["keep-daily"].as_u64(),
|
||||
param["keep-weekly"].as_u64(),
|
||||
param["keep-monthly"].as_u64(),
|
||||
param["keep-yearly"].as_u64(),
|
||||
)?;
|
||||
let prune_options = PruneOptions {
|
||||
keep_last: param["keep-last"].as_u64(),
|
||||
keep_daily: param["keep-daily"].as_u64(),
|
||||
keep_weekly: param["keep-weekly"].as_u64(),
|
||||
keep_monthly: param["keep-monthly"].as_u64(),
|
||||
keep_yearly: param["keep-yearly"].as_u64(),
|
||||
};
|
||||
|
||||
let mut prune_info = compute_prune_info(list, &prune_options)?;
|
||||
|
||||
prune_info.reverse(); // delete older snapshots first
|
||||
|
||||
|
@ -69,13 +69,55 @@ fn remove_incomplete_snapshots(
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PruneOptions {
|
||||
pub keep_last: Option<u64>,
|
||||
pub keep_daily: Option<u64>,
|
||||
pub keep_weekly: Option<u64>,
|
||||
pub keep_monthly: Option<u64>,
|
||||
pub keep_yearly: Option<u64>,
|
||||
}
|
||||
|
||||
impl PruneOptions {
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
keep_last: None,
|
||||
keep_daily: None,
|
||||
keep_weekly: None,
|
||||
keep_monthly: None,
|
||||
keep_yearly: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keep_last(mut self, value: Option<u64>) -> Self {
|
||||
self.keep_last = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn keep_daily(mut self, value: Option<u64>) -> Self {
|
||||
self.keep_daily = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn keep_weekly(mut self, value: Option<u64>) -> Self {
|
||||
self.keep_weekly = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn keep_monthly(mut self, value: Option<u64>) -> Self {
|
||||
self.keep_monthly = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn keep_yearly(mut self, value: Option<u64>) -> Self {
|
||||
self.keep_yearly = value;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_prune_info(
|
||||
mut list: Vec<BackupInfo>,
|
||||
keep_last: Option<u64>,
|
||||
keep_daily: Option<u64>,
|
||||
keep_weekly: Option<u64>,
|
||||
keep_monthly: Option<u64>,
|
||||
keep_yearly: Option<u64>,
|
||||
options: &PruneOptions,
|
||||
) -> Result<Vec<(BackupInfo, bool)>, Error> {
|
||||
|
||||
let mut mark = HashMap::new();
|
||||
@ -84,31 +126,31 @@ pub fn compute_prune_info(
|
||||
|
||||
remove_incomplete_snapshots(&mut mark, &list);
|
||||
|
||||
if let Some(keep_last) = keep_last {
|
||||
if let Some(keep_last) = options.keep_last {
|
||||
mark_selections(&mut mark, &list, keep_last as usize, |_local_time, info| {
|
||||
BackupDir::backup_time_to_string(info.backup_dir.backup_time())
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(keep_daily) = keep_daily {
|
||||
if let Some(keep_daily) = options.keep_daily {
|
||||
mark_selections(&mut mark, &list, keep_daily as usize, |local_time, _info| {
|
||||
format!("{}/{}/{}", local_time.year(), local_time.month(), local_time.day())
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(keep_weekly) = keep_weekly {
|
||||
if let Some(keep_weekly) = options.keep_weekly {
|
||||
mark_selections(&mut mark, &list, keep_weekly as usize, |local_time, _info| {
|
||||
format!("{}/{}", local_time.year(), local_time.iso_week().week())
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(keep_monthly) = keep_monthly {
|
||||
if let Some(keep_monthly) = options.keep_monthly {
|
||||
mark_selections(&mut mark, &list, keep_monthly as usize, |local_time, _info| {
|
||||
format!("{}/{}", local_time.year(), local_time.month())
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(keep_yearly) = keep_yearly {
|
||||
if let Some(keep_yearly) = options.keep_yearly {
|
||||
mark_selections(&mut mark, &list, keep_yearly as usize, |local_time, _info| {
|
||||
format!("{}/{}", local_time.year(), local_time.year())
|
||||
});
|
||||
|
@ -6,15 +6,10 @@ use proxmox_backup::backup::*;
|
||||
fn get_prune_list(
|
||||
list: Vec<BackupInfo>,
|
||||
return_kept: bool,
|
||||
keep_last: Option<u64>,
|
||||
keep_daily: Option<u64>,
|
||||
keep_weekly: Option<u64>,
|
||||
keep_monthly: Option<u64>,
|
||||
keep_yearly: Option<u64>,
|
||||
options: &PruneOptions,
|
||||
) -> Vec<PathBuf> {
|
||||
|
||||
let mut prune_info = BackupGroup::compute_prune_info(
|
||||
list, keep_last, keep_daily, keep_weekly, keep_monthly, keep_yearly).unwrap();
|
||||
let mut prune_info = compute_prune_info(list, options).unwrap();
|
||||
|
||||
prune_info.reverse();
|
||||
|
||||
@ -45,6 +40,7 @@ fn create_info(
|
||||
|
||||
BackupInfo { backup_dir, files }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prune_simple2() -> Result<(), Error> {
|
||||
|
||||
@ -61,14 +57,16 @@ fn test_prune_simple2() -> Result<(), Error> {
|
||||
orig_list.push(create_info("host/elsa/2019-12-04T11:59:15Z", false));
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, true, Some(1), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_daily(Some(1));
|
||||
let remove_list = get_prune_list(list, true, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
|
||||
];
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, true, Some(1), Some(1), None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(1)).keep_daily(Some(1));
|
||||
let remove_list = get_prune_list(list, true, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
|
||||
@ -76,7 +74,8 @@ fn test_prune_simple2() -> Result<(), Error> {
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, true, None, Some(1), Some(1), None, None);
|
||||
let options = PruneOptions::new().keep_daily(Some(1)).keep_weekly(Some(1));
|
||||
let remove_list = get_prune_list(list, true, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-01T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
|
||||
@ -84,7 +83,8 @@ fn test_prune_simple2() -> Result<(), Error> {
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, true, None, Some(1), Some(1), Some(1), None);
|
||||
let options = PruneOptions::new().keep_daily(Some(1)).keep_weekly(Some(1)).keep_monthly(Some(1));
|
||||
let remove_list = get_prune_list(list, true, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-11-22T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-01T11:59:15Z"),
|
||||
@ -93,7 +93,8 @@ fn test_prune_simple2() -> Result<(), Error> {
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, true, None, None, None, Some(1), Some(1));
|
||||
let options = PruneOptions::new().keep_monthly(Some(1)).keep_yearly(Some(1));
|
||||
let remove_list = get_prune_list(list, true, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2018-11-15T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
|
||||
@ -116,19 +117,22 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
// keep-last tests
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(4), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(4));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = Vec::new();
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(3), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(3));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
];
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(2), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(2));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
|
||||
@ -136,7 +140,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(1), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(1));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
|
||||
@ -145,7 +150,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(0), None, None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(0));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
|
||||
@ -156,19 +162,22 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-last, keep-daily mixed
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, Some(2), Some(2), None, None, None);
|
||||
let options = PruneOptions::new().keep_last(Some(2)).keep_daily(Some(2));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![];
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
// keep-daily test
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, Some(3), None, None, None);
|
||||
let options = PruneOptions::new().keep_daily(Some(3));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-04T11:59:15Z")];
|
||||
assert_eq!(remove_list, expect);
|
||||
|
||||
// keep-daily test
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, Some(2), None, None, None);
|
||||
let options = PruneOptions::new().keep_daily(Some(2));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
|
||||
@ -177,7 +186,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-weekly
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, None, Some(5), None, None);
|
||||
let options = PruneOptions::new().keep_weekly(Some(5));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
// all backup are within the same week, so we only keep a single file
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
@ -188,7 +198,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-daily + keep-weekly
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, Some(1), Some(5), None, None);
|
||||
let options = PruneOptions::new().keep_daily(Some(1)).keep_weekly(Some(5));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
|
||||
@ -198,7 +209,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-monthly
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, None, None, Some(6), None);
|
||||
let options = PruneOptions::new().keep_monthly(Some(6));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
// all backup are within the same month, so we only keep a single file
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
@ -209,7 +221,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-yearly
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, None, None, None, Some(7));
|
||||
let options = PruneOptions::new().keep_yearly(Some(7));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
// all backup are within the same year, so we only keep a single file
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
@ -220,7 +233,8 @@ fn test_prune_simple() -> Result<(), Error> {
|
||||
|
||||
// keep-weekly + keep-monthly + keep-yearly
|
||||
let list = orig_list.clone();
|
||||
let remove_list = get_prune_list(list, false, None, None, Some(5), Some(6), Some(7));
|
||||
let options = PruneOptions::new().keep_weekly(Some(5)).keep_monthly(Some(6)).keep_yearly(Some(7));
|
||||
let remove_list = get_prune_list(list, false, &options);
|
||||
// all backup are within one week, so we only keep a single file
|
||||
let expect: Vec<PathBuf> = vec![
|
||||
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
|
||||
|
Loading…
Reference in New Issue
Block a user