bakare/src/backup.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

use anyhow::Result;
use anyhow::*;
2018-10-04 15:29:19 +01:00
use std::path::Path;
2019-01-12 14:39:20 +00:00
2018-10-04 15:29:19 +01:00
use walkdir::WalkDir;
2019-01-12 14:39:20 +00:00
use crate::repository::Repository;
2018-10-04 15:33:01 +01:00
pub struct Engine<'a> {
2018-10-04 15:29:19 +01:00
source_path: &'a Path,
2019-09-01 11:05:22 +01:00
repository: &'a mut Repository<'a>,
2018-12-22 16:09:22 +00:00
}
2018-10-04 15:33:01 +01:00
impl<'a> Engine<'a> {
pub fn new(source_path: &'a Path, repository: &'a mut Repository<'a>) -> Result<Self> {
2019-09-07 15:08:47 +01:00
if source_path.ancestors().any(|a| a == repository.path()) {
return Err(anyhow!("source same as repository"));
2019-09-07 15:08:47 +01:00
}
Ok(Engine { source_path, repository })
2018-10-04 15:29:19 +01:00
}
pub fn backup(&mut self) -> Result<()> {
2018-10-04 15:29:19 +01:00
let walker = WalkDir::new(self.source_path);
let save_every = 16;
let mut save_counter = 0;
2018-10-04 15:29:19 +01:00
for maybe_entry in walker {
let entry = maybe_entry?;
if entry.path() != self.source_path {
2019-01-26 19:27:23 +00:00
self.repository.store(entry.path())?;
2018-10-04 15:29:19 +01:00
}
save_counter += 1;
if save_counter == save_every {
save_counter = 0;
self.repository.save_index()?;
}
2018-10-04 15:29:19 +01:00
}
self.repository.save_index()?;
2018-10-04 15:29:19 +01:00
Ok(())
}
}