diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 79e79bdb..c956c522 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -4,6 +4,8 @@ use crate::api_schema::*; use crate::api_schema::router::*; //use crate::server::rest::*; use serde_json::{json, Value}; +use std::collections::{HashSet, HashMap}; +use chrono::{DateTime, Datelike, Local}; //use hyper::StatusCode; //use hyper::rt::{Future, Stream}; @@ -14,6 +16,40 @@ use crate::backup::*; mod catar; +fn group_backups(backup_list: Vec) -> HashMap> { + + let mut group_hash = HashMap::new(); + + for info in backup_list { + let group_id = format!("{}/{}", info.backup_type, info.backup_id); + let time_list = group_hash.entry(group_id).or_insert(vec![]); + time_list.push(info); + } + + group_hash +} + +fn mark_selections, &BackupInfo) -> String> ( + mark: &mut HashSet, + list: &Vec, + keep: usize, + select_id: F, +){ + let mut hash = HashSet::new(); + for info in list { + let local_time = info.backup_time.with_timezone(&Local); + if hash.len() >= keep as usize { break; } + let backup_id = info.unique_id(); + let sel_id: String = select_id(local_time, &info); + if !hash.contains(&sel_id) { + hash.insert(sel_id); + //println!(" KEEP ID {} {}", backup_id, local_time.format("%c")); + mark.insert(backup_id); + } + } +} + + fn prune( param: Value, _info: &ApiMethod, @@ -26,8 +62,55 @@ fn prune( println!("Starting prune on store {}", store); - println!("PARAMS {:?}", param); + let backup_list = datastore.list_backups()?; + let group_hash = group_backups(backup_list); + + for (_group_id, mut list) in group_hash { + + let mut mark = HashSet::new(); + + list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); // new backups first + + if let Some(keep_last) = param["keep-last"].as_u64() { + list.iter().take(keep_last as usize).for_each(|info| { + let backup_id = format!(" {}/{}/{}", info.backup_type, info.backup_id, info.backup_time.timestamp()); + mark.insert(backup_id); + }); + } + + if let Some(keep_daily) = param["keep-daily"].as_u64() { + 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) = param["keep-weekly"].as_u64() { + 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) = param["keep-monthly"].as_u64() { + mark_selections(&mut mark, &list, keep_monthly as usize, |local_time, _info| { + format!("{}/{}", local_time.year(), local_time.month()) + }); + } + + if let Some(keep_yearly) = param["keep-yearly"].as_u64() { + mark_selections(&mut mark, &list, keep_yearly as usize, |local_time, _info| { + format!("{}/{}", local_time.year(), local_time.year()) + }); + } + + let mut remove_list: Vec<&BackupInfo> = list.iter().filter(|info| !mark.contains(&info.unique_id())).collect(); + + remove_list.sort_unstable_by(|a, b| a.backup_time.cmp(&b.backup_time)); // oldest backups first + + for info in remove_list { + datastore.remove_backup_dir(&info.backup_type, &info.backup_id, info.backup_time)?; + } + } Ok(json!(null)) } @@ -35,10 +118,30 @@ fn prune( pub fn add_common_prune_prameters(schema: ObjectSchema) -> ObjectSchema { schema + .optional( + "keep-last", + IntegerSchema::new("Number of backups to keep.") + .minimum(1) + ) .optional( "keep-daily", - IntegerSchema::new("Number of daily backups to keep") - .minimum(0) + IntegerSchema::new("Number of daily backups to keep.") + .minimum(1) + ) + .optional( + "keep-weekly", + IntegerSchema::new("Number of weekly backups to keep.") + .minimum(1) + ) + .optional( + "keep-monthly", + IntegerSchema::new("Number of monthly backups to keep.") + .minimum(1) + ) + .optional( + "keep-yearly", + IntegerSchema::new("Number of yearly backups to keep.") + .minimum(1) ) } diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index 162259c4..f21588aa 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -38,6 +38,13 @@ pub struct BackupInfo { pub files: Vec, } +impl BackupInfo { + + pub fn unique_id(&self) -> String { + format!("{}/{}/{}", self.backup_type, self.backup_id, self.backup_time.timestamp()) + } +} + lazy_static!{ static ref datastore_map: Mutex>> = Mutex::new(HashMap::new()); } @@ -151,6 +158,24 @@ impl DataStore { relative_path } + /// Remove a backup directory including all content + pub fn remove_backup_dir( + &self, + backup_type: &str, + backup_id: &str, + backup_time: DateTime, + ) -> Result<(), io::Error> { + + let relative_path = self.get_backup_dir(backup_type, backup_id, backup_time); + let mut full_path = self.base_path(); + full_path.push(&relative_path); + + log::info!("removing backup {:?}", full_path); + std::fs::remove_dir_all(full_path)?; + + Ok(()) + } + pub fn create_backup_dir( &self, backup_type: &str,