]> git.proxmox.com Git - proxmox-backup.git/blame - src/backup/backup_info.rs
src/backup/manifest.rs: impl TryFrom DataBlob
[proxmox-backup.git] / src / backup / backup_info.rs
CommitLineData
b3483782
DM
1use crate::tools;
2
3use failure::*;
4use regex::Regex;
c0977501 5use std::os::unix::io::RawFd;
b3483782 6
92acbd69 7use chrono::{DateTime, TimeZone, SecondsFormat, Utc};
b3483782
DM
8
9use std::path::{PathBuf, Path};
10use lazy_static::lazy_static;
11
12macro_rules! BACKUP_ID_RE { () => (r"[A-Za-z0-9][A-Za-z0-9_-]+") }
13macro_rules! BACKUP_TYPE_RE { () => (r"(?:host|vm|ct)") }
fa5d6977 14macro_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") }
b3483782
DM
15
16lazy_static!{
17 static ref BACKUP_FILE_REGEX: Regex = Regex::new(
c6d203bb 18 r"^.*\.([fd]idx|blob)$").unwrap();
b3483782
DM
19
20 static ref BACKUP_TYPE_REGEX: Regex = Regex::new(
21 concat!(r"^(", BACKUP_TYPE_RE!(), r")$")).unwrap();
22
23 static ref BACKUP_ID_REGEX: Regex = Regex::new(
24 concat!(r"^", BACKUP_ID_RE!(), r"$")).unwrap();
25
26 static ref BACKUP_DATE_REGEX: Regex = Regex::new(
27 concat!(r"^", BACKUP_TIME_RE!() ,r"$")).unwrap();
28
29 static ref GROUP_PATH_REGEX: Regex = Regex::new(
30 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), r")$")).unwrap();
31
32 static ref SNAPSHOT_PATH_REGEX: Regex = Regex::new(
33 concat!(r"(", BACKUP_TYPE_RE!(), ")/(", BACKUP_ID_RE!(), ")/(", BACKUP_TIME_RE!(), r")$")).unwrap();
34
35}
36
d57474e0 37/// BackupGroup is a directory containing a list of BackupDir
35a2d8a6 38#[derive(Debug, Clone)]
b3483782
DM
39pub struct BackupGroup {
40 /// Type of backup
41 backup_type: String,
42 /// Unique (for this type) ID
43 backup_id: String,
44}
45
46impl BackupGroup {
47
93b49ce3 48 pub fn new<T: Into<String>, U: Into<String>>(backup_type: T, backup_id: U) -> Self {
b3483782
DM
49 Self { backup_type: backup_type.into(), backup_id: backup_id.into() }
50 }
51
52 pub fn backup_type(&self) -> &str {
53 &self.backup_type
54 }
55
56 pub fn backup_id(&self) -> &str {
57 &self.backup_id
58 }
59
60 pub fn parse(path: &str) -> Result<Self, Error> {
61
62 let cap = GROUP_PATH_REGEX.captures(path)
63 .ok_or_else(|| format_err!("unable to parse backup group path '{}'", path))?;
64
65 Ok(Self {
66 backup_type: cap.get(1).unwrap().as_str().to_owned(),
67 backup_id: cap.get(2).unwrap().as_str().to_owned(),
68 })
69 }
70
71 pub fn group_path(&self) -> PathBuf {
72
73 let mut relative_path = PathBuf::new();
74
75 relative_path.push(&self.backup_type);
76
77 relative_path.push(&self.backup_id);
78
79 relative_path
80 }
c0977501
DM
81
82 pub fn list_backups(&self, base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
83
84 let mut list = vec![];
85
86 let mut path = base_path.to_owned();
87 path.push(self.group_path());
88
89 tools::scandir(libc::AT_FDCWD, &path, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
90 if file_type != nix::dir::Type::Directory { return Ok(()); }
91
fa5d6977
DM
92 let dt = backup_time.parse::<DateTime<Utc>>()?;
93 let backup_dir = BackupDir::new(self.backup_type.clone(), self.backup_id.clone(), dt.timestamp());
c0977501
DM
94 let files = list_backup_files(l2_fd, backup_time)?;
95
96 list.push(BackupInfo { backup_dir, files });
97
98 Ok(())
99 })?;
100 Ok(list)
101 }
aeeac29b 102
b3483782
DM
103}
104
105/// Uniquely identify a Backup (relative to data store)
d57474e0
DM
106///
107/// We also call this a backup snaphost.
35a2d8a6 108#[derive(Debug, Clone)]
b3483782
DM
109pub struct BackupDir {
110 /// Backup group
111 group: BackupGroup,
112 /// Backup timestamp
fa5d6977 113 backup_time: DateTime<Utc>,
b3483782
DM
114}
115
116impl BackupDir {
117
391d3107
WB
118 pub fn new<T, U>(backup_type: T, backup_id: U, timestamp: i64) -> Self
119 where
120 T: Into<String>,
121 U: Into<String>,
122 {
b3483782 123 // Note: makes sure that nanoseconds is 0
391d3107
WB
124 Self {
125 group: BackupGroup::new(backup_type.into(), backup_id.into()),
fa5d6977 126 backup_time: Utc.timestamp(timestamp, 0),
391d3107 127 }
b3483782 128 }
51a4f63f 129 pub fn new_with_group(group: BackupGroup, timestamp: i64) -> Self {
fa5d6977 130 Self { group, backup_time: Utc.timestamp(timestamp, 0) }
51a4f63f 131 }
b3483782
DM
132
133 pub fn group(&self) -> &BackupGroup {
134 &self.group
135 }
136
fa5d6977 137 pub fn backup_time(&self) -> DateTime<Utc> {
b3483782
DM
138 self.backup_time
139 }
140
141 pub fn parse(path: &str) -> Result<Self, Error> {
142
143 let cap = SNAPSHOT_PATH_REGEX.captures(path)
144 .ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'", path))?;
145
146 let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
fa5d6977
DM
147 let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Utc>>()?;
148 Ok(BackupDir::from((group, backup_time.timestamp())))
b3483782
DM
149 }
150
151 pub fn relative_path(&self) -> PathBuf {
152
153 let mut relative_path = self.group.group_path();
154
fa5d6977 155 relative_path.push(Self::backup_time_to_string(self.backup_time));
b3483782
DM
156
157 relative_path
158 }
fa5d6977
DM
159
160 pub fn backup_time_to_string(backup_time: DateTime<Utc>) -> String {
161 backup_time.to_rfc3339_opts(SecondsFormat::Secs, true)
162 }
b3483782
DM
163}
164
391d3107
WB
165impl From<(BackupGroup, i64)> for BackupDir {
166 fn from((group, timestamp): (BackupGroup, i64)) -> Self {
fa5d6977 167 Self { group, backup_time: Utc.timestamp(timestamp, 0) }
391d3107
WB
168 }
169}
170
d57474e0 171/// Detailed Backup Information, lists files inside a BackupDir
b02a52e3 172#[derive(Debug, Clone)]
b3483782
DM
173pub struct BackupInfo {
174 /// the backup directory
175 pub backup_dir: BackupDir,
176 /// List of data files
177 pub files: Vec<String>,
178}
179
180impl BackupInfo {
181
38a6cdda
DM
182 pub fn new(base_path: &Path, backup_dir: BackupDir) -> Result<BackupInfo, Error> {
183 let mut path = base_path.to_owned();
184 path.push(backup_dir.relative_path());
185
186 let files = list_backup_files(libc::AT_FDCWD, &path)?;
187
188 Ok(BackupInfo { backup_dir, files })
189 }
190
51a4f63f
DM
191 /// Finds the latest backup inside a backup group
192 pub fn last_backup(base_path: &Path, group: &BackupGroup) -> Result<Option<BackupInfo>, Error> {
193 let backups = group.list_backups(base_path)?;
194 Ok(backups.into_iter().max_by_key(|item| item.backup_dir.backup_time()))
195 }
196
b3483782
DM
197 pub fn sort_list(list: &mut Vec<BackupInfo>, ascendending: bool) {
198 if ascendending { // oldest first
199 list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time));
200 } else { // newest first
201 list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time));
202 }
203 }
204
58e99e13
DM
205 pub fn list_files(base_path: &Path, backup_dir: &BackupDir) -> Result<Vec<String>, Error> {
206 let mut path = base_path.to_owned();
207 path.push(backup_dir.relative_path());
208
c0977501 209 let files = list_backup_files(libc::AT_FDCWD, &path)?;
58e99e13
DM
210
211 Ok(files)
212 }
213
214 pub fn list_backups(base_path: &Path) -> Result<Vec<BackupInfo>, Error> {
b3483782
DM
215 let mut list = vec![];
216
58e99e13 217 tools::scandir(libc::AT_FDCWD, base_path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| {
b3483782
DM
218 if file_type != nix::dir::Type::Directory { return Ok(()); }
219 tools::scandir(l0_fd, backup_type, &BACKUP_ID_REGEX, |l1_fd, backup_id, file_type| {
220 if file_type != nix::dir::Type::Directory { return Ok(()); }
221 tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
222 if file_type != nix::dir::Type::Directory { return Ok(()); }
223
fa5d6977
DM
224 let dt = backup_time.parse::<DateTime<Utc>>()?;
225 let backup_dir = BackupDir::new(backup_type, backup_id, dt.timestamp());
b3483782 226
c0977501 227 let files = list_backup_files(l2_fd, backup_time)?;
b3483782 228
c0977501 229 list.push(BackupInfo { backup_dir, files });
b3483782
DM
230
231 Ok(())
232 })
233 })
234 })?;
235 Ok(list)
236 }
237}
c0977501
DM
238
239fn list_backup_files<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<Vec<String>, Error> {
240 let mut files = vec![];
241
242 tools::scandir(dirfd, path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
243 if file_type != nix::dir::Type::File { return Ok(()); }
244 files.push(filename.to_owned());
245 Ok(())
246 })?;
247
248 Ok(files)
249}