Format restore logic

This commit is contained in:
thePR0M3TH3AN
2025-05-21 19:42:52 -04:00
parent 99177a3b26
commit 15e892055f

View File

@@ -9,6 +9,7 @@
mod cli; // sub-command definitions and argument structs
/* ── shared modules re-exported from libmarlin ─────────────────── */
use libmarlin::backup::BackupManager;
use libmarlin::db::take_dirty;
use libmarlin::{config, db, logging, scan, utils::determine_scan_root};
@@ -106,8 +107,21 @@ fn main() -> Result<()> {
Commands::Restore { backup_path } => {
drop(conn);
db::restore(&backup_path, &cfg.db_path)
.with_context(|| format!("Failed to restore DB from {}", backup_path.display()))?;
if backup_path.exists() {
db::restore(&backup_path, &cfg.db_path).with_context(|| {
format!("Failed to restore DB from {}", backup_path.display())
})?;
} else {
let backups_dir = cfg.db_path.parent().unwrap().join("backups");
let manager = BackupManager::new(&cfg.db_path, &backups_dir)?;
let name = backup_path
.file_name()
.and_then(|n| n.to_str())
.context("invalid backup file name")?;
manager.restore_from_backup(name).with_context(|| {
format!("Failed to restore DB from {}", backup_path.display())
})?;
}
println!("Restored DB from {}", backup_path.display());
db::open(&cfg.db_path).with_context(|| {
format!("Could not open restored DB at {}", cfg.db_path.display())