add make_oss_key, oss-backupd-config.json

This commit is contained in:
2019-11-30 10:17:44 +08:00
parent 7282c547a5
commit da4e6397d2
3 changed files with 77 additions and 2 deletions

View File

@@ -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" }

18
oss-backupd-config.json Normal file
View File

@@ -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",
}
]
}

View File

@@ -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<String>,
pub file_name: Option<String>,
pub oss_config: Option<OSSConfig>,
pub encrypt_pubkey_file: Option<String>,
}
pub struct OSSBackupdConfig {
pub oss_config: Option<OSSConfig>,
pub prefix: Option<String>,
pub host: Option<String>,
pub items: Vec<OSSBackupdConfigItem>,
}
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<OSSBackupdConfigItem> = 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<OSSConfig>, root_encrypt_pubkey_file: &Option<String>) -> 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,
}