2019-03-05 06:18:12 +00:00
|
|
|
use crate::tools;
|
|
|
|
|
|
|
|
use failure::*;
|
|
|
|
use regex::Regex;
|
2019-05-11 08:19:34 +00:00
|
|
|
use std::os::unix::io::RawFd;
|
2019-12-05 07:55:19 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2019-12-04 14:49:11 +00:00
|
|
|
use chrono::{DateTime, Datelike, TimeZone, SecondsFormat, Utc, Local};
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
macro_rules! BACKUP_ID_RE { () => (r"[A-Za-z0-9][A-Za-z0-9_-]+") }
|
|
|
|
macro_rules! BACKUP_TYPE_RE { () => (r"(?:host|vm|ct)") }
|
2019-07-22 08:12:51 +00:00
|
|
|
macro_rules! BACKUP_TIME_RE { () => (r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z") }
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
lazy_static!{
|
|
|
|
static ref BACKUP_FILE_REGEX: Regex = Regex::new(
|
2019-06-25 05:07:45 +00:00
|
|
|
r"^.*\.([fd]idx|blob)$").unwrap();
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
static ref BACKUP_TYPE_REGEX: Regex = Regex::new(
|
|
|
|
concat!(r"^(", BACKUP_TYPE_RE!(), r")$")).unwrap();
|
|
|
|
|
|
|
|
static ref BACKUP_ID_REGEX: Regex = Regex::new(
|
|
|
|
concat!(r"^", BACKUP_ID_RE!(), r"$")).unwrap();
|
|
|
|
|
|
|
|
static ref BACKUP_DATE_REGEX: Regex = Regex::new(
|
|
|
|
concat!(r"^", BACKUP_TIME_RE!() ,r"$")).unwrap();
|
|
|
|
|
|
|
|
static ref GROUP_PATH_REGEX: Regex = Regex::new(
|
|
|
|
concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), r")$")).unwrap();
|
|
|
|
|
|
|
|
static ref SNAPSHOT_PATH_REGEX: Regex = Regex::new(
|
|
|
|
concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), ")/(", BACKUP_TIME_RE!(), r")$")).unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:28:13 +00:00
|
|
|
/// BackupGroup is a directory containing a list of BackupDir
|
2019-05-10 04:59:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupGroup {
|
|
|
|
/// Type of backup
|
|
|
|
backup_type: String,
|
|
|
|
/// Unique (for this type) ID
|
|
|
|
backup_id: String,
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:55:19 +00:00
|
|
|
enum PruneMark { Keep, Remove }
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
impl BackupGroup {
|
|
|
|
|
2019-03-05 08:13:07 +00:00
|
|
|
pub fn new<T: Into<String>, U: Into<String>>(backup_type: T, backup_id: U) -> Self {
|
2019-03-05 06:18:12 +00:00
|
|
|
Self { backup_type: backup_type.into(), backup_id: backup_id.into() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn backup_type(&self) -> &str {
|
|
|
|
&self.backup_type
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn backup_id(&self) -> &str {
|
|
|
|
&self.backup_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(path: &str) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
let cap = GROUP_PATH_REGEX.captures(path)
|
|
|
|
.ok_or_else(|| format_err!("unable to parse backup group path '{}'", path))?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
backup_type: cap.get(1).unwrap().as_str().to_owned(),
|
|
|
|
backup_id: cap.get(2).unwrap().as_str().to_owned(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn group_path(&self) -> PathBuf {
|
|
|
|
|
|
|
|
let mut relative_path = PathBuf::new();
|
|
|
|
|
|
|
|
relative_path.push(&self.backup_type);
|
|
|
|
|
|
|
|
relative_path.push(&self.backup_id);
|
|
|
|
|
|
|
|
relative_path
|
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
|
|
|
|
pub fn list_backups(&self, base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
|
|
|
|
|
|
|
|
let mut list = vec![];
|
|
|
|
|
|
|
|
let mut path = base_path.to_owned();
|
|
|
|
path.push(self.group_path());
|
|
|
|
|
|
|
|
tools::scandir(libc::AT_FDCWD, &path, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
|
|
|
|
if file_type != nix::dir::Type::Directory { return Ok(()); }
|
|
|
|
|
2019-07-22 08:12:51 +00:00
|
|
|
let dt = backup_time.parse::<DateTime<Utc>>()?;
|
|
|
|
let backup_dir = BackupDir::new(self.backup_type.clone(), self.backup_id.clone(), dt.timestamp());
|
2019-05-11 08:19:34 +00:00
|
|
|
let files = list_backup_files(l2_fd, backup_time)?;
|
|
|
|
|
|
|
|
list.push(BackupInfo { backup_dir, files });
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
Ok(list)
|
|
|
|
}
|
2019-12-04 14:49:11 +00:00
|
|
|
|
|
|
|
fn mark_selections<F: Fn(DateTime<Local>, &BackupInfo) -> String> (
|
2019-12-05 07:55:19 +00:00
|
|
|
mark: &mut HashMap<PathBuf, PruneMark>,
|
2019-12-04 14:49:11 +00:00
|
|
|
list: &Vec<BackupInfo>,
|
|
|
|
keep: usize,
|
|
|
|
select_id: F,
|
2019-12-05 07:55:19 +00:00
|
|
|
) {
|
|
|
|
|
2019-12-04 14:49:11 +00:00
|
|
|
let mut hash = HashSet::new();
|
|
|
|
for info in list {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
2019-12-05 07:55:19 +00:00
|
|
|
if let Some(_) = mark.get(&backup_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let local_time = info.backup_dir.backup_time().with_timezone(&Local);
|
2019-12-04 14:49:11 +00:00
|
|
|
let sel_id: String = select_id(local_time, &info);
|
|
|
|
if !hash.contains(&sel_id) {
|
2019-12-05 07:55:19 +00:00
|
|
|
if hash.len() >= keep { break; }
|
2019-12-04 14:49:11 +00:00
|
|
|
hash.insert(sel_id);
|
2019-12-05 07:55:19 +00:00
|
|
|
mark.insert(backup_id, PruneMark::Keep);
|
|
|
|
} else {
|
|
|
|
mark.insert(backup_id, PruneMark::Remove);
|
2019-12-04 14:49:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compute_prune_list(
|
|
|
|
mut list: Vec<BackupInfo>,
|
|
|
|
keep_last: Option<u64>,
|
|
|
|
keep_daily: Option<u64>,
|
|
|
|
keep_weekly: Option<u64>,
|
|
|
|
keep_monthly: Option<u64>,
|
|
|
|
keep_yearly: Option<u64>,
|
|
|
|
) -> Result<Vec<BackupInfo>, Error> {
|
|
|
|
|
2019-12-05 07:55:19 +00:00
|
|
|
let mut mark = HashMap::new();
|
|
|
|
|
2019-12-04 14:49:11 +00:00
|
|
|
BackupInfo::sort_list(&mut list, false);
|
2019-12-05 07:55:19 +00:00
|
|
|
|
2019-12-04 14:49:11 +00:00
|
|
|
if let Some(keep_last) = keep_last {
|
2019-12-05 07:55:19 +00:00
|
|
|
for _ in 0..keep_last {
|
|
|
|
if list.is_empty() { break; }
|
|
|
|
let info = list.remove(0);
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
|
|
|
mark.insert(backup_id, PruneMark::Keep);
|
|
|
|
}
|
2019-12-04 14:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(keep_daily) = keep_daily {
|
|
|
|
Self::mark_selections(&mut mark, &list, keep_daily as usize, |local_time, _info| {
|
|
|
|
format!("{}/{}/{}", local_time.year(), local_time.month(), local_time.day())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(keep_weekly) = keep_weekly {
|
|
|
|
Self::mark_selections(&mut mark, &list, keep_weekly as usize, |local_time, _info| {
|
|
|
|
format!("{}/{}", local_time.year(), local_time.iso_week().week())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(keep_monthly) = keep_monthly {
|
|
|
|
Self::mark_selections(&mut mark, &list, keep_monthly as usize, |local_time, _info| {
|
|
|
|
format!("{}/{}", local_time.year(), local_time.month())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(keep_yearly) = keep_yearly {
|
|
|
|
Self::mark_selections(&mut mark, &list, keep_yearly as usize, |local_time, _info| {
|
|
|
|
format!("{}/{}", local_time.year(), local_time.year())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut remove_list: Vec<BackupInfo> = list.into_iter()
|
2019-12-05 07:55:19 +00:00
|
|
|
.filter(|info| {
|
|
|
|
let backup_id = info.backup_dir.relative_path();
|
|
|
|
match mark.get(&backup_id) {
|
|
|
|
Some(PruneMark::Keep) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2019-12-04 14:49:11 +00:00
|
|
|
|
|
|
|
BackupInfo::sort_list(&mut remove_list, true);
|
|
|
|
|
|
|
|
Ok(remove_list)
|
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Uniquely identify a Backup (relative to data store)
|
2019-03-05 06:28:13 +00:00
|
|
|
///
|
|
|
|
/// We also call this a backup snaphost.
|
2019-05-10 04:59:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupDir {
|
|
|
|
/// Backup group
|
|
|
|
group: BackupGroup,
|
|
|
|
/// Backup timestamp
|
2019-07-22 08:12:51 +00:00
|
|
|
backup_time: DateTime<Utc>,
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackupDir {
|
|
|
|
|
2019-03-05 08:16:54 +00:00
|
|
|
pub fn new<T, U>(backup_type: T, backup_id: U, timestamp: i64) -> Self
|
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
U: Into<String>,
|
|
|
|
{
|
2019-03-05 06:18:12 +00:00
|
|
|
// Note: makes sure that nanoseconds is 0
|
2019-03-05 08:16:54 +00:00
|
|
|
Self {
|
|
|
|
group: BackupGroup::new(backup_type.into(), backup_id.into()),
|
2019-07-22 08:12:51 +00:00
|
|
|
backup_time: Utc.timestamp(timestamp, 0),
|
2019-03-05 08:16:54 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-05-11 10:07:09 +00:00
|
|
|
pub fn new_with_group(group: BackupGroup, timestamp: i64) -> Self {
|
2019-07-22 08:12:51 +00:00
|
|
|
Self { group, backup_time: Utc.timestamp(timestamp, 0) }
|
2019-05-11 10:07:09 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
pub fn group(&self) -> &BackupGroup {
|
|
|
|
&self.group
|
|
|
|
}
|
|
|
|
|
2019-07-22 08:12:51 +00:00
|
|
|
pub fn backup_time(&self) -> DateTime<Utc> {
|
2019-03-05 06:18:12 +00:00
|
|
|
self.backup_time
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(path: &str) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
let cap = SNAPSHOT_PATH_REGEX.captures(path)
|
|
|
|
.ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'", path))?;
|
|
|
|
|
|
|
|
let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
|
2019-07-22 08:12:51 +00:00
|
|
|
let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Utc>>()?;
|
|
|
|
Ok(BackupDir::from((group, backup_time.timestamp())))
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn relative_path(&self) -> PathBuf {
|
|
|
|
|
|
|
|
let mut relative_path = self.group.group_path();
|
|
|
|
|
2019-07-22 08:12:51 +00:00
|
|
|
relative_path.push(Self::backup_time_to_string(self.backup_time));
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
relative_path
|
|
|
|
}
|
2019-07-22 08:12:51 +00:00
|
|
|
|
|
|
|
pub fn backup_time_to_string(backup_time: DateTime<Utc>) -> String {
|
|
|
|
backup_time.to_rfc3339_opts(SecondsFormat::Secs, true)
|
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 08:16:54 +00:00
|
|
|
impl From<(BackupGroup, i64)> for BackupDir {
|
|
|
|
fn from((group, timestamp): (BackupGroup, i64)) -> Self {
|
2019-07-22 08:12:51 +00:00
|
|
|
Self { group, backup_time: Utc.timestamp(timestamp, 0) }
|
2019-03-05 08:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:28:13 +00:00
|
|
|
/// Detailed Backup Information, lists files inside a BackupDir
|
2019-05-11 09:21:13 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupInfo {
|
|
|
|
/// the backup directory
|
|
|
|
pub backup_dir: BackupDir,
|
|
|
|
/// List of data files
|
|
|
|
pub files: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackupInfo {
|
|
|
|
|
2019-08-07 06:27:52 +00:00
|
|
|
pub fn new(base_path: &Path, backup_dir: BackupDir) -> Result<BackupInfo, Error> {
|
|
|
|
let mut path = base_path.to_owned();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
|
|
|
|
let files = list_backup_files(libc::AT_FDCWD, &path)?;
|
|
|
|
|
|
|
|
Ok(BackupInfo { backup_dir, files })
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:07:09 +00:00
|
|
|
/// Finds the latest backup inside a backup group
|
|
|
|
pub fn last_backup(base_path: &Path, group: &BackupGroup) -> Result<Option<BackupInfo>, Error> {
|
|
|
|
let backups = group.list_backups(base_path)?;
|
|
|
|
Ok(backups.into_iter().max_by_key(|item| item.backup_dir.backup_time()))
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
pub fn sort_list(list: &mut Vec<BackupInfo>, ascendending: bool) {
|
|
|
|
if ascendending { // oldest first
|
|
|
|
list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time));
|
|
|
|
} else { // newest first
|
|
|
|
list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 09:49:01 +00:00
|
|
|
pub fn list_files(base_path: &Path, backup_dir: &BackupDir) -> Result<Vec<String>, Error> {
|
|
|
|
let mut path = base_path.to_owned();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
let files = list_backup_files(libc::AT_FDCWD, &path)?;
|
2019-03-06 09:49:01 +00:00
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_backups(base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
|
2019-03-05 06:18:12 +00:00
|
|
|
let mut list = vec![];
|
|
|
|
|
2019-03-06 09:49:01 +00:00
|
|
|
tools::scandir(libc::AT_FDCWD, base_path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| {
|
2019-03-05 06:18:12 +00:00
|
|
|
if file_type != nix::dir::Type::Directory { return Ok(()); }
|
|
|
|
tools::scandir(l0_fd, backup_type, &BACKUP_ID_REGEX, |l1_fd, backup_id, file_type| {
|
|
|
|
if file_type != nix::dir::Type::Directory { return Ok(()); }
|
|
|
|
tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
|
|
|
|
if file_type != nix::dir::Type::Directory { return Ok(()); }
|
|
|
|
|
2019-07-22 08:12:51 +00:00
|
|
|
let dt = backup_time.parse::<DateTime<Utc>>()?;
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, dt.timestamp());
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
let files = list_backup_files(l2_fd, backup_time)?;
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
list.push(BackupInfo { backup_dir, files });
|
2019-03-05 06:18:12 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
|
|
|
|
fn list_backup_files<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<Vec<String>, Error> {
|
|
|
|
let mut files = vec![];
|
|
|
|
|
|
|
|
tools::scandir(dirfd, path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
|
|
|
|
if file_type != nix::dir::Type::File { return Ok(()); }
|
|
|
|
files.push(filename.to_owned());
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|