feat: show message describing what layer is backing up
parent
29636ffcdb
commit
03e21fda87
|
@ -1,3 +1,3 @@
|
||||||
[alias]
|
[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"
|
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"
|
||||||
|
|
|
@ -6,8 +6,8 @@ use std::str::FromStr;
|
||||||
pub struct ManagerConfig {
|
pub struct ManagerConfig {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub frequency: u32,
|
pub frequency: u32,
|
||||||
pub chain_len: u64,
|
|
||||||
pub chains: u64,
|
pub chains: u64,
|
||||||
|
pub chain_len: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
@ -2,6 +2,7 @@ use super::{Manager, ManagerConfig};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
@ -13,7 +14,7 @@ where
|
||||||
config_dir: PathBuf,
|
config_dir: PathBuf,
|
||||||
world_dir: PathBuf,
|
world_dir: PathBuf,
|
||||||
default_metadata: T,
|
default_metadata: T,
|
||||||
managers: Vec<Manager<T>>,
|
managers: HashMap<String, Manager<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> MetaManager<T>
|
impl<T> MetaManager<T>
|
||||||
|
@ -31,7 +32,7 @@ where
|
||||||
config_dir: config_dir.into(),
|
config_dir: config_dir.into(),
|
||||||
world_dir: world_dir.into(),
|
world_dir: world_dir.into(),
|
||||||
default_metadata,
|
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
|
// Backup dir itself should exist, but we control its contents, so we can create
|
||||||
// separate directories for each layer
|
// separate directories for each layer
|
||||||
let path = self.backup_dir.join(&config.name);
|
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(
|
let mut manager = Manager::new(
|
||||||
path,
|
path,
|
||||||
|
@ -51,7 +60,7 @@ where
|
||||||
chrono::Duration::minutes(config.frequency.into()),
|
chrono::Duration::minutes(config.frequency.into()),
|
||||||
);
|
);
|
||||||
manager.load()?;
|
manager.load()?;
|
||||||
self.managers.push(manager);
|
self.managers.insert(config.name.clone(), manager);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -64,14 +73,24 @@ where
|
||||||
Ok(())
|
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<chrono::DateTime<Utc>> {
|
pub fn next_scheduled_time(&self) -> Option<chrono::DateTime<Utc>> {
|
||||||
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<()> {
|
pub fn perform_backup_cycle(&mut self) -> io::Result<()> {
|
||||||
if let Some(manager) = self
|
if let Some(manager) = self
|
||||||
.managers
|
.managers
|
||||||
.iter_mut()
|
.values_mut()
|
||||||
.min_by_key(|m| m.next_scheduled_time())
|
.min_by_key(|m| m.next_scheduled_time())
|
||||||
{
|
{
|
||||||
manager.create_backup()?;
|
manager.create_backup()?;
|
||||||
|
|
|
@ -48,7 +48,8 @@ impl ServerProcess {
|
||||||
/// Perform a backup by disabling the server's save feature and flushing its data, before
|
/// Perform a backup by disabling the server's save feature and flushing its data, before
|
||||||
/// creating an archive file.
|
/// creating an archive file.
|
||||||
pub fn backup(&mut self) -> std::io::Result<()> {
|
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
|
// Make sure the server isn't modifying the files during the backup
|
||||||
self.custom("save-off")?;
|
self.custom("save-off")?;
|
||||||
|
@ -73,11 +74,14 @@ impl ServerProcess {
|
||||||
);
|
);
|
||||||
|
|
||||||
if res.is_ok() {
|
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 {
|
} else {
|
||||||
self.custom(&format!(
|
self.custom(&format!(
|
||||||
"an error occured after {} while backing up the server",
|
"an error occured after {} while creating backup for layer '{}'",
|
||||||
duration_str
|
duration_str, layer_name
|
||||||
))?;
|
))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue