2020-01-05 14:15:12 +00:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2020-04-28 08:11:15 +00:00
|
|
|
use std::io::{self, Write};
|
2019-08-13 10:59:03 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-06-24 04:58:14 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-11-30 15:22:18 +00:00
|
|
|
use std::str::FromStr;
|
2020-10-16 07:31:12 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::fs::File;
|
2019-08-13 10:59:03 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2018-12-22 16:37:25 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-07-31 05:19:14 +00:00
|
|
|
|
2020-10-23 14:32:32 +00:00
|
|
|
use proxmox::tools::fs::{replace_file, file_read_optional_string, CreateOptions, open_file_locked};
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2020-08-11 08:50:38 +00:00
|
|
|
use super::backup_info::{BackupGroup, BackupDir};
|
2020-01-23 12:31:52 +00:00
|
|
|
use super::chunk_store::ChunkStore;
|
2019-08-13 10:59:03 +00:00
|
|
|
use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
|
|
|
|
use super::fixed_index::{FixedIndexReader, FixedIndexWriter};
|
2020-10-16 07:31:12 +00:00
|
|
|
use super::manifest::{MANIFEST_BLOB_NAME, MANIFEST_LOCK_NAME, CLIENT_LOG_BLOB_NAME, BackupManifest};
|
2019-02-28 09:21:56 +00:00
|
|
|
use super::index::*;
|
2019-12-31 14:23:41 +00:00
|
|
|
use super::{DataBlob, ArchiveType, archive_type};
|
2020-10-20 07:07:32 +00:00
|
|
|
use crate::config::datastore::{self, DataStoreConfig};
|
2020-10-12 09:46:34 +00:00
|
|
|
use crate::task::TaskState;
|
2019-08-13 10:59:03 +00:00
|
|
|
use crate::tools;
|
2020-08-27 13:55:57 +00:00
|
|
|
use crate::tools::format::HumanByte;
|
2020-08-11 08:50:37 +00:00
|
|
|
use crate::tools::fs::{lock_dir_noblock, DirLockGuard};
|
2020-10-23 11:33:21 +00:00
|
|
|
use crate::api2::types::{Authid, GarbageCollectionStatus};
|
2020-10-12 09:46:34 +00:00
|
|
|
use crate::server::UPID;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-08-13 10:59:03 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// Datastore Management
|
|
|
|
///
|
|
|
|
/// A Datastore can store severals backups, and provides the
|
|
|
|
/// management interface for backup.
|
2018-12-17 12:00:39 +00:00
|
|
|
pub struct DataStore {
|
2019-01-15 10:38:26 +00:00
|
|
|
chunk_store: Arc<ChunkStore>,
|
2018-12-22 15:58:16 +00:00
|
|
|
gc_mutex: Mutex<bool>,
|
2019-04-11 10:04:25 +00:00
|
|
|
last_gc_status: Mutex<GarbageCollectionStatus>,
|
2020-10-20 08:08:25 +00:00
|
|
|
verify_new: bool,
|
2018-12-17 12:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DataStore {
|
|
|
|
|
2018-12-22 16:37:25 +00:00
|
|
|
pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
|
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let (config, _digest) = datastore::config()?;
|
|
|
|
let config: datastore::DataStoreConfig = config.lookup("datastore", name)?;
|
2020-10-19 14:45:22 +00:00
|
|
|
let path = PathBuf::from(&config.path);
|
2018-12-22 16:37:25 +00:00
|
|
|
|
2019-03-18 09:00:58 +00:00
|
|
|
let mut map = DATASTORE_MAP.lock().unwrap();
|
2018-12-22 16:37:25 +00:00
|
|
|
|
|
|
|
if let Some(datastore) = map.get(name) {
|
|
|
|
// Compare Config - if changed, create new Datastore object!
|
2020-10-20 08:08:25 +00:00
|
|
|
if datastore.chunk_store.base == path &&
|
|
|
|
datastore.verify_new == config.verify_new.unwrap_or(false)
|
|
|
|
{
|
2018-12-22 16:37:25 +00:00
|
|
|
return Ok(datastore.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:45:22 +00:00
|
|
|
let datastore = DataStore::open_with_path(name, &path, config)?;
|
2019-02-15 13:33:03 +00:00
|
|
|
|
|
|
|
let datastore = Arc::new(datastore);
|
|
|
|
map.insert(name.to_string(), datastore.clone());
|
2018-12-22 16:37:25 +00:00
|
|
|
|
2019-02-15 13:33:03 +00:00
|
|
|
Ok(datastore)
|
2018-12-22 16:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 08:08:25 +00:00
|
|
|
fn open_with_path(store_name: &str, path: &Path, config: DataStoreConfig) -> Result<Self, Error> {
|
2018-12-19 12:40:26 +00:00
|
|
|
let chunk_store = ChunkStore::open(store_name, path)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2020-10-23 14:32:32 +00:00
|
|
|
let mut gc_status_path = chunk_store.base_path();
|
|
|
|
gc_status_path.push(".gc-status");
|
|
|
|
|
|
|
|
let gc_status = if let Some(state) = file_read_optional_string(gc_status_path)? {
|
|
|
|
match serde_json::from_str(&state) {
|
|
|
|
Ok(state) => state,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("error reading gc-status: {}", err);
|
|
|
|
GarbageCollectionStatus::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GarbageCollectionStatus::default()
|
|
|
|
};
|
2019-04-11 10:04:25 +00:00
|
|
|
|
2018-12-17 12:00:39 +00:00
|
|
|
Ok(Self {
|
2019-01-15 10:38:26 +00:00
|
|
|
chunk_store: Arc::new(chunk_store),
|
2018-12-22 15:58:16 +00:00
|
|
|
gc_mutex: Mutex::new(false),
|
2019-04-11 10:04:25 +00:00
|
|
|
last_gc_status: Mutex::new(gc_status),
|
2020-10-20 08:08:25 +00:00
|
|
|
verify_new: config.verify_new.unwrap_or(false),
|
2018-12-17 12:00:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:39:09 +00:00
|
|
|
pub fn get_chunk_iterator(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
2020-09-07 15:30:34 +00:00
|
|
|
impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize, bool)>,
|
2019-02-14 10:39:09 +00:00
|
|
|
Error
|
|
|
|
> {
|
2019-07-04 07:26:44 +00:00
|
|
|
self.chunk_store.get_chunk_iterator()
|
2019-02-14 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-07-04 06:09:48 +00:00
|
|
|
let full_path = self.chunk_store.relative_path(filename.as_ref());
|
|
|
|
|
|
|
|
let index = FixedIndexReader::open(&full_path)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub fn create_dynamic_writer<P: AsRef<Path>>(
|
2018-12-31 16:30:08 +00:00
|
|
|
&self, filename: P,
|
2019-02-12 11:05:33 +00:00
|
|
|
) -> Result<DynamicIndexWriter, Error> {
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let index = DynamicIndexWriter::create(
|
2019-05-29 06:49:57 +00:00
|
|
|
self.chunk_store.clone(), filename.as_ref())?;
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
let full_path = self.chunk_store.relative_path(filename.as_ref());
|
|
|
|
|
|
|
|
let index = DynamicIndexReader::open(&full_path)?;
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:21:56 +00:00
|
|
|
pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let filename = filename.as_ref();
|
|
|
|
let out: Box<dyn IndexFile + Send> =
|
2019-12-31 14:23:41 +00:00
|
|
|
match archive_type(filename)? {
|
|
|
|
ArchiveType::DynamicIndex => Box::new(self.open_dynamic_reader(filename)?),
|
|
|
|
ArchiveType::FixedIndex => Box::new(self.open_fixed_reader(filename)?),
|
2019-02-28 09:21:56 +00:00
|
|
|
_ => bail!("cannot open index file of unknown type: {:?}", filename),
|
|
|
|
};
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:58:14 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.chunk_store.name()
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:01:37 +00:00
|
|
|
pub fn base_path(&self) -> PathBuf {
|
|
|
|
self.chunk_store.base_path()
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:04:14 +00:00
|
|
|
/// Cleanup a backup directory
|
2020-01-05 14:15:12 +00:00
|
|
|
///
|
|
|
|
/// Removes all files not mentioned in the manifest.
|
|
|
|
pub fn cleanup_backup_dir(&self, backup_dir: &BackupDir, manifest: &BackupManifest
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(backup_dir.relative_path());
|
|
|
|
|
|
|
|
let mut wanted_files = HashSet::new();
|
|
|
|
wanted_files.insert(MANIFEST_BLOB_NAME.to_string());
|
2020-05-30 12:39:38 +00:00
|
|
|
wanted_files.insert(CLIENT_LOG_BLOB_NAME.to_string());
|
2020-01-05 14:15:12 +00:00
|
|
|
manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); });
|
|
|
|
|
|
|
|
for item in tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
|
|
|
|
if let Ok(item) = item {
|
|
|
|
if let Some(file_type) = item.file_type() {
|
|
|
|
if file_type != nix::dir::Type::File { continue; }
|
|
|
|
}
|
|
|
|
let file_name = item.file_name().to_bytes();
|
|
|
|
if file_name == b"." || file_name == b".." { continue; };
|
|
|
|
|
|
|
|
if let Ok(name) = std::str::from_utf8(file_name) {
|
|
|
|
if wanted_files.contains(name) { continue; }
|
|
|
|
}
|
|
|
|
println!("remove unused file {:?}", item.file_name());
|
|
|
|
let dirfd = item.parent_fd();
|
|
|
|
let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-17 10:24:55 +00:00
|
|
|
|
2020-01-22 14:04:08 +00:00
|
|
|
/// Returns the absolute path for a backup_group
|
|
|
|
pub fn group_path(&self, backup_group: &BackupGroup) -> PathBuf {
|
2020-01-17 10:24:55 +00:00
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(backup_group.group_path());
|
2020-01-22 14:04:08 +00:00
|
|
|
full_path
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the absolute path for backup_dir
|
|
|
|
pub fn snapshot_path(&self, backup_dir: &BackupDir) -> PathBuf {
|
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(backup_dir.relative_path());
|
|
|
|
full_path
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a complete backup group including all snapshots
|
2020-01-23 09:14:46 +00:00
|
|
|
pub fn remove_backup_group(&self, backup_group: &BackupGroup) -> Result<(), Error> {
|
2020-01-22 14:04:08 +00:00
|
|
|
|
|
|
|
let full_path = self.group_path(backup_group);
|
2020-01-17 10:24:55 +00:00
|
|
|
|
2020-08-11 08:50:38 +00:00
|
|
|
let _guard = tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?;
|
2020-07-29 12:33:11 +00:00
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
log::info!("removing backup group {:?}", full_path);
|
2020-10-14 12:16:37 +00:00
|
|
|
|
|
|
|
// remove all individual backup dirs first to ensure nothing is using them
|
|
|
|
for snap in backup_group.list_backups(&self.base_path())? {
|
|
|
|
self.remove_backup_dir(&snap.backup_dir, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no snapshots left, we can now safely remove the empty folder
|
2020-01-23 09:14:46 +00:00
|
|
|
std::fs::remove_dir_all(&full_path)
|
2020-01-23 08:58:14 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
format_err!(
|
2020-10-14 12:16:37 +00:00
|
|
|
"removing backup group directory {:?} failed - {}",
|
2020-01-23 08:58:14 +00:00
|
|
|
full_path,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
})?;
|
2020-01-17 10:24:55 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-28 11:51:27 +00:00
|
|
|
/// Remove a backup directory including all content
|
2020-07-29 12:33:11 +00:00
|
|
|
pub fn remove_backup_dir(&self, backup_dir: &BackupDir, force: bool) -> Result<(), Error> {
|
2019-02-28 11:51:27 +00:00
|
|
|
|
2020-01-22 14:04:08 +00:00
|
|
|
let full_path = self.snapshot_path(backup_dir);
|
2019-02-28 11:51:27 +00:00
|
|
|
|
2020-10-16 07:31:12 +00:00
|
|
|
let (_guard, _manifest_guard);
|
2020-07-29 12:33:11 +00:00
|
|
|
if !force {
|
2020-10-14 12:16:32 +00:00
|
|
|
_guard = lock_dir_noblock(&full_path, "snapshot", "possibly running or in use")?;
|
2020-10-16 07:31:12 +00:00
|
|
|
_manifest_guard = self.lock_manifest(backup_dir);
|
2020-07-29 12:33:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 08:58:14 +00:00
|
|
|
log::info!("removing backup snapshot {:?}", full_path);
|
2020-01-23 09:14:46 +00:00
|
|
|
std::fs::remove_dir_all(&full_path)
|
2020-01-23 08:58:14 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"removing backup snapshot {:?} failed - {}",
|
|
|
|
full_path,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
})?;
|
2019-02-28 11:51:27 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:04:08 +00:00
|
|
|
/// Returns the time of the last successful backup
|
|
|
|
///
|
|
|
|
/// Or None if there is no backup in the group (or the group dir does not exist).
|
2020-09-12 13:10:47 +00:00
|
|
|
pub fn last_successful_backup(&self, backup_group: &BackupGroup) -> Result<Option<i64>, Error> {
|
2020-01-22 14:04:08 +00:00
|
|
|
let base_path = self.base_path();
|
|
|
|
let mut group_path = base_path.clone();
|
|
|
|
group_path.push(backup_group.group_path());
|
|
|
|
|
|
|
|
if group_path.exists() {
|
|
|
|
backup_group.last_successful_backup(&base_path)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
/// Returns the backup owner.
|
|
|
|
///
|
2020-10-23 11:33:21 +00:00
|
|
|
/// The backup owner is the entity who first created the backup group.
|
|
|
|
pub fn get_owner(&self, backup_group: &BackupGroup) -> Result<Authid, Error> {
|
2020-04-28 08:11:15 +00:00
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(backup_group.group_path());
|
|
|
|
full_path.push("owner");
|
|
|
|
let owner = proxmox::tools::fs::file_read_firstline(full_path)?;
|
2020-08-06 13:46:01 +00:00
|
|
|
Ok(owner.trim_end().parse()?) // remove trailing newline
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the backup owner.
|
2020-08-06 13:46:01 +00:00
|
|
|
pub fn set_owner(
|
|
|
|
&self,
|
|
|
|
backup_group: &BackupGroup,
|
2020-10-23 11:33:21 +00:00
|
|
|
auth_id: &Authid,
|
2020-08-06 13:46:01 +00:00
|
|
|
force: bool,
|
|
|
|
) -> Result<(), Error> {
|
2020-04-28 08:11:15 +00:00
|
|
|
let mut path = self.base_path();
|
|
|
|
path.push(backup_group.group_path());
|
|
|
|
path.push("owner");
|
|
|
|
|
|
|
|
let mut open_options = std::fs::OpenOptions::new();
|
|
|
|
open_options.write(true);
|
|
|
|
open_options.truncate(true);
|
|
|
|
|
|
|
|
if force {
|
|
|
|
open_options.create(true);
|
|
|
|
} else {
|
|
|
|
open_options.create_new(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file = open_options.open(&path)
|
|
|
|
.map_err(|err| format_err!("unable to create owner file {:?} - {}", path, err))?;
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
writeln!(file, "{}", auth_id)
|
2020-04-28 08:11:15 +00:00
|
|
|
.map_err(|err| format_err!("unable to write owner file {:?} - {}", path, err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:48:33 +00:00
|
|
|
/// Create (if it does not already exists) and lock a backup group
|
2020-04-28 08:11:15 +00:00
|
|
|
///
|
|
|
|
/// And set the owner to 'userid'. If the group already exists, it returns the
|
|
|
|
/// current owner (instead of setting the owner).
|
2020-07-30 08:48:33 +00:00
|
|
|
///
|
2020-08-25 16:52:31 +00:00
|
|
|
/// This also acquires an exclusive lock on the directory and returns the lock guard.
|
2020-08-06 13:46:01 +00:00
|
|
|
pub fn create_locked_backup_group(
|
|
|
|
&self,
|
|
|
|
backup_group: &BackupGroup,
|
2020-10-23 11:33:21 +00:00
|
|
|
auth_id: &Authid,
|
|
|
|
) -> Result<(Authid, DirLockGuard), Error> {
|
2019-02-27 09:02:22 +00:00
|
|
|
// create intermediate path first:
|
2020-04-28 08:11:15 +00:00
|
|
|
let base_path = self.base_path();
|
|
|
|
|
|
|
|
let mut full_path = base_path.clone();
|
|
|
|
full_path.push(backup_group.backup_type());
|
2019-02-27 09:02:22 +00:00
|
|
|
std::fs::create_dir_all(&full_path)?;
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
full_path.push(backup_group.backup_id());
|
|
|
|
|
|
|
|
// create the last component now
|
|
|
|
match std::fs::create_dir(&full_path) {
|
|
|
|
Ok(_) => {
|
2020-08-11 08:50:37 +00:00
|
|
|
let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?;
|
2020-10-23 11:33:21 +00:00
|
|
|
self.set_owner(backup_group, auth_id, false)?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let owner = self.get_owner(backup_group)?; // just to be sure
|
2020-07-30 08:48:33 +00:00
|
|
|
Ok((owner, guard))
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
|
2020-08-11 08:50:37 +00:00
|
|
|
let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let owner = self.get_owner(backup_group)?; // just to be sure
|
2020-07-30 08:48:33 +00:00
|
|
|
Ok((owner, guard))
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
Err(err) => bail!("unable to create backup group {:?} - {}", full_path, err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new backup snapshot inside a BackupGroup
|
|
|
|
///
|
|
|
|
/// The BackupGroup directory needs to exist.
|
2020-08-11 08:50:39 +00:00
|
|
|
pub fn create_locked_backup_dir(&self, backup_dir: &BackupDir)
|
|
|
|
-> Result<(PathBuf, bool, DirLockGuard), Error>
|
|
|
|
{
|
2019-03-05 06:18:12 +00:00
|
|
|
let relative_path = backup_dir.relative_path();
|
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(&relative_path);
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2020-08-11 08:50:39 +00:00
|
|
|
let lock = ||
|
|
|
|
lock_dir_noblock(&full_path, "snapshot", "internal error - tried creating snapshot that's already in use");
|
|
|
|
|
2019-02-27 09:02:22 +00:00
|
|
|
match std::fs::create_dir(&full_path) {
|
2020-08-11 08:50:39 +00:00
|
|
|
Ok(_) => Ok((relative_path, true, lock()?)),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false, lock()?)),
|
|
|
|
Err(e) => Err(e.into())
|
2019-02-27 09:02:22 +00:00
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
|
2019-01-18 11:01:37 +00:00
|
|
|
let base = self.base_path();
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
let mut list = vec![];
|
|
|
|
|
2019-01-18 11:24:58 +00:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
let walker = WalkDir::new(&base).same_file_system(true).into_iter();
|
|
|
|
|
|
|
|
// make sure we skip .chunks (and other hidden files to keep it simple)
|
|
|
|
fn is_hidden(entry: &walkdir::DirEntry) -> bool {
|
|
|
|
entry.file_name()
|
|
|
|
.to_str()
|
|
|
|
.map(|s| s.starts_with("."))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2020-07-22 14:01:50 +00:00
|
|
|
let handle_entry_err = |err: walkdir::Error| {
|
|
|
|
if let Some(inner) = err.io_error() {
|
|
|
|
let path = err.path().unwrap_or(Path::new(""));
|
|
|
|
match inner.kind() {
|
|
|
|
io::ErrorKind::PermissionDenied => {
|
|
|
|
// only allow to skip ext4 fsck directory, avoid GC if, for example,
|
|
|
|
// a user got file permissions wrong on datastore rsync to new server
|
|
|
|
if err.depth() > 1 || !path.ends_with("lost+found") {
|
|
|
|
bail!("cannot continue garbage-collection safely, permission denied on: {}", path.display())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => bail!("unexpected error on datastore traversal: {} - {}", inner, path.display()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
};
|
2019-01-18 11:24:58 +00:00
|
|
|
for entry in walker.filter_entry(|e| !is_hidden(e)) {
|
2020-07-22 14:01:50 +00:00
|
|
|
let path = match entry {
|
|
|
|
Ok(entry) => entry.into_path(),
|
|
|
|
Err(err) => {
|
|
|
|
handle_entry_err(err)?;
|
|
|
|
continue
|
|
|
|
},
|
|
|
|
};
|
2019-12-31 14:23:41 +00:00
|
|
|
if let Ok(archive_type) = archive_type(&path) {
|
|
|
|
if archive_type == ArchiveType::FixedIndex || archive_type == ArchiveType::DynamicIndex {
|
2019-01-18 11:24:58 +00:00
|
|
|
list.push(path);
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2019-07-04 05:57:43 +00:00
|
|
|
// mark chunks used by ``index`` as used
|
|
|
|
fn index_mark_used_chunks<I: IndexFile>(
|
|
|
|
&self,
|
|
|
|
index: I,
|
|
|
|
file_name: &Path, // only used for error reporting
|
|
|
|
status: &mut GarbageCollectionStatus,
|
2020-10-12 09:46:34 +00:00
|
|
|
worker: &dyn TaskState,
|
2019-07-04 05:57:43 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
status.index_file_count += 1;
|
|
|
|
status.index_data_bytes += index.index_bytes();
|
|
|
|
|
|
|
|
for pos in 0..index.index_count() {
|
2020-10-12 09:46:34 +00:00
|
|
|
worker.check_abort()?;
|
2019-07-04 05:57:43 +00:00
|
|
|
tools::fail_on_shutdown()?;
|
|
|
|
let digest = index.index_digest(pos).unwrap();
|
|
|
|
if let Err(err) = self.chunk_store.touch_chunk(digest) {
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_warn!(
|
|
|
|
worker,
|
|
|
|
"warning: unable to access chunk {}, required by {:?} - {}",
|
|
|
|
proxmox::tools::digest_to_hex(digest),
|
|
|
|
file_name,
|
|
|
|
err,
|
|
|
|
);
|
2020-11-12 14:50:08 +00:00
|
|
|
|
|
|
|
// touch any corresponding .bad files to keep them around, meaning if a chunk is
|
|
|
|
// rewritten correctly they will be removed automatically, as well as if no index
|
|
|
|
// file requires the chunk anymore (won't get to this loop then)
|
|
|
|
for i in 0..=9 {
|
|
|
|
let bad_ext = format!("{}.bad", i);
|
|
|
|
let mut bad_path = PathBuf::new();
|
|
|
|
bad_path.push(self.chunk_path(digest).0);
|
|
|
|
bad_path.set_extension(bad_ext);
|
|
|
|
self.chunk_store.cond_touch_path(&bad_path, false)?;
|
|
|
|
}
|
2019-07-04 05:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
fn mark_used_chunks(
|
|
|
|
&self,
|
|
|
|
status: &mut GarbageCollectionStatus,
|
|
|
|
worker: &dyn TaskState,
|
|
|
|
) -> Result<(), Error> {
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
let image_list = self.list_images()?;
|
2020-09-02 08:03:53 +00:00
|
|
|
let image_count = image_list.len();
|
|
|
|
|
|
|
|
let mut done = 0;
|
|
|
|
let mut last_percentage: usize = 0;
|
|
|
|
|
2020-11-30 15:22:18 +00:00
|
|
|
let mut strange_paths_count: u64 = 0;
|
|
|
|
|
2020-11-02 19:50:24 +00:00
|
|
|
for img in image_list {
|
2019-04-01 10:13:02 +00:00
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
worker.check_abort()?;
|
2019-04-01 10:13:02 +00:00
|
|
|
tools::fail_on_shutdown()?;
|
|
|
|
|
2020-11-30 15:22:18 +00:00
|
|
|
if let Some(backup_dir_path) = img.parent() {
|
|
|
|
let backup_dir_path = backup_dir_path.strip_prefix(self.base_path())?;
|
|
|
|
if let Some(backup_dir_str) = backup_dir_path.to_str() {
|
|
|
|
if BackupDir::from_str(backup_dir_str).is_err() {
|
|
|
|
strange_paths_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:50:24 +00:00
|
|
|
let path = self.chunk_store.relative_path(&img);
|
2020-11-30 15:22:18 +00:00
|
|
|
|
2020-11-02 19:50:24 +00:00
|
|
|
match std::fs::File::open(&path) {
|
2020-10-16 06:01:38 +00:00
|
|
|
Ok(file) => {
|
2020-11-02 19:50:24 +00:00
|
|
|
if let Ok(archive_type) = archive_type(&img) {
|
2020-10-16 06:01:38 +00:00
|
|
|
if archive_type == ArchiveType::FixedIndex {
|
2020-11-02 19:50:24 +00:00
|
|
|
let index = FixedIndexReader::new(file).map_err(|e| {
|
|
|
|
format_err!("can't read index '{}' - {}", path.to_string_lossy(), e)
|
2020-11-02 11:34:35 +00:00
|
|
|
})?;
|
2020-11-02 19:50:24 +00:00
|
|
|
self.index_mark_used_chunks(index, &img, status, worker)?;
|
2020-10-16 06:01:38 +00:00
|
|
|
} else if archive_type == ArchiveType::DynamicIndex {
|
2020-11-02 19:50:24 +00:00
|
|
|
let index = DynamicIndexReader::new(file).map_err(|e| {
|
|
|
|
format_err!("can't read index '{}' - {}", path.to_string_lossy(), e)
|
2020-11-02 11:34:35 +00:00
|
|
|
})?;
|
2020-11-02 19:50:24 +00:00
|
|
|
self.index_mark_used_chunks(index, &img, status, worker)?;
|
2020-10-16 06:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 19:50:24 +00:00
|
|
|
Err(err) if err.kind() == io::ErrorKind::NotFound => (), // ignore vanished files
|
|
|
|
Err(err) => bail!("can't open index {} - {}", path.to_string_lossy(), err),
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
2020-09-02 08:03:53 +00:00
|
|
|
done += 1;
|
|
|
|
|
|
|
|
let percentage = done*100/image_count;
|
|
|
|
if percentage > last_percentage {
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
2020-11-30 15:22:17 +00:00
|
|
|
"marked {}% ({} of {} index files)",
|
2020-10-12 09:46:34 +00:00
|
|
|
percentage,
|
|
|
|
done,
|
|
|
|
image_count,
|
|
|
|
);
|
2020-09-02 08:03:53 +00:00
|
|
|
last_percentage = percentage;
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 15:22:18 +00:00
|
|
|
if strange_paths_count > 0 {
|
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
|
|
|
"found (and marked) {} index files outside of expected directory scheme",
|
|
|
|
strange_paths_count,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
Ok(())
|
2019-04-11 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_gc_status(&self) -> GarbageCollectionStatus {
|
|
|
|
self.last_gc_status.lock().unwrap().clone()
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2020-05-20 06:59:45 +00:00
|
|
|
pub fn garbage_collection_running(&self) -> bool {
|
|
|
|
if let Ok(_) = self.gc_mutex.try_lock() { false } else { true }
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
pub fn garbage_collection(&self, worker: &dyn TaskState, upid: &UPID) -> Result<(), Error> {
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2018-12-25 12:29:27 +00:00
|
|
|
if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() {
|
2018-12-22 13:04:05 +00:00
|
|
|
|
2020-10-01 10:38:04 +00:00
|
|
|
// avoids that we run GC if an old daemon process has still a
|
|
|
|
// running backup writer, which is not save as we have no "oldest
|
|
|
|
// writer" information and thus no safe atime cutoff
|
2019-03-22 08:42:15 +00:00
|
|
|
let _exclusive_lock = self.chunk_store.try_exclusive_lock()?;
|
|
|
|
|
2020-10-01 10:38:38 +00:00
|
|
|
let phase1_start_time = proxmox::tools::time::epoch_i64();
|
2020-08-27 13:55:57 +00:00
|
|
|
let oldest_writer = self.chunk_store.oldest_writer().unwrap_or(phase1_start_time);
|
2019-03-31 15:21:36 +00:00
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
let mut gc_status = GarbageCollectionStatus::default();
|
2020-10-12 09:46:34 +00:00
|
|
|
gc_status.upid = Some(upid.to_string());
|
|
|
|
|
|
|
|
crate::task_log!(worker, "Start GC phase1 (mark used chunks)");
|
|
|
|
|
|
|
|
self.mark_used_chunks(&mut gc_status, worker)?;
|
|
|
|
|
|
|
|
crate::task_log!(worker, "Start GC phase2 (sweep unused chunks)");
|
|
|
|
self.chunk_store.sweep_unused_chunks(
|
|
|
|
oldest_writer,
|
|
|
|
phase1_start_time,
|
|
|
|
&mut gc_status,
|
|
|
|
worker,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
|
|
|
"Removed garbage: {}",
|
|
|
|
HumanByte::from(gc_status.removed_bytes),
|
|
|
|
);
|
|
|
|
crate::task_log!(worker, "Removed chunks: {}", gc_status.removed_chunks);
|
2020-04-06 07:50:40 +00:00
|
|
|
if gc_status.pending_bytes > 0 {
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
|
|
|
"Pending removals: {} (in {} chunks)",
|
|
|
|
HumanByte::from(gc_status.pending_bytes),
|
|
|
|
gc_status.pending_chunks,
|
|
|
|
);
|
2020-04-06 07:50:40 +00:00
|
|
|
}
|
2020-09-07 15:30:34 +00:00
|
|
|
if gc_status.removed_bad > 0 {
|
2020-10-29 13:45:32 +00:00
|
|
|
crate::task_log!(worker, "Removed bad chunks: {}", gc_status.removed_bad);
|
2020-09-07 15:30:34 +00:00
|
|
|
}
|
2020-04-06 07:50:40 +00:00
|
|
|
|
2020-10-29 09:24:31 +00:00
|
|
|
if gc_status.still_bad > 0 {
|
2020-10-29 13:45:32 +00:00
|
|
|
crate::task_log!(worker, "Leftover bad chunks: {}", gc_status.still_bad);
|
2020-10-29 09:24:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
|
|
|
"Original data usage: {}",
|
|
|
|
HumanByte::from(gc_status.index_data_bytes),
|
|
|
|
);
|
2019-12-19 06:09:39 +00:00
|
|
|
|
|
|
|
if gc_status.index_data_bytes > 0 {
|
2020-08-27 13:55:57 +00:00
|
|
|
let comp_per = (gc_status.disk_bytes as f64 * 100.)/gc_status.index_data_bytes as f64;
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(
|
|
|
|
worker,
|
|
|
|
"On-Disk usage: {} ({:.2}%)",
|
|
|
|
HumanByte::from(gc_status.disk_bytes),
|
|
|
|
comp_per,
|
|
|
|
);
|
2019-12-19 06:09:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(worker, "On-Disk chunks: {}", gc_status.disk_chunks);
|
2019-12-19 06:09:39 +00:00
|
|
|
|
2020-10-29 09:37:43 +00:00
|
|
|
let deduplication_factor = if gc_status.disk_bytes > 0 {
|
|
|
|
(gc_status.index_data_bytes as f64)/(gc_status.disk_bytes as f64)
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
|
|
|
|
crate::task_log!(worker, "Deduplication factor: {:.2}", deduplication_factor);
|
|
|
|
|
2019-12-19 06:09:39 +00:00
|
|
|
if gc_status.disk_chunks > 0 {
|
|
|
|
let avg_chunk = gc_status.disk_bytes/(gc_status.disk_chunks as u64);
|
2020-10-12 09:46:34 +00:00
|
|
|
crate::task_log!(worker, "Average chunk size: {}", HumanByte::from(avg_chunk));
|
2019-12-19 06:09:39 +00:00
|
|
|
}
|
2018-12-22 15:58:16 +00:00
|
|
|
|
2020-10-23 14:32:32 +00:00
|
|
|
if let Ok(serialized) = serde_json::to_string(&gc_status) {
|
|
|
|
let mut path = self.base_path();
|
|
|
|
path.push(".gc-status");
|
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = backup, group(r)= backup
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(backup_user.uid)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
|
|
|
// ignore errors
|
|
|
|
let _ = replace_file(path, serialized.as_bytes(), options);
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:25 +00:00
|
|
|
*self.last_gc_status.lock().unwrap() = gc_status;
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
} else {
|
2019-04-06 15:57:38 +00:00
|
|
|
bail!("Start GC failed - (already running/locked)");
|
2018-12-22 15:58:16 +00:00
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-06 09:19:07 +00:00
|
|
|
|
2020-01-02 10:00:33 +00:00
|
|
|
pub fn try_shared_chunk_store_lock(&self) -> Result<tools::ProcessLockSharedGuard, Error> {
|
|
|
|
self.chunk_store.try_shared_lock()
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
|
|
|
|
self.chunk_store.chunk_path(digest)
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:26:28 +00:00
|
|
|
pub fn cond_touch_chunk(&self, digest: &[u8; 32], fail_if_not_exist: bool) -> Result<bool, Error> {
|
|
|
|
self.chunk_store.cond_touch_chunk(digest, fail_if_not_exist)
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
pub fn insert_chunk(
|
2019-03-06 09:19:07 +00:00
|
|
|
&self,
|
2019-10-06 08:31:06 +00:00
|
|
|
chunk: &DataBlob,
|
|
|
|
digest: &[u8; 32],
|
2019-03-06 09:19:07 +00:00
|
|
|
) -> Result<(bool, u64), Error> {
|
2019-10-06 08:31:06 +00:00
|
|
|
self.chunk_store.insert_chunk(chunk, digest)
|
2019-03-06 09:19:07 +00:00
|
|
|
}
|
2020-06-24 04:58:14 +00:00
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
pub fn load_blob(&self, backup_dir: &BackupDir, filename: &str) -> Result<DataBlob, Error> {
|
2020-06-24 04:58:14 +00:00
|
|
|
let mut path = self.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(filename);
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
proxmox::try_block!({
|
|
|
|
let mut file = std::fs::File::open(&path)?;
|
|
|
|
DataBlob::load_from_reader(&mut file)
|
|
|
|
}).map_err(|err| format_err!("unable to load blob '{:?}' - {}", path, err))
|
|
|
|
}
|
2020-07-31 05:19:14 +00:00
|
|
|
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
pub fn load_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
|
|
|
|
|
|
|
let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest);
|
|
|
|
|
|
|
|
proxmox::try_block!({
|
|
|
|
let mut file = std::fs::File::open(&chunk_path)?;
|
|
|
|
DataBlob::load_from_reader(&mut file)
|
|
|
|
}).map_err(|err| format_err!(
|
|
|
|
"store '{}', unable to load chunk '{}' - {}",
|
|
|
|
self.name(),
|
|
|
|
digest_str,
|
|
|
|
err,
|
|
|
|
))
|
2020-10-16 07:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lock_manifest(
|
|
|
|
&self,
|
|
|
|
backup_dir: &BackupDir,
|
|
|
|
) -> Result<File, Error> {
|
|
|
|
let mut path = self.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(&MANIFEST_LOCK_NAME);
|
|
|
|
|
|
|
|
// update_manifest should never take a long time, so if someone else has
|
|
|
|
// the lock we can simply block a bit and should get it soon
|
|
|
|
open_file_locked(&path, Duration::from_secs(5), true)
|
|
|
|
.map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"unable to acquire manifest lock {:?} - {}", &path, err
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2020-07-31 05:19:14 +00:00
|
|
|
|
2020-10-16 07:31:12 +00:00
|
|
|
/// Load the manifest without a lock. Must not be written back.
|
2020-07-08 07:19:24 +00:00
|
|
|
pub fn load_manifest(
|
|
|
|
&self,
|
|
|
|
backup_dir: &BackupDir,
|
2020-07-31 08:25:30 +00:00
|
|
|
) -> Result<(BackupManifest, u64), Error> {
|
2020-07-28 08:23:16 +00:00
|
|
|
let blob = self.load_blob(backup_dir, MANIFEST_BLOB_NAME)?;
|
|
|
|
let raw_size = blob.raw_size();
|
2020-06-24 04:58:14 +00:00
|
|
|
let manifest = BackupManifest::try_from(blob)?;
|
2020-07-31 08:25:30 +00:00
|
|
|
Ok((manifest, raw_size))
|
2020-06-24 04:58:14 +00:00
|
|
|
}
|
2020-07-31 05:19:14 +00:00
|
|
|
|
2020-10-16 07:31:12 +00:00
|
|
|
/// Update the manifest of the specified snapshot. Never write a manifest directly,
|
|
|
|
/// only use this method - anything else may break locking guarantees.
|
|
|
|
pub fn update_manifest(
|
2020-07-31 05:19:14 +00:00
|
|
|
&self,
|
|
|
|
backup_dir: &BackupDir,
|
2020-10-16 07:31:12 +00:00
|
|
|
update_fn: impl FnOnce(&mut BackupManifest),
|
2020-07-31 05:19:14 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-10-16 07:31:12 +00:00
|
|
|
|
|
|
|
let _guard = self.lock_manifest(backup_dir)?;
|
|
|
|
let (mut manifest, _) = self.load_manifest(&backup_dir)?;
|
|
|
|
|
|
|
|
update_fn(&mut manifest);
|
|
|
|
|
2020-10-14 12:16:35 +00:00
|
|
|
let manifest = serde_json::to_value(manifest)?;
|
2020-07-31 05:19:14 +00:00
|
|
|
let manifest = serde_json::to_string_pretty(&manifest)?;
|
|
|
|
let blob = DataBlob::encode(manifest.as_bytes(), None, true)?;
|
|
|
|
let raw_data = blob.raw_data();
|
|
|
|
|
|
|
|
let mut path = self.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(MANIFEST_BLOB_NAME);
|
|
|
|
|
2020-10-16 07:31:12 +00:00
|
|
|
// atomic replace invalidates flock - no other writes past this point!
|
2020-07-31 05:19:14 +00:00
|
|
|
replace_file(&path, raw_data, CreateOptions::new())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-20 08:08:25 +00:00
|
|
|
|
|
|
|
pub fn verify_new(&self) -> bool {
|
|
|
|
self.verify_new
|
|
|
|
}
|
2018-12-17 12:00:39 +00:00
|
|
|
}
|