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
impl BackupManager
Sourcepub fn new(backup_dir: PathBuf) -> Result<Self>
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.
Sourcepub fn create_backup(&self, file_path: &Path, job_id: &str) -> Result<PathBuf>
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 upjob_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.
Sourcepub fn restore(&self, backup_path: &Path, target_path: &Path) -> Result<()>
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.
Sourcepub fn cleanup(&self, older_than_secs: u64) -> Result<()>
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).