From 38b0dfa51143471a5845680372872065de50fa8f Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 1 Mar 2019 12:32:10 +0100 Subject: [PATCH] src/backup/datastore.rs: split BackupInfo --- src/api2/admin/datastore.rs | 27 ++++++------ src/api2/admin/datastore/catar.rs | 8 +++- src/backup/datastore.rs | 71 +++++++++++++++---------------- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index c956c522..6eb0bfad 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -6,6 +6,7 @@ use crate::api_schema::router::*; use serde_json::{json, Value}; use std::collections::{HashSet, HashMap}; use chrono::{DateTime, Datelike, Local}; +use std::path::PathBuf; //use hyper::StatusCode; //use hyper::rt::{Future, Stream}; @@ -21,7 +22,7 @@ fn group_backups(backup_list: Vec) -> HashMap) -> HashMap, &BackupInfo) -> String> ( - mark: &mut HashSet, + 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); + let local_time = info.backup_dir.backup_time.with_timezone(&Local); if hash.len() >= keep as usize { break; } - let backup_id = info.unique_id(); + let backup_id = info.backup_dir.relative_path(); let sel_id: String = select_id(local_time, &info); if !hash.contains(&sel_id) { hash.insert(sel_id); @@ -70,12 +71,11 @@ fn prune( let mut mark = HashSet::new(); - list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); // new backups first + list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.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); + mark.insert(info.backup_dir.relative_path()); }); } @@ -103,12 +103,13 @@ fn prune( }); } - let mut remove_list: Vec<&BackupInfo> = list.iter().filter(|info| !mark.contains(&info.unique_id())).collect(); + let mut remove_list: Vec<&BackupInfo> = list.iter() + .filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect(); - remove_list.sort_unstable_by(|a, b| a.backup_time.cmp(&b.backup_time)); // oldest backups first + remove_list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time)); // oldest backups first for info in remove_list { - datastore.remove_backup_dir(&info.backup_type, &info.backup_id, info.backup_time)?; + datastore.remove_backup_dir(&info.backup_dir)?; } } @@ -222,9 +223,9 @@ fn get_backup_list( for info in datastore.list_backups()? { list.push(json!({ - "backup_type": info.backup_type, - "backup_id": info.backup_id, - "backup_time": info.backup_time.timestamp(), + "backup_type": info.backup_dir.backup_type, + "backup_id": info.backup_dir.backup_id, + "backup_time": info.backup_dir.backup_time.timestamp(), "files": info.files, })); } diff --git a/src/api2/admin/datastore/catar.rs b/src/api2/admin/datastore/catar.rs index 80b2e4bd..3683f467 100644 --- a/src/api2/admin/datastore/catar.rs +++ b/src/api2/admin/datastore/catar.rs @@ -145,7 +145,13 @@ fn download_catar( let datastore = DataStore::lookup_datastore(store)?; - let mut path = datastore.get_backup_dir(backup_type, backup_id, backup_time); + let backup_dir = BackupDir { + backup_type: backup_type.to_string(), + backup_id: backup_id.to_string(), + backup_time, + }; + + let mut path = backup_dir.relative_path(); let mut full_archive_name = PathBuf::from(archive_name); full_archive_name.set_extension("didx"); diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index faf2a08f..a84368a6 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -26,25 +26,44 @@ pub struct DataStore { gc_mutex: Mutex, } -/// Detailed Backup Information +/// Uniquely identify backups (relative to data store) #[derive(Debug)] -pub struct BackupInfo { +pub struct BackupDir { /// Type of backup pub backup_type: String, /// Unique (for this type) ID pub backup_id: String, /// Backup timestamp pub backup_time: DateTime, +} + +impl BackupDir { + + pub fn relative_path(&self) -> PathBuf { + + let mut relative_path = PathBuf::new(); + + relative_path.push(&self.backup_type); + + relative_path.push(&self.backup_id); + + let date_str = self.backup_time.format("%Y-%m-%dT%H:%M:%S").to_string(); + + relative_path.push(&date_str); + + relative_path + } +} + +/// Detailed Backup Information +#[derive(Debug)] +pub struct BackupInfo { + /// the backup directory + pub backup_dir: BackupDir, /// List of data files 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()); @@ -153,35 +172,11 @@ impl DataStore { self.chunk_store.base_path() } - pub fn get_backup_dir( - &self, - backup_type: &str, - backup_id: &str, - backup_time: DateTime, - ) -> PathBuf { - - let mut relative_path = PathBuf::new(); - - relative_path.push(backup_type); - - relative_path.push(backup_id); - - let date_str = backup_time.format("%Y-%m-%dT%H:%M:%S").to_string(); - - relative_path.push(&date_str); - - relative_path - } - /// Remove a backup directory including all content - pub fn remove_backup_dir( - &self, - backup_type: &str, - backup_id: &str, - backup_time: DateTime, + pub fn remove_backup_dir(&self, backup_dir: &BackupDir, ) -> Result<(), io::Error> { - let relative_path = self.get_backup_dir(backup_type, backup_id, backup_time); + let relative_path = backup_dir.relative_path(); let mut full_path = self.base_path(); full_path.push(&relative_path); @@ -253,9 +248,11 @@ impl DataStore { })?; list.push(BackupInfo { - backup_type: backup_type.to_owned(), - backup_id: backup_id.to_owned(), - backup_time: dt, + backup_dir: BackupDir { + backup_type: backup_type.to_owned(), + backup_id: backup_id.to_owned(), + backup_time: dt, + }, files, });