use result
This commit is contained in:
94
src/main.rs
94
src/main.rs
@@ -1,5 +1,5 @@
|
|||||||
#[macro_use]
|
// #[macro_use]
|
||||||
extern crate lazy_static;
|
// extern crate lazy_static;
|
||||||
extern crate sequoia_openpgp as openpgp;
|
extern crate sequoia_openpgp as openpgp;
|
||||||
pub mod oss_util;
|
pub mod oss_util;
|
||||||
pub mod pgp_util;
|
pub mod pgp_util;
|
||||||
@@ -51,75 +51,45 @@ fn main() -> XResult<()> {
|
|||||||
for config_item in &oss_backupd_config.items {
|
for config_item in &oss_backupd_config.items {
|
||||||
item_index += 1;
|
item_index += 1;
|
||||||
|
|
||||||
|
if let Err(e) = process_config_item(&options, &config_item, &oss_backupd_config, item_index) {
|
||||||
|
print_message(MessageType::ERROR, &format!("Config {} not found, at item index: {}", e, item_index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print_message(MessageType::OK, "Backup all file(s) finished!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
|
||||||
|
oss_backupd_config :&OSSBackupdConfig, item_index: i32) -> Result<(), String> {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item));
|
print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item));
|
||||||
}
|
}
|
||||||
|
|
||||||
let encrypt_pubkey_file = match &config_item.encrypt_pubkey_file {
|
let encrypt_pubkey_file = config_item.encrypt_pubkey_file.as_ref().ok_or("encrypt_pubkey_file".to_owned())?;
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config encrypt_pubkey_file not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(encrypt_pubkey_file) => encrypt_pubkey_file,
|
|
||||||
};
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file));
|
print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file));
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = match &config_item.target {
|
let target = config_item.target.as_ref().ok_or("target".to_owned())?;
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config target not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(target) => target,
|
|
||||||
};
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "<empty>", target)));
|
print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "<empty>", target)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let oss_config = match &config_item.oss_config {
|
let oss_config = config_item.oss_config.as_ref().ok_or("oss_config".to_owned())?;
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config oss_config not found, at item index: {}", item_index));
|
let endpoint = oss_config.endpoint.as_ref().ok_or("oss_config#endpoint".to_owned())?;
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(oss_config) => oss_config,
|
|
||||||
};
|
|
||||||
let endpoint = match &oss_config.endpoint {
|
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config oss_config#endpoint not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Some(endpoint) => endpoint,
|
|
||||||
};
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint));
|
print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint));
|
||||||
}
|
}
|
||||||
|
|
||||||
let access_key_id = match &oss_config.access_key_id {
|
let access_key_id = oss_config.access_key_id.as_ref().ok_or("oss_config#access_key_id".to_owned())?;
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config oss_config#access_key_id not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(access_key_id) => access_key_id,
|
|
||||||
};
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id));
|
print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
let access_key_secret = match &oss_config.access_key_secret {
|
let access_key_secret = oss_config.access_key_secret.as_ref().ok_or("oss_config#access_key_secret".to_owned())?;
|
||||||
None => {
|
let bucket = &oss_config.bucket.as_ref().ok_or("oss_config#bucket".to_owned())?;
|
||||||
print_message(MessageType::ERROR, &format!("Config oss_config#access_key_secret not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(access_key_secret) => access_key_secret,
|
|
||||||
};
|
|
||||||
let bucket = match &oss_config.bucket {
|
|
||||||
None => {
|
|
||||||
print_message(MessageType::ERROR, &format!("Config oss_config#bucket not found, at item index: {}", item_index));
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
Some(bucket) => bucket,
|
|
||||||
};
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket));
|
print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket));
|
||||||
}
|
}
|
||||||
@@ -146,7 +116,7 @@ fn main() -> XResult<()> {
|
|||||||
let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) {
|
let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index));
|
||||||
continue;
|
return Ok(());
|
||||||
},
|
},
|
||||||
Ok(open_pgp_tool) => open_pgp_tool,
|
Ok(open_pgp_tool) => open_pgp_tool,
|
||||||
};
|
};
|
||||||
@@ -158,7 +128,7 @@ fn main() -> XResult<()> {
|
|||||||
}
|
}
|
||||||
if let Err(e) = zip_util::zip_file(target, temp_zip_file) {
|
if let Err(e) = zip_util::zip_file(target, temp_zip_file) {
|
||||||
print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index));
|
||||||
continue;
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
@@ -167,7 +137,7 @@ fn main() -> XResult<()> {
|
|||||||
if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) {
|
if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) {
|
||||||
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
||||||
fs::remove_file(temp_zip_file).ok();
|
fs::remove_file(temp_zip_file).ok();
|
||||||
continue;
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let file_temp_pgp_file = match File::open(temp_pgp_file) {
|
let file_temp_pgp_file = match File::open(temp_pgp_file) {
|
||||||
@@ -175,7 +145,7 @@ fn main() -> XResult<()> {
|
|||||||
print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index));
|
||||||
fs::remove_file(temp_zip_file).ok();
|
fs::remove_file(temp_zip_file).ok();
|
||||||
fs::remove_file(temp_pgp_file).ok();
|
fs::remove_file(temp_pgp_file).ok();
|
||||||
continue;
|
return Ok(());
|
||||||
},
|
},
|
||||||
Ok(file_temp_pgp_file) => file_temp_pgp_file,
|
Ok(file_temp_pgp_file) => file_temp_pgp_file,
|
||||||
};
|
};
|
||||||
@@ -187,13 +157,13 @@ fn main() -> XResult<()> {
|
|||||||
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
||||||
fs::remove_file(temp_zip_file).ok();
|
fs::remove_file(temp_zip_file).ok();
|
||||||
fs::remove_file(temp_pgp_file).ok();
|
fs::remove_file(temp_pgp_file).ok();
|
||||||
continue;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name));
|
print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name));
|
||||||
}
|
}
|
||||||
match process_oss_files(&oss_client, bucket, path, meta_file_name, &new_file, default_limit) {
|
match process_oss_files(&options, &oss_client, bucket, path, meta_file_name, &new_file, default_limit) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
||||||
},
|
},
|
||||||
@@ -203,26 +173,24 @@ fn main() -> XResult<()> {
|
|||||||
};
|
};
|
||||||
fs::remove_file(temp_zip_file).ok();
|
fs::remove_file(temp_zip_file).ok();
|
||||||
fs::remove_file(temp_pgp_file).ok();
|
fs::remove_file(temp_pgp_file).ok();
|
||||||
}
|
|
||||||
|
|
||||||
print_message(MessageType::OK, "Backup all file(s) finished!");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_oss_files(oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> {
|
pub fn process_oss_files(options: &Options, oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> {
|
||||||
let meta_file_key = &format!("{}/{}", path, meta_file_name);
|
let meta_file_key = &format!("{}/{}", path, meta_file_name);
|
||||||
if *opt::IS_DEBUG {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Read meta file: {}", meta_file_key));
|
print_message(MessageType::DEBUG, &format!("Read meta file: {}", meta_file_key));
|
||||||
}
|
}
|
||||||
let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? {
|
let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? {
|
||||||
None => "[]".to_owned(),
|
None => "[]".to_owned(),
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
};
|
};
|
||||||
if *opt::IS_DEBUG {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content));
|
print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content));
|
||||||
}
|
}
|
||||||
let (removed_file, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?;
|
let (removed_file, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?;
|
||||||
if *opt::IS_DEBUG {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Processed meta file content: {}", &new_meta_file_content));
|
print_message(MessageType::DEBUG, &format!("Processed meta file content: {}", &new_meta_file_content));
|
||||||
}
|
}
|
||||||
oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?;
|
oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?;
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
use rust_util::{
|
use rust_util::{
|
||||||
XResult,
|
XResult,
|
||||||
util_env::is_env_on,
|
|
||||||
};
|
};
|
||||||
use argparse::{ArgumentParser, StoreTrue, Store};
|
use argparse::{ArgumentParser, StoreTrue, Store};
|
||||||
|
|
||||||
lazy_static! {
|
// lazy_static! {
|
||||||
pub static ref IS_DEBUG: bool = is_env_on("DEBUG");
|
// pub static ref IS_DEBUG: bool = rust_util::util_env::is_env_on("DEBUG");
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
pub version: bool,
|
pub version: bool,
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ impl<'a> OSSClient<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://endpoint, or http://endpoint -> endpoint
|
||||||
fn remove_endpoint_http_or_s(endpoint: &str) -> String {
|
fn remove_endpoint_http_or_s(endpoint: &str) -> String {
|
||||||
let mut endpoint = endpoint.to_owned();
|
let mut endpoint = endpoint.to_owned();
|
||||||
if endpoint.starts_with("http://") {
|
if endpoint.starts_with("http://") {
|
||||||
|
|||||||
Reference in New Issue
Block a user