//! Recovery points: configuration + database dumps stored under `.manager/backups//`. //! //! Rules enforced here: a recovery point only becomes visible once complete (built in a `.partial` folder), //! every artefact is checksummed, restore never drops anything (the replaced database is kept under a new name) //! and restoring a database requires the game servers to be stopped. use std::fs; use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use crate::db::{self, Account, Db}; use crate::driver::{self, Verb}; use crate::error::{Error, Result}; use crate::fsx; use crate::layout::read_ports; use crate::process::{self, ServiceState}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Kind { /// characters + auth databases, configs, manager metadata. Quick, /// Quick + the (large) world database. Full, Config, /// characters + auth databases only. Database, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Trigger { Manual, Automatic, BeforeUpdate, BeforeRestore, BeforeMigration, BeforeBots, BeforeRepair, BeforeDangerousChange, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Component { /// "characters" | "auth" | "world" | "configs" pub name: String, /// File (databases) or folder (configs), relative to the recovery point. pub path: String, pub bytes: u64, pub sha256: Option, pub tables: Option, pub files: Option>, #[serde(default)] pub file_sha256: std::collections::BTreeMap, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RecoveryPoint { pub schema: u32, pub id: String, pub kind: Kind, pub trigger: Trigger, pub label: Option, pub created_at: String, pub manager_version: String, pub components: Vec, #[serde(default)] pub realm: crate::realms::Mode, #[serde(default, skip_serializing_if = "Option::is_none")] pub(crate) mysql_snapshot: Option, } const LOCATION_FILE: &str = "backup-location.json"; #[derive(Serialize, Deserialize)] struct StoredLocation { path: String, } fn default_dir(meta: &Path) -> PathBuf { meta.join("backups") } /// The folder the owner chose for backups (for example on another drive), if any. fn custom_dir(meta: &Path) -> Option { let stored: StoredLocation = fsx::read_json(&meta.join(LOCATION_FILE)).ok()?; let path = PathBuf::from(stored.path.trim()); path.is_absolute().then_some(path) } /// Where new backups go. fn backups_dir(meta: &Path) -> PathBuf { custom_dir(meta).unwrap_or_else(|| default_dir(meta)) } /// Every folder that may hold backups: the chosen one first, then the default one, so backups made before the /// folder was changed stay visible and restorable. fn backup_roots(meta: &Path) -> Vec { let mut roots: Vec = custom_dir(meta).into_iter().collect(); let default = default_dir(meta); if !roots.contains(&default) { roots.push(default); } roots } #[derive(Debug, Serialize)] pub struct BackupLocation { pub path: String, pub default_path: String, pub is_default: bool, } pub fn location(meta: &Path) -> BackupLocation { let default = default_dir(meta); let current = backups_dir(meta); BackupLocation { is_default: current == default, path: current.to_string_lossy().into_owned(), default_path: default.to_string_lossy().into_owned(), } } /// Choose where new backups are stored (`None` = the default folder next to the server). Existing backups stay where /// they are and remain listed. pub fn set_location(root: &Path, meta: &Path, path: Option<&str>) -> Result { let _lock = crate::update::operation_lock(meta)?; crate::update::ensure_recovered(meta)?; match path.map(str::trim).filter(|p| !p.is_empty()) { None => { let file = meta.join(LOCATION_FILE); if file.exists() { fs::remove_file(file)?; } } Some(chosen) => { let dir = PathBuf::from(chosen); if !dir.is_absolute() { return Err(Error::Invalid( "Choose a full folder path, for example D:\\CoA backups.".into(), )); } if fsx::ensure_within(root, &dir).is_ok() { return Err(Error::Invalid("Backups cannot be stored inside the server folder: they would be part of what is being backed up.".into())); } fs::create_dir_all(&dir) .map_err(|e| Error::Invalid(format!("The folder cannot be created: {e}")))?; let probe = dir.join(format!(".coa-write-test-{}", uuid::Uuid::new_v4().simple())); fs::write(&probe, b"x") .map_err(|e| Error::Invalid(format!("The folder is not writable: {e}")))?; let _ = fs::remove_file(&probe); fsx::atomic_write_json( &meta.join(LOCATION_FILE), &StoredLocation { path: dir.to_string_lossy().into_owned(), }, )?; } } Ok(location(meta)) } /// The `backup.json` of a recovery point, wherever its folder is. pub(crate) fn point_json(meta: &Path, id: &str) -> Result { Ok(point_dir(meta, id)?.join("backup.json")) } fn id_ok(id: &str) -> bool { !id.is_empty() && id.len() < 100 && id .chars() .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')) && !id.contains("..") } fn point_dir(meta: &Path, id: &str) -> Result { if !id_ok(id) { return Err(Error::PathRejected(format!("bad backup id {id:?}"))); } if let Some(found) = backup_roots(meta) .into_iter() .map(|root| root.join(id)) .find(|dir| dir.is_dir()) { return Ok(found); } Ok(backups_dir(meta).join(id)) } pub fn list(meta: &Path) -> Vec { let mut out: Vec = Vec::new(); for root in backup_roots(meta) { let found: Vec = fs::read_dir(&root) .map(|rd| { rd.filter_map(|e| e.ok()) .filter_map(|e| fsx::read_json(&e.path().join("backup.json")).ok()) .collect() }) .unwrap_or_default(); for point in found { if !out.iter().any(|p| p.id == point.id) { out.push(point); } } } out.sort_by(|a, b| b.id.cmp(&a.id)); out } pub fn get(meta: &Path, id: &str) -> Result { let dir = point_dir(meta, id)?; // The folder holding the backups may itself be a junction or symlink to another drive (the owner moved it there); // what must hold is that the recovery point stays inside that folder. let holder = dir .parent() .ok_or_else(|| Error::PathRejected(format!("bad backup folder for {id}")))?; let point: RecoveryPoint = fsx::read_json(&fsx::ensure_within(holder, &dir.join("backup.json"))?).map_err(|_| { Error::Invalid(format!( "backup {id} was not found or its metadata is damaged" )) })?; if point.id != id || point.schema != 1 { return Err(Error::Invalid(format!( "Backup {id} has inconsistent identity or an unsupported schema." ))); } Ok(point) } /// Files that make up "configuration": everything a user or the Manager may have changed, minus secrets. pub fn config_files(root: &Path) -> Vec { const MAX: u64 = 8 * 1024 * 1024; let mut out = Vec::new(); fn walk(root: &Path, dir: &Path, out: &mut Vec) { let Ok(rd) = fs::read_dir(dir) else { return }; for e in rd.flatten() { let p = e.path(); let Ok(md) = e.metadata() else { continue }; if md.is_dir() { walk(root, &p, out); } else if md.len() <= MAX { let name = p.file_name().unwrap().to_string_lossy().to_lowercase(); if name.ends_with(".bak") || name.ends_with(".orig") || name.ends_with(".old") || name.contains(".bak-") { continue; } if let Ok(rel) = p.strip_prefix(root) { out.push(rel.to_string_lossy().replace('\\', "/")); } } } } walk(root, &root.join("Core/configs"), &mut out); if let Ok(rd) = fs::read_dir(root.join("Settings")) { for e in rd.flatten() { let n = e.file_name().to_string_lossy().to_string(); // database.json holds generated database passwords; it is launcher state, not configuration. if e.path().is_file() && (n.contains(".template") || n == "repack.json" || n == "docker.json") { out.push(format!("Settings/{n}")); } } } if root.join("RELEASE.json").is_file() { out.push("RELEASE.json".into()); } walk(root, &root.join("Settings/realm-profiles"), &mut out); if root.join("Settings/realm-profile.json").is_file() { out.push("Settings/realm-profile.json".into()); } out.sort(); out } fn copy_configs(root: &Path, dir: &Path) -> Result { let files = config_files(root); let target = dir.join("files"); let mut bytes = 0; let mut file_sha256 = std::collections::BTreeMap::new(); for rel in &files { let src = fsx::safe_join(root, rel)?; let dst = fsx::safe_join(&target, rel)?; let content = fs::read(&src)?; fsx::atomic_write(&dst, &content)?; bytes += content.len() as u64; file_sha256.insert(rel.clone(), fsx::sha256_bytes(&content)); } Ok(Component { name: "configs".into(), path: "files".into(), bytes, sha256: None, tables: None, files: Some(files), file_sha256, }) } /// Make sure the database is up for `f`. If we had to start it (and nothing else is running), stop it again. pub fn with_database(root: &Path, f: impl FnOnce(&Db) -> Result) -> Result { let db = Db::from_repack(root, Account::Admin)?; let observed = process::observe(root, &read_ports(root)); let mysql_was_up = observed.mysql.state == ServiceState::Running && db.ping(); if !mysql_was_up { let out = driver::run(root, Verb::StartMysql)?; if !out.ok { // Keep the launcher's own last lines: the title alone ("Something went wrong") hides the cause. return Err(Error::Invalid(format!( "The database could not be started. {}", driver::startup_failure(root, &out) ))); } } let result = f(&db); if !mysql_was_up { let now = process::observe(root, &read_ports(root)); if now.world.state == ServiceState::Stopped && now.auth.state == ServiceState::Stopped && !crate::multiworld::is_running(root) { let _ = driver::run(root, Verb::StopAll); } } result } /// Run `f` only if the database is already up; `None` (and nothing started) otherwise. pub fn with_running_database( root: &Path, f: impl FnOnce(&Db) -> Result, ) -> Result> { let db = Db::from_repack(root, Account::Admin)?; let observed = process::observe(root, &read_ports(root)); if observed.mysql.state != ServiceState::Running || !db.ping() { return Ok(None); } f(&db).map(Some) } fn wanted_databases(kind: Kind) -> &'static [&'static str] { match kind { Kind::Quick | Kind::Database => &["characters", "auth"], Kind::Full => &["characters", "auth", "world"], Kind::Config => &[], } } /// Create a recovery point. The server may be running: dumps are consistent snapshots (`--single-transaction`). pub fn create( root: &Path, meta: &Path, kind: Kind, trigger: Trigger, label: Option, progress: &dyn Fn(&str), ) -> Result { let id = format!( "{}-{}-{}", chrono::Utc::now().format("%Y%m%d-%H%M%S"), match trigger { Trigger::Manual => "manual", Trigger::Automatic => "auto", Trigger::BeforeUpdate => "before-update", Trigger::BeforeRestore => "before-restore", Trigger::BeforeMigration => "before-migration", Trigger::BeforeBots => "before-bots", Trigger::BeforeRepair => "before-repair", Trigger::BeforeDangerousChange => "before-change", }, uuid::Uuid::new_v4().simple() ); let final_dir = point_dir(meta, &id)?; let partial = backups_dir(meta).join(format!("{id}.partial")); fs::create_dir_all(&partial)?; let build = || -> Result> { let mut components = Vec::new(); let realm = crate::realms::state(root)?; let mut databases: Vec<(String, &str)> = wanted_databases(kind) .iter() .map(|name| Ok((name.to_string(), realm.active.schema(name)?))) .collect::>()?; if kind != Kind::Config { for other in realm.modes().into_iter().filter(|m| *m != realm.active) { databases.push(( format!("{}-characters", other.name()), other.schema("characters")?, )); if kind == Kind::Full { databases.push((format!("{}-world", other.name()), other.schema("world")?)); } } } if kind != Kind::Config && with_database(root, |db| db.schema_exists("acore_playerbots"))? { databases.push(("playerbots".into(), "acore_playerbots")); } for (name, schema) in databases { progress(&format!("Backing up {name} database")); let component = with_database(root, |db| { let db = db.clone().for_realm(crate::realms::Mode::Coa); let size = db.schema_bytes(schema)?; fsx::require_space(&partial, size / 2 + 32 * 1024 * 1024)?; let tables = db.tables(schema)?.len(); let file = format!("{name}.sql.zst"); let (bytes, sha) = db.dump_to(schema, &partial.join(&file))?; Ok(Component { name, path: file, bytes, sha256: Some(sha), tables: Some(tables), files: None, file_sha256: Default::default(), }) })?; components.push(component); } if kind != Kind::Database { progress("Saving configuration"); components.push(copy_configs(root, &partial)?); } Ok(components) }; let components = match build() { Ok(c) => c, Err(e) => { let _ = fs::remove_dir_all(&partial); // our own unfinished folder return Err(e); } }; let mysql_snapshot = if kind == Kind::Full && matches!(trigger, Trigger::BeforeUpdate | Trigger::BeforeRepair) && cfg!(windows) && !crate::docker::is_docker(root) { progress("Saving a complete stopped MySQL recovery copy"); match crate::mysql_snapshot::capture(root, &partial) { Ok(snapshot) => Some(snapshot), Err(error) => { if let Ok(owned) = fsx::ensure_within(&backups_dir(meta), &partial) { let _ = fs::remove_dir_all(owned); } return Err(error); } } } else { None }; let point = RecoveryPoint { realm: crate::realms::state(root)?.active, schema: 1, id: id.clone(), kind, trigger, label, created_at: chrono::Utc::now().to_rfc3339(), manager_version: crate::MANAGER_VERSION.into(), components, mysql_snapshot, }; fsx::atomic_write_json(&partial.join("backup.json"), &point)?; fs::rename(&partial, &final_dir)?; tracing::info!(%id, ?kind, ?trigger, "recovery point created"); if trigger == Trigger::Automatic { prune_automatic(meta, 10); } Ok(point) } /// Keep the newest `keep` automatic recovery points; manual and safety ones are never pruned. pub fn prune_automatic(meta: &Path, keep: usize) { if crate::update::ensure_recovered(meta).is_err() { return; } let autos: Vec<_> = list(meta) .into_iter() .filter(|p| p.trigger == Trigger::Automatic) .collect(); for p in autos.into_iter().skip(keep) { if let Ok(dir) = point_dir(meta, &p.id) { let _ = fs::remove_dir_all(dir); } } } #[derive(Debug, Serialize)] pub struct VerifyReport { pub ok: bool, pub problems: Vec, } pub fn verify(meta: &Path, id: &str) -> Result { let point = get(meta, id)?; let dir = point_dir(meta, id)?; let mut problems = Vec::new(); if let Some(snapshot) = &point.mysql_snapshot { if let Err(error) = crate::mysql_snapshot::verify(&dir, snapshot) { problems.push(error.to_string()); } } let mut names = std::collections::BTreeSet::new(); let mut schemas = std::collections::BTreeSet::new(); for c in &point.components { if !names.insert(&c.name) { problems.push(format!("{} appears more than once", c.name)); } let path = fsx::ensure_within(&dir, &fsx::safe_join(&dir, &c.path)?)?; if c.name != "configs" && (c.sha256.is_none() || c.tables.is_none()) { problems.push(format!("{} lacks database integrity metadata", c.name)); } if c.name == "configs" { if c.sha256.is_some() || c.files.is_none() { problems.push("configuration integrity metadata is inconsistent".into()); } } else { let schema = if c.name.contains('-') || c.name == "playerbots" { db::schema_of(&c.name) } else { point.realm.schema(&c.name) }; match schema { Ok(schema) if schemas.insert(schema) => {} _ => problems.push(format!( "{} has an unknown or duplicate database target", c.name )), } } match (&c.sha256, &c.files) { (Some(sha), _) => match fsx::sha256_file(&path) { Ok(actual) if actual.eq_ignore_ascii_case(sha) => {} Ok(_) => problems.push(format!("{} is damaged (checksum mismatch)", c.name)), Err(_) => problems.push(format!("{} is missing", c.name)), }, (None, Some(files)) => { for f in files { if !c.file_sha256.is_empty() && !c.file_sha256.contains_key(f) { problems.push(format!("configuration file {f} lacks its checksum")); } if !fsx::safe_join(&path, f) .map(|p| p.is_file()) .unwrap_or(false) { problems.push(format!("configuration file {f} is missing")); } else if let Some(expected) = c.file_sha256.get(f) { let file = fsx::ensure_within(&dir, &fsx::safe_join(&path, f)?)?; if fsx::sha256_file(&file)?.as_str() != expected { problems.push(format!("configuration file {f} is damaged")); } } } } _ => problems.push(format!("{} lacks an integrity inventory", c.name)), } } Ok(VerifyReport { ok: problems.is_empty(), problems, }) } /// Delete one recovery point (its own folder only). pub fn delete(meta: &Path, id: &str) -> Result<()> { let _lock = crate::update::operation_lock(meta)?; crate::update::ensure_recovered(meta)?; if crate::update::unfinished(meta).is_some_and(|t| t.recovery_point.as_deref() == Some(id)) { return Err(Error::Invalid( "This recovery point is required by an unfinished update and cannot be deleted.".into(), )); } let dir = point_dir(meta, id)?; get(meta, id)?; // must be a real recovery point fs::remove_dir_all(dir)?; Ok(()) } /// Put configuration files back. Files the backup does not contain are left alone. A safety point is taken first. pub fn restore_configs(root: &Path, meta: &Path, id: &str) -> Result { let point = get(meta, id)?; if point.realm != crate::realms::state(root)?.active { return Err(Error::Invalid( "Select the realm this backup belongs to before restoring it.".into(), )); } let comp = point .components .iter() .find(|c| c.name == "configs") .ok_or_else(|| Error::Invalid("this backup has no configuration".into()))?; if !verify(meta, id)?.ok { return Err(Error::Invalid( "This backup is damaged and cannot be restored.".into(), )); } let safety = create( root, meta, Kind::Config, Trigger::BeforeRestore, Some(format!("before restoring {id}")), &|_| {}, )?; let src_root = point_dir(meta, id)?.join(&comp.path); for rel in comp.files.as_deref().unwrap_or_default() { let dst = fsx::ensure_within(root, &fsx::safe_join(root, rel)?)?; fsx::atomic_write(&dst, &fs::read(fsx::safe_join(&src_root, rel)?)?)?; } Ok(safety) } #[derive(Debug, Serialize)] pub struct DbRestore { /// The database the restore replaced, kept intact under this name. pub previous_schema: String, pub safety_backup: String, pub tables_restored: usize, } /// Restore one database from a recovery point without dropping anything: /// import into a staging schema, sanity-check it, then swap tables atomically and keep the old schema. pub fn restore_database(root: &Path, meta: &Path, id: &str, name: &str) -> Result { let point = get(meta, id)?; if point.realm != crate::realms::state(root)?.active { return Err(Error::Invalid( "Select the realm this backup belongs to before restoring it.".into(), )); } let comp = point .components .iter() .find(|c| c.name == name && c.sha256.is_some()) .ok_or_else(|| Error::Invalid(format!("this backup has no {name} database")))?; if !verify(meta, id)?.ok { return Err(Error::Invalid( "This backup is damaged and cannot be restored.".into(), )); } let now = process::observe(root, &read_ports(root)); if now.world.state != ServiceState::Stopped || now.auth.state != ServiceState::Stopped || crate::multiworld::is_running(root) { return Err(Error::Invalid( "Stop the server before restoring a database.".into(), )); } let live = if name.contains('-') || name == "playerbots" { db::schema_of(name)? } else { point.realm.schema(name)? }; let expected_tables = comp.tables.unwrap_or(0); let dump = point_dir(meta, id)?.join(&comp.path); let stamp = uuid::Uuid::new_v4().simple().to_string()[..16].to_string(); // 1. Safety copy of the current state, so even a wrong restore is reversible. let safety = create( root, meta, Kind::Database, Trigger::BeforeRestore, Some(format!("before restoring {name} from {id}")), &|_| {}, )?; with_database(root, |db| { let db = db.clone().for_realm(crate::realms::Mode::Coa); let staging = format!("{live}_restore_{stamp}"); let old = format!("{live}_before_restore_{stamp}"); if db.schema_exists(&staging)? || db.schema_exists(&old)? { return Err(Error::Invalid( "A previous restore left its work schemas behind; try again in a moment.".into(), )); } // 2. Import into staging. db.query(&format!( "CREATE DATABASE `{staging}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" ))?; db.import_from(&staging, &dump)?; // 3. Sanity checks. let staged = db.tables(&staging)?; if comp.tables.is_some_and(|expected| staged.len() != expected) || (comp.tables.is_none() && staged.is_empty()) { return Err(Error::Invalid(format!("The restored copy looks incomplete ({} of {expected_tables} tables); nothing was changed.", staged.len()))); } if db.extra_objects(&staging)? > 0 || db.extra_objects(live)? > 0 { return Err(Error::Invalid("This database contains routines, triggers or views; automatic restore does not support that. Nothing was changed.".into())); } // 4. Atomic swap: live tables move to the `before_restore` schema, staged tables move into place. let current = db.tables(live)?; db.query(&format!( "CREATE DATABASE `{old}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" ))?; let mut renames = Vec::new(); for t in ¤t { renames.push(format!("`{live}`.`{t}` TO `{old}`.`{t}`")); } for t in &staged { renames.push(format!("`{staging}`.`{t}` TO `{live}`.`{t}`")); } if !renames.is_empty() { db.query(&format!("RENAME TABLE {};", renames.join(", ")))?; } Ok(DbRestore { previous_schema: old, safety_backup: safety.id.clone(), tables_restored: staged.len(), }) }) } #[cfg(test)] mod tests { use super::*; fn setup() -> (tempfile::TempDir, PathBuf, PathBuf) { let dir = tempfile::tempdir().unwrap(); let root = dir.path().join("srv"); let meta = dir.path().join("srv.manager"); crate::layout::testkit::fake_repack(&root); fs::write( root.join("Settings/database.json"), r#"{"rootPassword":"secret-root","appPassword":"secret-app"}"#, ) .unwrap_or_else(|_| { fs::create_dir_all(root.join("Settings")).unwrap(); fs::write(root.join("Settings/database.json"), "{}").unwrap(); }); fs::create_dir_all(&meta).unwrap(); (dir, root, meta) } #[test] fn backup_deletion_obeys_the_installation_lock() { let (_d, root, meta) = setup(); let point = create(&root, &meta, Kind::Config, Trigger::Manual, None, &|_| {}).unwrap(); let lock = crate::update::operation_lock(&meta).unwrap(); assert!(delete(&meta, &point.id).is_err()); assert!(get(&meta, &point.id).is_ok()); drop(lock); delete(&meta, &point.id).unwrap(); assert!(get(&meta, &point.id).is_err()); } #[test] fn damaged_configuration_is_detected_before_restore_and_copies_are_retained() { let (_d, root, meta) = setup(); let point = create( &root, &meta, Kind::Config, Trigger::Automatic, None, &|_| {}, ) .unwrap(); let file = point_dir(&meta, &point.id) .unwrap() .join("files/Core/configs/worldserver.conf"); fs::write(&file, b"corrupted").unwrap(); assert!(!verify(&meta, &point.id).unwrap().ok); let original = fs::read(root.join("Core/configs/worldserver.conf")).unwrap(); assert!(restore_configs(&root, &meta, &point.id).is_err()); assert_eq!( fs::read(root.join("Core/configs/worldserver.conf")).unwrap(), original ); assert_eq!(list(&meta).len(), 1); fs::create_dir_all(meta.join("updates/broken")).unwrap(); fs::write(meta.join("updates/broken/txn.json"), b"{damaged").unwrap(); assert!(delete(&meta, &point.id).is_err()); prune_automatic(&meta, 0); assert!(file.exists()); } #[test] fn config_backup_excludes_secrets_and_clutter_and_restores_byte_exact() { let (_d, root, meta) = setup(); fs::create_dir_all(root.join("Settings")).unwrap(); fs::write( root.join("Settings/database.json"), r#"{"rootPassword":"x"}"#, ) .unwrap(); fs::write(root.join("Settings/worldserver.conf.template"), "A = 1\n").unwrap(); fs::write(root.join("Core/configs/modules/old.conf.bak"), "junk").unwrap(); let files = config_files(&root); assert!(files.contains(&"Core/configs/worldserver.conf".to_string())); assert!(files.contains(&"Settings/worldserver.conf.template".to_string())); assert!( !files.iter().any(|f| f.contains("database.json")), "secrets excluded" ); assert!(!files.iter().any(|f| f.ends_with(".bak"))); let p = create( &root, &meta, Kind::Config, Trigger::Manual, Some("test".into()), &|_| {}, ) .unwrap(); assert!(verify(&meta, &p.id).unwrap().ok); assert_eq!(list(&meta).len(), 1); assert!(!backups_dir(&meta) .join(format!("{}.partial", p.id)) .exists()); let original = fs::read(root.join("Core/configs/worldserver.conf")).unwrap(); fs::write( root.join("Core/configs/worldserver.conf"), "[worldserver]\nRealmID = 99\n", ) .unwrap(); fs::write(root.join("Core/configs/unrelated_new.conf"), "keep").unwrap(); let safety = restore_configs(&root, &meta, &p.id).unwrap(); assert_eq!( fs::read(root.join("Core/configs/worldserver.conf")).unwrap(), original ); assert!( root.join("Core/configs/unrelated_new.conf").is_file(), "restore never deletes other files" ); assert_eq!(safety.trigger, Trigger::BeforeRestore); assert!(list(&meta).len() >= 2); } #[test] fn backups_can_live_in_a_chosen_folder_and_old_ones_stay_visible() { let (d, root, meta) = setup(); let before = create( &root, &meta, Kind::Config, Trigger::Manual, Some("default folder".into()), &|_| {}, ) .unwrap(); assert!(location(&meta).is_default); // inside the server folder is refused, a relative path is refused assert!(set_location(&root, &meta, Some(root.join("backups").to_str().unwrap())).is_err()); assert!(set_location(&root, &meta, Some("backups")).is_err()); let elsewhere = d.path().join("other drive").join("CoA backups"); let loc = set_location(&root, &meta, Some(elsewhere.to_str().unwrap())).unwrap(); assert!(!loc.is_default && elsewhere.is_dir()); let after = create( &root, &meta, Kind::Config, Trigger::Manual, Some("chosen folder".into()), &|_| {}, ) .unwrap(); assert!( elsewhere.join(&after.id).join("backup.json").is_file(), "new backups go to the chosen folder" ); assert!(!default_dir(&meta).join(&after.id).exists()); let ids: Vec<_> = list(&meta).into_iter().map(|p| p.id).collect(); assert!( ids.contains(&before.id) && ids.contains(&after.id), "the old backup is still listed" ); assert!(verify(&meta, &before.id).unwrap().ok && verify(&meta, &after.id).unwrap().ok); assert_eq!( point_json(&meta, &after.id).unwrap(), elsewhere.join(&after.id).join("backup.json") ); restore_configs(&root, &meta, &after.id).unwrap(); delete(&meta, &after.id).unwrap(); assert!(!elsewhere.join(&after.id).exists()); assert!(set_location(&root, &meta, None).unwrap().is_default); assert!(list(&meta).iter().any(|p| p.id == before.id)); } #[test] fn verify_detects_tampering_and_restore_refuses_damaged_backup() { let (_d, root, meta) = setup(); let p = create(&root, &meta, Kind::Config, Trigger::Manual, None, &|_| {}).unwrap(); let victim = point_dir(&meta, &p.id) .unwrap() .join("files/Core/configs/worldserver.conf"); fs::remove_file(&victim).unwrap(); let v = verify(&meta, &p.id).unwrap(); assert!(!v.ok && v.problems[0].contains("worldserver.conf")); assert!(restore_configs(&root, &meta, &p.id).is_err()); } #[test] fn ids_are_validated_and_delete_only_touches_real_backups() { let (d, root, meta) = setup(); for bad in ["", "..", "../x", "a/b", "a\\b"] { assert!(point_dir(&meta, bad).is_err(), "{bad:?}"); } let victim = d.path().join("srv.manager/important"); fs::create_dir_all(&victim).unwrap(); assert!(delete(&meta, "important").is_err(), "not a recovery point"); assert!(victim.exists()); let p = create(&root, &meta, Kind::Config, Trigger::Manual, None, &|_| {}).unwrap(); delete(&meta, &p.id).unwrap(); assert!(list(&meta).is_empty()); } #[test] fn automatic_points_are_pruned_but_manual_and_safety_are_kept() { let (_d, root, meta) = setup(); let mk = |t: Trigger, id: &str| { let mut p = create(&root, &meta, Kind::Config, t, None, &|_| {}).unwrap(); // give each a distinct, sortable id by renaming its folder let from = point_dir(&meta, &p.id).unwrap(); p.id = id.to_string(); fsx::atomic_write_json(&from.join("backup.json"), &p).unwrap(); fs::rename(from, backups_dir(&meta).join(id)).unwrap(); }; mk(Trigger::Manual, "20260101-000000-manual"); mk(Trigger::BeforeRestore, "20260102-000000-before-restore"); for i in 0..5 { mk(Trigger::Automatic, &format!("2026020{i}-000000-auto")); } prune_automatic(&meta, 2); let left: Vec = list(&meta).into_iter().map(|p| p.id).collect(); assert_eq!(left.iter().filter(|i| i.ends_with("-auto")).count(), 2); assert!(left.contains(&"20260101-000000-manual".to_string())); assert!(left.contains(&"20260102-000000-before-restore".to_string())); assert!( left.contains(&"20260204-000000-auto".to_string()), "newest automatic kept" ); } }