From ec8a9bb95374cbba67922574e616fee8339106c8 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 3 Jun 2019 10:39:44 +0200 Subject: [PATCH] src/bin/proxmox-backup-client.rs: implement upload_config --- src/bin/proxmox-backup-client.rs | 42 +++++++++++++++++++++++--------- src/client/http_client.rs | 7 +++--- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index ff1196a0..7e590972 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -28,7 +28,7 @@ use futures::*; use tokio::sync::mpsc; lazy_static! { - static ref BACKUPSPEC_REGEX: Regex = Regex::new(r"^([a-zA-Z0-9_-]+\.(?:pxar|img)):(.+)$").unwrap(); + static ref BACKUPSPEC_REGEX: Regex = Regex::new(r"^([a-zA-Z0-9_-]+\.(?:pxar|img|conf)):(.+)$").unwrap(); } @@ -401,7 +401,7 @@ fn create_backup( let mut upload_list = vec![]; - enum BackupType { PXAR, IMAGE }; + enum BackupType { PXAR, IMAGE, CONFIG }; for backupspec in backupspec_list { let (target, filename) = parse_backupspec(backupspec.as_str().unwrap())?; @@ -414,20 +414,36 @@ fn create_backup( }; let file_type = metadata.file_type(); - if file_type.is_dir() { + let extension = Path::new(target).extension().map(|s| s.to_str().unwrap()).unwrap(); - upload_list.push((BackupType::PXAR, filename.to_owned(), target.to_owned(), 0)); + match extension { + "pxar" => { + if !file_type.is_dir() { + bail!("got unexpected file type (expected directory)"); + } + upload_list.push((BackupType::PXAR, filename.to_owned(), target.to_owned(), 0)); + } + "img" => { - } else if file_type.is_file() || file_type.is_block_device() { + if !(file_type.is_file() || file_type.is_block_device()) { + bail!("got unexpected file type (expected file or block device)"); + } - let size = tools::image_size(&PathBuf::from(filename))?; + let size = tools::image_size(&PathBuf::from(filename))?; - if size == 0 { bail!("got zero-sized file '{}'", filename); } + if size == 0 { bail!("got zero-sized file '{}'", filename); } - upload_list.push((BackupType::IMAGE, filename.to_owned(), target.to_owned(), size)); - - } else { - bail!("unsupported file type (expected a directory, file or block device)"); + upload_list.push((BackupType::IMAGE, filename.to_owned(), target.to_owned(), size)); + } + "conf" => { + if !file_type.is_file() { + bail!("got unexpected file type (expected regular file)"); + } + upload_list.push((BackupType::CONFIG, filename.to_owned(), target.to_owned(), metadata.len())); + } + _ => { + bail!("got unknown archive extension '{}'", extension); + } } } @@ -444,6 +460,10 @@ fn create_backup( for (backup_type, filename, target, size) in upload_list { match backup_type { + BackupType::CONFIG => { + println!("Upload config file '{}' to '{:?}' as {}", filename, repo, target); + client.upload_config(&filename, &target).wait()?; + } BackupType::PXAR => { println!("Upload directory '{}' to '{:?}' as {}", filename, repo, target); backup_directory(&client, &filename, &target, chunk_size_opt, all_file_systems, verbose)?; diff --git a/src/client/http_client.rs b/src/client/http_client.rs index 1f33bc03..51defb23 100644 --- a/src/client/http_client.rs +++ b/src/client/http_client.rs @@ -431,14 +431,15 @@ impl BackupClient { self.h2.clone().post("finish", None).map(|_| ()) } - pub fn upload_config( + pub fn upload_config>( &self, + src_path: P, file_name: &str, - src_path: std::path::PathBuf, - ) -> impl Future { + ) -> impl Future { let h2 = self.h2.clone(); let file_name = file_name.to_owned(); + let src_path = src_path.as_ref().to_owned(); let task = tokio::fs::File::open(src_path) .map_err(Error::from)