diff --git a/.cargo/config.toml b/.cargo/config.toml index ed8b85c..c5834ea 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [alias] -runs = "run -- run --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar --frequency 2" +runs = "run -- run --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar --layers 2min,2,4,4;3min,3,2,2" runrs = "run --release -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar" diff --git a/src/backup/manager/config.rs b/src/backup/manager/config.rs index c5ba217..8adbd5a 100644 --- a/src/backup/manager/config.rs +++ b/src/backup/manager/config.rs @@ -6,8 +6,8 @@ use std::str::FromStr; pub struct ManagerConfig { pub name: String, pub frequency: u32, - pub chain_len: u64, pub chains: u64, + pub chain_len: u64, } #[derive(Debug)] diff --git a/src/backup/manager/meta.rs b/src/backup/manager/meta.rs index 5878f34..bf5cc4b 100644 --- a/src/backup/manager/meta.rs +++ b/src/backup/manager/meta.rs @@ -2,6 +2,7 @@ use super::{Manager, ManagerConfig}; use chrono::Utc; use serde::Deserialize; use serde::Serialize; +use std::collections::HashMap; use std::io; use std::path::PathBuf; @@ -13,7 +14,7 @@ where config_dir: PathBuf, world_dir: PathBuf, default_metadata: T, - managers: Vec>, + managers: HashMap>, } impl MetaManager @@ -31,7 +32,7 @@ where config_dir: config_dir.into(), world_dir: world_dir.into(), default_metadata, - managers: Vec::new(), + managers: HashMap::new(), } } @@ -39,7 +40,15 @@ where // Backup dir itself should exist, but we control its contents, so we can create // separate directories for each layer let path = self.backup_dir.join(&config.name); - std::fs::create_dir(&path)?; + + // If the directory already exists, that's okay + match std::fs::create_dir(&path) { + Ok(()) => (), + Err(e) => match e.kind() { + io::ErrorKind::AlreadyExists => (), + _ => return Err(e), + }, + }; let mut manager = Manager::new( path, @@ -51,7 +60,7 @@ where chrono::Duration::minutes(config.frequency.into()), ); manager.load()?; - self.managers.push(manager); + self.managers.insert(config.name.clone(), manager); Ok(()) } @@ -64,14 +73,24 @@ where Ok(()) } + pub fn next_scheduled_layer(&self) -> Option<&str> { + self.managers + .iter() + .min_by_key(|(_, m)| m.next_scheduled_time()) + .map(|(k, _)| k.as_str()) + } + pub fn next_scheduled_time(&self) -> Option> { - self.managers.iter().map(|m| m.next_scheduled_time()).min() + self.managers + .values() + .map(|m| m.next_scheduled_time()) + .min() } pub fn perform_backup_cycle(&mut self) -> io::Result<()> { if let Some(manager) = self .managers - .iter_mut() + .values_mut() .min_by_key(|m| m.next_scheduled_time()) { manager.create_backup()?; diff --git a/src/server/process.rs b/src/server/process.rs index ef6822e..7748c1d 100644 --- a/src/server/process.rs +++ b/src/server/process.rs @@ -48,7 +48,8 @@ impl ServerProcess { /// Perform a backup by disabling the server's save feature and flushing its data, before /// creating an archive file. pub fn backup(&mut self) -> std::io::Result<()> { - self.custom("say backing up server")?; + let layer_name = String::from(self.backups.next_scheduled_layer().unwrap()); + self.custom(&format!("say starting backup for layer '{}'", layer_name))?; // Make sure the server isn't modifying the files during the backup self.custom("save-off")?; @@ -73,11 +74,14 @@ impl ServerProcess { ); if res.is_ok() { - self.custom(&format!("say server backed up in {}", duration_str))?; + self.custom(&format!( + "say backup created for layer '{}' in {}", + layer_name, duration_str + ))?; } else { self.custom(&format!( - "an error occured after {} while backing up the server", - duration_str + "an error occured after {} while creating backup for layer '{}'", + duration_str, layer_name ))?; }