Compare commits

...

2 Commits

Author SHA1 Message Date
Jef Roosens 55c5f24937
feat: specify output dirs for restore instead of using config & world
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline was successful Details
ci/woodpecker/push/build Pipeline was successful Details
2023-07-04 15:36:12 +02:00
Jef Roosens 36c441b8c2
feat: respect backup list filter option 2023-07-04 14:15:00 +02:00
3 changed files with 38 additions and 9 deletions

View File

@ -122,10 +122,11 @@ where
&self,
layer: &str,
start_time: chrono::DateTime<Utc>,
dirs: &Vec<(PathBuf, PathBuf)>,
) -> Option<io::Result<()>> {
self.managers
.get(layer)
.map(|manager| manager.restore_backup(start_time))
.map(|manager| manager.restore_backup(start_time, dirs))
}
pub fn managers(&self) -> &HashMap<String, Manager<T>> {

View File

@ -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<Utc>) -> io::Result<()> {
pub fn restore_backup(
&self,
start_time: chrono::DateTime<Utc>,
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(());

View File

@ -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"))
@ -131,7 +150,12 @@ impl BackupListArgs {
pub fn run(&self, cli: &Cli) -> io::Result<()> {
let meta = cli.meta()?;
for (name, manager) in meta.managers().iter() {
// A bit scuffed? Sure
for (name, manager) in meta
.managers()
.iter()
.filter(|(name, _)| self.layer.is_none() || &self.layer.as_ref().unwrap() == name)
{
println!("{}", name);
for chain in manager.chains().iter().filter(|c| !c.is_empty()) {