6 use chrono
::{DateTime, TimeZone, Local}
;
8 use std
::path
::{PathBuf, Path}
;
9 use lazy_static
::lazy_static
;
11 macro_rules
! BACKUP_ID_RE { () => (r"[A-Za-z0-9][A-Za-z0-9_-]+") }
12 macro_rules
! BACKUP_TYPE_RE { () => (r"(?:host|vm|ct)") }
13 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}
\+[0-9]{2}
:[0-9]{2}
") }
16 static ref BACKUP_FILE_REGEX: Regex = Regex::new(
17 r"^
.*\.([fd
]idx
)$
").unwrap();
19 static ref BACKUP_TYPE_REGEX: Regex = Regex::new(
20 concat!(r"^
(", BACKUP_TYPE_RE!(), r")$
")).unwrap();
22 static ref BACKUP_ID_REGEX: Regex = Regex::new(
23 concat!(r"^
", BACKUP_ID_RE!(), r"$
")).unwrap();
25 static ref BACKUP_DATE_REGEX: Regex = Regex::new(
26 concat!(r"^
", BACKUP_TIME_RE!() ,r"$
")).unwrap();
28 static ref GROUP_PATH_REGEX: Regex = Regex::new(
29 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), r")$
")).unwrap();
31 static ref SNAPSHOT_PATH_REGEX: Regex = Regex::new(
32 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), ")/(", BACKUP_TIME_RE!(), r")$
")).unwrap();
38 pub struct BackupGroup {
41 /// Unique (for this type) ID
47 pub fn new<T: Into<String>>(backup_type: T, backup_id: T) -> Self {
48 Self { backup_type: backup_type.into(), backup_id: backup_id.into() }
51 pub fn backup_type(&self) -> &str {
55 pub fn backup_id(&self) -> &str {
59 pub fn parse(path: &str) -> Result<Self, Error> {
61 let cap = GROUP_PATH_REGEX.captures(path)
62 .ok_or_else(|| format_err!("unable to parse backup group path '{}'
", path))?;
65 backup_type: cap.get(1).unwrap().as_str().to_owned(),
66 backup_id: cap.get(2).unwrap().as_str().to_owned(),
70 pub fn group_path(&self) -> PathBuf {
72 let mut relative_path = PathBuf::new();
74 relative_path.push(&self.backup_type);
76 relative_path.push(&self.backup_id);
82 /// Uniquely identify a Backup (relative to data store)
84 pub struct BackupDir {
88 backup_time: DateTime<Local>,
93 pub fn new(group: BackupGroup, timestamp: i64) -> Self {
94 // Note: makes sure that nanoseconds is 0
95 Self { group, backup_time: Local.timestamp(timestamp, 0) }
98 pub fn group(&self) -> &BackupGroup {
102 pub fn backup_time(&self) -> DateTime<Local> {
106 pub fn parse(path: &str) -> Result<Self, Error> {
108 let cap = SNAPSHOT_PATH_REGEX.captures(path)
109 .ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'
", path))?;
111 let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
112 let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Local>>()?;
113 Ok(BackupDir::new(group, backup_time.timestamp()))
116 pub fn relative_path(&self) -> PathBuf {
118 let mut relative_path = self.group.group_path();
120 relative_path.push(self.backup_time.to_rfc3339());
126 /// Detailed Backup Information
128 pub struct BackupInfo {
129 /// the backup directory
130 pub backup_dir: BackupDir,
131 /// List of data files
132 pub files: Vec<String>,
137 pub fn sort_list(list: &mut Vec<BackupInfo>, ascendending: bool) {
138 if ascendending { // oldest first
139 list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time));
140 } else { // newest first
141 list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time));
145 pub fn list_backups(path: &Path) -> Result<Vec<BackupInfo>, Error> {
146 let mut list = vec![];
148 tools::scandir(libc::AT_FDCWD, path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| {
149 if file_type != nix::dir::Type::Directory { return Ok(()); }
150 tools::scandir(l0_fd, backup_type, &BACKUP_ID_REGEX, |l1_fd, backup_id, file_type| {
151 if file_type != nix::dir::Type::Directory { return Ok(()); }
152 tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
153 if file_type != nix::dir::Type::Directory { return Ok(()); }
155 let dt = backup_time.parse::<DateTime<Local>>()?;
157 let mut files = vec![];
159 tools::scandir(l2_fd, backup_time, &BACKUP_FILE_REGEX, |_, filename, file_type| {
160 if file_type != nix::dir::Type::File { return Ok(()); }
161 files.push(filename.to_owned());
165 list.push(BackupInfo {
166 backup_dir: BackupDir::new(BackupGroup::new(backup_type, backup_id), dt.timestamp()),