2021-04-14 10:53:19 +00:00
|
|
|
use nix::dir::Dir;
|
2020-07-29 11:29:13 +00:00
|
|
|
use std::collections::HashSet;
|
2021-04-14 10:53:19 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2020-09-01 09:17:13 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::time::Instant;
|
2020-07-29 11:29:13 +00:00
|
|
|
|
2020-08-25 15:30:27 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2020-06-24 11:11:45 +00:00
|
|
|
|
2020-09-26 08:23:44 +00:00
|
|
|
use crate::{
|
|
|
|
api2::types::*,
|
|
|
|
backup::{
|
|
|
|
DataStore,
|
2020-11-30 15:27:21 +00:00
|
|
|
StoreProgress,
|
2020-09-26 08:23:44 +00:00
|
|
|
DataBlob,
|
|
|
|
BackupGroup,
|
|
|
|
BackupDir,
|
|
|
|
BackupInfo,
|
2020-10-29 06:59:19 +00:00
|
|
|
BackupManifest,
|
2020-09-26 08:23:44 +00:00
|
|
|
IndexFile,
|
|
|
|
CryptMode,
|
|
|
|
FileInfo,
|
|
|
|
ArchiveType,
|
|
|
|
archive_type,
|
|
|
|
},
|
2020-10-12 09:46:34 +00:00
|
|
|
server::UPID,
|
|
|
|
task::TaskState,
|
|
|
|
task_log,
|
2020-10-14 12:16:33 +00:00
|
|
|
tools::fs::lock_dir_noblock_shared,
|
2021-04-14 10:53:19 +00:00
|
|
|
tools::ParallelHandler,
|
2020-06-24 11:11:45 +00:00
|
|
|
};
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
/// A VerifyWorker encapsulates a task worker, datastore and information about which chunks have
|
|
|
|
/// already been verified or detected as corrupt.
|
|
|
|
pub struct VerifyWorker {
|
|
|
|
worker: Arc<dyn TaskState + Send + Sync>,
|
|
|
|
datastore: Arc<DataStore>,
|
2021-04-14 10:53:19 +00:00
|
|
|
verified_chunks: Arc<Mutex<HashSet<[u8; 32]>>>,
|
|
|
|
corrupt_chunks: Arc<Mutex<HashSet<[u8; 32]>>>,
|
2021-01-25 13:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VerifyWorker {
|
|
|
|
/// Creates a new VerifyWorker for a given task worker and datastore.
|
|
|
|
pub fn new(worker: Arc<dyn TaskState + Send + Sync>, datastore: Arc<DataStore>) -> Self {
|
|
|
|
Self {
|
|
|
|
worker,
|
|
|
|
datastore,
|
|
|
|
// start with 16k chunks == up to 64G data
|
2021-04-14 10:53:19 +00:00
|
|
|
verified_chunks: Arc::new(Mutex::new(HashSet::with_capacity(16 * 1024))),
|
2021-01-25 13:42:58 +00:00
|
|
|
// start with 64 chunks since we assume there are few corrupt ones
|
|
|
|
corrupt_chunks: Arc::new(Mutex::new(HashSet::with_capacity(64))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
fn verify_blob(
|
|
|
|
datastore: Arc<DataStore>,
|
|
|
|
backup_dir: &BackupDir,
|
|
|
|
info: &FileInfo,
|
|
|
|
) -> Result<(), Error> {
|
2020-07-28 08:23:16 +00:00
|
|
|
let blob = datastore.load_blob(backup_dir, &info.filename)?;
|
2020-06-24 11:11:45 +00:00
|
|
|
|
2020-07-29 11:29:13 +00:00
|
|
|
let raw_size = blob.raw_size();
|
2020-06-24 11:11:45 +00:00
|
|
|
if raw_size != info.size {
|
|
|
|
bail!("wrong size ({} != {})", info.size, raw_size);
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
let csum = openssl::sha::sha256(blob.raw_data());
|
2020-06-24 11:11:45 +00:00
|
|
|
if csum != info.csum {
|
|
|
|
bail!("wrong index checksum");
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:10:43 +00:00
|
|
|
match blob.crypt_mode()? {
|
|
|
|
CryptMode::Encrypt => Ok(()),
|
|
|
|
CryptMode::None => {
|
|
|
|
// digest already verified above
|
|
|
|
blob.decode(None, None)?;
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
CryptMode::SignOnly => bail!("Invalid CryptMode for blob"),
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:30:33 +00:00
|
|
|
fn rename_corrupted_chunk(
|
|
|
|
datastore: Arc<DataStore>,
|
|
|
|
digest: &[u8;32],
|
2020-10-12 09:46:34 +00:00
|
|
|
worker: &dyn TaskState,
|
2020-09-07 15:30:33 +00:00
|
|
|
) {
|
|
|
|
let (path, digest_str) = datastore.chunk_path(digest);
|
|
|
|
|
|
|
|
let mut counter = 0;
|
|
|
|
let mut new_path = path.clone();
|
2020-09-08 10:29:53 +00:00
|
|
|
loop {
|
2020-09-07 15:30:33 +00:00
|
|
|
new_path.set_file_name(format!("{}.{}.bad", digest_str, counter));
|
2021-04-14 10:53:19 +00:00
|
|
|
if new_path.exists() && counter < 9 {
|
|
|
|
counter += 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-09-07 15:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match std::fs::rename(&path, &new_path) {
|
|
|
|
Ok(_) => {
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(worker, "corrupted chunk renamed to {:?}", &new_path);
|
2020-09-07 15:30:33 +00:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
match err.kind() {
|
|
|
|
std::io::ErrorKind::NotFound => { /* ignored */ },
|
2020-10-12 09:46:34 +00:00
|
|
|
_ => task_log!(worker, "could not rename corrupted chunk {:?} - {}", &path, err)
|
2020-09-07 15:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:14:45 +00:00
|
|
|
fn verify_index_chunks(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-09-01 09:17:13 +00:00
|
|
|
index: Box<dyn IndexFile + Send>,
|
2020-08-10 11:25:08 +00:00
|
|
|
crypt_mode: CryptMode,
|
2020-06-26 06:14:45 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-10-01 12:48:49 +00:00
|
|
|
let errors = Arc::new(AtomicUsize::new(0));
|
2020-06-26 06:14:45 +00:00
|
|
|
|
2020-09-01 09:17:13 +00:00
|
|
|
let start_time = Instant::now();
|
2020-06-26 06:14:45 +00:00
|
|
|
|
2020-09-01 09:17:13 +00:00
|
|
|
let mut read_bytes = 0;
|
|
|
|
let mut decoded_bytes = 0;
|
2020-08-25 06:52:24 +00:00
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
let worker2 = Arc::clone(&verify_worker.worker);
|
|
|
|
let datastore2 = Arc::clone(&verify_worker.datastore);
|
|
|
|
let corrupt_chunks2 = Arc::clone(&verify_worker.corrupt_chunks);
|
|
|
|
let verified_chunks2 = Arc::clone(&verify_worker.verified_chunks);
|
2020-10-01 12:48:49 +00:00
|
|
|
let errors2 = Arc::clone(&errors);
|
2020-09-26 09:14:37 +00:00
|
|
|
|
|
|
|
let decoder_pool = ParallelHandler::new(
|
2021-04-14 10:53:19 +00:00
|
|
|
"verify chunk decoder",
|
|
|
|
4,
|
|
|
|
move |(chunk, digest, size): (DataBlob, [u8; 32], u64)| {
|
2020-09-26 09:14:37 +00:00
|
|
|
let chunk_crypt_mode = match chunk.crypt_mode() {
|
|
|
|
Err(err) => {
|
|
|
|
corrupt_chunks2.lock().unwrap().insert(digest);
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(worker2, "can't verify chunk, unknown CryptMode - {}", err);
|
2020-09-26 09:14:37 +00:00
|
|
|
errors2.fetch_add(1, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
},
|
|
|
|
Ok(mode) => mode,
|
|
|
|
};
|
|
|
|
|
|
|
|
if chunk_crypt_mode != crypt_mode {
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(
|
|
|
|
worker2,
|
2020-09-26 09:14:37 +00:00
|
|
|
"chunk CryptMode {:?} does not match index CryptMode {:?}",
|
|
|
|
chunk_crypt_mode,
|
|
|
|
crypt_mode
|
2020-10-12 09:46:34 +00:00
|
|
|
);
|
2020-09-26 09:14:37 +00:00
|
|
|
errors2.fetch_add(1, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = chunk.verify_unencrypted(size as usize, &digest) {
|
|
|
|
corrupt_chunks2.lock().unwrap().insert(digest);
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(worker2, "{}", err);
|
2020-09-26 09:14:37 +00:00
|
|
|
errors2.fetch_add(1, Ordering::SeqCst);
|
2020-10-12 09:46:34 +00:00
|
|
|
rename_corrupted_chunk(datastore2.clone(), &digest, &worker2);
|
2020-09-26 09:14:37 +00:00
|
|
|
} else {
|
|
|
|
verified_chunks2.lock().unwrap().insert(digest);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-04-13 14:35:36 +00:00
|
|
|
let index_count = index.index_count();
|
|
|
|
let mut chunk_list = Vec::with_capacity(index_count);
|
2020-07-29 11:29:13 +00:00
|
|
|
|
2021-04-13 14:35:36 +00:00
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
|
|
|
for pos in 0..index_count {
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker.check_abort()?;
|
2020-09-02 07:50:17 +00:00
|
|
|
crate::tools::fail_on_shutdown()?;
|
2020-09-01 09:17:13 +00:00
|
|
|
|
2020-09-26 09:14:37 +00:00
|
|
|
let info = index.chunk_info(pos).unwrap();
|
2020-08-10 11:25:08 +00:00
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
if verify_worker.verified_chunks.lock().unwrap().contains(&info.digest) {
|
2020-09-26 09:14:37 +00:00
|
|
|
continue; // already verified
|
|
|
|
}
|
2020-09-01 09:17:13 +00:00
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
if verify_worker.corrupt_chunks.lock().unwrap().contains(&info.digest) {
|
2020-09-26 09:14:37 +00:00
|
|
|
let digest_str = proxmox::tools::digest_to_hex(&info.digest);
|
2021-01-25 13:42:58 +00:00
|
|
|
task_log!(verify_worker.worker, "chunk {} was marked as corrupt", digest_str);
|
2020-09-01 09:17:13 +00:00
|
|
|
errors.fetch_add(1, Ordering::SeqCst);
|
2020-09-26 09:14:37 +00:00
|
|
|
continue;
|
2020-08-10 11:25:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 14:35:36 +00:00
|
|
|
match verify_worker.datastore.stat_chunk(&info.digest) {
|
|
|
|
Err(err) => {
|
|
|
|
verify_worker.corrupt_chunks.lock().unwrap().insert(info.digest);
|
|
|
|
task_log!(verify_worker.worker, "can't verify chunk, stat failed - {}", err);
|
|
|
|
errors.fetch_add(1, Ordering::SeqCst);
|
2021-04-14 10:53:19 +00:00
|
|
|
rename_corrupted_chunk(
|
|
|
|
verify_worker.datastore.clone(),
|
|
|
|
&info.digest,
|
|
|
|
&verify_worker.worker,
|
|
|
|
);
|
2021-04-13 14:35:36 +00:00
|
|
|
}
|
|
|
|
Ok(metadata) => {
|
|
|
|
chunk_list.push((pos, metadata.ino()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:55:49 +00:00
|
|
|
// sorting by inode improves data locality, which makes it lots faster on spinners
|
2021-04-14 10:53:19 +00:00
|
|
|
chunk_list.sort_unstable_by(|(_, ino_a), (_, ino_b)| ino_a.cmp(&ino_b));
|
2021-04-13 14:35:36 +00:00
|
|
|
|
|
|
|
for (pos, _) in chunk_list {
|
|
|
|
verify_worker.worker.check_abort()?;
|
|
|
|
crate::tools::fail_on_shutdown()?;
|
|
|
|
|
|
|
|
let info = index.chunk_info(pos).unwrap();
|
|
|
|
|
2021-04-15 08:00:04 +00:00
|
|
|
// we must always recheck this here, the parallel worker below alter it!
|
|
|
|
// Else we miss skipping repeated chunks from the same index, and re-verify them all
|
|
|
|
if verify_worker.verified_chunks.lock().unwrap().contains(&info.digest) {
|
|
|
|
continue; // already verified
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
match verify_worker.datastore.load_chunk(&info.digest) {
|
2020-09-26 09:14:37 +00:00
|
|
|
Err(err) => {
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.corrupt_chunks.lock().unwrap().insert(info.digest);
|
|
|
|
task_log!(verify_worker.worker, "can't verify chunk, load failed - {}", err);
|
2020-09-26 09:14:37 +00:00
|
|
|
errors.fetch_add(1, Ordering::SeqCst);
|
2021-04-14 10:53:19 +00:00
|
|
|
rename_corrupted_chunk(
|
|
|
|
verify_worker.datastore.clone(),
|
|
|
|
&info.digest,
|
|
|
|
&verify_worker.worker,
|
|
|
|
);
|
2020-09-26 09:14:37 +00:00
|
|
|
}
|
|
|
|
Ok(chunk) => {
|
2021-04-13 14:35:36 +00:00
|
|
|
let size = info.size();
|
2020-09-26 09:14:37 +00:00
|
|
|
read_bytes += chunk.raw_size();
|
|
|
|
decoder_pool.send((chunk, info.digest, size))?;
|
|
|
|
decoded_bytes += size;
|
|
|
|
}
|
2020-07-29 11:29:13 +00:00
|
|
|
}
|
2020-06-26 06:14:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 09:14:37 +00:00
|
|
|
decoder_pool.complete()?;
|
|
|
|
|
2020-09-01 09:17:13 +00:00
|
|
|
let elapsed = start_time.elapsed().as_secs_f64();
|
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
let read_bytes_mib = (read_bytes as f64) / (1024.0 * 1024.0);
|
|
|
|
let decoded_bytes_mib = (decoded_bytes as f64) / (1024.0 * 1024.0);
|
2020-09-01 09:17:13 +00:00
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
let read_speed = read_bytes_mib / elapsed;
|
|
|
|
let decode_speed = decoded_bytes_mib / elapsed;
|
2020-09-01 09:17:13 +00:00
|
|
|
|
|
|
|
let error_count = errors.load(Ordering::SeqCst);
|
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-12 09:46:34 +00:00
|
|
|
" verified {:.2}/{:.2} MiB in {:.2} seconds, speed {:.2}/{:.2} MiB/s ({} errors)",
|
|
|
|
read_bytes_mib,
|
|
|
|
decoded_bytes_mib,
|
|
|
|
elapsed,
|
|
|
|
read_speed,
|
|
|
|
decode_speed,
|
|
|
|
error_count,
|
|
|
|
);
|
2020-09-01 09:17:13 +00:00
|
|
|
|
|
|
|
if errors.load(Ordering::SeqCst) > 0 {
|
2020-07-30 07:09:03 +00:00
|
|
|
bail!("chunks could not be verified");
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:14:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:29:13 +00:00
|
|
|
fn verify_fixed_index(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-07-29 11:29:13 +00:00
|
|
|
backup_dir: &BackupDir,
|
|
|
|
info: &FileInfo,
|
|
|
|
) -> Result<(), Error> {
|
2020-06-24 11:11:45 +00:00
|
|
|
let mut path = backup_dir.relative_path();
|
|
|
|
path.push(&info.filename);
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
let index = verify_worker.datastore.open_fixed_reader(&path)?;
|
2020-06-24 11:11:45 +00:00
|
|
|
|
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
if size != info.size {
|
|
|
|
bail!("wrong size ({} != {})", info.size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if csum != info.csum {
|
|
|
|
bail!("wrong index checksum");
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
verify_index_chunks(verify_worker, Box::new(index), info.chunk_crypt_mode())
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:29:13 +00:00
|
|
|
fn verify_dynamic_index(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-07-29 11:29:13 +00:00
|
|
|
backup_dir: &BackupDir,
|
|
|
|
info: &FileInfo,
|
|
|
|
) -> Result<(), Error> {
|
2020-06-24 11:11:45 +00:00
|
|
|
let mut path = backup_dir.relative_path();
|
|
|
|
path.push(&info.filename);
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
let index = verify_worker.datastore.open_dynamic_reader(&path)?;
|
2020-06-24 11:11:45 +00:00
|
|
|
|
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
if size != info.size {
|
|
|
|
bail!("wrong size ({} != {})", info.size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if csum != info.csum {
|
|
|
|
bail!("wrong index checksum");
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
verify_index_chunks(verify_worker, Box::new(index), info.chunk_crypt_mode())
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify a single backup snapshot
|
|
|
|
///
|
|
|
|
/// This checks all archives inside a backup snapshot.
|
|
|
|
/// Errors are logged to the worker log.
|
|
|
|
///
|
2020-06-25 10:55:34 +00:00
|
|
|
/// Returns
|
|
|
|
/// - Ok(true) if verify is successful
|
|
|
|
/// - Ok(false) if there were verification errors
|
|
|
|
/// - Err(_) if task was aborted
|
2020-07-29 11:29:13 +00:00
|
|
|
pub fn verify_backup_dir(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-07-29 11:29:13 +00:00
|
|
|
backup_dir: &BackupDir,
|
2020-10-12 09:46:34 +00:00
|
|
|
upid: UPID,
|
2020-10-29 06:59:19 +00:00
|
|
|
filter: Option<&dyn Fn(&BackupManifest) -> bool>,
|
2020-07-29 11:29:13 +00:00
|
|
|
) -> Result<bool, Error> {
|
2020-10-20 08:08:24 +00:00
|
|
|
let snap_lock = lock_dir_noblock_shared(
|
2021-01-25 13:42:58 +00:00
|
|
|
&verify_worker.datastore.snapshot_path(&backup_dir),
|
2020-10-14 12:16:33 +00:00
|
|
|
"snapshot",
|
2021-04-14 10:53:19 +00:00
|
|
|
"locked by another operation",
|
|
|
|
);
|
2020-10-20 08:08:24 +00:00
|
|
|
match snap_lock {
|
2021-04-14 10:53:19 +00:00
|
|
|
Ok(snap_lock) => {
|
|
|
|
verify_backup_dir_with_lock(verify_worker, backup_dir, upid, filter, snap_lock)
|
|
|
|
}
|
2020-10-20 08:08:24 +00:00
|
|
|
Err(err) => {
|
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-20 08:08:24 +00:00
|
|
|
"SKIPPED: verify {}:{} - could not acquire snapshot lock: {}",
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.datastore.name(),
|
2020-10-20 08:08:24 +00:00
|
|
|
backup_dir,
|
|
|
|
err,
|
|
|
|
);
|
|
|
|
Ok(true)
|
|
|
|
}
|
2020-10-14 12:16:33 +00:00
|
|
|
}
|
2020-10-20 08:08:24 +00:00
|
|
|
}
|
2020-10-14 12:16:33 +00:00
|
|
|
|
2020-10-20 08:08:24 +00:00
|
|
|
/// See verify_backup_dir
|
|
|
|
pub fn verify_backup_dir_with_lock(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-10-20 08:08:24 +00:00
|
|
|
backup_dir: &BackupDir,
|
|
|
|
upid: UPID,
|
2020-10-29 06:59:19 +00:00
|
|
|
filter: Option<&dyn Fn(&BackupManifest) -> bool>,
|
2020-10-20 08:08:24 +00:00
|
|
|
_snap_lock: Dir,
|
|
|
|
) -> Result<bool, Error> {
|
2021-01-25 13:42:58 +00:00
|
|
|
let manifest = match verify_worker.datastore.load_manifest(&backup_dir) {
|
2020-07-31 08:25:30 +00:00
|
|
|
Ok((manifest, _)) => manifest,
|
2020-06-24 11:11:45 +00:00
|
|
|
Err(err) => {
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-12 09:46:34 +00:00
|
|
|
"verify {}:{} - manifest load error: {}",
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.datastore.name(),
|
2020-10-12 09:46:34 +00:00
|
|
|
backup_dir,
|
|
|
|
err,
|
|
|
|
);
|
2020-06-25 10:55:34 +00:00
|
|
|
return Ok(false);
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-29 06:59:19 +00:00
|
|
|
if let Some(filter) = filter {
|
2021-01-18 13:12:27 +00:00
|
|
|
if !filter(&manifest) {
|
2020-10-29 06:59:19 +00:00
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-29 06:59:19 +00:00
|
|
|
"SKIPPED: verify {}:{} (recently verified)",
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.datastore.name(),
|
2020-10-29 06:59:19 +00:00
|
|
|
backup_dir,
|
|
|
|
);
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
task_log!(verify_worker.worker, "verify {}:{}", verify_worker.datastore.name(), backup_dir);
|
2020-06-24 11:11:45 +00:00
|
|
|
|
|
|
|
let mut error_count = 0;
|
|
|
|
|
2020-09-15 08:19:23 +00:00
|
|
|
let mut verify_result = VerifyState::Ok;
|
2020-06-24 11:11:45 +00:00
|
|
|
for info in manifest.files() {
|
|
|
|
let result = proxmox::try_block!({
|
2021-01-25 13:42:58 +00:00
|
|
|
task_log!(verify_worker.worker, " check {}", info.filename);
|
2020-06-24 11:11:45 +00:00
|
|
|
match archive_type(&info.filename)? {
|
2021-04-14 10:53:19 +00:00
|
|
|
ArchiveType::FixedIndex => verify_fixed_index(verify_worker, &backup_dir, info),
|
|
|
|
ArchiveType::DynamicIndex => verify_dynamic_index(verify_worker, &backup_dir, info),
|
|
|
|
ArchiveType::Blob => {
|
|
|
|
verify_blob(verify_worker.datastore.clone(), &backup_dir, info)
|
|
|
|
}
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
});
|
2020-06-25 10:55:34 +00:00
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker.check_abort()?;
|
2020-09-02 07:50:17 +00:00
|
|
|
crate::tools::fail_on_shutdown()?;
|
2020-06-25 10:55:34 +00:00
|
|
|
|
2020-06-24 11:11:45 +00:00
|
|
|
if let Err(err) = result {
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-12 09:46:34 +00:00
|
|
|
"verify {}:{}/{} failed: {}",
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.datastore.name(),
|
2020-10-12 09:46:34 +00:00
|
|
|
backup_dir,
|
|
|
|
info.filename,
|
|
|
|
err,
|
|
|
|
);
|
2020-06-24 11:11:45 +00:00
|
|
|
error_count += 1;
|
2020-09-15 08:19:23 +00:00
|
|
|
verify_result = VerifyState::Failed;
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:30:27 +00:00
|
|
|
let verify_state = SnapshotVerifyState {
|
2020-09-15 08:19:23 +00:00
|
|
|
state: verify_result,
|
2020-10-12 09:46:34 +00:00
|
|
|
upid,
|
2020-08-25 15:30:27 +00:00
|
|
|
};
|
2020-10-16 07:31:12 +00:00
|
|
|
let verify_state = serde_json::to_value(verify_state)?;
|
2021-04-14 10:53:19 +00:00
|
|
|
verify_worker
|
|
|
|
.datastore
|
|
|
|
.update_manifest(&backup_dir, |manifest| {
|
|
|
|
manifest.unprotected["verify_state"] = verify_state;
|
|
|
|
})
|
|
|
|
.map_err(|err| format_err!("unable to update manifest blob - {}", err))?;
|
2020-08-25 15:30:27 +00:00
|
|
|
|
2020-06-25 10:55:34 +00:00
|
|
|
Ok(error_count == 0)
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 10:55:34 +00:00
|
|
|
/// Verify all backups inside a backup group
|
|
|
|
///
|
|
|
|
/// Errors are logged to the worker log.
|
|
|
|
///
|
|
|
|
/// Returns
|
2020-09-02 05:43:04 +00:00
|
|
|
/// - Ok((count, failed_dirs)) where failed_dirs had verification errors
|
2020-06-25 10:55:34 +00:00
|
|
|
/// - Err(_) if task was aborted
|
2020-10-28 12:19:21 +00:00
|
|
|
pub fn verify_backup_group(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-09-01 11:33:04 +00:00
|
|
|
group: &BackupGroup,
|
2020-11-30 15:27:21 +00:00
|
|
|
progress: &mut StoreProgress,
|
2020-10-12 09:46:34 +00:00
|
|
|
upid: &UPID,
|
2020-10-29 06:59:19 +00:00
|
|
|
filter: Option<&dyn Fn(&BackupManifest) -> bool>,
|
2020-11-30 15:27:21 +00:00
|
|
|
) -> Result<Vec<String>, Error> {
|
2020-07-30 07:09:05 +00:00
|
|
|
let mut errors = Vec::new();
|
2021-01-25 13:42:58 +00:00
|
|
|
let mut list = match group.list_backups(&verify_worker.datastore.base_path()) {
|
2020-06-24 11:11:45 +00:00
|
|
|
Ok(list) => list,
|
|
|
|
Err(err) => {
|
2020-10-12 09:46:34 +00:00
|
|
|
task_log!(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.worker,
|
2020-10-12 09:46:34 +00:00
|
|
|
"verify group {}:{} - unable to list backups: {}",
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker.datastore.name(),
|
2020-10-12 09:46:34 +00:00
|
|
|
group,
|
|
|
|
err,
|
|
|
|
);
|
2020-11-30 15:27:21 +00:00
|
|
|
return Ok(errors);
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-30 15:27:21 +00:00
|
|
|
let snapshot_count = list.len();
|
2021-04-14 10:53:19 +00:00
|
|
|
task_log!(
|
|
|
|
verify_worker.worker,
|
|
|
|
"verify group {}:{} ({} snapshots)",
|
|
|
|
verify_worker.datastore.name(),
|
|
|
|
group,
|
|
|
|
snapshot_count
|
|
|
|
);
|
2020-06-24 11:11:45 +00:00
|
|
|
|
2020-11-30 15:27:21 +00:00
|
|
|
progress.group_snapshots = snapshot_count as u64;
|
2020-09-02 05:43:04 +00:00
|
|
|
|
2020-06-24 11:11:45 +00:00
|
|
|
BackupInfo::sort_list(&mut list, false); // newest first
|
2020-11-30 15:27:21 +00:00
|
|
|
for (pos, info) in list.into_iter().enumerate() {
|
2021-04-14 10:53:19 +00:00
|
|
|
if !verify_backup_dir(verify_worker, &info.backup_dir, upid.clone(), filter)? {
|
2020-07-30 07:09:05 +00:00
|
|
|
errors.push(info.backup_dir.to_string());
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
2020-11-30 15:27:21 +00:00
|
|
|
progress.done_snapshots = pos as u64 + 1;
|
2021-04-14 10:53:19 +00:00
|
|
|
task_log!(verify_worker.worker, "percentage done: {}", progress);
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 15:27:21 +00:00
|
|
|
Ok(errors)
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:36:39 +00:00
|
|
|
/// Verify all (owned) backups inside a datastore
|
2020-06-25 10:55:34 +00:00
|
|
|
///
|
|
|
|
/// Errors are logged to the worker log.
|
|
|
|
///
|
|
|
|
/// Returns
|
2020-07-30 07:09:05 +00:00
|
|
|
/// - Ok(failed_dirs) where failed_dirs had verification errors
|
2020-06-25 10:55:34 +00:00
|
|
|
/// - Err(_) if task was aborted
|
2020-10-28 12:19:21 +00:00
|
|
|
pub fn verify_all_backups(
|
2021-01-25 13:42:58 +00:00
|
|
|
verify_worker: &VerifyWorker,
|
2020-10-12 09:46:34 +00:00
|
|
|
upid: &UPID,
|
2020-10-30 11:36:39 +00:00
|
|
|
owner: Option<Authid>,
|
2020-10-29 06:59:19 +00:00
|
|
|
filter: Option<&dyn Fn(&BackupManifest) -> bool>,
|
2020-10-12 09:46:34 +00:00
|
|
|
) -> Result<Vec<String>, Error> {
|
2020-07-30 07:09:05 +00:00
|
|
|
let mut errors = Vec::new();
|
2021-01-25 13:42:58 +00:00
|
|
|
let worker = Arc::clone(&verify_worker.worker);
|
2020-06-24 11:11:45 +00:00
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
task_log!(worker, "verify datastore {}", verify_worker.datastore.name());
|
2020-11-10 12:52:50 +00:00
|
|
|
|
2020-10-30 11:36:39 +00:00
|
|
|
if let Some(owner) = &owner {
|
2020-11-10 12:52:50 +00:00
|
|
|
task_log!(worker, "limiting to backups owned by {}", owner);
|
2020-10-30 11:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let filter_by_owner = |group: &BackupGroup| {
|
2021-01-25 13:42:58 +00:00
|
|
|
match (verify_worker.datastore.get_owner(group), &owner) {
|
2020-11-10 12:52:49 +00:00
|
|
|
(Ok(ref group_owner), Some(owner)) => {
|
|
|
|
group_owner == owner
|
|
|
|
|| (group_owner.is_token()
|
|
|
|
&& !owner.is_token()
|
|
|
|
&& group_owner.user() == owner.user())
|
|
|
|
},
|
|
|
|
(Ok(_), None) => true,
|
|
|
|
(Err(err), Some(_)) => {
|
|
|
|
// intentionally not in task log
|
|
|
|
// the task user might not be allowed to see this group!
|
|
|
|
println!("Failed to get owner of group '{}' - {}", group, err);
|
|
|
|
false
|
|
|
|
},
|
|
|
|
(Err(err), None) => {
|
|
|
|
// we don't filter by owner, but we want to log the error
|
|
|
|
task_log!(
|
|
|
|
worker,
|
|
|
|
"Failed to get owner of group '{} - {}",
|
|
|
|
group,
|
|
|
|
err,
|
|
|
|
);
|
|
|
|
errors.push(group.to_string());
|
|
|
|
true
|
|
|
|
},
|
2020-10-30 11:36:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-25 13:42:58 +00:00
|
|
|
let mut list = match BackupInfo::list_backup_groups(&verify_worker.datastore.base_path()) {
|
2020-09-10 06:54:29 +00:00
|
|
|
Ok(list) => list
|
|
|
|
.into_iter()
|
|
|
|
.filter(|group| !(group.backup_type() == "host" && group.backup_id() == "benchmark"))
|
2020-10-30 11:36:39 +00:00
|
|
|
.filter(filter_by_owner)
|
2020-09-10 06:54:29 +00:00
|
|
|
.collect::<Vec<BackupGroup>>(),
|
2020-06-24 11:11:45 +00:00
|
|
|
Err(err) => {
|
2021-04-14 10:53:19 +00:00
|
|
|
task_log!(worker, "unable to list backups: {}", err,);
|
2020-07-30 07:09:05 +00:00
|
|
|
return Ok(errors);
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-25 06:38:47 +00:00
|
|
|
list.sort_unstable();
|
|
|
|
|
2020-11-30 15:27:21 +00:00
|
|
|
let group_count = list.len();
|
|
|
|
task_log!(worker, "found {} groups", group_count);
|
2020-06-24 11:11:45 +00:00
|
|
|
|
2020-11-30 15:27:21 +00:00
|
|
|
let mut progress = StoreProgress::new(group_count as u64);
|
|
|
|
|
|
|
|
for (pos, group) in list.into_iter().enumerate() {
|
|
|
|
progress.done_groups = pos as u64;
|
|
|
|
progress.done_snapshots = 0;
|
|
|
|
progress.group_snapshots = 0;
|
|
|
|
|
2021-04-14 10:53:19 +00:00
|
|
|
let mut group_errors =
|
|
|
|
verify_backup_group(verify_worker, &group, &mut progress, upid, filter)?;
|
2020-07-30 07:09:05 +00:00
|
|
|
errors.append(&mut group_errors);
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:09:05 +00:00
|
|
|
Ok(errors)
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|