feat: create backups from cli for specific layer
parent
1cfe13674d
commit
e373fc85f1
|
@ -1,3 +1,3 @@
|
|||
[alias]
|
||||
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"
|
||||
runs = "run -- --config data/config --backup data/backups --world data/worlds --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"
|
||||
|
|
|
@ -101,4 +101,19 @@ where
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a manual backup for a specific layer
|
||||
pub fn create_backup(&mut self, layer: &str) -> Option<io::Result<()>> {
|
||||
if let Some(manager) = self.managers.get_mut(layer) {
|
||||
let mut res = manager.create_backup();
|
||||
|
||||
if res.is_ok() {
|
||||
res = manager.remove_old_backups();
|
||||
}
|
||||
|
||||
Some(res)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
use crate::cli::Cli;
|
||||
use clap::{Args, Subcommand};
|
||||
use std::io;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum BackupCommands {
|
||||
Create(BackupCreateArgs),
|
||||
List,
|
||||
}
|
||||
|
||||
|
@ -10,3 +13,30 @@ pub struct BackupArgs {
|
|||
#[command(subcommand)]
|
||||
pub command: BackupCommands,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct BackupCreateArgs {
|
||||
/// What layer to create a backup in
|
||||
layer: String,
|
||||
}
|
||||
|
||||
impl BackupArgs {
|
||||
pub fn run(&self, cli: &Cli) -> io::Result<()> {
|
||||
match &self.command {
|
||||
BackupCommands::Create(args) => args.run(cli),
|
||||
BackupCommands::List => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BackupCreateArgs {
|
||||
pub fn run(&self, cli: &Cli) -> io::Result<()> {
|
||||
let mut meta = cli.meta()?;
|
||||
|
||||
if let Some(res) = meta.create_backup(&self.layer) {
|
||||
res
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Unknown layer"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
mod backup;
|
||||
mod run;
|
||||
|
||||
pub use crate::backup::MetaManager;
|
||||
pub use crate::server::Metadata;
|
||||
pub use backup::{BackupArgs, BackupCommands};
|
||||
pub use run::RunArgs;
|
||||
|
||||
|
@ -68,7 +70,23 @@ impl Cli {
|
|||
pub fn run(&self) -> io::Result<()> {
|
||||
match &self.command {
|
||||
Commands::Run(args) => args.run(self),
|
||||
Commands::Backup(_) => Ok(()),
|
||||
Commands::Backup(args) => args.run(self),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience method to initialize backup manager from the cli arguments
|
||||
pub fn meta(&self) -> io::Result<MetaManager<Metadata>> {
|
||||
let metadata = Metadata {
|
||||
server_type: self.server,
|
||||
server_version: self.server_version.clone(),
|
||||
};
|
||||
let dirs = vec![
|
||||
(PathBuf::from("config"), self.config.clone()),
|
||||
(PathBuf::from("worlds"), self.world.clone()),
|
||||
];
|
||||
let mut meta = MetaManager::new(self.backup.clone(), dirs, metadata);
|
||||
meta.add_all(&self.layers)?;
|
||||
|
||||
Ok(meta)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue