add backup count configable
This commit is contained in:
@@ -52,6 +52,7 @@ pub struct OSSBackupdConfigItem {
|
||||
pub file_name: Option<String>,
|
||||
pub oss_config: Option<OSSConfig>,
|
||||
pub encrypt_pubkey_file: Option<String>,
|
||||
pub backup_count: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -60,6 +61,7 @@ pub struct OSSBackupdConfig {
|
||||
pub prefix: Option<String>,
|
||||
pub host: Option<String>,
|
||||
pub items: Vec<OSSBackupdConfigItem>,
|
||||
pub backup_count: Option<u32>,
|
||||
}
|
||||
|
||||
impl OSSBackupdConfig {
|
||||
@@ -130,25 +132,28 @@ pub fn parse_config(config_json: &json::JsonValue) -> OSSBackupdConfig {
|
||||
let encrypt_pubkey_file = get_string_value(config_json, "encrypt_pubkey_file");
|
||||
let prefix = get_string_value(config_json, "prefix");
|
||||
let host = get_string_value(config_json, "host");
|
||||
let backup_count = get_u32_value(config_json, "backup_count");
|
||||
let items = &config_json["items"];
|
||||
let mut items_objects: Vec<OSSBackupdConfigItem> = vec![];
|
||||
if items.is_array() {
|
||||
for i in 0..items.len() {
|
||||
items_objects.push(parse_oss_backupd_config_item(&items[i], &root_oss_config_object, &encrypt_pubkey_file));
|
||||
items_objects.push(parse_oss_backupd_config_item(&items[i], &root_oss_config_object, &encrypt_pubkey_file, &backup_count));
|
||||
}
|
||||
}
|
||||
|
||||
OSSBackupdConfig {
|
||||
oss_config: root_oss_config_object,
|
||||
prefix: prefix,
|
||||
host: host,
|
||||
prefix,
|
||||
host,
|
||||
backup_count,
|
||||
items: items_objects,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object: &Option<OSSConfig>, root_encrypt_pubkey_file: &Option<String>) -> OSSBackupdConfigItem {
|
||||
fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object: &Option<OSSConfig>, root_encrypt_pubkey_file: &Option<String>, root_backup_count: &Option<u32>) -> OSSBackupdConfigItem {
|
||||
let target = get_string_value(item, "target");
|
||||
let file_name = get_string_value(item, "file_name");
|
||||
let mut backup_count = get_u32_value(item, "backup_count");
|
||||
let mut encrypt_pubkey_file = get_string_value(item, "encrypt_pubkey_file");
|
||||
let mut oss_config = parse_sub_oss_config(item);
|
||||
if root_oss_config_object.is_some() {
|
||||
@@ -181,12 +186,16 @@ fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object:
|
||||
if encrypt_pubkey_file.is_none() && root_encrypt_pubkey_file.is_some() {
|
||||
encrypt_pubkey_file = root_encrypt_pubkey_file.clone();
|
||||
}
|
||||
if backup_count.is_none() && root_backup_count.is_some() {
|
||||
backup_count = root_backup_count.clone();
|
||||
}
|
||||
|
||||
OSSBackupdConfigItem {
|
||||
target: target,
|
||||
file_name: file_name,
|
||||
oss_config: oss_config,
|
||||
encrypt_pubkey_file: encrypt_pubkey_file,
|
||||
target,
|
||||
file_name,
|
||||
oss_config,
|
||||
backup_count,
|
||||
encrypt_pubkey_file,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,6 +226,15 @@ fn get_string_value(json: &json::JsonValue, key: &str) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_u32_value(json: &json::JsonValue, key: &str) -> Option<u32> {
|
||||
let value = &json[key];
|
||||
match value {
|
||||
json::JsonValue::String(v) => v.parse().ok(),
|
||||
json::JsonValue::Number(v) => Some(f64::from(*v) as u32),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_json(custom_oss_backupd_config: Option<&str>, verbose: bool) -> Option<json::JsonValue> {
|
||||
let config_content = get_config_content(custom_oss_backupd_config, verbose)?;
|
||||
match json::parse(&config_content) {
|
||||
|
||||
11
src/main.rs
11
src/main.rs
@@ -104,12 +104,17 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
}
|
||||
|
||||
let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret);
|
||||
let default_limit = 10_usize; // TODO read from config!
|
||||
let mut backup_count = config_item.backup_count.unwrap_or(10u32) as usize;
|
||||
if backup_count < 1_usize {
|
||||
backup_count = 1_usize;
|
||||
} else if backup_count > 10000_usize {
|
||||
backup_count = 10000_usize;
|
||||
}
|
||||
let meta_file_name = &format!("{}/ossbackupd_meta_{}_{}.json", &oss_backupd_config.get_prefix(), &oss_backupd_config.get_host(), &config_item.get_file_name());
|
||||
let new_file = format!("{}/{}", path, config_item.make_oss_key(&oss_backupd_config, "gpg"));
|
||||
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit));
|
||||
print_message(MessageType::DEBUG, &format!("Backup count: {}", backup_count));
|
||||
print_message(MessageType::DEBUG, &format!("Meta file name: {}", meta_file_name));
|
||||
print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file));
|
||||
}
|
||||
@@ -179,7 +184,7 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name));
|
||||
}
|
||||
match process_oss_files(&options, &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, backup_count) {
|
||||
Err(e) => {
|
||||
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user