]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/backup_info.rs
684f63c05f4ba9cfa9535b8a51adcd88f6981a4f
[proxmox-backup.git] / src / backup / backup_info.rs
1 use crate::tools;
2
3 use failure::*;
4 use regex::Regex;
5
6 use chrono::{DateTime, TimeZone, Local};
7
8 use std::path::{PathBuf, Path};
9 use lazy_static::lazy_static;
10
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}") }
14
15 lazy_static!{
16 static ref BACKUP_FILE_REGEX: Regex = Regex::new(
17 r"^.*\.([fd]idx)$").unwrap();
18
19 static ref BACKUP_TYPE_REGEX: Regex = Regex::new(
20 concat!(r"^(", BACKUP_TYPE_RE!(), r")$")).unwrap();
21
22 static ref BACKUP_ID_REGEX: Regex = Regex::new(
23 concat!(r"^", BACKUP_ID_RE!(), r"$")).unwrap();
24
25 static ref BACKUP_DATE_REGEX: Regex = Regex::new(
26 concat!(r"^", BACKUP_TIME_RE!() ,r"$")).unwrap();
27
28 static ref GROUP_PATH_REGEX: Regex = Regex::new(
29 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), r")$")).unwrap();
30
31 static ref SNAPSHOT_PATH_REGEX: Regex = Regex::new(
32 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), ")/(", BACKUP_TIME_RE!(), r")$")).unwrap();
33
34 }
35
36 /// Group Backups
37 #[derive(Debug)]
38 pub struct BackupGroup {
39 /// Type of backup
40 backup_type: String,
41 /// Unique (for this type) ID
42 backup_id: String,
43 }
44
45 impl BackupGroup {
46
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() }
49 }
50
51 pub fn backup_type(&self) -> &str {
52 &self.backup_type
53 }
54
55 pub fn backup_id(&self) -> &str {
56 &self.backup_id
57 }
58
59 pub fn parse(path: &str) -> Result<Self, Error> {
60
61 let cap = GROUP_PATH_REGEX.captures(path)
62 .ok_or_else(|| format_err!("unable to parse backup group path '{}'", path))?;
63
64 Ok(Self {
65 backup_type: cap.get(1).unwrap().as_str().to_owned(),
66 backup_id: cap.get(2).unwrap().as_str().to_owned(),
67 })
68 }
69
70 pub fn group_path(&self) -> PathBuf {
71
72 let mut relative_path = PathBuf::new();
73
74 relative_path.push(&self.backup_type);
75
76 relative_path.push(&self.backup_id);
77
78 relative_path
79 }
80 }
81
82 /// Uniquely identify a Backup (relative to data store)
83 #[derive(Debug)]
84 pub struct BackupDir {
85 /// Backup group
86 group: BackupGroup,
87 /// Backup timestamp
88 backup_time: DateTime<Local>,
89 }
90
91 impl BackupDir {
92
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) }
96 }
97
98 pub fn group(&self) -> &BackupGroup {
99 &self.group
100 }
101
102 pub fn backup_time(&self) -> DateTime<Local> {
103 self.backup_time
104 }
105
106 pub fn parse(path: &str) -> Result<Self, Error> {
107
108 let cap = SNAPSHOT_PATH_REGEX.captures(path)
109 .ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'", path))?;
110
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()))
114 }
115
116 pub fn relative_path(&self) -> PathBuf {
117
118 let mut relative_path = self.group.group_path();
119
120 relative_path.push(self.backup_time.to_rfc3339());
121
122 relative_path
123 }
124 }
125
126 /// Detailed Backup Information
127 #[derive(Debug)]
128 pub struct BackupInfo {
129 /// the backup directory
130 pub backup_dir: BackupDir,
131 /// List of data files
132 pub files: Vec<String>,
133 }
134
135 impl BackupInfo {
136
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));
142 }
143 }
144
145 pub fn list_backups(path: &Path) -> Result<Vec<BackupInfo>, Error> {
146 let mut list = vec![];
147
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(()); }
154
155 let dt = backup_time.parse::<DateTime<Local>>()?;
156
157 let mut files = vec![];
158
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());
162 Ok(())
163 })?;
164
165 list.push(BackupInfo {
166 backup_dir: BackupDir::new(BackupGroup::new(backup_type, backup_id), dt.timestamp()),
167 files,
168 });
169
170 Ok(())
171 })
172 })
173 })?;
174 Ok(list)
175 }
176 }