diff --git a/src/backup/manager/meta.rs b/src/backup/manager/meta.rs index de82576..2e0d703 100644 --- a/src/backup/manager/meta.rs +++ b/src/backup/manager/meta.rs @@ -122,10 +122,11 @@ where &self, layer: &str, start_time: chrono::DateTime, + dirs: &Vec<(PathBuf, PathBuf)>, ) -> Option> { self.managers .get(layer) - .map(|manager| manager.restore_backup(start_time)) + .map(|manager| manager.restore_backup(start_time, dirs)) } pub fn managers(&self) -> &HashMap> { diff --git a/src/backup/manager/mod.rs b/src/backup/manager/mod.rs index 47488d9..7969021 100644 --- a/src/backup/manager/mod.rs +++ b/src/backup/manager/mod.rs @@ -161,7 +161,11 @@ where /// Restore the backup with the given start time by restoring its chain up to and including the /// backup, in order. - pub fn restore_backup(&self, start_time: chrono::DateTime) -> io::Result<()> { + pub fn restore_backup( + &self, + start_time: chrono::DateTime, + dirs: &Vec<(PathBuf, PathBuf)>, + ) -> io::Result<()> { // Iterate over each chain, skipping elements until the element with the given start time // is possibly found. for chain in &self.chains { @@ -172,7 +176,7 @@ where .position(|b| b.start_time.trunc_subsecs(0) == start_time) { for backup in chain.iter().take(index + 1) { - backup.restore(&self.backup_dir, &self.dirs)?; + backup.restore(&self.backup_dir, dirs)?; } return Ok(()); diff --git a/src/cli/backup.rs b/src/cli/backup.rs index a77eae9..225fb42 100644 --- a/src/cli/backup.rs +++ b/src/cli/backup.rs @@ -38,9 +38,16 @@ pub struct BackupListArgs { pub struct BackupRestoreArgs { /// Path to the backup inside the backup directory path: PathBuf, + /// Directory to store config in + output_config: PathBuf, + /// Directory to store worlds in + output_worlds: PathBuf, /// Whether to overwrite the contents of the existing directories #[arg(short, long, default_value_t = false)] force: bool, + /// Create output directories if they don't exist + #[arg(short, long, default_value_t = false)] + make: bool, } impl BackupArgs { @@ -69,6 +76,15 @@ impl BackupRestoreArgs { pub fn run(&self, cli: &Cli) -> io::Result<()> { let backup_dir = cli.backup.canonicalize()?; + // Create directories if needed + if self.make { + std::fs::create_dir_all(&self.output_config)?; + std::fs::create_dir_all(&self.output_worlds)?; + } + + let output_config = self.output_config.canonicalize()?; + let output_worlds = self.output_worlds.canonicalize()?; + // Parse input path let path = self.path.canonicalize()?; @@ -97,11 +113,9 @@ impl BackupRestoreArgs { let meta = cli.meta()?; // Clear previous contents of directories - let mut entries = cli - .config - .canonicalize()? + let mut entries = output_config .read_dir()? - .chain(cli.world.canonicalize()?.read_dir()?) + .chain(output_worlds.read_dir()?) .peekable(); if entries.peek().is_some() && !self.force { @@ -118,8 +132,13 @@ impl BackupRestoreArgs { } } + let dirs = vec![ + (PathBuf::from("config"), output_config), + (PathBuf::from("worlds"), output_worlds), + ]; + // Restore the backup - if let Some(res) = meta.restore_backup(&layer, timestamp) { + if let Some(res) = meta.restore_backup(&layer, timestamp, &dirs) { res } else { Err(other("Unknown layer"))