]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/datastore.rs
src/bin/proxmox-backup-client.rs: add human readable date to prune list
[proxmox-backup.git] / src / backup / datastore.rs
1 use std::collections::{HashSet, HashMap};
2 use std::io::{self, Write};
3 use std::path::{Path, PathBuf};
4 use std::sync::{Arc, Mutex};
5
6 use anyhow::{bail, format_err, Error};
7 use lazy_static::lazy_static;
8 use chrono::{DateTime, Utc};
9
10 use super::backup_info::{BackupGroup, BackupDir};
11 use super::chunk_store::ChunkStore;
12 use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
13 use super::fixed_index::{FixedIndexReader, FixedIndexWriter};
14 use super::manifest::{MANIFEST_BLOB_NAME, BackupManifest};
15 use super::index::*;
16 use super::{DataBlob, ArchiveType, archive_type};
17 use crate::config::datastore;
18 use crate::server::WorkerTask;
19 use crate::tools;
20 use crate::api2::types::GarbageCollectionStatus;
21
22 lazy_static! {
23 static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
24 }
25
26 /// Datastore Management
27 ///
28 /// A Datastore can store severals backups, and provides the
29 /// management interface for backup.
30 pub struct DataStore {
31 chunk_store: Arc<ChunkStore>,
32 gc_mutex: Mutex<bool>,
33 last_gc_status: Mutex<GarbageCollectionStatus>,
34 }
35
36 impl DataStore {
37
38 pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
39
40 let (config, _digest) = datastore::config()?;
41 let config: datastore::DataStoreConfig = config.lookup("datastore", name)?;
42
43 let mut map = DATASTORE_MAP.lock().unwrap();
44
45 if let Some(datastore) = map.get(name) {
46 // Compare Config - if changed, create new Datastore object!
47 if datastore.chunk_store.base == PathBuf::from(&config.path) {
48 return Ok(datastore.clone());
49 }
50 }
51
52 let datastore = DataStore::open(name)?;
53
54 let datastore = Arc::new(datastore);
55 map.insert(name.to_string(), datastore.clone());
56
57 Ok(datastore)
58 }
59
60 pub fn open(store_name: &str) -> Result<Self, Error> {
61
62 let (config, _digest) = datastore::config()?;
63 let (_, store_config) = config.sections.get(store_name)
64 .ok_or(format_err!("no such datastore '{}'", store_name))?;
65
66 let path = store_config["path"].as_str().unwrap();
67
68 let chunk_store = ChunkStore::open(store_name, path)?;
69
70 let gc_status = GarbageCollectionStatus::default();
71
72 Ok(Self {
73 chunk_store: Arc::new(chunk_store),
74 gc_mutex: Mutex::new(false),
75 last_gc_status: Mutex::new(gc_status),
76 })
77 }
78
79 pub fn get_chunk_iterator(
80 &self,
81 ) -> Result<
82 impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize)>,
83 Error
84 > {
85 self.chunk_store.get_chunk_iterator()
86 }
87
88 pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
89
90 let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
91
92 Ok(index)
93 }
94
95 pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
96
97 let full_path = self.chunk_store.relative_path(filename.as_ref());
98
99 let index = FixedIndexReader::open(&full_path)?;
100
101 Ok(index)
102 }
103
104 pub fn create_dynamic_writer<P: AsRef<Path>>(
105 &self, filename: P,
106 ) -> Result<DynamicIndexWriter, Error> {
107
108 let index = DynamicIndexWriter::create(
109 self.chunk_store.clone(), filename.as_ref())?;
110
111 Ok(index)
112 }
113
114 pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> {
115
116 let full_path = self.chunk_store.relative_path(filename.as_ref());
117
118 let index = DynamicIndexReader::open(&full_path)?;
119
120 Ok(index)
121 }
122
123 pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error>
124 where
125 P: AsRef<Path>,
126 {
127 let filename = filename.as_ref();
128 let out: Box<dyn IndexFile + Send> =
129 match archive_type(filename)? {
130 ArchiveType::DynamicIndex => Box::new(self.open_dynamic_reader(filename)?),
131 ArchiveType::FixedIndex => Box::new(self.open_fixed_reader(filename)?),
132 _ => bail!("cannot open index file of unknown type: {:?}", filename),
133 };
134 Ok(out)
135 }
136
137 pub fn base_path(&self) -> PathBuf {
138 self.chunk_store.base_path()
139 }
140
141 /// Clenaup a backup directory
142 ///
143 /// Removes all files not mentioned in the manifest.
144 pub fn cleanup_backup_dir(&self, backup_dir: &BackupDir, manifest: &BackupManifest
145 ) -> Result<(), Error> {
146
147 let mut full_path = self.base_path();
148 full_path.push(backup_dir.relative_path());
149
150 let mut wanted_files = HashSet::new();
151 wanted_files.insert(MANIFEST_BLOB_NAME.to_string());
152 manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); });
153
154 for item in tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
155 if let Ok(item) = item {
156 if let Some(file_type) = item.file_type() {
157 if file_type != nix::dir::Type::File { continue; }
158 }
159 let file_name = item.file_name().to_bytes();
160 if file_name == b"." || file_name == b".." { continue; };
161
162 if let Ok(name) = std::str::from_utf8(file_name) {
163 if wanted_files.contains(name) { continue; }
164 }
165 println!("remove unused file {:?}", item.file_name());
166 let dirfd = item.parent_fd();
167 let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) };
168 }
169 }
170
171 Ok(())
172 }
173
174 /// Returns the absolute path for a backup_group
175 pub fn group_path(&self, backup_group: &BackupGroup) -> PathBuf {
176 let mut full_path = self.base_path();
177 full_path.push(backup_group.group_path());
178 full_path
179 }
180
181 /// Returns the absolute path for backup_dir
182 pub fn snapshot_path(&self, backup_dir: &BackupDir) -> PathBuf {
183 let mut full_path = self.base_path();
184 full_path.push(backup_dir.relative_path());
185 full_path
186 }
187
188 /// Remove a complete backup group including all snapshots
189 pub fn remove_backup_group(&self, backup_group: &BackupGroup) -> Result<(), Error> {
190
191 let full_path = self.group_path(backup_group);
192
193 log::info!("removing backup group {:?}", full_path);
194 std::fs::remove_dir_all(&full_path)
195 .map_err(|err| {
196 format_err!(
197 "removing backup group {:?} failed - {}",
198 full_path,
199 err,
200 )
201 })?;
202
203 Ok(())
204 }
205
206 /// Remove a backup directory including all content
207 pub fn remove_backup_dir(&self, backup_dir: &BackupDir) -> Result<(), Error> {
208
209 let full_path = self.snapshot_path(backup_dir);
210
211 log::info!("removing backup snapshot {:?}", full_path);
212 std::fs::remove_dir_all(&full_path)
213 .map_err(|err| {
214 format_err!(
215 "removing backup snapshot {:?} failed - {}",
216 full_path,
217 err,
218 )
219 })?;
220
221 Ok(())
222 }
223
224 /// Returns the time of the last successful backup
225 ///
226 /// Or None if there is no backup in the group (or the group dir does not exist).
227 pub fn last_successful_backup(&self, backup_group: &BackupGroup) -> Result<Option<DateTime<Utc>>, Error> {
228 let base_path = self.base_path();
229 let mut group_path = base_path.clone();
230 group_path.push(backup_group.group_path());
231
232 if group_path.exists() {
233 backup_group.last_successful_backup(&base_path)
234 } else {
235 Ok(None)
236 }
237 }
238
239 /// Returns the backup owner.
240 ///
241 /// The backup owner is the user who first created the backup group.
242 pub fn get_owner(&self, backup_group: &BackupGroup) -> Result<String, Error> {
243 let mut full_path = self.base_path();
244 full_path.push(backup_group.group_path());
245 full_path.push("owner");
246 let owner = proxmox::tools::fs::file_read_firstline(full_path)?;
247 Ok(owner.trim_end().to_string()) // remove trailing newline
248 }
249
250 /// Set the backup owner.
251 pub fn set_owner(&self, backup_group: &BackupGroup, userid: &str, force: bool) -> Result<(), Error> {
252 let mut path = self.base_path();
253 path.push(backup_group.group_path());
254 path.push("owner");
255
256 let mut open_options = std::fs::OpenOptions::new();
257 open_options.write(true);
258 open_options.truncate(true);
259
260 if force {
261 open_options.create(true);
262 } else {
263 open_options.create_new(true);
264 }
265
266 let mut file = open_options.open(&path)
267 .map_err(|err| format_err!("unable to create owner file {:?} - {}", path, err))?;
268
269 write!(file, "{}\n", userid)
270 .map_err(|err| format_err!("unable to write owner file {:?} - {}", path, err))?;
271
272 Ok(())
273 }
274
275 /// Create a backup group if it does not already exists.
276 ///
277 /// And set the owner to 'userid'. If the group already exists, it returns the
278 /// current owner (instead of setting the owner).
279 pub fn create_backup_group(&self, backup_group: &BackupGroup, userid: &str) -> Result<String, Error> {
280
281 // create intermediate path first:
282 let base_path = self.base_path();
283
284 let mut full_path = base_path.clone();
285 full_path.push(backup_group.backup_type());
286 std::fs::create_dir_all(&full_path)?;
287
288 full_path.push(backup_group.backup_id());
289
290 // create the last component now
291 match std::fs::create_dir(&full_path) {
292 Ok(_) => {
293 self.set_owner(backup_group, userid, false)?;
294 let owner = self.get_owner(backup_group)?; // just to be sure
295 Ok(owner)
296 }
297 Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
298 let owner = self.get_owner(backup_group)?; // just to be sure
299 Ok(owner)
300 }
301 Err(err) => bail!("unable to create backup group {:?} - {}", full_path, err),
302 }
303 }
304
305 /// Creates a new backup snapshot inside a BackupGroup
306 ///
307 /// The BackupGroup directory needs to exist.
308 pub fn create_backup_dir(&self, backup_dir: &BackupDir) -> Result<(PathBuf, bool), io::Error> {
309 let relative_path = backup_dir.relative_path();
310 let mut full_path = self.base_path();
311 full_path.push(&relative_path);
312
313 match std::fs::create_dir(&full_path) {
314 Ok(_) => Ok((relative_path, true)),
315 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false)),
316 Err(e) => Err(e)
317 }
318 }
319
320 pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
321 let base = self.base_path();
322
323 let mut list = vec![];
324
325 use walkdir::WalkDir;
326
327 let walker = WalkDir::new(&base).same_file_system(true).into_iter();
328
329 // make sure we skip .chunks (and other hidden files to keep it simple)
330 fn is_hidden(entry: &walkdir::DirEntry) -> bool {
331 entry.file_name()
332 .to_str()
333 .map(|s| s.starts_with("."))
334 .unwrap_or(false)
335 }
336
337 for entry in walker.filter_entry(|e| !is_hidden(e)) {
338 let path = entry?.into_path();
339 if let Ok(archive_type) = archive_type(&path) {
340 if archive_type == ArchiveType::FixedIndex || archive_type == ArchiveType::DynamicIndex {
341 list.push(path);
342 }
343 }
344 }
345
346 Ok(list)
347 }
348
349 // mark chunks used by ``index`` as used
350 fn index_mark_used_chunks<I: IndexFile>(
351 &self,
352 index: I,
353 file_name: &Path, // only used for error reporting
354 status: &mut GarbageCollectionStatus,
355 ) -> Result<(), Error> {
356
357 status.index_file_count += 1;
358 status.index_data_bytes += index.index_bytes();
359
360 for pos in 0..index.index_count() {
361 tools::fail_on_shutdown()?;
362 let digest = index.index_digest(pos).unwrap();
363 if let Err(err) = self.chunk_store.touch_chunk(digest) {
364 bail!("unable to access chunk {}, required by {:?} - {}",
365 proxmox::tools::digest_to_hex(digest), file_name, err);
366 }
367 }
368 Ok(())
369 }
370
371 fn mark_used_chunks(&self, status: &mut GarbageCollectionStatus) -> Result<(), Error> {
372
373 let image_list = self.list_images()?;
374
375 for path in image_list {
376
377 tools::fail_on_shutdown()?;
378
379 if let Ok(archive_type) = archive_type(&path) {
380 if archive_type == ArchiveType::FixedIndex {
381 let index = self.open_fixed_reader(&path)?;
382 self.index_mark_used_chunks(index, &path, status)?;
383 } else if archive_type == ArchiveType::DynamicIndex {
384 let index = self.open_dynamic_reader(&path)?;
385 self.index_mark_used_chunks(index, &path, status)?;
386 }
387 }
388 }
389
390 Ok(())
391 }
392
393 pub fn last_gc_status(&self) -> GarbageCollectionStatus {
394 self.last_gc_status.lock().unwrap().clone()
395 }
396
397 pub fn garbage_collection(&self, worker: Arc<WorkerTask>) -> Result<(), Error> {
398
399 if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() {
400
401 let _exclusive_lock = self.chunk_store.try_exclusive_lock()?;
402
403 let now = unsafe { libc::time(std::ptr::null_mut()) };
404
405 let oldest_writer = self.chunk_store.oldest_writer().unwrap_or(now);
406
407 let mut gc_status = GarbageCollectionStatus::default();
408 gc_status.upid = Some(worker.to_string());
409
410 worker.log("Start GC phase1 (mark used chunks)");
411
412 self.mark_used_chunks(&mut gc_status)?;
413
414 worker.log("Start GC phase2 (sweep unused chunks)");
415 self.chunk_store.sweep_unused_chunks(oldest_writer, &mut gc_status, worker.clone())?;
416
417 worker.log(&format!("Removed bytes: {}", gc_status.removed_bytes));
418 worker.log(&format!("Removed chunks: {}", gc_status.removed_chunks));
419 if gc_status.pending_bytes > 0 {
420 worker.log(&format!("Pending removals: {} bytes ({} chunks)", gc_status.pending_bytes, gc_status.pending_chunks));
421 }
422
423 worker.log(&format!("Original data bytes: {}", gc_status.index_data_bytes));
424
425 if gc_status.index_data_bytes > 0 {
426 let comp_per = (gc_status.disk_bytes*100)/gc_status.index_data_bytes;
427 worker.log(&format!("Disk bytes: {} ({} %)", gc_status.disk_bytes, comp_per));
428 }
429
430 worker.log(&format!("Disk chunks: {}", gc_status.disk_chunks));
431
432 if gc_status.disk_chunks > 0 {
433 let avg_chunk = gc_status.disk_bytes/(gc_status.disk_chunks as u64);
434 worker.log(&format!("Average chunk size: {}", avg_chunk));
435 }
436
437 *self.last_gc_status.lock().unwrap() = gc_status;
438
439 } else {
440 bail!("Start GC failed - (already running/locked)");
441 }
442
443 Ok(())
444 }
445
446 pub fn try_shared_chunk_store_lock(&self) -> Result<tools::ProcessLockSharedGuard, Error> {
447 self.chunk_store.try_shared_lock()
448 }
449
450 pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
451 self.chunk_store.chunk_path(digest)
452 }
453
454 pub fn cond_touch_chunk(&self, digest: &[u8; 32], fail_if_not_exist: bool) -> Result<bool, Error> {
455 self.chunk_store.cond_touch_chunk(digest, fail_if_not_exist)
456 }
457
458 pub fn insert_chunk(
459 &self,
460 chunk: &DataBlob,
461 digest: &[u8; 32],
462 ) -> Result<(bool, u64), Error> {
463 self.chunk_store.insert_chunk(chunk, digest)
464 }
465 }