Albatross/src/remote/file.rs

48 lines
1.2 KiB
Rust

use crate::config::remote::FileConfig;
use crate::error::Result;
use crate::remote::{PathLocation, RemoteBackupSite};
use std::path::PathBuf;
pub struct FileBackup {
/// Target directory on the file system
target_dir: PathBuf,
/// Number of backups to keep
backups_to_keep: usize,
}
impl FileBackup {
/// New FileBackup
pub fn new(config: &FileConfig, backups_to_keep: usize) -> Result<Self> {
Ok(Self {
target_dir: config.path.clone(),
backups_to_keep,
})
}
}
impl RemoteBackupSite for FileBackup {
type FileType = PathLocation;
fn backup_to_remote(&mut self, file: PathBuf) -> Result<()> {
let dest = self.target_dir.join(file.file_name().unwrap());
std::fs::copy(file, dest)?;
Ok(())
}
fn get_backups(&mut self) -> Result<Vec<Self::FileType>> {
Ok(self
.target_dir
.read_dir()?
.filter_map(|file| Self::FileType::new(file.unwrap().path()))
.collect())
}
fn remove_backup(&mut self, backup: Self::FileType) -> Result<()> {
Ok(std::fs::remove_file(backup.location)?)
}
fn backups_to_keep(&self) -> usize {
self.backups_to_keep
}
}