mirror of
https://git.proxmox.com/git/vma-to-pbs
synced 2025-08-14 11:56:52 +00:00
use level-based logging instead of println
Use log level "info" by default and prevent spamming messages for every single chunk uploaded. To re-enable these messages, set the RUST_LOG environment variable to "debug". Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
This commit is contained in:
parent
54ad876f25
commit
51567bbfc2
@ -8,7 +8,9 @@ edition = "2021"
|
|||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
bincode = "1.3"
|
bincode = "1.3"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
env_logger = "0.10"
|
||||||
hyper = "0.14.5"
|
hyper = "0.14.5"
|
||||||
|
log = "0.4"
|
||||||
pico-args = "0.5"
|
pico-args = "0.5"
|
||||||
md5 = "0.7.0"
|
md5 = "0.7.0"
|
||||||
regex = "1.7"
|
regex = "1.7"
|
||||||
|
28
src/main.rs
28
src/main.rs
@ -6,6 +6,7 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use anyhow::{bail, Context, Error};
|
use anyhow::{bail, Context, Error};
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
|
use env_logger::Target;
|
||||||
use proxmox_sys::linux::tty;
|
use proxmox_sys::linux::tty;
|
||||||
use proxmox_time::epoch_i64;
|
use proxmox_time::epoch_i64;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@ -132,7 +133,7 @@ fn parse_args() -> Result<BackupVmaToPbsArgs, Error> {
|
|||||||
|
|
||||||
match (encrypt, keyfile.is_some()) {
|
match (encrypt, keyfile.is_some()) {
|
||||||
(true, false) => bail!("--encrypt requires a --keyfile!"),
|
(true, false) => bail!("--encrypt requires a --keyfile!"),
|
||||||
(false, true) => println!(
|
(false, true) => log::info!(
|
||||||
"--keyfile given, but --encrypt not set -> backup will be signed, but not encrypted!"
|
"--keyfile given, but --encrypt not set -> backup will be signed, but not encrypted!"
|
||||||
),
|
),
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -194,7 +195,7 @@ fn parse_args() -> Result<BackupVmaToPbsArgs, Error> {
|
|||||||
|
|
||||||
Some(key_password)
|
Some(key_password)
|
||||||
} else if vma_file_path.is_none() {
|
} else if vma_file_path.is_none() {
|
||||||
println!(
|
log::info!(
|
||||||
"Please use --key-password-file to provide the password when passing the VMA file \
|
"Please use --key-password-file to provide the password when passing the VMA file \
|
||||||
to stdin, if required."
|
to stdin, if required."
|
||||||
);
|
);
|
||||||
@ -250,13 +251,17 @@ fn parse_args() -> Result<BackupVmaToPbsArgs, Error> {
|
|||||||
let Some((_, [backup_id, timestr, ext])) =
|
let Some((_, [backup_id, timestr, ext])) =
|
||||||
re.captures(file_name).map(|c| c.extract())
|
re.captures(file_name).map(|c| c.extract())
|
||||||
else {
|
else {
|
||||||
// Skip the file, since it is not a VMA backup
|
log::debug!("Skip \"{file_name}\", since it is not a VMA backup");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(ref vmid) = vmid {
|
if let Some(ref vmid) = vmid {
|
||||||
if backup_id != vmid {
|
if backup_id != vmid {
|
||||||
// Skip the backup, since it does not match the specified vmid
|
log::debug!(
|
||||||
|
"Skip backup with VMID {}, since it does not match specified VMID {}",
|
||||||
|
backup_id,
|
||||||
|
vmid
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,13 +317,13 @@ fn parse_args() -> Result<BackupVmaToPbsArgs, Error> {
|
|||||||
bail!("Did not find any backup archives");
|
bail!("Did not find any backup archives");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
log::info!(
|
||||||
"Found {total_vma_count} backup archive(s) of {} different VMID(s):",
|
"Found {total_vma_count} backup archive(s) of {} different VMID(s):",
|
||||||
grouped_vmas.len()
|
grouped_vmas.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
for (backup_id, vma_group) in &grouped_vmas {
|
for (backup_id, vma_group) in &grouped_vmas {
|
||||||
println!("- VMID {backup_id}: {} backups", vma_group.len());
|
log::info!("- VMID {backup_id}: {} backups", vma_group.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
if !yes {
|
if !yes {
|
||||||
@ -361,7 +366,18 @@ fn parse_args() -> Result<BackupVmaToPbsArgs, Error> {
|
|||||||
Ok(options)
|
Ok(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn init_cli_logger() {
|
||||||
|
env_logger::Builder::from_env(env_logger::Env::new().filter_or("RUST_LOG", "info"))
|
||||||
|
.format_level(false)
|
||||||
|
.format_target(false)
|
||||||
|
.format_timestamp(None)
|
||||||
|
.target(Target::Stdout)
|
||||||
|
.init();
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
|
init_cli_logger();
|
||||||
|
|
||||||
let args = parse_args()?;
|
let args = parse_args()?;
|
||||||
vma2pbs(args)?;
|
vma2pbs(args)?;
|
||||||
|
|
||||||
|
@ -82,8 +82,8 @@ fn create_pbs_backup_task(
|
|||||||
pbs_args: &PbsArgs,
|
pbs_args: &PbsArgs,
|
||||||
backup_args: &VmaBackupArgs,
|
backup_args: &VmaBackupArgs,
|
||||||
) -> Result<*mut ProxmoxBackupHandle, Error> {
|
) -> Result<*mut ProxmoxBackupHandle, Error> {
|
||||||
println!(
|
log::info!(
|
||||||
"backup time: {}",
|
"\tbackup time: {}",
|
||||||
epoch_to_rfc3339(backup_args.backup_time)?
|
epoch_to_rfc3339(backup_args.backup_time)?
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ where
|
|||||||
let config_name = config.name;
|
let config_name = config.name;
|
||||||
let config_data = config.content;
|
let config_data = config.content;
|
||||||
|
|
||||||
println!("CFG: size: {} name: {}", config_data.len(), config_name);
|
log::info!("\tCFG: size: {} name: {config_name}", config_data.len());
|
||||||
|
|
||||||
let config_name_cstr = CString::new(config_name)?;
|
let config_name_cstr = CString::new(config_name)?;
|
||||||
|
|
||||||
@ -190,10 +190,7 @@ where
|
|||||||
let device_name = vma_reader.get_device_name(device_id.try_into()?)?;
|
let device_name = vma_reader.get_device_name(device_id.try_into()?)?;
|
||||||
let device_size = vma_reader.get_device_size(device_id.try_into()?)?;
|
let device_size = vma_reader.get_device_size(device_id.try_into()?)?;
|
||||||
|
|
||||||
println!(
|
log::info!("\tDEV: dev_id={device_id} size: {device_size} devname: {device_name}");
|
||||||
"DEV: dev_id={} size: {} devname: {}",
|
|
||||||
device_id, device_size, device_name
|
|
||||||
);
|
|
||||||
|
|
||||||
let device_name_cstr = CString::new(device_name)?;
|
let device_name_cstr = CString::new(device_name)?;
|
||||||
let pbs_device_id = proxmox_backup_register_image(
|
let pbs_device_id = proxmox_backup_register_image(
|
||||||
@ -276,10 +273,8 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let pbs_upload_chunk = |pbs_chunk_buffer: Option<&[u8]>| {
|
let pbs_upload_chunk = |pbs_chunk_buffer: Option<&[u8]>| {
|
||||||
println!(
|
log::debug!(
|
||||||
"Uploading dev_id: {} offset: {:#0X} - {:#0X}",
|
"\tUploading dev_id: {dev_id} offset: {pbs_chunk_offset:#0X} - {:#0X}",
|
||||||
dev_id,
|
|
||||||
pbs_chunk_offset,
|
|
||||||
pbs_chunk_offset + pbs_chunk_size,
|
pbs_chunk_offset + pbs_chunk_size,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -466,13 +461,13 @@ fn set_notes(
|
|||||||
|
|
||||||
pub fn vma2pbs(args: BackupVmaToPbsArgs) -> Result<(), Error> {
|
pub fn vma2pbs(args: BackupVmaToPbsArgs) -> Result<(), Error> {
|
||||||
let pbs_args = &args.pbs_args;
|
let pbs_args = &args.pbs_args;
|
||||||
println!("PBS repository: {}", pbs_args.pbs_repository);
|
log::info!("PBS repository: {}", pbs_args.pbs_repository);
|
||||||
if let Some(ns) = &pbs_args.namespace {
|
if let Some(ns) = &pbs_args.namespace {
|
||||||
println!("PBS namespace: {}", ns);
|
log::info!("PBS namespace: {ns}");
|
||||||
}
|
}
|
||||||
println!("PBS fingerprint: {}", pbs_args.fingerprint);
|
log::info!("PBS fingerprint: {}", pbs_args.fingerprint);
|
||||||
println!("compress: {}", pbs_args.compress);
|
log::info!("compress: {}", pbs_args.compress);
|
||||||
println!("encrypt: {}", pbs_args.encrypt);
|
log::info!("encrypt: {}", pbs_args.encrypt);
|
||||||
|
|
||||||
let start_transfer_time = SystemTime::now();
|
let start_transfer_time = SystemTime::now();
|
||||||
|
|
||||||
@ -485,8 +480,8 @@ pub fn vma2pbs(args: BackupVmaToPbsArgs) -> Result<(), Error> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if args.skip_failed {
|
if args.skip_failed {
|
||||||
eprintln!("{}", err_msg);
|
log::warn!("{}", err_msg);
|
||||||
println!("Skipping VMID {}", backup_args.backup_id);
|
log::info!("Skipping VMID {}", backup_args.backup_id);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
bail!(err_msg);
|
bail!(err_msg);
|
||||||
@ -500,15 +495,15 @@ pub fn vma2pbs(args: BackupVmaToPbsArgs) -> Result<(), Error> {
|
|||||||
let minutes = total_seconds / 60;
|
let minutes = total_seconds / 60;
|
||||||
let seconds = total_seconds % 60;
|
let seconds = total_seconds % 60;
|
||||||
let milliseconds = transfer_duration.as_millis() % 1000;
|
let milliseconds = transfer_duration.as_millis() % 1000;
|
||||||
println!("Backup finished within {minutes} minutes, {seconds} seconds and {milliseconds} ms");
|
log::info!("Backup finished within {minutes} minutes, {seconds} seconds and {milliseconds} ms");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn upload_vma_file(pbs_args: &PbsArgs, backup_args: &VmaBackupArgs) -> Result<(), Error> {
|
fn upload_vma_file(pbs_args: &PbsArgs, backup_args: &VmaBackupArgs) -> Result<(), Error> {
|
||||||
match &backup_args.vma_file_path {
|
match &backup_args.vma_file_path {
|
||||||
Some(vma_file_path) => println!("Uploading VMA backup from {vma_file_path:?}"),
|
Some(vma_file_path) => log::info!("Uploading VMA backup from {vma_file_path:?}"),
|
||||||
None => println!("Uploading VMA backup from (stdin)"),
|
None => log::info!("Uploading VMA backup from (stdin)"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let vma_file: Box<dyn BufRead> = match &backup_args.compression {
|
let vma_file: Box<dyn BufRead> = match &backup_args.compression {
|
||||||
|
Loading…
Reference in New Issue
Block a user