diff --git a/Cargo.toml b/Cargo.toml index 7e32ff1..5ebd6d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ base64 = "0.11.0" reqwest = "0.9.22" sequoia-openpgp = "0.12.0" oss-rust-sdk = "0.1.12" +chrono = "0.4.10" rust_util = { git = "https://github.com/jht5945/rust_util" } diff --git a/oss-backupd-config.json b/oss-backupd-config.json new file mode 100644 index 0000000..37ca66d --- /dev/null +++ b/oss-backupd-config.json @@ -0,0 +1,18 @@ +{ + "oss_config": { + "endpoint": "", + "access_key_id": "", + "access_key_secret": "", + "bucket": "", + "path": "" + }, + "host": "sample_host", + "encrypt_pubkey_file": "", + "items": [ + { + "oss_config": null, + "target": "", + "file_name": "sample_file", + } + ] +} \ No newline at end of file diff --git a/src/config_util.rs b/src/config_util.rs index ca432ec..7fd88d3 100644 --- a/src/config_util.rs +++ b/src/config_util.rs @@ -7,6 +7,10 @@ use rust_util::{ new_box_ioerror, util_msg::*, }; +use chrono::{ + prelude::*, + Utc, +}; pub const ETC_OSS_BACKUPD_CONFIG: &str = "/etc/oss-backupd/config.json"; pub const OSS_BACKUPD_CONFIG: &str = "oss-backupd-config.json"; @@ -21,12 +25,14 @@ pub const DOT_OSS_BACKUPD_CONFIG: &str = ".oss-backupd-config.json"; "bucket": "", "path": "" }, - encrypt_pubkey_file: "", + "host": "", + "encrypt_pubkey_file": "", "items": [ { "oss_config": null, "target": "", - "encrypt_pubkey_file": "" + "file_name": "", + "encrypt_pubkey_file": null } ] } @@ -42,18 +48,64 @@ pub struct OSSConfig { pub struct OSSBackupdConfigItem { pub target: Option, + pub file_name: Option, pub oss_config: Option, pub encrypt_pubkey_file: Option, } pub struct OSSBackupdConfig { pub oss_config: Option, + pub prefix: Option, + pub host: Option, pub items: Vec, } +pub fn make_oss_key(oss_backupd_config: &OSSBackupdConfig, oss_backupd_config_item: &OSSBackupdConfigItem, suffix: &str) -> String { + let mut key = String::with_capacity(1024); + key.push_str(&(if oss_backupd_config.prefix.is_some() { + remove_start_end_slash(&oss_backupd_config.prefix.as_ref().unwrap().as_str()) + } else { + "default_oss_backupd".to_string() + })); + key.push_str("/"); + key.push_str(&(if oss_backupd_config.host.is_some() { + remove_start_end_slash(&oss_backupd_config.host.as_ref().unwrap().as_str()) + } else { + "default_host".to_string() + })); + key.push_str("/"); + key.push_str(if oss_backupd_config_item.file_name.is_some() { + oss_backupd_config_item.file_name.as_ref().unwrap().as_str() + } else { + "default_file_name" + }); + key.push_str("_"); + let ymdhms = Utc::now().format("%Y%m%d_%H%M%S").to_string(); + key.push_str(&ymdhms); + + if suffix != "" { + key.push_str(&format!(".{}", suffix)); + } + + key +} + +pub fn remove_start_end_slash(s: &str) -> String { + let mut ss = s; + while ss.starts_with("/") { + ss = &ss[1..] + } + while ss.ends_with("/") { + ss = &ss[0..(ss.len() - 1)]; + } + ss.to_string() +} + pub fn parse_config(config_json: &json::JsonValue) -> OSSBackupdConfig { let root_oss_config_object = parse_sub_oss_config(config_json); 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 items = &config_json["items"]; let mut items_objects: Vec = vec![]; if items.is_array() { @@ -64,12 +116,15 @@ pub fn parse_config(config_json: &json::JsonValue) -> OSSBackupdConfig { OSSBackupdConfig { oss_config: root_oss_config_object, + prefix: prefix, + host: host, items: items_objects, } } pub fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object: &Option, root_encrypt_pubkey_file: &Option) -> OSSBackupdConfigItem { let target = get_string_value(item, "target"); + let file_name = get_string_value(item, "file_name"); let mut encrypt_pubkey_file = get_string_value(item, "encrypt_pubkey_file"); let mut oss_config = parse_sub_oss_config(item); if oss_config.is_some() && root_oss_config_object.is_some() { @@ -100,6 +155,7 @@ pub fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_obj OSSBackupdConfigItem { target: target, + file_name: file_name, oss_config: oss_config, encrypt_pubkey_file: encrypt_pubkey_file, }