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