pub struct DbBackupManager {
db_dir: PathBuf,
current_version: GitVersion,
}Expand description
Manages versioned SQLite database backups.
Fields§
§db_dir: PathBuf§current_version: GitVersionImplementations§
Source§impl DbBackupManager
impl DbBackupManager
Sourcepub fn new(db_dir: PathBuf, current_version: GitVersion) -> Self
pub fn new(db_dir: PathBuf, current_version: GitVersion) -> Self
Creates a backup manager for the database directory.
db_dir is the directory containing cadmus.sqlite. Backups are stored
in db_dir/backups/.
Sourcefn backup_dir(&self) -> PathBuf
fn backup_dir(&self) -> PathBuf
Returns the path to the backup directory.
Sourcefn manifest_path(&self) -> PathBuf
fn manifest_path(&self) -> PathBuf
Returns the path to the backup manifest.
Sourcepub async fn create_backup(
&self,
pool: &SqlitePool,
retention: usize,
) -> Result<PathBuf, Error>
pub async fn create_backup( &self, pool: &SqlitePool, retention: usize, ) -> Result<PathBuf, Error>
Creates a backup of the current database using the SQLite online backup API.
The backup is stored as backups/cadmus-v<version>.sqlite. The manifest is
updated and old backups exceeding the retention limit are removed.
Sourcepub async fn restore_best_backup(
&self,
active_path: &Path,
newer_version: &GitVersion,
) -> Result<PathBuf, RestoreError>
pub async fn restore_best_backup( &self, active_path: &Path, newer_version: &GitVersion, ) -> Result<PathBuf, RestoreError>
Restores the best available backup for the target version.
The restore is performed in three steps to avoid losing the active database if an error occurs mid-way:
- Copy the backup to a staging path (
cadmus-v<version>-restore-staged.sqlite). - Rename the active database to
cadmus-v<newer_version>-demoted.sqlite. - Rename the staged copy to the active path.
If step 3 fails the demoted database is renamed back to active_path as
a best-effort rollback. Demoted files are not tracked in the manifest and
are never automatically deleted — they remain as a safety net for manual
recovery.
Returns the path of the restored backup file on success.
§Errors
RestoreError::NoBackupFound— no manifest entry exists for the target version.RestoreError::BackupFileMissing— the manifest references a file that is absent on disk.RestoreError::Io— an I/O or filesystem error occurred during the restore.
Sourcepub fn find_best_backup(
&self,
target_version: &GitVersion,
) -> Result<Option<BackupEntry>, Error>
pub fn find_best_backup( &self, target_version: &GitVersion, ) -> Result<Option<BackupEntry>, Error>
Finds the best backup for the target version.
Prefers an exact version match. Otherwise, returns the newest backup whose version is less than or equal to the target version.
Sourcefn read_manifest(&self) -> Result<BackupManifest, Error>
fn read_manifest(&self) -> Result<BackupManifest, Error>
Reads the backup manifest from disk.
Sourcefn write_manifest(&self, manifest: &BackupManifest) -> Result<(), Error>
fn write_manifest(&self, manifest: &BackupManifest) -> Result<(), Error>
Writes the backup manifest to disk.
Sourceasync fn update_manifest_and_cleanup(
&self,
filename: &str,
created_at: DateTime<Utc>,
migration_hash: MigrationHash,
retention: usize,
) -> Result<(), Error>
async fn update_manifest_and_cleanup( &self, filename: &str, created_at: DateTime<Utc>, migration_hash: MigrationHash, retention: usize, ) -> Result<(), Error>
Updates the manifest with a new backup entry and removes old backups.
The current version’s entry is always retained. Old backups from other
versions are removed until the total count is within retention.
Trait Implementations§
Source§impl Clone for DbBackupManager
impl Clone for DbBackupManager
Source§fn clone(&self) -> DbBackupManager
fn clone(&self) -> DbBackupManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for DbBackupManager
impl RefUnwindSafe for DbBackupManager
impl Send for DbBackupManager
impl Sync for DbBackupManager
impl Unpin for DbBackupManager
impl UnsafeUnpin for DbBackupManager
impl UnwindSafe for DbBackupManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more