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

export-backup
Jef Roosens 2023-07-04 15:36:12 +02:00
parent 36c441b8c2
commit 55c5f24937
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 32 additions and 8 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"))