src/backup/datastore.rs: impl last_backup helper

This commit is contained in:
Dietmar Maurer 2019-05-11 11:21:13 +02:00
parent c09775011a
commit b02a52e3a5
4 changed files with 19 additions and 2 deletions

View File

@ -73,14 +73,19 @@ fn upgrade_to_backup_protocol(
let username = rpcenv.get_user().unwrap(); let username = rpcenv.get_user().unwrap();
let env_type = rpcenv.env_type(); let env_type = rpcenv.env_type();
let last_backup = datastore.last_backup(backup_type, backup_id)?;
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time.timestamp()); let backup_dir = BackupDir::new(backup_type, backup_id, backup_time.timestamp());
let (path, is_new) = datastore.create_backup_dir(&backup_dir)?; let (path, is_new) = datastore.create_backup_dir(&backup_dir)?;
if !is_new { bail!("backup directorty already exists."); } if !is_new { bail!("backup directorty already exists."); }
WorkerTask::spawn("backup", Some(worker_id), &username.clone(), true, move |worker| { WorkerTask::spawn("backup", Some(worker_id), &username.clone(), true, move |worker| {
let backup_env = BackupEnvironment::new( let mut backup_env = BackupEnvironment::new(
env_type, username.clone(), worker.clone(), datastore, backup_dir, path); env_type, username.clone(), worker.clone(), datastore, backup_dir, path);
backup_env.last_backup = last_backup;
let service = BackupService::new(backup_env, worker.clone()); let service = BackupService::new(backup_env, worker.clone());
let abort_future = worker.abort_future(); let abort_future = worker.abort_future();

View File

@ -27,6 +27,7 @@ pub struct BackupEnvironment {
pub datastore: Arc<DataStore>, pub datastore: Arc<DataStore>,
pub backup_dir: BackupDir, pub backup_dir: BackupDir,
pub path: PathBuf, pub path: PathBuf,
pub last_backup: Option<BackupInfo>,
state: Arc<Mutex<SharedBackupState>> state: Arc<Mutex<SharedBackupState>>
} }
@ -54,6 +55,7 @@ impl BackupEnvironment {
formatter: &JSON_FORMATTER, formatter: &JSON_FORMATTER,
backup_dir, backup_dir,
path, path,
last_backup: None,
state: Arc::new(Mutex::new(state)), state: Arc::new(Mutex::new(state)),
} }
} }

View File

@ -161,7 +161,7 @@ impl From<(BackupGroup, i64)> for BackupDir {
} }
/// Detailed Backup Information, lists files inside a BackupDir /// Detailed Backup Information, lists files inside a BackupDir
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct BackupInfo { pub struct BackupInfo {
/// the backup directory /// the backup directory
pub backup_dir: BackupDir, pub backup_dir: BackupDir,

View File

@ -168,6 +168,16 @@ impl DataStore {
} }
} }
/// Finds the latest backup inside a backup group
pub fn last_backup(&self, backup_type: &str, backup_id: &str) -> Result<Option<BackupInfo>, Error> {
let group = BackupGroup::new(backup_type, backup_id);
let backups = group.list_backups(&self.base_path())?;
Ok(backups.into_iter().max_by_key(|item| item.backup_dir.backup_time()))
}
pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> { pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
let base = self.base_path(); let base = self.base_path();