add zip, pgp and put file
This commit is contained in:
@@ -12,6 +12,10 @@
|
|||||||
{
|
{
|
||||||
"target": "",
|
"target": "",
|
||||||
"file_name": "sample_file"
|
"file_name": "sample_file"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"target": "",
|
||||||
|
"file_name": "sample_file_2"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -28,10 +28,10 @@ pub const DOT_OSS_BACKUPD_CONFIG: &str = ".oss-backupd-config.json";
|
|||||||
"encrypt_pubkey_file": "",
|
"encrypt_pubkey_file": "",
|
||||||
"items": [
|
"items": [
|
||||||
{
|
{
|
||||||
"oss_config": null,
|
"oss_config": null, // OPT @oss_config
|
||||||
"target": "",
|
"target": "",
|
||||||
"file_name": "",
|
"file_name": "",
|
||||||
"encrypt_pubkey_file": null
|
"encrypt_pubkey_file": null // OPT @encrypt_pubkey_file
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
123
src/main.rs
123
src/main.rs
@@ -8,6 +8,7 @@ pub mod zip_util;
|
|||||||
pub mod opt;
|
pub mod opt;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fs::{self, File},
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
use rust_util::{
|
use rust_util::{
|
||||||
@@ -17,7 +18,7 @@ use rust_util::{
|
|||||||
};
|
};
|
||||||
use oss_util::*;
|
use oss_util::*;
|
||||||
use config_util::*;
|
use config_util::*;
|
||||||
// use pgp_util::OpenPGPTool;
|
use pgp_util::OpenPGPTool;
|
||||||
use opt::{
|
use opt::{
|
||||||
Options,
|
Options,
|
||||||
};
|
};
|
||||||
@@ -60,11 +61,121 @@ fn main() -> XResult<()> {
|
|||||||
// TODO ...
|
// TODO ...
|
||||||
|
|
||||||
println!("{:?}", oss_backupd_config);
|
println!("{:?}", oss_backupd_config);
|
||||||
println!("");
|
let mut item_index = 0;
|
||||||
for i in &oss_backupd_config.items {
|
for config_item in &oss_backupd_config.items {
|
||||||
println!("{:?}", i);
|
println!(">>>>>> index: {}, {:?}", item_index, config_item);
|
||||||
println!("{}", i.make_oss_key(&oss_backupd_config, "gpg"));
|
println!("______ {}", config_item.make_oss_key(&oss_backupd_config, "gpg"));
|
||||||
println!("{}", i.make_oss_key(&oss_backupd_config, "asc"));
|
// println!("{}", config_item.make_oss_key(&oss_backupd_config, "asc"));
|
||||||
|
|
||||||
|
let encrypt_pubkey_file = match &config_item.encrypt_pubkey_file {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
let target = match &config_item.target {
|
||||||
|
None => {
|
||||||
|
print_message(MessageType::ERROR, &format!("Config target not found, at item index: {}", item_index));
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Some(target) => target,
|
||||||
|
};
|
||||||
|
|
||||||
|
let oss_config = match &config_item.oss_config {
|
||||||
|
None => {
|
||||||
|
print_message(MessageType::ERROR, &format!("Config oss_config not found, at item index: {}", item_index));
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
let access_key_id = match &oss_config.access_key_id {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
let access_key_secret = match &oss_config.access_key_secret {
|
||||||
|
None => {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
let path = &match &oss_config.path {
|
||||||
|
None => format!("default_path_at_{}", item_index),
|
||||||
|
Some(path) => path.to_owned(),
|
||||||
|
};
|
||||||
|
let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret);
|
||||||
|
let default_limit = 10_usize; // TODO ...
|
||||||
|
let meta_file_name = &format!("ossbackupd_meta_{}.json", path);
|
||||||
|
let new_file = config_item.make_oss_key(&oss_backupd_config, "gpg");
|
||||||
|
|
||||||
|
let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) {
|
||||||
|
Err(e) => {
|
||||||
|
print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index));
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Ok(open_pgp_tool) => open_pgp_tool,
|
||||||
|
};
|
||||||
|
|
||||||
|
let temp_zip_file = &format!("temp_file_{}.zip", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||||
|
let temp_pgp_file = &format!("temp_file_{}.gpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||||
|
print_message(MessageType::INFO, &format!("Compress 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));
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
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));
|
||||||
|
fs::remove_file(temp_zip_file).ok();
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_temp_pgp_file = match File::open(temp_pgp_file) {
|
||||||
|
Err(e) => {
|
||||||
|
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_pgp_file).ok();
|
||||||
|
continue;},
|
||||||
|
Ok(file_temp_pgp_file) => file_temp_pgp_file,
|
||||||
|
};
|
||||||
|
if let Err(e) = oss_client.put_file(bucket, &new_file, 1000, file_temp_pgp_file) {
|
||||||
|
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_pgp_file).ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO ...
|
||||||
|
match process_oss_files(&oss_client, bucket, path, meta_file_name, &new_file, default_limit) {
|
||||||
|
Err(e) => {
|
||||||
|
print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index));
|
||||||
|
},
|
||||||
|
Ok(_) => {
|
||||||
|
print_message(MessageType::ERROR, &format!("Success, at item index: {}", item_index));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
fs::remove_file(temp_zip_file).ok();
|
||||||
|
fs::remove_file(temp_pgp_file).ok();
|
||||||
|
|
||||||
|
item_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user