]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/datastore.rs
7dd2624c825dfff4e528f40a837607d7997455e7
[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 use std::convert::TryFrom;
6
7 use anyhow::{bail, format_err, Error};
8 use lazy_static::lazy_static;
9
10 use proxmox::tools::fs::{replace_file, CreateOptions};
11
12 use super::backup_info::{BackupGroup, BackupDir};
13 use super::chunk_store::ChunkStore;
14 use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
15 use super::fixed_index::{FixedIndexReader, FixedIndexWriter};
16 use super::manifest::{MANIFEST_BLOB_NAME, CLIENT_LOG_BLOB_NAME, BackupManifest};
17 use super::index::*;
18 use super::{DataBlob, ArchiveType, archive_type};
19 use crate::config::datastore;
20 use crate::task::TaskState;
21 use crate::tools;
22 use crate::tools::format::HumanByte;
23 use crate::tools::fs::{lock_dir_noblock, DirLockGuard};
24 use crate::api2::types::{GarbageCollectionStatus, Userid};
25 use crate::server::UPID;
26
27 lazy_static! {
28 static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
29 }
30
31 /// Datastore Management
32 ///
33 /// A Datastore can store severals backups, and provides the
34 /// management interface for backup.
35 pub struct DataStore {
36 chunk_store: Arc<ChunkStore>,
37 gc_mutex: Mutex<bool>,
38 last_gc_status: Mutex<GarbageCollectionStatus>,
39 }
40
41 impl DataStore {
42
43 pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
44
45 let (config, _digest) = datastore::config()?;
46 let config: datastore::DataStoreConfig = config.lookup("datastore", name)?;
47
48 let mut map = DATASTORE_MAP.lock().unwrap();
49
50 if let Some(datastore) = map.get(name) {
51 // Compare Config - if changed, create new Datastore object!
52 if datastore.chunk_store.base == PathBuf::from(&config.path) {
53 return Ok(datastore.clone());
54 }
55 }
56
57 let datastore = DataStore::open(name)?;
58
59 let datastore = Arc::new(datastore);
60 map.insert(name.to_string(), datastore.clone());
61
62 Ok(datastore)
63 }
64
65 pub fn open(store_name: &str) -> Result<Self, Error> {
66
67 let (config, _digest) = datastore::config()?;
68 let (_, store_config) = config.sections.get(store_name)
69 .ok_or(format_err!("no such datastore '{}'", store_name))?;
70
71 let path = store_config["path"].as_str().unwrap();
72
73 Self::open_with_path(store_name, Path::new(path))
74 }
75
76 pub fn open_with_path(store_name: &str, path: &Path) -> Result<Self, Error> {
77 let chunk_store = ChunkStore::open(store_name, path)?;
78
79 let gc_status = GarbageCollectionStatus::default();
80
81 Ok(Self {
82 chunk_store: Arc::new(chunk_store),
83 gc_mutex: Mutex::new(false),
84 last_gc_status: Mutex::new(gc_status),
85 })
86 }
87
88 pub fn get_chunk_iterator(
89 &self,
90 ) -> Result<
91 impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize, bool)>,
92 Error
93 > {
94 self.chunk_store.get_chunk_iterator()
95 }
96
97 pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
98
99 let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
100
101 Ok(index)
102 }
103
104 pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
105
106 let full_path = self.chunk_store.relative_path(filename.as_ref());
107
108 let index = FixedIndexReader::open(&full_path)?;
109
110 Ok(index)
111 }
112
113 pub fn create_dynamic_writer<P: AsRef<Path>>(
114 &self, filename: P,
115 ) -> Result<DynamicIndexWriter, Error> {
116
117 let index = DynamicIndexWriter::create(
118 self.chunk_store.clone(), filename.as_ref())?;
119
120 Ok(index)
121 }
122
123 pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> {
124
125 let full_path = self.chunk_store.relative_path(filename.as_ref());
126
127 let index = DynamicIndexReader::open(&full_path)?;
128
129 Ok(index)
130 }
131
132 pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error>
133 where
134 P: AsRef<Path>,
135 {
136 let filename = filename.as_ref();
137 let out: Box<dyn IndexFile + Send> =
138 match archive_type(filename)? {
139 ArchiveType::DynamicIndex => Box::new(self.open_dynamic_reader(filename)?),
140 ArchiveType::FixedIndex => Box::new(self.open_fixed_reader(filename)?),
141 _ => bail!("cannot open index file of unknown type: {:?}", filename),
142 };
143 Ok(out)
144 }
145
146 pub fn name(&self) -> &str {
147 self.chunk_store.name()
148 }
149
150 pub fn base_path(&self) -> PathBuf {
151 self.chunk_store.base_path()
152 }
153
154 /// Cleanup a backup directory
155 ///
156 /// Removes all files not mentioned in the manifest.
157 pub fn cleanup_backup_dir(&self, backup_dir: &BackupDir, manifest: &BackupManifest
158 ) -> Result<(), Error> {
159
160 let mut full_path = self.base_path();
161 full_path.push(backup_dir.relative_path());
162
163 let mut wanted_files = HashSet::new();
164 wanted_files.insert(MANIFEST_BLOB_NAME.to_string());
165 wanted_files.insert(CLIENT_LOG_BLOB_NAME.to_string());
166 manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); });
167
168 for item in tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
169 if let Ok(item) = item {
170 if let Some(file_type) = item.file_type() {
171 if file_type != nix::dir::Type::File { continue; }
172 }
173 let file_name = item.file_name().to_bytes();
174 if file_name == b"." || file_name == b".." { continue; };
175
176 if let Ok(name) = std::str::from_utf8(file_name) {
177 if wanted_files.contains(name) { continue; }
178 }
179 println!("remove unused file {:?}", item.file_name());
180 let dirfd = item.parent_fd();
181 let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) };
182 }
183 }
184
185 Ok(())
186 }
187
188 /// Returns the absolute path for a backup_group
189 pub fn group_path(&self, backup_group: &BackupGroup) -> PathBuf {
190 let mut full_path = self.base_path();
191 full_path.push(backup_group.group_path());
192 full_path
193 }
194
195 /// Returns the absolute path for backup_dir
196 pub fn snapshot_path(&self, backup_dir: &BackupDir) -> PathBuf {
197 let mut full_path = self.base_path();
198 full_path.push(backup_dir.relative_path());
199 full_path
200 }
201
202 /// Remove a complete backup group including all snapshots
203 pub fn remove_backup_group(&self, backup_group: &BackupGroup) -> Result<(), Error> {
204
205 let full_path = self.group_path(backup_group);
206
207 let _guard = tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?;
208
209 log::info!("removing backup group {:?}", full_path);
210
211 // remove all individual backup dirs first to ensure nothing is using them
212 for snap in backup_group.list_backups(&self.base_path())? {
213 self.remove_backup_dir(&snap.backup_dir, false)?;
214 }
215
216 // no snapshots left, we can now safely remove the empty folder
217 std::fs::remove_dir_all(&full_path)
218 .map_err(|err| {
219 format_err!(
220 "removing backup group directory {:?} failed - {}",
221 full_path,
222 err,
223 )
224 })?;
225
226 Ok(())
227 }
228
229 /// Remove a backup directory including all content
230 pub fn remove_backup_dir(&self, backup_dir: &BackupDir, force: bool) -> Result<(), Error> {
231
232 let full_path = self.snapshot_path(backup_dir);
233
234 let _guard;
235 if !force {
236 _guard = lock_dir_noblock(&full_path, "snapshot", "possibly running or in use")?;
237 }
238
239 log::info!("removing backup snapshot {:?}", full_path);
240 std::fs::remove_dir_all(&full_path)
241 .map_err(|err| {
242 format_err!(
243 "removing backup snapshot {:?} failed - {}",
244 full_path,
245 err,
246 )
247 })?;
248
249 Ok(())
250 }
251
252 /// Returns the time of the last successful backup
253 ///
254 /// Or None if there is no backup in the group (or the group dir does not exist).
255 pub fn last_successful_backup(&self, backup_group: &BackupGroup) -> Result<Option<i64>, Error> {
256 let base_path = self.base_path();
257 let mut group_path = base_path.clone();
258 group_path.push(backup_group.group_path());
259
260 if group_path.exists() {
261 backup_group.last_successful_backup(&base_path)
262 } else {
263 Ok(None)
264 }
265 }
266
267 /// Returns the backup owner.
268 ///
269 /// The backup owner is the user who first created the backup group.
270 pub fn get_owner(&self, backup_group: &BackupGroup) -> Result<Userid, Error> {
271 let mut full_path = self.base_path();
272 full_path.push(backup_group.group_path());
273 full_path.push("owner");
274 let owner = proxmox::tools::fs::file_read_firstline(full_path)?;
275 Ok(owner.trim_end().parse()?) // remove trailing newline
276 }
277
278 /// Set the backup owner.
279 pub fn set_owner(
280 &self,
281 backup_group: &BackupGroup,
282 userid: &Userid,
283 force: bool,
284 ) -> Result<(), Error> {
285 let mut path = self.base_path();
286 path.push(backup_group.group_path());
287 path.push("owner");
288
289 let mut open_options = std::fs::OpenOptions::new();
290 open_options.write(true);
291 open_options.truncate(true);
292
293 if force {
294 open_options.create(true);
295 } else {
296 open_options.create_new(true);
297 }
298
299 let mut file = open_options.open(&path)
300 .map_err(|err| format_err!("unable to create owner file {:?} - {}", path, err))?;
301
302 writeln!(file, "{}", userid)
303 .map_err(|err| format_err!("unable to write owner file {:?} - {}", path, err))?;
304
305 Ok(())
306 }
307
308 /// Create (if it does not already exists) and lock a backup group
309 ///
310 /// And set the owner to 'userid'. If the group already exists, it returns the
311 /// current owner (instead of setting the owner).
312 ///
313 /// This also acquires an exclusive lock on the directory and returns the lock guard.
314 pub fn create_locked_backup_group(
315 &self,
316 backup_group: &BackupGroup,
317 userid: &Userid,
318 ) -> Result<(Userid, DirLockGuard), Error> {
319 // create intermediate path first:
320 let base_path = self.base_path();
321
322 let mut full_path = base_path.clone();
323 full_path.push(backup_group.backup_type());
324 std::fs::create_dir_all(&full_path)?;
325
326 full_path.push(backup_group.backup_id());
327
328 // create the last component now
329 match std::fs::create_dir(&full_path) {
330 Ok(_) => {
331 let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?;
332 self.set_owner(backup_group, userid, false)?;
333 let owner = self.get_owner(backup_group)?; // just to be sure
334 Ok((owner, guard))
335 }
336 Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
337 let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?;
338 let owner = self.get_owner(backup_group)?; // just to be sure
339 Ok((owner, guard))
340 }
341 Err(err) => bail!("unable to create backup group {:?} - {}", full_path, err),
342 }
343 }
344
345 /// Creates a new backup snapshot inside a BackupGroup
346 ///
347 /// The BackupGroup directory needs to exist.
348 pub fn create_locked_backup_dir(&self, backup_dir: &BackupDir)
349 -> Result<(PathBuf, bool, DirLockGuard), Error>
350 {
351 let relative_path = backup_dir.relative_path();
352 let mut full_path = self.base_path();
353 full_path.push(&relative_path);
354
355 let lock = ||
356 lock_dir_noblock(&full_path, "snapshot", "internal error - tried creating snapshot that's already in use");
357
358 match std::fs::create_dir(&full_path) {
359 Ok(_) => Ok((relative_path, true, lock()?)),
360 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false, lock()?)),
361 Err(e) => Err(e.into())
362 }
363 }
364
365 pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
366 let base = self.base_path();
367
368 let mut list = vec![];
369
370 use walkdir::WalkDir;
371
372 let walker = WalkDir::new(&base).same_file_system(true).into_iter();
373
374 // make sure we skip .chunks (and other hidden files to keep it simple)
375 fn is_hidden(entry: &walkdir::DirEntry) -> bool {
376 entry.file_name()
377 .to_str()
378 .map(|s| s.starts_with("."))
379 .unwrap_or(false)
380 }
381 let handle_entry_err = |err: walkdir::Error| {
382 if let Some(inner) = err.io_error() {
383 let path = err.path().unwrap_or(Path::new(""));
384 match inner.kind() {
385 io::ErrorKind::PermissionDenied => {
386 // only allow to skip ext4 fsck directory, avoid GC if, for example,
387 // a user got file permissions wrong on datastore rsync to new server
388 if err.depth() > 1 || !path.ends_with("lost+found") {
389 bail!("cannot continue garbage-collection safely, permission denied on: {}", path.display())
390 }
391 },
392 _ => bail!("unexpected error on datastore traversal: {} - {}", inner, path.display()),
393 }
394 }
395 Ok(())
396 };
397 for entry in walker.filter_entry(|e| !is_hidden(e)) {
398 let path = match entry {
399 Ok(entry) => entry.into_path(),
400 Err(err) => {
401 handle_entry_err(err)?;
402 continue
403 },
404 };
405 if let Ok(archive_type) = archive_type(&path) {
406 if archive_type == ArchiveType::FixedIndex || archive_type == ArchiveType::DynamicIndex {
407 list.push(path);
408 }
409 }
410 }
411
412 Ok(list)
413 }
414
415 // mark chunks used by ``index`` as used
416 fn index_mark_used_chunks<I: IndexFile>(
417 &self,
418 index: I,
419 file_name: &Path, // only used for error reporting
420 status: &mut GarbageCollectionStatus,
421 worker: &dyn TaskState,
422 ) -> Result<(), Error> {
423
424 status.index_file_count += 1;
425 status.index_data_bytes += index.index_bytes();
426
427 for pos in 0..index.index_count() {
428 worker.check_abort()?;
429 tools::fail_on_shutdown()?;
430 let digest = index.index_digest(pos).unwrap();
431 if let Err(err) = self.chunk_store.touch_chunk(digest) {
432 crate::task_warn!(
433 worker,
434 "warning: unable to access chunk {}, required by {:?} - {}",
435 proxmox::tools::digest_to_hex(digest),
436 file_name,
437 err,
438 );
439 }
440 }
441 Ok(())
442 }
443
444 fn mark_used_chunks(
445 &self,
446 status: &mut GarbageCollectionStatus,
447 worker: &dyn TaskState,
448 ) -> Result<(), Error> {
449
450 let image_list = self.list_images()?;
451
452 let image_count = image_list.len();
453
454 let mut done = 0;
455
456 let mut last_percentage: usize = 0;
457
458 for path in image_list {
459
460 worker.check_abort()?;
461 tools::fail_on_shutdown()?;
462
463 if let Ok(archive_type) = archive_type(&path) {
464 if archive_type == ArchiveType::FixedIndex {
465 let index = self.open_fixed_reader(&path)?;
466 self.index_mark_used_chunks(index, &path, status, worker)?;
467 } else if archive_type == ArchiveType::DynamicIndex {
468 let index = self.open_dynamic_reader(&path)?;
469 self.index_mark_used_chunks(index, &path, status, worker)?;
470 }
471 }
472 done += 1;
473
474 let percentage = done*100/image_count;
475 if percentage > last_percentage {
476 crate::task_log!(
477 worker,
478 "percentage done: phase1 {}% ({} of {} index files)",
479 percentage,
480 done,
481 image_count,
482 );
483 last_percentage = percentage;
484 }
485 }
486
487 Ok(())
488 }
489
490 pub fn last_gc_status(&self) -> GarbageCollectionStatus {
491 self.last_gc_status.lock().unwrap().clone()
492 }
493
494 pub fn garbage_collection_running(&self) -> bool {
495 if let Ok(_) = self.gc_mutex.try_lock() { false } else { true }
496 }
497
498 pub fn garbage_collection(&self, worker: &dyn TaskState, upid: &UPID) -> Result<(), Error> {
499
500 if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() {
501
502 // avoids that we run GC if an old daemon process has still a
503 // running backup writer, which is not save as we have no "oldest
504 // writer" information and thus no safe atime cutoff
505 let _exclusive_lock = self.chunk_store.try_exclusive_lock()?;
506
507 let phase1_start_time = proxmox::tools::time::epoch_i64();
508 let oldest_writer = self.chunk_store.oldest_writer().unwrap_or(phase1_start_time);
509
510 let mut gc_status = GarbageCollectionStatus::default();
511 gc_status.upid = Some(upid.to_string());
512
513 crate::task_log!(worker, "Start GC phase1 (mark used chunks)");
514
515 self.mark_used_chunks(&mut gc_status, worker)?;
516
517 crate::task_log!(worker, "Start GC phase2 (sweep unused chunks)");
518 self.chunk_store.sweep_unused_chunks(
519 oldest_writer,
520 phase1_start_time,
521 &mut gc_status,
522 worker,
523 )?;
524
525 crate::task_log!(
526 worker,
527 "Removed garbage: {}",
528 HumanByte::from(gc_status.removed_bytes),
529 );
530 crate::task_log!(worker, "Removed chunks: {}", gc_status.removed_chunks);
531 if gc_status.pending_bytes > 0 {
532 crate::task_log!(
533 worker,
534 "Pending removals: {} (in {} chunks)",
535 HumanByte::from(gc_status.pending_bytes),
536 gc_status.pending_chunks,
537 );
538 }
539 if gc_status.removed_bad > 0 {
540 crate::task_log!(worker, "Removed bad files: {}", gc_status.removed_bad);
541 }
542
543 crate::task_log!(
544 worker,
545 "Original data usage: {}",
546 HumanByte::from(gc_status.index_data_bytes),
547 );
548
549 if gc_status.index_data_bytes > 0 {
550 let comp_per = (gc_status.disk_bytes as f64 * 100.)/gc_status.index_data_bytes as f64;
551 crate::task_log!(
552 worker,
553 "On-Disk usage: {} ({:.2}%)",
554 HumanByte::from(gc_status.disk_bytes),
555 comp_per,
556 );
557 }
558
559 crate::task_log!(worker, "On-Disk chunks: {}", gc_status.disk_chunks);
560
561 if gc_status.disk_chunks > 0 {
562 let avg_chunk = gc_status.disk_bytes/(gc_status.disk_chunks as u64);
563 crate::task_log!(worker, "Average chunk size: {}", HumanByte::from(avg_chunk));
564 }
565
566 *self.last_gc_status.lock().unwrap() = gc_status;
567
568 } else {
569 bail!("Start GC failed - (already running/locked)");
570 }
571
572 Ok(())
573 }
574
575 pub fn try_shared_chunk_store_lock(&self) -> Result<tools::ProcessLockSharedGuard, Error> {
576 self.chunk_store.try_shared_lock()
577 }
578
579 pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
580 self.chunk_store.chunk_path(digest)
581 }
582
583 pub fn cond_touch_chunk(&self, digest: &[u8; 32], fail_if_not_exist: bool) -> Result<bool, Error> {
584 self.chunk_store.cond_touch_chunk(digest, fail_if_not_exist)
585 }
586
587 pub fn insert_chunk(
588 &self,
589 chunk: &DataBlob,
590 digest: &[u8; 32],
591 ) -> Result<(bool, u64), Error> {
592 self.chunk_store.insert_chunk(chunk, digest)
593 }
594
595 pub fn load_blob(&self, backup_dir: &BackupDir, filename: &str) -> Result<DataBlob, Error> {
596 let mut path = self.base_path();
597 path.push(backup_dir.relative_path());
598 path.push(filename);
599
600 proxmox::try_block!({
601 let mut file = std::fs::File::open(&path)?;
602 DataBlob::load_from_reader(&mut file)
603 }).map_err(|err| format_err!("unable to load blob '{:?}' - {}", path, err))
604 }
605
606
607 pub fn load_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
608
609 let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest);
610
611 proxmox::try_block!({
612 let mut file = std::fs::File::open(&chunk_path)?;
613 DataBlob::load_from_reader(&mut file)
614 }).map_err(|err| format_err!(
615 "store '{}', unable to load chunk '{}' - {}",
616 self.name(),
617 digest_str,
618 err,
619 ))
620 }
621
622 pub fn load_manifest(
623 &self,
624 backup_dir: &BackupDir,
625 ) -> Result<(BackupManifest, u64), Error> {
626 let blob = self.load_blob(backup_dir, MANIFEST_BLOB_NAME)?;
627 let raw_size = blob.raw_size();
628 let manifest = BackupManifest::try_from(blob)?;
629 Ok((manifest, raw_size))
630 }
631
632 pub fn store_manifest(
633 &self,
634 backup_dir: &BackupDir,
635 manifest: BackupManifest,
636 ) -> Result<(), Error> {
637 let manifest = serde_json::to_value(manifest)?;
638 let manifest = serde_json::to_string_pretty(&manifest)?;
639 let blob = DataBlob::encode(manifest.as_bytes(), None, true)?;
640 let raw_data = blob.raw_data();
641
642 let mut path = self.base_path();
643 path.push(backup_dir.relative_path());
644 path.push(MANIFEST_BLOB_NAME);
645
646 replace_file(&path, raw_data, CreateOptions::new())?;
647
648 Ok(())
649 }
650 }