Skip to main content

BackupManager

Struct BackupManager 

Source
pub struct BackupManager { /* private fields */ }
Expand description

Manages file backups for undo/rollback operations.

Backups are stored under {backup_dir}/{job_id}/{filename}. The manager creates directories as needed.

Implementations§

Source§

impl BackupManager

Source

pub fn new(backup_dir: PathBuf) -> Result<Self>

Creates a new backup manager rooted at backup_dir.

The directory is created (recursively) if it does not exist.

§Errors

Returns crate::Error::BackupError if the backup directory cannot be created.

Source

pub fn create_backup(&self, file_path: &Path, job_id: &str) -> Result<PathBuf>

Creates a backup of a file or directory before mutation.

Copies file_path to {backup_dir}/{job_id}/{filename}. If a backup already exists, appends a numeric suffix (.1, .2, etc.) to preserve all pre-mutation states. The first backup (no suffix) always contains the original file state before any writes in this job.

§Arguments
  • file_path — Path to the file or directory to back up
  • job_id — Job ID used as the backup subdirectory name
§Returns

The path to the backup file or directory.

§Errors

Returns crate::Error::BackupError if the source does not exist, exceeds the 100MB size limit, or the copy fails.

Source

pub fn restore(&self, backup_path: &Path, target_path: &Path) -> Result<()>

Restores a file or directory from a backup.

Before overwriting target_path, creates a pre-restore backup so the current state can be recovered if the restore itself causes issues. Copies backup_path to target_path, overwriting the target. Handles both files and directories recursively.

§Errors

Returns crate::Error::BackupError if the backup does not exist, the pre-restore backup fails, or the copy fails.

Source

pub fn cleanup(&self, older_than_secs: u64) -> Result<()>

Deletes old backups older than the given age.

Scans backup_dir for job subdirectories and removes those whose modification time is older than older_than_secs.

§Security

Uses symlink_metadata() to avoid following symlinks during cleanup. Symlinks are skipped rather than deleted, preventing accidental deletion of symlink targets (e.g., remove_dir_all on a symlink to /etc would be catastrophic).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.