now can run
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
sample.gpg
|
||||||
|
sample-backupd-config.json
|
||||||
# ---> macOS
|
# ---> macOS
|
||||||
# General
|
# General
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
BIN
sample.gpg
BIN
sample.gpg
Binary file not shown.
@@ -62,37 +62,49 @@ pub struct OSSBackupdConfig {
|
|||||||
pub items: Vec<OSSBackupdConfigItem>,
|
pub items: Vec<OSSBackupdConfigItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OSSBackupdConfig {
|
||||||
|
|
||||||
|
pub fn get_host(&self) -> String {
|
||||||
|
match &self.host {
|
||||||
|
Some(h) => remove_start_end_slash(&h),
|
||||||
|
None => "default_host".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_prefix(&self) -> String {
|
||||||
|
match &self.prefix {
|
||||||
|
Some(p) => remove_start_end_slash(&p),
|
||||||
|
None => "default_oss_backupd".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl OSSBackupdConfigItem {
|
impl OSSBackupdConfigItem {
|
||||||
|
|
||||||
pub fn make_oss_key(&self, oss_backupd_config: &OSSBackupdConfig, suffix: &str) -> String {
|
pub fn make_oss_key(&self, oss_backupd_config: &OSSBackupdConfig, suffix: &str) -> String {
|
||||||
real_make_oss_key(oss_backupd_config, &self, suffix)
|
real_make_oss_key(oss_backupd_config, &self, suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_file_name(&self) -> String {
|
||||||
|
match &self.file_name {
|
||||||
|
Some(f) => f.clone(),
|
||||||
|
None => "default_file_name".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn real_make_oss_key(oss_backupd_config: &OSSBackupdConfig, oss_backupd_config_item: &OSSBackupdConfigItem, suffix: &str) -> String {
|
fn real_make_oss_key(oss_backupd_config: &OSSBackupdConfig, oss_backupd_config_item: &OSSBackupdConfigItem, suffix: &str) -> String {
|
||||||
let mut key = String::with_capacity(1024);
|
let mut key = String::with_capacity(1024);
|
||||||
key.push_str(&(if oss_backupd_config.prefix.is_some() {
|
key.push_str(&oss_backupd_config.get_prefix());
|
||||||
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("/");
|
||||||
key.push_str(&(if oss_backupd_config.host.is_some() {
|
key.push_str(&oss_backupd_config.get_host());
|
||||||
remove_start_end_slash(&oss_backupd_config.host.as_ref().unwrap().as_str())
|
|
||||||
} else {
|
|
||||||
"default_host".to_string()
|
|
||||||
}));
|
|
||||||
key.push_str("/");
|
key.push_str("/");
|
||||||
key.push_str(if oss_backupd_config_item.file_name.is_some() {
|
key.push_str(&oss_backupd_config_item.get_file_name());
|
||||||
oss_backupd_config_item.file_name.as_ref().unwrap().as_str()
|
|
||||||
} else {
|
|
||||||
"default_file_name"
|
|
||||||
});
|
|
||||||
key.push_str("_");
|
key.push_str("_");
|
||||||
let ymdhms = Utc::now().format("%Y%m%d_%H%M%S").to_string();
|
let ymdhms = Utc::now().format("%Y%m%d_%H%M%S").to_string();
|
||||||
key.push_str(&ymdhms);
|
key.push_str(&ymdhms);
|
||||||
|
|
||||||
if suffix != "" {
|
if !suffix.is_empty() {
|
||||||
key.push_str(&format!(".{}", suffix));
|
key.push_str(&format!(".{}", suffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
src/main.rs
21
src/main.rs
@@ -28,7 +28,7 @@ use opt::{
|
|||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
let options = Options::new_and_parse_args()?;
|
let options = Options::new_and_parse_args()?;
|
||||||
if options.version {
|
if options.version {
|
||||||
print_message(MessageType::INFO, "ossbackupd v0.1");
|
print_message(MessageType::INFO, "ossbackupd v0.1.0");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ fn main() -> XResult<()> {
|
|||||||
let mut item_index = -1;
|
let mut item_index = -1;
|
||||||
for config_item in &oss_backupd_config.items {
|
for config_item in &oss_backupd_config.items {
|
||||||
item_index += 1;
|
item_index += 1;
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
@@ -133,9 +133,9 @@ fn main() -> XResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret);
|
let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret);
|
||||||
let default_limit = 10_usize;
|
let default_limit = 10_usize; // TODO read from config!
|
||||||
let meta_file_name = &format!("ossbackupd_meta_{}.json", path);
|
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 = config_item.make_oss_key(&oss_backupd_config, "gpg");
|
let new_file = format!("{}/{}", path, config_item.make_oss_key(&oss_backupd_config, "gpg"));
|
||||||
|
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit));
|
print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit));
|
||||||
@@ -197,16 +197,14 @@ fn main() -> XResult<()> {
|
|||||||
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
||||||
},
|
},
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
print_message(MessageType::ERROR, &format!("Success, at item index: {}", item_index));
|
print_message(MessageType::OK, &format!("Success, at item index: {}", 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.verbose {
|
print_message(MessageType::OK, "Backup all file(s) finished!");
|
||||||
print_message(MessageType::DEBUG, "Backup all file(s) finished!");
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,9 +227,8 @@ pub fn process_oss_files(oss_client: &OSSClient, bucket_name: &str, path: &str,
|
|||||||
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)?;
|
||||||
if !removed_file.is_empty() {
|
if !removed_file.is_empty() {
|
||||||
for rm_file in removed_file {
|
for rm_file in removed_file {
|
||||||
let rm_file_key = &format!("{}/{}", path, rm_file);
|
print_message(MessageType::INFO, &format!("Remove OSS key: {}", &rm_file));
|
||||||
print_message(MessageType::INFO, &format!("Remove OSS key: {}", rm_file_key));
|
oss_client.delete_file(bucket_name, &rm_file)?;
|
||||||
oss_client.delete_file(bucket_name, rm_file_key)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user