5 use std
::os
::unix
::io
::RawFd
;
6 use std
::collections
::HashSet
;
8 use chrono
::{DateTime, Datelike, TimeZone, SecondsFormat, Utc, Local}
;
10 use std
::path
::{PathBuf, Path}
;
11 use lazy_static
::lazy_static
;
13 macro_rules
! BACKUP_ID_RE { () => (r"[A-Za-z0-9][A-Za-z0-9_-]+") }
14 macro_rules
! BACKUP_TYPE_RE { () => (r"(?:host|vm|ct)") }
15 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
") }
18 static ref BACKUP_FILE_REGEX: Regex = Regex::new(
19 r"^
.*\.([fd
]idx
|blob
)$
").unwrap();
21 static ref BACKUP_TYPE_REGEX: Regex = Regex::new(
22 concat!(r"^
(", BACKUP_TYPE_RE!(), r")$
")).unwrap();
24 static ref BACKUP_ID_REGEX: Regex = Regex::new(
25 concat!(r"^
", BACKUP_ID_RE!(), r"$
")).unwrap();
27 static ref BACKUP_DATE_REGEX: Regex = Regex::new(
28 concat!(r"^
", BACKUP_TIME_RE!() ,r"$
")).unwrap();
30 static ref GROUP_PATH_REGEX: Regex = Regex::new(
31 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), r")$
")).unwrap();
33 static ref SNAPSHOT_PATH_REGEX: Regex = Regex::new(
34 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), ")/(", BACKUP_TIME_RE!(), r")$
")).unwrap();
38 /// BackupGroup is a directory containing a list of BackupDir
39 #[derive(Debug, Clone)]
40 pub struct BackupGroup {
43 /// Unique (for this type) ID
49 pub fn new<T: Into<String>, U: Into<String>>(backup_type: T, backup_id: U) -> Self {
50 Self { backup_type: backup_type.into(), backup_id: backup_id.into() }
53 pub fn backup_type(&self) -> &str {
57 pub fn backup_id(&self) -> &str {
61 pub fn parse(path: &str) -> Result<Self, Error> {
63 let cap = GROUP_PATH_REGEX.captures(path)
64 .ok_or_else(|| format_err!("unable to parse backup group path '{}'
", path))?;
67 backup_type: cap.get(1).unwrap().as_str().to_owned(),
68 backup_id: cap.get(2).unwrap().as_str().to_owned(),
72 pub fn group_path(&self) -> PathBuf {
74 let mut relative_path = PathBuf::new();
76 relative_path.push(&self.backup_type);
78 relative_path.push(&self.backup_id);
83 pub fn list_backups(&self, base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
85 let mut list = vec![];
87 let mut path = base_path.to_owned();
88 path.push(self.group_path());
90 tools::scandir(libc::AT_FDCWD, &path, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
91 if file_type != nix::dir::Type::Directory { return Ok(()); }
93 let dt = backup_time.parse::<DateTime<Utc>>()?;
94 let backup_dir = BackupDir::new(self.backup_type.clone(), self.backup_id.clone(), dt.timestamp());
95 let files = list_backup_files(l2_fd, backup_time)?;
97 list.push(BackupInfo { backup_dir, files });
104 fn mark_selections<F: Fn(DateTime<Local>, &BackupInfo) -> String> (
105 mark: &mut HashSet<PathBuf>,
106 list: &Vec<BackupInfo>,
110 let mut hash = HashSet::new();
112 let local_time = info.backup_dir.backup_time().with_timezone(&Local);
113 if hash.len() >= keep as usize { break; }
114 let backup_id = info.backup_dir.relative_path();
115 let sel_id: String = select_id(local_time, &info);
116 if !hash.contains(&sel_id) {
118 mark.insert(backup_id);
123 pub fn compute_prune_list(
124 mut list: Vec<BackupInfo>,
125 keep_last: Option<u64>,
126 keep_daily: Option<u64>,
127 keep_weekly: Option<u64>,
128 keep_monthly: Option<u64>,
129 keep_yearly: Option<u64>,
130 ) -> Result<Vec<BackupInfo>, Error> {
132 let mut mark = HashSet::new();
134 BackupInfo::sort_list(&mut list, false);
136 if let Some(keep_last) = keep_last {
137 list.iter().take(keep_last as usize).for_each(|info| {
138 mark.insert(info.backup_dir.relative_path());
142 if let Some(keep_daily) = keep_daily {
143 Self::mark_selections(&mut mark, &list, keep_daily as usize, |local_time, _info| {
144 format!("{}
/{}
/{}
", local_time.year(), local_time.month(), local_time.day())
148 if let Some(keep_weekly) = keep_weekly {
149 Self::mark_selections(&mut mark, &list, keep_weekly as usize, |local_time, _info| {
150 format!("{}
/{}
", local_time.year(), local_time.iso_week().week())
154 if let Some(keep_monthly) = keep_monthly {
155 Self::mark_selections(&mut mark, &list, keep_monthly as usize, |local_time, _info| {
156 format!("{}
/{}
", local_time.year(), local_time.month())
160 if let Some(keep_yearly) = keep_yearly {
161 Self::mark_selections(&mut mark, &list, keep_yearly as usize, |local_time, _info| {
162 format!("{}
/{}
", local_time.year(), local_time.year())
166 let mut remove_list: Vec<BackupInfo> = list.into_iter()
167 .filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect();
169 BackupInfo::sort_list(&mut remove_list, true);
175 /// Uniquely identify a Backup (relative to data store)
177 /// We also call this a backup snaphost.
178 #[derive(Debug, Clone)]
179 pub struct BackupDir {
183 backup_time: DateTime<Utc>,
188 pub fn new<T, U>(backup_type: T, backup_id: U, timestamp: i64) -> Self
193 // Note: makes sure that nanoseconds is 0
195 group: BackupGroup::new(backup_type.into(), backup_id.into()),
196 backup_time: Utc.timestamp(timestamp, 0),
199 pub fn new_with_group(group: BackupGroup, timestamp: i64) -> Self {
200 Self { group, backup_time: Utc.timestamp(timestamp, 0) }
203 pub fn group(&self) -> &BackupGroup {
207 pub fn backup_time(&self) -> DateTime<Utc> {
211 pub fn parse(path: &str) -> Result<Self, Error> {
213 let cap = SNAPSHOT_PATH_REGEX.captures(path)
214 .ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'
", path))?;
216 let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
217 let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Utc>>()?;
218 Ok(BackupDir::from((group, backup_time.timestamp())))
221 pub fn relative_path(&self) -> PathBuf {
223 let mut relative_path = self.group.group_path();
225 relative_path.push(Self::backup_time_to_string(self.backup_time));
230 pub fn backup_time_to_string(backup_time: DateTime<Utc>) -> String {
231 backup_time.to_rfc3339_opts(SecondsFormat::Secs, true)
235 impl From<(BackupGroup, i64)> for BackupDir {
236 fn from((group, timestamp): (BackupGroup, i64)) -> Self {
237 Self { group, backup_time: Utc.timestamp(timestamp, 0) }
241 /// Detailed Backup Information, lists files inside a BackupDir
242 #[derive(Debug, Clone)]
243 pub struct BackupInfo {
244 /// the backup directory
245 pub backup_dir: BackupDir,
246 /// List of data files
247 pub files: Vec<String>,
252 pub fn new(base_path: &Path, backup_dir: BackupDir) -> Result<BackupInfo, Error> {
253 let mut path = base_path.to_owned();
254 path.push(backup_dir.relative_path());
256 let files = list_backup_files(libc::AT_FDCWD, &path)?;
258 Ok(BackupInfo { backup_dir, files })
261 /// Finds the latest backup inside a backup group
262 pub fn last_backup(base_path: &Path, group: &BackupGroup) -> Result<Option<BackupInfo>, Error> {
263 let backups = group.list_backups(base_path)?;
264 Ok(backups.into_iter().max_by_key(|item| item.backup_dir.backup_time()))
267 pub fn sort_list(list: &mut Vec<BackupInfo>, ascendending: bool) {
268 if ascendending { // oldest first
269 list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time));
270 } else { // newest first
271 list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time));
275 pub fn list_files(base_path: &Path, backup_dir: &BackupDir) -> Result<Vec<String>, Error> {
276 let mut path = base_path.to_owned();
277 path.push(backup_dir.relative_path());
279 let files = list_backup_files(libc::AT_FDCWD, &path)?;
284 pub fn list_backups(base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
285 let mut list = vec![];
287 tools::scandir(libc::AT_FDCWD, base_path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| {
288 if file_type != nix::dir::Type::Directory { return Ok(()); }
289 tools::scandir(l0_fd, backup_type, &BACKUP_ID_REGEX, |l1_fd, backup_id, file_type| {
290 if file_type != nix::dir::Type::Directory { return Ok(()); }
291 tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
292 if file_type != nix::dir::Type::Directory { return Ok(()); }
294 let dt = backup_time.parse::<DateTime<Utc>>()?;
295 let backup_dir = BackupDir::new(backup_type, backup_id, dt.timestamp());
297 let files = list_backup_files(l2_fd, backup_time)?;
299 list.push(BackupInfo { backup_dir, files });
309 fn list_backup_files<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<Vec<String>, Error> {
310 let mut files = vec![];
312 tools::scandir(dirfd, path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
313 if file_type != nix::dir::Type::File { return Ok(()); }
314 files.push(filename.to_owned());