2018-12-14 07:28:56 +00:00
|
|
|
use failure::*;
|
2019-09-19 14:33:09 +00:00
|
|
|
use nix::unistd::{fork, ForkResult, pipe};
|
|
|
|
use std::os::unix::io::RawFd;
|
2019-07-22 08:12:51 +00:00
|
|
|
use chrono::{Local, Utc, TimeZone};
|
2019-03-08 15:55:54 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-07-24 05:48:59 +00:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2019-09-19 14:33:09 +00:00
|
|
|
use std::ffi::OsStr;
|
2019-11-08 12:13:39 +00:00
|
|
|
use std::io::{Write, Seek, SeekFrom};
|
2019-08-09 07:46:49 +00:00
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
use proxmox::{sortable, identity};
|
2019-08-03 11:05:38 +00:00
|
|
|
use proxmox::tools::fs::{file_get_contents, file_get_json, file_set_contents, image_size};
|
2019-11-21 13:14:54 +00:00
|
|
|
use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment};
|
|
|
|
use proxmox::api::schema::*;
|
2019-12-02 10:56:29 +00:00
|
|
|
use proxmox::api::cli::*;
|
2019-12-10 12:43:53 +00:00
|
|
|
use proxmox::api::api;
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2018-12-20 09:32:49 +00:00
|
|
|
use proxmox_backup::tools;
|
2019-07-26 07:07:29 +00:00
|
|
|
use proxmox_backup::api2::types::*;
|
2019-02-14 10:11:39 +00:00
|
|
|
use proxmox_backup::client::*;
|
2019-02-19 14:19:12 +00:00
|
|
|
use proxmox_backup::backup::*;
|
2019-08-09 09:03:24 +00:00
|
|
|
use proxmox_backup::pxar::{ self, catalog::* };
|
2019-07-03 13:45:12 +00:00
|
|
|
|
2018-12-20 09:32:49 +00:00
|
|
|
//use proxmox_backup::backup::image_index::*;
|
|
|
|
//use proxmox_backup::config::datastore;
|
2019-03-14 09:54:09 +00:00
|
|
|
//use proxmox_backup::pxar::encoder::*;
|
2019-01-18 15:50:15 +00:00
|
|
|
//use proxmox_backup::backup::datastore::*;
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-03-03 07:51:37 +00:00
|
|
|
use serde_json::{json, Value};
|
2019-06-03 04:43:47 +00:00
|
|
|
//use hyper::Body;
|
2019-08-09 07:46:49 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-11-21 08:36:41 +00:00
|
|
|
//use regex::Regex;
|
2019-03-13 08:47:12 +00:00
|
|
|
use xdg::BaseDirectories;
|
2019-02-26 11:27:28 +00:00
|
|
|
|
2019-04-28 08:55:03 +00:00
|
|
|
use futures::*;
|
2019-05-28 08:12:44 +00:00
|
|
|
use tokio::sync::mpsc;
|
2019-02-26 11:27:28 +00:00
|
|
|
|
2019-11-21 13:14:54 +00:00
|
|
|
proxmox::api::const_regex! {
|
2019-11-21 08:36:41 +00:00
|
|
|
BACKUPSPEC_REGEX = r"^([a-zA-Z0-9_-]+\.(?:pxar|img|conf|log)):(.+)$";
|
2019-02-26 11:27:28 +00:00
|
|
|
}
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
const REPO_URL_SCHEMA: Schema = StringSchema::new("Repository URL.")
|
|
|
|
.format(&BACKUP_REPO_URL)
|
|
|
|
.max_length(256)
|
|
|
|
.schema();
|
2019-03-13 08:47:12 +00:00
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
fn get_default_repository() -> Option<String> {
|
|
|
|
std::env::var("PBS_REPOSITORY").ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_repository_from_value(
|
|
|
|
param: &Value,
|
|
|
|
) -> Result<BackupRepository, Error> {
|
|
|
|
|
|
|
|
let repo_url = param["repository"]
|
|
|
|
.as_str()
|
|
|
|
.map(String::from)
|
|
|
|
.or_else(get_default_repository)
|
|
|
|
.ok_or_else(|| format_err!("unable to get (default) repository"))?;
|
|
|
|
|
|
|
|
let repo: BackupRepository = repo_url.parse()?;
|
|
|
|
|
|
|
|
Ok(repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_repository_from_map(
|
|
|
|
param: &HashMap<String, String>,
|
|
|
|
) -> Option<BackupRepository> {
|
|
|
|
|
|
|
|
param.get("repository")
|
|
|
|
.map(String::from)
|
|
|
|
.or_else(get_default_repository)
|
|
|
|
.and_then(|repo_url| repo_url.parse::<BackupRepository>().ok())
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
fn record_repository(repo: &BackupRepository) {
|
|
|
|
|
|
|
|
let base = match BaseDirectories::with_prefix("proxmox-backup") {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// usually $HOME/.cache/proxmox-backup/repo-list
|
|
|
|
let path = match base.place_cache_file("repo-list") {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let mut data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
|
2019-03-13 08:47:12 +00:00
|
|
|
|
|
|
|
let repo = repo.to_string();
|
|
|
|
|
|
|
|
data[&repo] = json!{ data[&repo].as_i64().unwrap_or(0) + 1 };
|
|
|
|
|
|
|
|
let mut map = serde_json::map::Map::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut max_used = 0;
|
|
|
|
let mut max_repo = None;
|
|
|
|
for (repo, count) in data.as_object().unwrap() {
|
|
|
|
if map.contains_key(repo) { continue; }
|
|
|
|
if let Some(count) = count.as_i64() {
|
|
|
|
if count > max_used {
|
|
|
|
max_used = count;
|
|
|
|
max_repo = Some(repo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(repo) = max_repo {
|
|
|
|
map.insert(repo.to_owned(), json!(max_used));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if map.len() > 10 { // store max. 10 repos
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_data = json!(map);
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let _ = file_set_contents(path, new_data.to_string().as_bytes(), None);
|
2019-03-13 08:47:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 11:26:01 +00:00
|
|
|
fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2019-03-13 08:47:12 +00:00
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let base = match BaseDirectories::with_prefix("proxmox-backup") {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
|
|
|
// usually $HOME/.cache/proxmox-backup/repo-list
|
|
|
|
let path = match base.place_cache_file("repo-list") {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
|
2019-03-13 08:47:12 +00:00
|
|
|
|
|
|
|
if let Some(map) = data.as_object() {
|
2019-03-13 11:26:01 +00:00
|
|
|
for (repo, _count) in map {
|
2019-03-13 08:47:12 +00:00
|
|
|
result.push(repo.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:00:22 +00:00
|
|
|
async fn view_task_result(
|
|
|
|
client: HttpClient,
|
|
|
|
result: Value,
|
|
|
|
output_format: &str,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let data = &result["data"];
|
|
|
|
if output_format == "text" {
|
|
|
|
if let Some(upid) = data.as_str() {
|
|
|
|
display_task_log(client, upid, true).await?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format_and_print_result(&data, &output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async fn backup_directory<P: AsRef<Path>>(
|
2019-10-12 11:53:11 +00:00
|
|
|
client: &BackupWriter,
|
2019-03-01 08:35:41 +00:00
|
|
|
dir_path: P,
|
2019-02-19 14:19:12 +00:00
|
|
|
archive_name: &str,
|
2019-05-30 11:28:24 +00:00
|
|
|
chunk_size: Option<usize>,
|
2019-07-24 05:48:59 +00:00
|
|
|
device_set: Option<HashSet<u64>>,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-07-24 10:21:25 +00:00
|
|
|
skip_lost_and_found: bool,
|
2019-06-13 09:47:23 +00:00
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
2019-11-08 09:35:48 +00:00
|
|
|
catalog: Arc<Mutex<CatalogWriter<SenderWriter>>>,
|
2019-08-01 10:39:02 +00:00
|
|
|
) -> Result<BackupStats, Error> {
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-08-09 07:46:49 +00:00
|
|
|
let pxar_stream = PxarBackupStream::open(dir_path.as_ref(), device_set, verbose, skip_lost_and_found, catalog)?;
|
2019-08-28 15:20:32 +00:00
|
|
|
let mut chunk_stream = ChunkStream::new(pxar_stream, chunk_size);
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let (mut tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks
|
2019-01-02 10:02:56 +00:00
|
|
|
|
2019-05-28 08:12:44 +00:00
|
|
|
let stream = rx
|
2019-08-28 15:20:32 +00:00
|
|
|
.map_err(Error::from);
|
2019-03-01 08:35:41 +00:00
|
|
|
|
2019-05-28 08:12:44 +00:00
|
|
|
// spawn chunker inside a separate task so that it can run parallel
|
2019-08-28 15:20:32 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let _ = tx.send_all(&mut chunk_stream).await;
|
|
|
|
});
|
2019-03-01 08:35:41 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let stats = client
|
|
|
|
.upload_stream(archive_name, stream, "dynamic", None, crypt_config)
|
|
|
|
.await?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-08-01 10:39:02 +00:00
|
|
|
Ok(stats)
|
2018-12-27 09:11:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async fn backup_image<P: AsRef<Path>>(
|
2019-10-12 11:53:11 +00:00
|
|
|
client: &BackupWriter,
|
2019-05-30 10:46:01 +00:00
|
|
|
image_path: P,
|
|
|
|
archive_name: &str,
|
|
|
|
image_size: u64,
|
2019-05-30 11:28:24 +00:00
|
|
|
chunk_size: Option<usize>,
|
2019-06-03 04:43:47 +00:00
|
|
|
_verbose: bool,
|
2019-06-13 09:47:23 +00:00
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
2019-08-01 10:39:02 +00:00
|
|
|
) -> Result<BackupStats, Error> {
|
2019-05-30 10:46:01 +00:00
|
|
|
|
|
|
|
let path = image_path.as_ref().to_owned();
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let file = tokio::fs::File::open(path).await?;
|
2019-05-30 10:46:01 +00:00
|
|
|
|
|
|
|
let stream = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new())
|
|
|
|
.map_err(Error::from);
|
|
|
|
|
2019-05-30 11:28:24 +00:00
|
|
|
let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024));
|
2019-05-30 10:46:01 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let stats = client
|
|
|
|
.upload_stream(archive_name, stream, "fixed", Some(image_size), crypt_config)
|
|
|
|
.await?;
|
2019-05-30 10:46:01 +00:00
|
|
|
|
2019-08-01 10:39:02 +00:00
|
|
|
Ok(stats)
|
2019-05-30 10:46:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 09:23:53 +00:00
|
|
|
fn strip_server_file_expenstion(name: &str) -> String {
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
|
|
|
|
name[..name.len()-5].to_owned()
|
2019-07-31 09:23:53 +00:00
|
|
|
} else {
|
2019-10-25 16:04:37 +00:00
|
|
|
name.to_owned() // should not happen
|
2019-03-11 09:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 10:29:05 +00:00
|
|
|
fn list_backup_groups(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-03-02 10:29:05 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let mut result = async_main(async move {
|
|
|
|
client.get(&path, None).await
|
|
|
|
})?;
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
2019-03-02 10:29:05 +00:00
|
|
|
// fixme: implement and use output formatter instead ..
|
2019-03-06 06:20:51 +00:00
|
|
|
let list = result["data"].as_array_mut().unwrap();
|
|
|
|
|
|
|
|
list.sort_unstable_by(|a, b| {
|
|
|
|
let a_id = a["backup-id"].as_str().unwrap();
|
|
|
|
let a_backup_type = a["backup-type"].as_str().unwrap();
|
|
|
|
let b_id = b["backup-id"].as_str().unwrap();
|
|
|
|
let b_backup_type = b["backup-type"].as_str().unwrap();
|
|
|
|
|
|
|
|
let type_order = a_backup_type.cmp(b_backup_type);
|
|
|
|
if type_order == std::cmp::Ordering::Equal {
|
|
|
|
a_id.cmp(b_id)
|
|
|
|
} else {
|
|
|
|
type_order
|
|
|
|
}
|
|
|
|
});
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
2019-03-02 10:29:05 +00:00
|
|
|
for item in list {
|
|
|
|
|
2019-03-02 15:28:36 +00:00
|
|
|
let id = item["backup-id"].as_str().unwrap();
|
|
|
|
let btype = item["backup-type"].as_str().unwrap();
|
|
|
|
let epoch = item["last-backup"].as_i64().unwrap();
|
2019-07-22 08:12:51 +00:00
|
|
|
let last_backup = Utc.timestamp(epoch, 0);
|
2019-03-02 15:28:36 +00:00
|
|
|
let backup_count = item["backup-count"].as_u64().unwrap();
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-03-04 12:38:23 +00:00
|
|
|
let group = BackupGroup::new(btype, id);
|
2019-03-02 10:29:05 +00:00
|
|
|
|
|
|
|
let path = group.group_path().to_str().unwrap().to_owned();
|
2019-03-02 15:28:36 +00:00
|
|
|
|
2019-07-31 09:23:53 +00:00
|
|
|
let files = item["files"].as_array().unwrap().iter()
|
|
|
|
.map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect();
|
2019-03-02 15:28:36 +00:00
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
if output_format == "text" {
|
2019-07-22 08:12:51 +00:00
|
|
|
println!(
|
|
|
|
"{:20} | {} | {:5} | {}",
|
|
|
|
path,
|
|
|
|
BackupDir::backup_time_to_string(last_backup),
|
|
|
|
backup_count,
|
|
|
|
tools::join(&files, ' '),
|
|
|
|
);
|
2019-07-16 11:35:25 +00:00
|
|
|
} else {
|
|
|
|
result.push(json!({
|
|
|
|
"backup-type": btype,
|
|
|
|
"backup-id": id,
|
|
|
|
"last-backup": epoch,
|
|
|
|
"backup-count": backup_count,
|
|
|
|
"files": files,
|
|
|
|
}));
|
|
|
|
}
|
2019-03-02 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 11:16:32 +00:00
|
|
|
if output_format != "text" { format_and_print_result(&result.into(), &output_format); }
|
2019-07-16 11:35:25 +00:00
|
|
|
|
2019-03-02 10:29:05 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
fn list_snapshots(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-03-02 15:20:50 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-05-13 07:12:03 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-07-18 07:11:43 +00:00
|
|
|
let mut args = json!({});
|
|
|
|
if let Some(path) = param["group"].as_str() {
|
|
|
|
let group = BackupGroup::parse(path)?;
|
|
|
|
args["backup-type"] = group.backup_type().into();
|
|
|
|
args["backup-id"] = group.backup_id().into();
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let result = async_main(async move {
|
|
|
|
client.get(&path, Some(args)).await
|
|
|
|
})?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
let list = result["data"].as_array().unwrap();
|
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
let mut result = vec![];
|
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
for item in list {
|
|
|
|
|
|
|
|
let id = item["backup-id"].as_str().unwrap();
|
|
|
|
let btype = item["backup-type"].as_str().unwrap();
|
|
|
|
let epoch = item["backup-time"].as_i64().unwrap();
|
|
|
|
|
2019-03-05 08:16:54 +00:00
|
|
|
let snapshot = BackupDir::new(btype, id, epoch);
|
2019-03-02 15:20:50 +00:00
|
|
|
|
|
|
|
let path = snapshot.relative_path().to_str().unwrap().to_owned();
|
|
|
|
|
2019-07-31 09:23:53 +00:00
|
|
|
let files = item["files"].as_array().unwrap().iter()
|
|
|
|
.map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect();
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
if output_format == "text" {
|
2019-08-06 08:56:21 +00:00
|
|
|
let size_str = if let Some(size) = item["size"].as_u64() {
|
|
|
|
size.to_string()
|
|
|
|
} else {
|
|
|
|
String::from("-")
|
|
|
|
};
|
|
|
|
println!("{} | {} | {}", path, size_str, tools::join(&files, ' '));
|
2019-07-16 11:35:25 +00:00
|
|
|
} else {
|
2019-08-06 08:56:21 +00:00
|
|
|
let mut data = json!({
|
2019-07-16 11:35:25 +00:00
|
|
|
"backup-type": btype,
|
|
|
|
"backup-id": id,
|
|
|
|
"backup-time": epoch,
|
|
|
|
"files": files,
|
2019-08-06 08:56:21 +00:00
|
|
|
});
|
|
|
|
if let Some(size) = item["size"].as_u64() {
|
|
|
|
data["size"] = size.into();
|
|
|
|
}
|
|
|
|
result.push(data);
|
2019-07-16 11:35:25 +00:00
|
|
|
}
|
2019-03-02 15:20:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 07:52:11 +00:00
|
|
|
if output_format != "text" { format_and_print_result(&result.into(), &output_format); }
|
2019-07-16 11:35:25 +00:00
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:29:00 +00:00
|
|
|
fn forget_snapshots(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-03-03 10:29:00 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-03-03 10:29:00 +00:00
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let mut client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2019-05-13 07:12:03 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let result = async_main(async move {
|
|
|
|
client.delete(&path, Some(json!({
|
|
|
|
"backup-type": snapshot.group().backup_type(),
|
|
|
|
"backup-id": snapshot.group().backup_id(),
|
|
|
|
"backup-time": snapshot.backup_time().timestamp(),
|
|
|
|
}))).await
|
|
|
|
})?;
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
2019-03-03 10:29:00 +00:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-08-10 07:12:17 +00:00
|
|
|
fn api_login(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async move { client.login().await })?;
|
2019-08-10 07:12:17 +00:00
|
|
|
|
|
|
|
record_repository(&repo);
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn api_logout(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
|
|
|
delete_ticket_info(repo.host(), repo.user())?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-08-09 09:34:47 +00:00
|
|
|
fn dump_catalog(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
2019-08-09 09:34:47 +00:00
|
|
|
|
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, _) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-08-16 07:19:01 +00:00
|
|
|
Some(Arc::new(CryptConfig::new(key)?))
|
2019-08-09 09:34:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-08-09 09:34:47 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async move {
|
2019-10-12 10:57:08 +00:00
|
|
|
let client = BackupReader::start(
|
|
|
|
client,
|
2019-10-12 13:50:26 +00:00
|
|
|
crypt_config.clone(),
|
2019-08-28 15:20:32 +00:00
|
|
|
repo.store(),
|
|
|
|
&snapshot.group().backup_type(),
|
|
|
|
&snapshot.group().backup_id(),
|
2019-10-12 10:57:08 +00:00
|
|
|
snapshot.backup_time(),
|
|
|
|
true,
|
|
|
|
).await?;
|
2019-08-09 09:34:47 +00:00
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
let manifest = client.download_manifest().await?;
|
2019-09-04 06:47:14 +00:00
|
|
|
|
2019-11-08 11:40:10 +00:00
|
|
|
let index = client.download_dynamic_index(&manifest, CATALOG_NAME).await?;
|
2019-11-08 09:35:48 +00:00
|
|
|
|
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
|
|
|
|
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
|
|
|
|
|
|
|
|
let mut reader = BufferedDynamicReader::new(index, chunk_reader);
|
|
|
|
|
|
|
|
let mut catalogfile = std::fs::OpenOptions::new()
|
2019-08-28 15:20:32 +00:00
|
|
|
.write(true)
|
2019-11-08 09:35:48 +00:00
|
|
|
.read(true)
|
2019-08-28 15:20:32 +00:00
|
|
|
.custom_flags(libc::O_TMPFILE)
|
|
|
|
.open("/tmp")?;
|
2019-08-09 09:34:47 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
std::io::copy(&mut reader, &mut catalogfile)
|
|
|
|
.map_err(|err| format_err!("unable to download catalog - {}", err))?;
|
2019-09-04 06:47:14 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
catalogfile.seek(SeekFrom::Start(0))?;
|
2019-08-14 13:07:28 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
let mut catalog_reader = CatalogReader::new(catalogfile);
|
2019-08-09 09:34:47 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
catalog_reader.dump()?;
|
2019-08-09 09:34:47 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
|
|
|
Ok::<(), Error>(())
|
|
|
|
})?;
|
2019-08-09 09:34:47 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-07-31 09:23:53 +00:00
|
|
|
fn list_snapshot_files(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
|
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-07-31 09:23:53 +00:00
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/files", repo.store());
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let mut result = async_main(async move {
|
|
|
|
client.get(&path, Some(json!({
|
|
|
|
"backup-type": snapshot.group().backup_type(),
|
|
|
|
"backup-id": snapshot.group().backup_id(),
|
|
|
|
"backup-time": snapshot.backup_time().timestamp(),
|
|
|
|
}))).await
|
|
|
|
})?;
|
2019-07-31 09:23:53 +00:00
|
|
|
|
|
|
|
record_repository(&repo);
|
|
|
|
|
2019-08-05 11:22:19 +00:00
|
|
|
let list: Value = result["data"].take();
|
2019-07-31 09:23:53 +00:00
|
|
|
|
|
|
|
if output_format == "text" {
|
2019-08-05 11:22:19 +00:00
|
|
|
for item in list.as_array().unwrap().iter() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
strip_server_file_expenstion(item["filename"].as_str().unwrap()),
|
|
|
|
item["size"].as_u64().unwrap_or(0),
|
|
|
|
);
|
2019-07-31 09:23:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-05 11:22:19 +00:00
|
|
|
format_and_print_result(&list, &output_format);
|
2019-07-31 09:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:10:45 +00:00
|
|
|
fn start_garbage_collection(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-02-20 13:10:45 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-12-09 12:29:23 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
2019-02-20 13:10:45 +00:00
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let mut client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-02-20 13:10:45 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/gc", repo.store());
|
2019-02-20 13:10:45 +00:00
|
|
|
|
2019-12-09 12:29:23 +00:00
|
|
|
async_main(async {
|
|
|
|
let result = client.post(&path, None).await?;
|
2019-02-20 13:10:45 +00:00
|
|
|
|
2019-12-09 12:29:23 +00:00
|
|
|
record_repository(&repo);
|
2019-03-13 08:47:12 +00:00
|
|
|
|
2019-12-09 17:00:22 +00:00
|
|
|
view_task_result(client, result, &output_format).await
|
2019-12-09 12:29:23 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
2019-02-20 13:10:45 +00:00
|
|
|
}
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
fn parse_backupspec(value: &str) -> Result<(&str, &str), Error> {
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
if let Some(caps) = (BACKUPSPEC_REGEX.regex_obj)().captures(value) {
|
2019-02-26 11:27:28 +00:00
|
|
|
return Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()));
|
|
|
|
}
|
|
|
|
bail!("unable to parse directory specification '{}'", value);
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
fn spawn_catalog_upload(
|
|
|
|
client: Arc<BackupWriter>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
Arc<Mutex<CatalogWriter<SenderWriter>>>,
|
|
|
|
tokio::sync::oneshot::Receiver<Result<BackupStats, Error>>
|
|
|
|
), Error>
|
|
|
|
{
|
|
|
|
let (catalog_tx, catalog_rx) = mpsc::channel(10); // allow to buffer 10 writes
|
|
|
|
let catalog_stream = catalog_rx.map_err(Error::from);
|
|
|
|
let catalog_chunk_size = 512*1024;
|
|
|
|
let catalog_chunk_stream = ChunkStream::new(catalog_stream, Some(catalog_chunk_size));
|
|
|
|
|
|
|
|
let catalog = Arc::new(Mutex::new(CatalogWriter::new(SenderWriter::new(catalog_tx))?));
|
|
|
|
|
|
|
|
let (catalog_result_tx, catalog_result_rx) = tokio::sync::oneshot::channel();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let catalog_upload_result = client
|
|
|
|
.upload_stream(CATALOG_NAME, catalog_chunk_stream, "dynamic", None, crypt_config)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(ref err) = catalog_upload_result {
|
|
|
|
eprintln!("catalog upload error - {}", err);
|
|
|
|
client.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = catalog_result_tx.send(catalog_upload_result);
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok((catalog, catalog_result_rx))
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn create_backup(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-02-26 11:27:28 +00:00
|
|
|
|
|
|
|
let backupspec_list = tools::required_array_param(¶m, "backupspec")?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-03-08 08:33:53 +00:00
|
|
|
let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-07-24 10:21:25 +00:00
|
|
|
let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-03-04 07:01:09 +00:00
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-07-25 11:44:01 +00:00
|
|
|
let backup_time_opt = param["backup-time"].as_i64();
|
|
|
|
|
2019-05-30 11:28:24 +00:00
|
|
|
let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize);
|
2018-12-21 10:18:08 +00:00
|
|
|
|
2019-02-19 14:19:12 +00:00
|
|
|
if let Some(size) = chunk_size_opt {
|
|
|
|
verify_chunk_size(size)?;
|
2018-12-21 10:18:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
2019-06-19 15:16:41 +00:00
|
|
|
|
2019-08-03 15:06:23 +00:00
|
|
|
let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename());
|
2019-03-06 06:02:52 +00:00
|
|
|
|
2019-07-26 07:07:29 +00:00
|
|
|
let backup_type = param["backup-type"].as_str().unwrap_or("host");
|
2019-07-25 11:44:01 +00:00
|
|
|
|
2019-07-24 05:48:59 +00:00
|
|
|
let include_dev = param["include-dev"].as_array();
|
|
|
|
|
|
|
|
let mut devices = if all_file_systems { None } else { Some(HashSet::new()) };
|
|
|
|
|
|
|
|
if let Some(include_dev) = include_dev {
|
|
|
|
if all_file_systems {
|
|
|
|
bail!("option 'all-file-systems' conflicts with option 'include-dev'");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
for path in include_dev {
|
|
|
|
let path = path.as_str().unwrap();
|
|
|
|
let stat = nix::sys::stat::stat(path)
|
|
|
|
.map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?;
|
|
|
|
set.insert(stat.st_dev);
|
|
|
|
}
|
|
|
|
devices = Some(set);
|
|
|
|
}
|
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
let mut upload_list = vec![];
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-07-24 11:07:02 +00:00
|
|
|
enum BackupType { PXAR, IMAGE, CONFIG, LOGFILE };
|
2019-05-30 10:46:01 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
let mut upload_catalog = false;
|
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
for backupspec in backupspec_list {
|
|
|
|
let (target, filename) = parse_backupspec(backupspec.as_str().unwrap())?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-05-30 10:19:23 +00:00
|
|
|
use std::os::unix::fs::FileTypeExt;
|
|
|
|
|
2019-09-03 14:17:23 +00:00
|
|
|
let metadata = std::fs::metadata(filename)
|
|
|
|
.map_err(|err| format_err!("unable to access '{}' - {}", filename, err))?;
|
2019-05-30 10:19:23 +00:00
|
|
|
let file_type = metadata.file_type();
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-08-07 08:10:14 +00:00
|
|
|
let extension = target.rsplit('.').next()
|
2019-10-25 16:04:37 +00:00
|
|
|
.ok_or_else(|| format_err!("missing target file extenion '{}'", target))?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-06-03 08:39:44 +00:00
|
|
|
match extension {
|
|
|
|
"pxar" => {
|
|
|
|
if !file_type.is_dir() {
|
|
|
|
bail!("got unexpected file type (expected directory)");
|
|
|
|
}
|
2019-08-07 08:10:14 +00:00
|
|
|
upload_list.push((BackupType::PXAR, filename.to_owned(), format!("{}.didx", target), 0));
|
2019-11-08 09:35:48 +00:00
|
|
|
upload_catalog = true;
|
2019-06-03 08:39:44 +00:00
|
|
|
}
|
|
|
|
"img" => {
|
2019-05-30 10:19:23 +00:00
|
|
|
|
2019-06-03 08:39:44 +00:00
|
|
|
if !(file_type.is_file() || file_type.is_block_device()) {
|
|
|
|
bail!("got unexpected file type (expected file or block device)");
|
|
|
|
}
|
2019-05-30 10:19:23 +00:00
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let size = image_size(&PathBuf::from(filename))?;
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-06-03 08:39:44 +00:00
|
|
|
if size == 0 { bail!("got zero-sized file '{}'", filename); }
|
2019-02-26 11:27:28 +00:00
|
|
|
|
2019-08-07 08:10:14 +00:00
|
|
|
upload_list.push((BackupType::IMAGE, filename.to_owned(), format!("{}.fidx", target), size));
|
2019-06-03 08:39:44 +00:00
|
|
|
}
|
|
|
|
"conf" => {
|
|
|
|
if !file_type.is_file() {
|
|
|
|
bail!("got unexpected file type (expected regular file)");
|
|
|
|
}
|
2019-08-07 08:10:14 +00:00
|
|
|
upload_list.push((BackupType::CONFIG, filename.to_owned(), format!("{}.blob", target), metadata.len()));
|
2019-06-03 08:39:44 +00:00
|
|
|
}
|
2019-07-24 11:07:02 +00:00
|
|
|
"log" => {
|
|
|
|
if !file_type.is_file() {
|
|
|
|
bail!("got unexpected file type (expected regular file)");
|
|
|
|
}
|
2019-08-07 08:10:14 +00:00
|
|
|
upload_list.push((BackupType::LOGFILE, filename.to_owned(), format!("{}.blob", target), metadata.len()));
|
2019-07-24 11:07:02 +00:00
|
|
|
}
|
2019-06-03 08:39:44 +00:00
|
|
|
_ => {
|
|
|
|
bail!("got unknown archive extension '{}'", extension);
|
|
|
|
}
|
2019-02-26 11:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0);
|
2019-02-26 11:27:28 +00:00
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-03-13 08:47:12 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
2019-07-25 11:44:01 +00:00
|
|
|
println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time));
|
|
|
|
|
2019-08-03 15:06:23 +00:00
|
|
|
println!("Client name: {}", proxmox::tools::nodename());
|
2019-07-25 11:44:01 +00:00
|
|
|
|
|
|
|
let start_time = Local::now();
|
|
|
|
|
2019-07-26 09:11:55 +00:00
|
|
|
println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
|
2019-03-01 05:48:41 +00:00
|
|
|
|
2019-06-26 05:32:34 +00:00
|
|
|
let (crypt_config, rsa_encrypted_key) = match keyfile {
|
|
|
|
None => (None, None),
|
2019-06-19 15:16:41 +00:00
|
|
|
Some(path) => {
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, created) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-06-26 05:32:34 +00:00
|
|
|
|
|
|
|
let crypt_config = CryptConfig::new(key)?;
|
|
|
|
|
|
|
|
let path = master_pubkey_path()?;
|
|
|
|
if path.exists() {
|
2019-08-03 11:05:38 +00:00
|
|
|
let pem_data = file_get_contents(&path)?;
|
2019-06-26 05:32:34 +00:00
|
|
|
let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
|
|
|
|
let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?;
|
|
|
|
(Some(Arc::new(crypt_config)), Some(enc_key))
|
|
|
|
} else {
|
|
|
|
(Some(Arc::new(crypt_config)), None)
|
|
|
|
}
|
2019-06-19 15:16:41 +00:00
|
|
|
}
|
|
|
|
};
|
2019-06-13 09:47:23 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async move {
|
2019-10-12 11:53:11 +00:00
|
|
|
let client = BackupWriter::start(
|
|
|
|
client,
|
|
|
|
repo.store(),
|
|
|
|
backup_type,
|
|
|
|
&backup_id,
|
|
|
|
backup_time,
|
|
|
|
verbose,
|
|
|
|
).await?;
|
2019-08-28 15:20:32 +00:00
|
|
|
|
2019-10-12 15:58:08 +00:00
|
|
|
let snapshot = BackupDir::new(backup_type, backup_id, backup_time.timestamp());
|
|
|
|
let mut manifest = BackupManifest::new(snapshot);
|
2019-08-28 15:20:32 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
let (catalog, catalog_result_rx) = spawn_catalog_upload(client.clone(), crypt_config.clone())?;
|
2019-08-28 15:20:32 +00:00
|
|
|
|
|
|
|
for (backup_type, filename, target, size) in upload_list {
|
|
|
|
match backup_type {
|
|
|
|
BackupType::CONFIG => {
|
|
|
|
println!("Upload config file '{}' to '{:?}' as {}", filename, repo, target);
|
|
|
|
let stats = client
|
|
|
|
.upload_blob_from_file(&filename, &target, crypt_config.clone(), true)
|
|
|
|
.await?;
|
2019-10-12 15:58:08 +00:00
|
|
|
manifest.add_file(target, stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
}
|
|
|
|
BackupType::LOGFILE => { // fixme: remove - not needed anymore ?
|
|
|
|
println!("Upload log file '{}' to '{:?}' as {}", filename, repo, target);
|
|
|
|
let stats = client
|
|
|
|
.upload_blob_from_file(&filename, &target, crypt_config.clone(), true)
|
|
|
|
.await?;
|
2019-10-12 15:58:08 +00:00
|
|
|
manifest.add_file(target, stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
}
|
|
|
|
BackupType::PXAR => {
|
|
|
|
println!("Upload directory '{}' to '{:?}' as {}", filename, repo, target);
|
|
|
|
catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?;
|
|
|
|
let stats = backup_directory(
|
|
|
|
&client,
|
|
|
|
&filename,
|
|
|
|
&target,
|
|
|
|
chunk_size_opt,
|
|
|
|
devices.clone(),
|
|
|
|
verbose,
|
|
|
|
skip_lost_and_found,
|
|
|
|
crypt_config.clone(),
|
|
|
|
catalog.clone(),
|
|
|
|
).await?;
|
2019-10-12 15:58:08 +00:00
|
|
|
manifest.add_file(target, stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
catalog.lock().unwrap().end_directory()?;
|
|
|
|
}
|
|
|
|
BackupType::IMAGE => {
|
|
|
|
println!("Upload image '{}' to '{:?}' as {}", filename, repo, target);
|
|
|
|
let stats = backup_image(
|
|
|
|
&client,
|
|
|
|
&filename,
|
|
|
|
&target,
|
|
|
|
size,
|
|
|
|
chunk_size_opt,
|
|
|
|
verbose,
|
|
|
|
crypt_config.clone(),
|
|
|
|
).await?;
|
2019-10-12 15:58:08 +00:00
|
|
|
manifest.add_file(target, stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
}
|
2019-05-30 10:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
// finalize and upload catalog
|
|
|
|
if upload_catalog {
|
|
|
|
let mutex = Arc::try_unwrap(catalog)
|
|
|
|
.map_err(|_| format_err!("unable to get catalog (still used)"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let mut catalog = mutex.into_inner().unwrap();
|
|
|
|
|
|
|
|
catalog.finish()?;
|
2019-08-09 07:46:49 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
drop(catalog); // close upload stream
|
2019-08-09 07:46:49 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
let stats = catalog_result_rx.await??;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
manifest.add_file(CATALOG_NAME.to_owned(), stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
}
|
2019-08-09 07:46:49 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
if let Some(rsa_encrypted_key) = rsa_encrypted_key {
|
|
|
|
let target = "rsa-encrypted.key";
|
|
|
|
println!("Upload RSA encoded key to '{:?}' as {}", repo, target);
|
|
|
|
let stats = client
|
|
|
|
.upload_blob_from_data(rsa_encrypted_key, target, None, false, false)
|
|
|
|
.await?;
|
2019-10-12 15:58:08 +00:00
|
|
|
manifest.add_file(format!("{}.blob", target), stats.size, stats.csum);
|
2019-08-28 15:20:32 +00:00
|
|
|
|
|
|
|
// openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t
|
|
|
|
/*
|
|
|
|
let mut buffer2 = vec![0u8; rsa.size() as usize];
|
|
|
|
let pem_data = file_get_contents("master-private.pem")?;
|
|
|
|
let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?;
|
|
|
|
let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?;
|
|
|
|
println!("TEST {} {:?}", len, buffer2);
|
|
|
|
*/
|
|
|
|
}
|
2019-06-24 11:56:37 +00:00
|
|
|
|
2019-10-12 15:58:08 +00:00
|
|
|
// create manifest (index.json)
|
|
|
|
let manifest = manifest.into_json();
|
2019-08-01 10:39:02 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
println!("Upload index.json to '{:?}'", repo);
|
2019-10-12 15:58:08 +00:00
|
|
|
let manifest = serde_json::to_string_pretty(&manifest)?.into();
|
2019-08-28 15:20:32 +00:00
|
|
|
client
|
2019-10-12 15:58:08 +00:00
|
|
|
.upload_blob_from_data(manifest, MANIFEST_BLOB_NAME, crypt_config.clone(), true, true)
|
2019-08-28 15:20:32 +00:00
|
|
|
.await?;
|
2019-08-01 10:39:02 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
client.finish().await?;
|
2019-05-28 08:12:44 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let end_time = Local::now();
|
|
|
|
let elapsed = end_time.signed_duration_since(start_time);
|
|
|
|
println!("Duration: {}", elapsed);
|
2019-03-05 07:11:40 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
})
|
2019-02-27 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-02-27 07:38:32 +00:00
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let data: Vec<&str> = arg.splitn(2, ':').collect();
|
|
|
|
|
2019-03-13 06:23:59 +00:00
|
|
|
if data.len() != 2 {
|
2019-03-14 09:54:09 +00:00
|
|
|
result.push(String::from("root.pxar:/"));
|
|
|
|
result.push(String::from("etc.pxar:/etc"));
|
2019-03-13 06:23:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-02-27 07:38:32 +00:00
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
let files = tools::complete_file_name(data[1], param);
|
2019-02-27 07:38:32 +00:00
|
|
|
|
|
|
|
for file in files {
|
|
|
|
result.push(format!("{}:{}", data[0], file));
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2018-12-14 07:28:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 09:50:46 +00:00
|
|
|
fn restore(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-03-06 09:50:46 +00:00
|
|
|
) -> Result<Value, Error> {
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(restore_do(param))
|
|
|
|
}
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-10-05 09:48:51 +00:00
|
|
|
fn dump_image<W: Write>(
|
|
|
|
client: Arc<BackupReader>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
|
|
|
index: FixedIndexReader,
|
|
|
|
mut writer: W,
|
2019-10-08 11:04:10 +00:00
|
|
|
verbose: bool,
|
2019-10-05 09:48:51 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
|
|
|
|
let mut chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
|
|
|
|
|
|
|
|
// Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy
|
|
|
|
// and thus slows down reading. Instead, directly use RemoteChunkReader
|
2019-10-08 11:04:10 +00:00
|
|
|
let mut per = 0;
|
|
|
|
let mut bytes = 0;
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
2019-10-05 09:48:51 +00:00
|
|
|
for pos in 0..index.index_count() {
|
|
|
|
let digest = index.index_digest(pos).unwrap();
|
|
|
|
let raw_data = chunk_reader.read_chunk(&digest)?;
|
|
|
|
writer.write_all(&raw_data)?;
|
2019-10-08 11:04:10 +00:00
|
|
|
bytes += raw_data.len();
|
|
|
|
if verbose {
|
|
|
|
let next_per = ((pos+1)*100)/index.index_count();
|
|
|
|
if per != next_per {
|
|
|
|
eprintln!("progress {}% (read {} bytes, duration {} sec)",
|
|
|
|
next_per, bytes, start_time.elapsed().as_secs());
|
|
|
|
per = next_per;
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 09:48:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 11:04:10 +00:00
|
|
|
let end_time = std::time::Instant::now();
|
|
|
|
let elapsed = end_time.duration_since(start_time);
|
|
|
|
eprintln!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)",
|
|
|
|
bytes,
|
|
|
|
elapsed.as_secs_f64(),
|
|
|
|
bytes as f64/(1024.0*1024.0*elapsed.as_secs_f64())
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2019-10-05 09:48:51 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async fn restore_do(param: Value) -> Result<Value, Error> {
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-07-03 13:45:12 +00:00
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-07-29 10:49:15 +00:00
|
|
|
let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-03-13 08:47:12 +00:00
|
|
|
|
|
|
|
record_repository(&repo);
|
2019-03-11 13:31:01 +00:00
|
|
|
|
2019-03-06 09:50:46 +00:00
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
|
2019-07-03 13:45:12 +00:00
|
|
|
let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
|
2019-03-11 13:31:01 +00:00
|
|
|
let group = BackupGroup::parse(path)?;
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-05-13 07:12:03 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
|
|
|
let result = client.get(&path, Some(json!({
|
2019-03-11 13:31:01 +00:00
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
2019-08-28 15:20:32 +00:00
|
|
|
}))).await?;
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let list = result["data"].as_array().unwrap();
|
2019-10-25 16:04:37 +00:00
|
|
|
if list.is_empty() {
|
2019-03-11 13:31:01 +00:00
|
|
|
bail!("backup group '{}' does not contain any snapshots:", path);
|
|
|
|
}
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-07-03 13:45:12 +00:00
|
|
|
let epoch = list[0]["backup-time"].as_i64().unwrap();
|
2019-07-22 08:12:51 +00:00
|
|
|
let backup_time = Utc.timestamp(epoch, 0);
|
2019-07-03 13:45:12 +00:00
|
|
|
(group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)
|
2019-03-11 13:31:01 +00:00
|
|
|
} else {
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
2019-07-03 13:45:12 +00:00
|
|
|
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
|
|
|
|
};
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let target = tools::required_string_param(¶m, "target")?;
|
2019-07-05 10:14:50 +00:00
|
|
|
let target = if target == "-" { None } else { Some(target) };
|
2019-03-06 10:18:46 +00:00
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
2019-03-06 10:18:46 +00:00
|
|
|
|
2019-07-03 13:45:12 +00:00
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, _) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-07-03 13:45:12 +00:00
|
|
|
Some(Arc::new(CryptConfig::new(key)?))
|
|
|
|
}
|
|
|
|
};
|
2019-03-11 13:31:01 +00:00
|
|
|
|
2019-07-05 07:19:56 +00:00
|
|
|
let server_archive_name = if archive_name.ends_with(".pxar") {
|
|
|
|
format!("{}.didx", archive_name)
|
|
|
|
} else if archive_name.ends_with(".img") {
|
|
|
|
format!("{}.fidx", archive_name)
|
|
|
|
} else {
|
2019-07-05 09:36:45 +00:00
|
|
|
format!("{}.blob", archive_name)
|
2019-07-05 07:19:56 +00:00
|
|
|
};
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-10-12 13:50:26 +00:00
|
|
|
let client = BackupReader::start(
|
|
|
|
client,
|
|
|
|
crypt_config.clone(),
|
|
|
|
repo.store(),
|
|
|
|
&backup_type,
|
|
|
|
&backup_id,
|
|
|
|
backup_time,
|
|
|
|
true,
|
|
|
|
).await?;
|
2019-07-03 13:45:12 +00:00
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
let manifest = client.download_manifest().await?;
|
2019-09-02 12:14:32 +00:00
|
|
|
|
2019-10-12 13:53:27 +00:00
|
|
|
if server_archive_name == MANIFEST_BLOB_NAME {
|
2019-10-13 08:09:12 +00:00
|
|
|
let backup_index_data = manifest.into_json().to_string();
|
2019-09-02 12:14:32 +00:00
|
|
|
if let Some(target) = target {
|
2019-10-12 13:50:26 +00:00
|
|
|
file_set_contents(target, backup_index_data.as_bytes(), None)?;
|
2019-09-02 12:14:32 +00:00
|
|
|
} else {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut writer = stdout.lock();
|
2019-10-12 13:50:26 +00:00
|
|
|
writer.write_all(backup_index_data.as_bytes())
|
2019-09-02 12:14:32 +00:00
|
|
|
.map_err(|err| format_err!("unable to pipe data - {}", err))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if server_archive_name.ends_with(".blob") {
|
2019-09-04 06:47:14 +00:00
|
|
|
|
2019-11-08 12:13:39 +00:00
|
|
|
let mut reader = client.download_blob(&manifest, &server_archive_name).await?;
|
2019-07-05 09:36:45 +00:00
|
|
|
|
2019-07-05 10:14:50 +00:00
|
|
|
if let Some(target) = target {
|
2019-09-02 12:13:31 +00:00
|
|
|
let mut writer = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(target)
|
|
|
|
.map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?;
|
|
|
|
std::io::copy(&mut reader, &mut writer)?;
|
2019-07-05 10:14:50 +00:00
|
|
|
} else {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut writer = stdout.lock();
|
2019-09-02 12:13:31 +00:00
|
|
|
std::io::copy(&mut reader, &mut writer)
|
2019-07-05 10:14:50 +00:00
|
|
|
.map_err(|err| format_err!("unable to pipe data - {}", err))?;
|
|
|
|
}
|
2019-07-05 09:36:45 +00:00
|
|
|
|
|
|
|
} else if server_archive_name.ends_with(".didx") {
|
2019-07-03 13:45:12 +00:00
|
|
|
|
2019-11-08 11:40:10 +00:00
|
|
|
let index = client.download_dynamic_index(&manifest, &server_archive_name).await?;
|
2019-09-03 11:12:16 +00:00
|
|
|
|
2019-07-05 08:42:46 +00:00
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
|
|
|
|
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
|
|
|
|
|
2019-07-05 07:19:56 +00:00
|
|
|
let mut reader = BufferedDynamicReader::new(index, chunk_reader);
|
2019-07-03 13:45:12 +00:00
|
|
|
|
2019-07-05 10:14:50 +00:00
|
|
|
if let Some(target) = target {
|
2019-07-03 13:45:12 +00:00
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
let feature_flags = pxar::flags::DEFAULT;
|
2019-11-26 08:56:48 +00:00
|
|
|
let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags);
|
|
|
|
decoder.set_callback(move |path| {
|
2019-07-05 10:14:50 +00:00
|
|
|
if verbose {
|
2019-10-08 11:04:10 +00:00
|
|
|
eprintln!("{:?}", path);
|
2019-07-05 10:14:50 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
2019-07-29 12:01:45 +00:00
|
|
|
decoder.set_allow_existing_dirs(allow_existing_dirs);
|
|
|
|
|
2019-07-16 16:19:44 +00:00
|
|
|
decoder.restore(Path::new(target), &Vec::new())?;
|
2019-07-05 10:14:50 +00:00
|
|
|
} else {
|
2019-10-05 09:48:51 +00:00
|
|
|
let mut writer = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.open("/dev/stdout")
|
|
|
|
.map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?;
|
2019-07-05 07:19:56 +00:00
|
|
|
|
2019-07-05 10:14:50 +00:00
|
|
|
std::io::copy(&mut reader, &mut writer)
|
|
|
|
.map_err(|err| format_err!("unable to pipe data - {}", err))?;
|
|
|
|
}
|
2019-07-05 07:19:56 +00:00
|
|
|
} else if server_archive_name.ends_with(".fidx") {
|
|
|
|
|
2019-11-08 11:43:56 +00:00
|
|
|
let index = client.download_fixed_index(&manifest, &server_archive_name).await?;
|
2019-09-03 11:12:16 +00:00
|
|
|
|
2019-10-05 09:48:51 +00:00
|
|
|
let mut writer = if let Some(target) = target {
|
|
|
|
std::fs::OpenOptions::new()
|
2019-07-05 10:14:50 +00:00
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(target)
|
2019-10-05 09:48:51 +00:00
|
|
|
.map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?
|
2019-07-05 10:14:50 +00:00
|
|
|
} else {
|
2019-10-05 09:48:51 +00:00
|
|
|
std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.open("/dev/stdout")
|
|
|
|
.map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?
|
|
|
|
};
|
2019-07-05 07:19:56 +00:00
|
|
|
|
2019-10-08 11:04:10 +00:00
|
|
|
dump_image(client.clone(), crypt_config.clone(), index, &mut writer, verbose)?;
|
2019-10-05 09:48:51 +00:00
|
|
|
|
|
|
|
} else {
|
2019-07-05 09:36:45 +00:00
|
|
|
bail!("unknown archive file extension (expected .pxar of .img)");
|
2019-06-25 11:00:37 +00:00
|
|
|
}
|
2019-06-26 07:18:59 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
2019-06-25 09:17:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 10:18:15 +00:00
|
|
|
fn upload_log(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let logfile = tools::required_string_param(¶m, "logfile")?;
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
|
|
|
let snapshot = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(snapshot)?;
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let mut client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-07-25 10:18:15 +00:00
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
2019-07-25 10:18:15 +00:00
|
|
|
|
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, _created) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-07-25 10:18:15 +00:00
|
|
|
let crypt_config = CryptConfig::new(key)?;
|
2019-08-16 07:19:01 +00:00
|
|
|
Some(Arc::new(crypt_config))
|
2019-07-25 10:18:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let data = file_get_contents(logfile)?;
|
2019-07-25 10:18:15 +00:00
|
|
|
|
2019-10-07 09:36:39 +00:00
|
|
|
let blob = DataBlob::encode(&data, crypt_config.as_ref().map(Arc::as_ref), true)?;
|
2019-07-25 10:18:15 +00:00
|
|
|
|
|
|
|
let raw_data = blob.into_inner();
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store());
|
|
|
|
|
|
|
|
let args = json!({
|
|
|
|
"backup-type": snapshot.group().backup_type(),
|
|
|
|
"backup-id": snapshot.group().backup_id(),
|
|
|
|
"backup-time": snapshot.backup_time().timestamp(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let body = hyper::Body::from(raw_data);
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async move {
|
|
|
|
client.upload("application/octet-stream", body, &path, Some(args)).await
|
|
|
|
})
|
2019-07-25 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:53:17 +00:00
|
|
|
fn prune(
|
2019-07-27 07:24:23 +00:00
|
|
|
mut param: Value,
|
2019-02-27 15:53:17 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-02-27 15:53:17 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let mut client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2019-07-27 06:49:14 +00:00
|
|
|
let group = tools::required_string_param(¶m, "group")?;
|
|
|
|
let group = BackupGroup::parse(group)?;
|
2019-12-07 15:11:26 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
2019-07-27 06:49:14 +00:00
|
|
|
|
2019-07-27 07:24:23 +00:00
|
|
|
param.as_object_mut().unwrap().remove("repository");
|
|
|
|
param.as_object_mut().unwrap().remove("group");
|
2019-12-07 15:11:26 +00:00
|
|
|
param.as_object_mut().unwrap().remove("output-format");
|
2019-07-27 07:24:23 +00:00
|
|
|
|
|
|
|
param["backup-type"] = group.backup_type().into();
|
|
|
|
param["backup-id"] = group.backup_id().into();
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2019-12-08 10:27:15 +00:00
|
|
|
async_main(async {
|
|
|
|
let result = client.post(&path, Some(param)).await?;
|
2019-12-06 12:35:25 +00:00
|
|
|
|
2019-12-08 10:27:15 +00:00
|
|
|
record_repository(&repo);
|
2019-12-08 09:59:47 +00:00
|
|
|
|
2019-12-09 17:00:22 +00:00
|
|
|
view_task_result(client, result, &output_format).await
|
|
|
|
})?;
|
2019-03-13 08:47:12 +00:00
|
|
|
|
2019-07-31 08:15:16 +00:00
|
|
|
Ok(Value::Null)
|
2019-02-27 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 11:35:25 +00:00
|
|
|
fn status(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
|
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
2019-07-16 11:35:25 +00:00
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/status", repo.store());
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let result = async_main(async move { client.get(&path, None).await })?;
|
2019-07-16 11:35:25 +00:00
|
|
|
let data = &result["data"];
|
|
|
|
|
|
|
|
record_repository(&repo);
|
|
|
|
|
|
|
|
if output_format == "text" {
|
|
|
|
let total = data["total"].as_u64().unwrap();
|
|
|
|
let used = data["used"].as_u64().unwrap();
|
|
|
|
let avail = data["avail"].as_u64().unwrap();
|
|
|
|
let roundup = total/200;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"total: {} used: {} ({} %) available: {}",
|
|
|
|
total,
|
|
|
|
used,
|
|
|
|
((used+roundup)*100)/total,
|
|
|
|
avail,
|
|
|
|
);
|
|
|
|
} else {
|
2019-07-18 07:52:11 +00:00
|
|
|
format_and_print_result(data, &output_format);
|
2019-07-16 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-04-28 08:55:03 +00:00
|
|
|
// like get, but simply ignore errors and return Null instead
|
2019-08-28 15:20:32 +00:00
|
|
|
async fn try_get(repo: &BackupRepository, url: &str) -> Value {
|
2019-03-13 10:56:37 +00:00
|
|
|
|
2019-09-16 10:35:23 +00:00
|
|
|
let client = match HttpClient::new(repo.host(), repo.user(), None) {
|
2019-04-30 09:44:35 +00:00
|
|
|
Ok(v) => v,
|
|
|
|
_ => return Value::Null,
|
|
|
|
};
|
2019-03-13 12:31:29 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let mut resp = match client.get(url, None).await {
|
2019-03-13 12:31:29 +00:00
|
|
|
Ok(v) => v,
|
|
|
|
_ => return Value::Null,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(map) = resp.as_object_mut() {
|
|
|
|
if let Some(data) = map.remove("data") {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Null
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async { complete_backup_group_do(param).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn complete_backup_group_do(param: &HashMap<String, String>) -> Vec<String> {
|
2019-03-13 10:56:37 +00:00
|
|
|
|
2019-03-13 12:31:29 +00:00
|
|
|
let mut result = vec![];
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = match extract_repository_from_map(param) {
|
2019-03-13 12:31:29 +00:00
|
|
|
Some(v) => v,
|
2019-03-13 10:56:37 +00:00
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
2019-03-13 12:31:29 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let data = try_get(&repo, &path).await;
|
2019-03-13 12:31:29 +00:00
|
|
|
|
|
|
|
if let Some(list) = data.as_array() {
|
2019-03-13 10:56:37 +00:00
|
|
|
for item in list {
|
2019-03-13 11:17:39 +00:00
|
|
|
if let (Some(backup_id), Some(backup_type)) =
|
|
|
|
(item["backup-id"].as_str(), item["backup-type"].as_str())
|
|
|
|
{
|
|
|
|
result.push(format!("{}/{}", backup_type, backup_id));
|
2019-03-13 10:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:31:29 +00:00
|
|
|
fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async { complete_group_or_snapshot_do(arg, param).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn complete_group_or_snapshot_do(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-03-13 12:31:29 +00:00
|
|
|
|
|
|
|
if arg.matches('/').count() < 2 {
|
2019-08-28 15:20:32 +00:00
|
|
|
let groups = complete_backup_group_do(param).await;
|
2019-08-09 08:08:45 +00:00
|
|
|
let mut result = vec![];
|
2019-03-13 12:31:29 +00:00
|
|
|
for group in groups {
|
|
|
|
result.push(group.to_string());
|
|
|
|
result.push(format!("{}/", group));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
complete_backup_snapshot_do(param).await
|
2019-08-09 08:08:45 +00:00
|
|
|
}
|
2019-03-13 12:31:29 +00:00
|
|
|
|
2019-08-09 08:22:32 +00:00
|
|
|
fn complete_backup_snapshot(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async { complete_backup_snapshot_do(param).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn complete_backup_snapshot_do(param: &HashMap<String, String>) -> Vec<String> {
|
2019-08-09 08:08:45 +00:00
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let repo = match extract_repository_from_map(param) {
|
|
|
|
Some(v) => v,
|
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
2019-03-13 12:31:29 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let data = try_get(&repo, &path).await;
|
2019-03-13 12:31:29 +00:00
|
|
|
|
|
|
|
if let Some(list) = data.as_array() {
|
|
|
|
for item in list {
|
|
|
|
if let (Some(backup_id), Some(backup_type), Some(backup_time)) =
|
|
|
|
(item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64())
|
|
|
|
{
|
|
|
|
let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
result.push(snapshot.relative_path().to_str().unwrap().to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:17:24 +00:00
|
|
|
fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-08-28 15:20:32 +00:00
|
|
|
async_main(async { complete_server_file_name_do(param).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<String> {
|
2019-03-14 07:09:35 +00:00
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
2019-07-16 05:30:04 +00:00
|
|
|
let repo = match extract_repository_from_map(param) {
|
2019-03-14 07:09:35 +00:00
|
|
|
Some(v) => v,
|
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
|
|
|
let snapshot = match param.get("snapshot") {
|
|
|
|
Some(path) => {
|
|
|
|
match BackupDir::parse(path) {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return result,
|
|
|
|
};
|
|
|
|
|
|
|
|
let query = tools::json_object_to_query(json!({
|
|
|
|
"backup-type": snapshot.group().backup_type(),
|
|
|
|
"backup-id": snapshot.group().backup_id(),
|
|
|
|
"backup-time": snapshot.backup_time().timestamp(),
|
|
|
|
})).unwrap();
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query);
|
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
let data = try_get(&repo, &path).await;
|
2019-03-14 07:09:35 +00:00
|
|
|
|
|
|
|
if let Some(list) = data.as_array() {
|
|
|
|
for item in list {
|
2019-08-07 04:48:18 +00:00
|
|
|
if let Some(filename) = item["filename"].as_str() {
|
2019-03-14 07:09:35 +00:00
|
|
|
result.push(filename.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:17:24 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
2019-07-31 09:23:53 +00:00
|
|
|
complete_server_file_name(arg, param)
|
2019-08-28 15:20:32 +00:00
|
|
|
.iter()
|
|
|
|
.map(|v| strip_server_file_expenstion(&v))
|
|
|
|
.collect()
|
2019-03-14 07:09:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:14:32 +00:00
|
|
|
fn complete_pxar_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
complete_server_file_name(arg, param)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|v| {
|
|
|
|
let name = strip_server_file_expenstion(&v);
|
|
|
|
if name.ends_with(".pxar") {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-03-13 11:26:01 +00:00
|
|
|
fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let mut size = 64;
|
|
|
|
loop {
|
|
|
|
result.push(size.to_string());
|
2019-10-25 16:04:37 +00:00
|
|
|
size *= 2;
|
2019-03-13 11:26:01 +00:00
|
|
|
if size > 4096 { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:17:22 +00:00
|
|
|
fn get_encryption_key_password() -> Result<Vec<u8>, Error> {
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2019-06-17 08:33:24 +00:00
|
|
|
// fixme: implement other input methods
|
|
|
|
|
|
|
|
use std::env::VarError::*;
|
|
|
|
match std::env::var("PBS_ENCRYPTION_PASSWORD") {
|
2019-06-18 09:17:22 +00:00
|
|
|
Ok(p) => return Ok(p.as_bytes().to_vec()),
|
2019-06-17 08:33:24 +00:00
|
|
|
Err(NotUnicode(_)) => bail!("PBS_ENCRYPTION_PASSWORD contains bad characters"),
|
|
|
|
Err(NotPresent) => {
|
|
|
|
// Try another method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're on a TTY, query the user for a password
|
|
|
|
if crate::tools::tty::stdin_isatty() {
|
2019-06-18 09:17:22 +00:00
|
|
|
return Ok(crate::tools::tty::read_password("Encryption Key Password: ")?);
|
2019-06-17 08:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bail!("no password input mechanism available");
|
|
|
|
}
|
|
|
|
|
2019-06-17 11:10:00 +00:00
|
|
|
fn key_create(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-06-18 15:38:50 +00:00
|
|
|
let path = tools::required_string_param(¶m, "path")?;
|
|
|
|
let path = PathBuf::from(path);
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
let kdf = param["kdf"].as_str().unwrap_or("scrypt");
|
2019-06-17 11:10:00 +00:00
|
|
|
|
|
|
|
let key = proxmox::sys::linux::random_data(32)?;
|
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
if kdf == "scrypt" {
|
|
|
|
// always read passphrase from tty
|
|
|
|
if !crate::tools::tty::stdin_isatty() {
|
|
|
|
bail!("unable to read passphrase - no tty");
|
|
|
|
}
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
let password = crate::tools::tty::read_password("Encryption Key Password: ")?;
|
|
|
|
|
2019-06-20 05:58:33 +00:00
|
|
|
let key_config = encrypt_key_with_passphrase(&key, &password)?;
|
2019-06-21 05:12:19 +00:00
|
|
|
|
2019-06-20 05:58:33 +00:00
|
|
|
store_key_config(&path, false, key_config)?;
|
2019-06-18 16:21:01 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
} else if kdf == "none" {
|
|
|
|
let created = Local.timestamp(Local::now().timestamp(), 0);
|
|
|
|
|
|
|
|
store_key_config(&path, false, KeyConfig {
|
|
|
|
kdf: None,
|
|
|
|
created,
|
2019-06-20 05:58:33 +00:00
|
|
|
modified: created,
|
2019-06-18 16:21:01 +00:00
|
|
|
data: key,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-06-17 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:56:37 +00:00
|
|
|
fn master_pubkey_path() -> Result<PathBuf, Error> {
|
|
|
|
let base = BaseDirectories::with_prefix("proxmox-backup")?;
|
|
|
|
|
|
|
|
// usually $HOME/.config/proxmox-backup/master-public.pem
|
|
|
|
let path = base.place_config_file("master-public.pem")?;
|
|
|
|
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
2019-06-21 05:34:17 +00:00
|
|
|
fn key_import_master_pubkey(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "path")?;
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let pem_data = file_get_contents(&path)?;
|
2019-06-21 05:34:17 +00:00
|
|
|
|
|
|
|
if let Err(err) = openssl::pkey::PKey::public_key_from_pem(&pem_data) {
|
|
|
|
bail!("Unable to decode PEM data - {}", err);
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:56:37 +00:00
|
|
|
let target_path = master_pubkey_path()?;
|
2019-06-21 05:34:17 +00:00
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
file_set_contents(&target_path, &pem_data, None)?;
|
2019-06-21 05:34:17 +00:00
|
|
|
|
|
|
|
println!("Imported public master key to {:?}", target_path);
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-06-21 05:12:19 +00:00
|
|
|
fn key_create_master_key(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
// we need a TTY to query the new password
|
|
|
|
if !crate::tools::tty::stdin_isatty() {
|
|
|
|
bail!("unable to create master key - no tty");
|
|
|
|
}
|
|
|
|
|
|
|
|
let rsa = openssl::rsa::Rsa::generate(4096)?;
|
|
|
|
let pkey = openssl::pkey::PKey::from_rsa(rsa)?;
|
|
|
|
|
|
|
|
let new_pw = String::from_utf8(crate::tools::tty::read_password("Master Key Password: ")?)?;
|
|
|
|
let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?;
|
|
|
|
|
|
|
|
if new_pw != verify_pw {
|
|
|
|
bail!("Password verification fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_pw.len() < 5 {
|
|
|
|
bail!("Password is too short!");
|
|
|
|
}
|
|
|
|
|
|
|
|
let pub_key: Vec<u8> = pkey.public_key_to_pem()?;
|
|
|
|
let filename_pub = "master-public.pem";
|
|
|
|
println!("Writing public master key to {}", filename_pub);
|
2019-08-03 11:05:38 +00:00
|
|
|
file_set_contents(filename_pub, pub_key.as_slice(), None)?;
|
2019-06-21 05:12:19 +00:00
|
|
|
|
|
|
|
let cipher = openssl::symm::Cipher::aes_256_cbc();
|
|
|
|
let priv_key: Vec<u8> = pkey.private_key_to_pem_pkcs8_passphrase(cipher, new_pw.as_bytes())?;
|
|
|
|
|
|
|
|
let filename_priv = "master-private.pem";
|
|
|
|
println!("Writing private master key to {}", filename_priv);
|
2019-08-03 11:05:38 +00:00
|
|
|
file_set_contents(filename_priv, priv_key.as_slice(), None)?;
|
2019-06-21 05:12:19 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
2019-06-17 11:10:00 +00:00
|
|
|
|
|
|
|
fn key_change_passphrase(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2019-06-18 15:38:50 +00:00
|
|
|
let path = tools::required_string_param(¶m, "path")?;
|
|
|
|
let path = PathBuf::from(path);
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
let kdf = param["kdf"].as_str().unwrap_or("scrypt");
|
|
|
|
|
2019-06-17 11:10:00 +00:00
|
|
|
// we need a TTY to query the new password
|
|
|
|
if !crate::tools::tty::stdin_isatty() {
|
|
|
|
bail!("unable to change passphrase - no tty");
|
|
|
|
}
|
|
|
|
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, created) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
if kdf == "scrypt" {
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
let new_pw = String::from_utf8(crate::tools::tty::read_password("New Password: ")?)?;
|
|
|
|
let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?;
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
if new_pw != verify_pw {
|
|
|
|
bail!("Password verification fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_pw.len() < 5 {
|
|
|
|
bail!("Password is too short!");
|
|
|
|
}
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-20 05:58:33 +00:00
|
|
|
let mut new_key_config = encrypt_key_with_passphrase(&key, new_pw.as_bytes())?;
|
|
|
|
new_key_config.created = created; // keep original value
|
|
|
|
|
|
|
|
store_key_config(&path, true, new_key_config)?;
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-06-18 16:21:01 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
} else if kdf == "none" {
|
2019-06-20 05:58:33 +00:00
|
|
|
let modified = Local.timestamp(Local::now().timestamp(), 0);
|
2019-06-18 16:21:01 +00:00
|
|
|
|
|
|
|
store_key_config(&path, true, KeyConfig {
|
|
|
|
kdf: None,
|
2019-06-20 05:58:33 +00:00
|
|
|
created, // keep original value
|
|
|
|
modified,
|
2019-06-19 15:16:41 +00:00
|
|
|
data: key.to_vec(),
|
2019-06-18 16:21:01 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-06-17 08:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn key_mgmt_cli() -> CliCommandMap {
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
const KDF_SCHEMA: Schema =
|
2019-06-18 16:21:01 +00:00
|
|
|
StringSchema::new("Key derivation function. Choose 'none' to store the key unecrypted.")
|
2019-11-21 08:36:41 +00:00
|
|
|
.format(&ApiStringFormat::Enum(&["scrypt", "none"]))
|
|
|
|
.default("scrypt")
|
|
|
|
.schema();
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_KEY_CREATE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&key_create),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Create a new encryption key.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("path", false, &StringSchema::new("File system path.").schema()),
|
|
|
|
("kdf", true, &KDF_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
2019-06-18 16:21:01 +00:00
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let key_create_cmd_def = CliCommand::new(&API_METHOD_KEY_CREATE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["path"])
|
2019-06-18 15:38:50 +00:00
|
|
|
.completion_cb("path", tools::complete_file_name);
|
2019-06-17 08:33:24 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_KEY_CHANGE_PASSPHRASE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&key_change_passphrase),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Change the passphrase required to decrypt the key.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("path", false, &StringSchema::new("File system path.").schema()),
|
|
|
|
("kdf", true, &KDF_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let key_change_passphrase_cmd_def = CliCommand::new(&API_METHOD_KEY_CHANGE_PASSPHRASE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["path"])
|
2019-06-18 15:38:50 +00:00
|
|
|
.completion_cb("path", tools::complete_file_name);
|
2019-06-17 11:10:00 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_KEY_CREATE_MASTER_KEY: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&key_create_master_key),
|
|
|
|
&ObjectSchema::new("Create a new 4096 bit RSA master pub/priv key pair.", &[])
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let key_create_master_key_cmd_def = CliCommand::new(&API_METHOD_KEY_CREATE_MASTER_KEY);
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_KEY_IMPORT_MASTER_PUBKEY: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&key_import_master_pubkey),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Import a new RSA public key and use it as master key. The key is expected to be in '.pem' format.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([ ("path", false, &StringSchema::new("File system path.").schema()) ]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let key_import_master_pubkey_cmd_def = CliCommand::new(&API_METHOD_KEY_IMPORT_MASTER_PUBKEY)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["path"])
|
2019-06-21 05:34:17 +00:00
|
|
|
.completion_cb("path", tools::complete_file_name);
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("create", key_create_cmd_def)
|
|
|
|
.insert("create-master-key", key_create_master_key_cmd_def)
|
|
|
|
.insert("import-master-pubkey", key_import_master_pubkey_cmd_def)
|
|
|
|
.insert("change-passphrase", key_change_passphrase_cmd_def)
|
2019-06-17 08:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 14:33:09 +00:00
|
|
|
fn mount(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
|
|
|
if verbose {
|
|
|
|
// This will stay in foreground with debug output enabled as None is
|
|
|
|
// passed for the RawFd.
|
|
|
|
return async_main(mount_do(param, None));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process should be deamonized.
|
|
|
|
// Make sure to fork before the async runtime is instantiated to avoid troubles.
|
|
|
|
let pipe = pipe()?;
|
|
|
|
match fork() {
|
2019-10-25 16:04:37 +00:00
|
|
|
Ok(ForkResult::Parent { .. }) => {
|
2019-09-19 14:33:09 +00:00
|
|
|
nix::unistd::close(pipe.1).unwrap();
|
|
|
|
// Blocks the parent process until we are ready to go in the child
|
|
|
|
let _res = nix::unistd::read(pipe.0, &mut [0]).unwrap();
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
Ok(ForkResult::Child) => {
|
|
|
|
nix::unistd::close(pipe.0).unwrap();
|
|
|
|
nix::unistd::setsid().unwrap();
|
|
|
|
async_main(mount_do(param, Some(pipe.1)))
|
|
|
|
}
|
|
|
|
Err(_) => bail!("failed to daemonize process"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> {
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
|
|
|
let target = tools::required_string_param(¶m, "target")?;
|
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
|
|
|
|
|
|
|
record_repository(&repo);
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
|
|
|
|
let group = BackupGroup::parse(path)?;
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
|
|
|
let result = client.get(&path, Some(json!({
|
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
|
|
|
}))).await?;
|
|
|
|
|
|
|
|
let list = result["data"].as_array().unwrap();
|
2019-10-25 16:04:37 +00:00
|
|
|
if list.is_empty() {
|
2019-09-19 14:33:09 +00:00
|
|
|
bail!("backup group '{}' does not contain any snapshots:", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let epoch = list[0]["backup-time"].as_i64().unwrap();
|
|
|
|
let backup_time = Utc.timestamp(epoch, 0);
|
|
|
|
(group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)
|
|
|
|
} else {
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
|
|
|
|
};
|
|
|
|
|
2019-10-25 16:04:37 +00:00
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
2019-09-19 14:33:09 +00:00
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
2019-10-17 08:28:21 +00:00
|
|
|
let (key, _) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
2019-09-19 14:33:09 +00:00
|
|
|
Some(Arc::new(CryptConfig::new(key)?))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let server_archive_name = if archive_name.ends_with(".pxar") {
|
|
|
|
format!("{}.didx", archive_name)
|
|
|
|
} else {
|
|
|
|
bail!("Can only mount pxar archives.");
|
|
|
|
};
|
|
|
|
|
2019-10-12 13:50:26 +00:00
|
|
|
let client = BackupReader::start(
|
|
|
|
client,
|
|
|
|
crypt_config.clone(),
|
|
|
|
repo.store(),
|
|
|
|
&backup_type,
|
|
|
|
&backup_id,
|
|
|
|
backup_time,
|
|
|
|
true,
|
|
|
|
).await?;
|
2019-09-19 14:33:09 +00:00
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
let manifest = client.download_manifest().await?;
|
2019-10-12 13:50:26 +00:00
|
|
|
|
2019-09-19 14:33:09 +00:00
|
|
|
if server_archive_name.ends_with(".didx") {
|
2019-11-08 11:40:10 +00:00
|
|
|
let index = client.download_dynamic_index(&manifest, &server_archive_name).await?;
|
2019-09-19 14:33:09 +00:00
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
|
|
|
|
let reader = BufferedDynamicReader::new(index, chunk_reader);
|
2019-11-26 08:56:48 +00:00
|
|
|
let decoder = pxar::Decoder::new(reader)?;
|
2019-09-19 14:33:09 +00:00
|
|
|
let options = OsStr::new("ro,default_permissions");
|
2019-11-26 09:56:41 +00:00
|
|
|
let mut session = pxar::fuse::Session::new(decoder, &options, pipe.is_none())
|
2019-09-19 14:33:09 +00:00
|
|
|
.map_err(|err| format_err!("pxar mount failed: {}", err))?;
|
|
|
|
|
|
|
|
// Mount the session but not call fuse deamonize as this will cause
|
|
|
|
// issues with the runtime after the fork
|
|
|
|
let deamonize = false;
|
|
|
|
session.mount(&Path::new(target), deamonize)?;
|
|
|
|
|
|
|
|
if let Some(pipe) = pipe {
|
|
|
|
nix::unistd::chdir(Path::new("/")).unwrap();
|
|
|
|
// Finish creation of deamon by redirecting filedescriptors.
|
|
|
|
let nullfd = nix::fcntl::open(
|
|
|
|
"/dev/null",
|
|
|
|
nix::fcntl::OFlag::O_RDWR,
|
|
|
|
nix::sys::stat::Mode::empty(),
|
|
|
|
).unwrap();
|
|
|
|
nix::unistd::dup2(nullfd, 0).unwrap();
|
|
|
|
nix::unistd::dup2(nullfd, 1).unwrap();
|
|
|
|
nix::unistd::dup2(nullfd, 2).unwrap();
|
|
|
|
if nullfd > 2 {
|
|
|
|
nix::unistd::close(nullfd).unwrap();
|
|
|
|
}
|
|
|
|
// Signal the parent process that we are done with the setup and it can
|
|
|
|
// terminate.
|
2019-10-25 16:04:37 +00:00
|
|
|
nix::unistd::write(pipe, &[0u8])?;
|
2019-09-19 14:33:09 +00:00
|
|
|
nix::unistd::close(pipe).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let multithreaded = true;
|
|
|
|
session.run_loop(multithreaded)?;
|
|
|
|
} else {
|
|
|
|
bail!("unknown archive file extension (expected .pxar)");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:07:25 +00:00
|
|
|
fn catalog_shell(
|
2019-11-21 14:00:01 +00:00
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2019-12-09 12:07:25 +00:00
|
|
|
async_main(catalog_shell_async(param))
|
2019-11-21 14:00:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:07:25 +00:00
|
|
|
async fn catalog_shell_async(param: Value) -> Result<Value, Error> {
|
2019-11-21 14:00:01 +00:00
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
|
|
|
|
|
|
|
let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
|
|
|
|
let group = BackupGroup::parse(path)?;
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
|
|
|
|
let result = client.get(&path, Some(json!({
|
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
|
|
|
}))).await?;
|
|
|
|
|
|
|
|
let list = result["data"].as_array().unwrap();
|
2019-11-26 13:32:48 +00:00
|
|
|
if list.is_empty() {
|
2019-11-21 14:00:01 +00:00
|
|
|
bail!("backup group '{}' does not contain any snapshots:", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let epoch = list[0]["backup-time"].as_i64().unwrap();
|
|
|
|
let backup_time = Utc.timestamp(epoch, 0);
|
|
|
|
(group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)
|
|
|
|
} else {
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
|
|
|
|
};
|
|
|
|
|
|
|
|
let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p));
|
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
|
|
|
let (key, _) = load_and_decrtypt_key(&path, &get_encryption_key_password)?;
|
|
|
|
Some(Arc::new(CryptConfig::new(key)?))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let server_archive_name = if archive_name.ends_with(".pxar") {
|
|
|
|
format!("{}.didx", archive_name)
|
|
|
|
} else {
|
|
|
|
bail!("Can only mount pxar archives.");
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = BackupReader::start(
|
|
|
|
client,
|
|
|
|
crypt_config.clone(),
|
|
|
|
repo.store(),
|
|
|
|
&backup_type,
|
|
|
|
&backup_id,
|
|
|
|
backup_time,
|
|
|
|
true,
|
|
|
|
).await?;
|
|
|
|
|
|
|
|
let tmpfile = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.read(true)
|
|
|
|
.custom_flags(libc::O_TMPFILE)
|
|
|
|
.open("/tmp")?;
|
|
|
|
|
|
|
|
let manifest = client.download_manifest().await?;
|
|
|
|
|
|
|
|
let index = client.download_dynamic_index(&manifest, &server_archive_name).await?;
|
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config.clone(), most_used);
|
|
|
|
let reader = BufferedDynamicReader::new(index, chunk_reader);
|
2019-11-26 08:56:48 +00:00
|
|
|
let mut decoder = pxar::Decoder::new(reader)?;
|
|
|
|
decoder.set_callback(|path| {
|
|
|
|
println!("{:?}", path);
|
|
|
|
Ok(())
|
|
|
|
});
|
2019-11-21 14:00:01 +00:00
|
|
|
|
|
|
|
let tmpfile = client.download(CATALOG_NAME, tmpfile).await?;
|
|
|
|
let index = DynamicIndexReader::new(tmpfile)
|
|
|
|
.map_err(|err| format_err!("unable to read catalog index - {}", err))?;
|
|
|
|
|
|
|
|
// Note: do not use values stored in index (not trusted) - instead, computed them again
|
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
manifest.verify_file(CATALOG_NAME, &csum, size)?;
|
|
|
|
|
|
|
|
let most_used = index.find_most_used_chunks(8);
|
|
|
|
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
|
|
|
|
let mut reader = BufferedDynamicReader::new(index, chunk_reader);
|
|
|
|
let mut catalogfile = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.read(true)
|
|
|
|
.custom_flags(libc::O_TMPFILE)
|
|
|
|
.open("/tmp")?;
|
|
|
|
|
|
|
|
std::io::copy(&mut reader, &mut catalogfile)
|
|
|
|
.map_err(|err| format_err!("unable to download catalog - {}", err))?;
|
|
|
|
|
|
|
|
catalogfile.seek(SeekFrom::Start(0))?;
|
|
|
|
let catalog_reader = CatalogReader::new(catalogfile);
|
|
|
|
let state = Shell::new(
|
|
|
|
catalog_reader,
|
|
|
|
&server_archive_name,
|
|
|
|
decoder,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
println!("Starting interactive shell");
|
|
|
|
state.shell()?;
|
|
|
|
|
|
|
|
record_repository(&repo);
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:07:25 +00:00
|
|
|
fn catalog_mgmt_cli() -> CliCommandMap {
|
|
|
|
|
|
|
|
#[sortable]
|
|
|
|
const API_METHOD_SHELL: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&catalog_shell),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Shell to interactively inspect and restore snapshots.",
|
|
|
|
&sorted!([
|
|
|
|
("snapshot", false, &StringSchema::new("Group/Snapshot path.").schema()),
|
|
|
|
("archive-name", false, &StringSchema::new("Backup archive name.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("keyfile", true, &StringSchema::new("Path to encryption key.").schema()),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let catalog_shell_cmd_def = CliCommand::new(&API_METHOD_SHELL)
|
|
|
|
.arg_param(&["snapshot", "archive-name"])
|
|
|
|
.completion_cb("repository", complete_repository)
|
2019-12-09 12:14:32 +00:00
|
|
|
.completion_cb("archive-name", complete_pxar_archive_name)
|
2019-12-09 12:07:25 +00:00
|
|
|
.completion_cb("snapshot", complete_group_or_snapshot);
|
|
|
|
|
|
|
|
#[sortable]
|
|
|
|
const API_METHOD_DUMP_CATALOG: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&dump_catalog),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Dump catalog.",
|
|
|
|
&sorted!([
|
|
|
|
("snapshot", false, &StringSchema::new("Snapshot path.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let catalog_dump_cmd_def = CliCommand::new(&API_METHOD_DUMP_CATALOG)
|
|
|
|
.arg_param(&["snapshot"])
|
|
|
|
.completion_cb("repository", complete_repository)
|
|
|
|
.completion_cb("snapshot", complete_backup_snapshot);
|
|
|
|
|
|
|
|
CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("dump", catalog_dump_cmd_def)
|
|
|
|
.insert("shell", catalog_shell_cmd_def)
|
2019-12-09 12:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:43:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
repository: {
|
|
|
|
schema: REPO_URL_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
limit: {
|
|
|
|
description: "The maximal number of tasks to list.",
|
|
|
|
type: Integer,
|
|
|
|
optional: true,
|
|
|
|
minimum: 1,
|
|
|
|
maximum: 1000,
|
|
|
|
default: 50,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List running server tasks for this repo user
|
|
|
|
fn task_list(param: Value) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
async_main(async {
|
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
|
|
|
|
|
|
|
let limit = param["limit"].as_u64().unwrap_or(50) as usize;
|
|
|
|
|
|
|
|
let args = json!({ "start": 0, "limit": limit, "userfilter": repo.user()});
|
|
|
|
let result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
|
|
|
|
|
|
|
|
let data = &result["data"];
|
|
|
|
|
|
|
|
if output_format == "text" {
|
|
|
|
for item in data.as_array().unwrap() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
item["upid"].as_str().unwrap(),
|
|
|
|
item["status"].as_str().unwrap_or("running"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format_and_print_result(data, &output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok::<_, Error>(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
repository: {
|
|
|
|
schema: REPO_URL_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
upid: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Display the task log.
|
|
|
|
fn task_log(param: Value) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
async_main(async {
|
|
|
|
let repo = extract_repository_from_value(¶m)?;
|
|
|
|
let upid = tools::required_string_param(¶m, "upid")?;
|
|
|
|
|
|
|
|
let client = HttpClient::new(repo.host(), repo.user(), None)?;
|
|
|
|
|
|
|
|
display_task_log(client, upid, true).await?;
|
|
|
|
|
|
|
|
Ok::<_, Error>(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn task_mgmt_cli() -> CliCommandMap {
|
|
|
|
|
|
|
|
let task_list_cmd_def = CliCommand::new(&API_METHOD_TASK_LIST)
|
|
|
|
.completion_cb("repository", complete_repository);
|
|
|
|
|
|
|
|
let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG)
|
|
|
|
.arg_param(&["upid"]);
|
|
|
|
|
|
|
|
CliCommandMap::new()
|
|
|
|
.insert("log", task_log_cmd_def)
|
|
|
|
.insert("list", task_list_cmd_def)
|
|
|
|
|
|
|
|
}
|
2019-12-09 12:07:25 +00:00
|
|
|
|
2019-06-17 08:33:24 +00:00
|
|
|
fn main() {
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
const BACKUP_SOURCE_SCHEMA: Schema = StringSchema::new("Backup source specification ([<label>:<path>]).")
|
|
|
|
.format(&ApiStringFormat::Pattern(&BACKUPSPEC_REGEX))
|
|
|
|
.schema();
|
2019-02-27 06:21:57 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_CREATE_BACKUP: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&create_backup),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Create (host) backup.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
2019-02-26 11:27:28 +00:00
|
|
|
"backupspec",
|
2019-11-21 08:36:41 +00:00
|
|
|
false,
|
|
|
|
&ArraySchema::new(
|
2019-03-11 09:23:16 +00:00
|
|
|
"List of backup source specifications ([<label.ext>:<path>] ...)",
|
2019-11-21 08:36:41 +00:00
|
|
|
&BACKUP_SOURCE_SCHEMA,
|
|
|
|
).min_length(1).schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"repository",
|
|
|
|
true,
|
|
|
|
&REPO_URL_SCHEMA
|
|
|
|
),
|
|
|
|
(
|
2019-07-24 05:48:59 +00:00
|
|
|
"include-dev",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&ArraySchema::new(
|
2019-07-24 05:48:59 +00:00
|
|
|
"Include mountpoints with same st_dev number (see ``man fstat``) as specified files.",
|
2019-11-21 08:36:41 +00:00
|
|
|
&StringSchema::new("Path to file.").schema()
|
|
|
|
).schema()
|
|
|
|
),
|
|
|
|
(
|
2019-06-19 15:16:41 +00:00
|
|
|
"keyfile",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&StringSchema::new("Path to encryption key. All data will be encrypted using this key.").schema()
|
|
|
|
),
|
|
|
|
(
|
2019-03-04 07:01:09 +00:00
|
|
|
"verbose",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BooleanSchema::new("Verbose output.")
|
|
|
|
.default(false)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
2019-07-24 10:21:25 +00:00
|
|
|
"skip-lost-and-found",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BooleanSchema::new("Skip lost+found directory")
|
|
|
|
.default(false)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
2019-07-26 07:07:29 +00:00
|
|
|
"backup-type",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BACKUP_TYPE_SCHEMA,
|
|
|
|
),
|
|
|
|
(
|
2019-07-26 07:07:29 +00:00
|
|
|
"backup-id",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BACKUP_ID_SCHEMA
|
|
|
|
),
|
|
|
|
(
|
2019-07-25 11:44:01 +00:00
|
|
|
"backup-time",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BACKUP_TIME_SCHEMA
|
|
|
|
),
|
|
|
|
(
|
2018-12-21 10:18:08 +00:00
|
|
|
"chunk-size",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
|
2018-12-21 10:18:08 +00:00
|
|
|
.minimum(64)
|
|
|
|
.maximum(4096)
|
|
|
|
.default(4096)
|
2019-11-21 08:36:41 +00:00
|
|
|
.schema()
|
|
|
|
),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let backup_cmd_def = CliCommand::new(&API_METHOD_CREATE_BACKUP)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["backupspec"])
|
2019-03-13 08:47:12 +00:00
|
|
|
.completion_cb("repository", complete_repository)
|
2019-03-13 11:26:01 +00:00
|
|
|
.completion_cb("backupspec", complete_backup_source)
|
2019-06-19 15:16:41 +00:00
|
|
|
.completion_cb("keyfile", tools::complete_file_name)
|
2019-03-13 11:26:01 +00:00
|
|
|
.completion_cb("chunk-size", complete_chunk_size);
|
2018-12-15 10:24:39 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_UPLOAD_LOG: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&upload_log),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Upload backup log file.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"snapshot",
|
|
|
|
false,
|
|
|
|
&StringSchema::new("Snapshot path.").schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"logfile",
|
|
|
|
false,
|
|
|
|
&StringSchema::new("The path to the log file you want to upload.").schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"repository",
|
|
|
|
true,
|
|
|
|
&REPO_URL_SCHEMA
|
|
|
|
),
|
|
|
|
(
|
2019-07-25 10:18:15 +00:00
|
|
|
"keyfile",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&StringSchema::new("Path to encryption key. All data will be encrypted using this key.").schema()
|
|
|
|
),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let upload_log_cmd_def = CliCommand::new(&API_METHOD_UPLOAD_LOG)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["snapshot", "logfile"])
|
2019-08-09 08:08:45 +00:00
|
|
|
.completion_cb("snapshot", complete_backup_snapshot)
|
2019-07-25 10:18:15 +00:00
|
|
|
.completion_cb("logfile", tools::complete_file_name)
|
|
|
|
.completion_cb("keyfile", tools::complete_file_name)
|
|
|
|
.completion_cb("repository", complete_repository);
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_LIST_BACKUP_GROUPS: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&list_backup_groups),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"List backup groups.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("output-format", true, &OUTPUT_FORMAT),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let list_cmd_def = CliCommand::new(&API_METHOD_LIST_BACKUP_GROUPS)
|
2019-03-13 08:47:12 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
2019-01-21 17:58:14 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_LIST_SNAPSHOTS: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&list_snapshots),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"List backup snapshots.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("group", true, &StringSchema::new("Backup group.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("output-format", true, &OUTPUT_FORMAT),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let snapshots_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOTS)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["group"])
|
2019-03-13 10:56:37 +00:00
|
|
|
.completion_cb("group", complete_backup_group)
|
2019-03-13 08:47:12 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_FORGET_SNAPSHOTS: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&forget_snapshots),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Forget (remove) backup snapshots.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("snapshot", false, &StringSchema::new("Snapshot path.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let forget_cmd_def = CliCommand::new(&API_METHOD_FORGET_SNAPSHOTS)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["snapshot"])
|
2019-03-13 12:31:29 +00:00
|
|
|
.completion_cb("repository", complete_repository)
|
2019-08-09 08:08:45 +00:00
|
|
|
.completion_cb("snapshot", complete_backup_snapshot);
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_START_GARBAGE_COLLECTION: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&start_garbage_collection),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Start garbage collection for a specific repository.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([ ("repository", true, &REPO_URL_SCHEMA) ]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let garbage_collect_cmd_def = CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
|
2019-03-13 08:47:12 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
2019-02-20 13:10:45 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_RESTORE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&restore),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Restore backup repository.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("snapshot", false, &StringSchema::new("Group/Snapshot path.").schema()),
|
|
|
|
("archive-name", false, &StringSchema::new("Backup archive name.").schema()),
|
|
|
|
(
|
|
|
|
"target",
|
|
|
|
false,
|
|
|
|
&StringSchema::new(
|
|
|
|
r###"Target directory path. Use '-' to write to stdandard output.
|
2019-07-05 10:14:50 +00:00
|
|
|
|
|
|
|
We do not extraxt '.pxar' archives when writing to stdandard output.
|
|
|
|
|
|
|
|
"###
|
2019-11-21 08:36:41 +00:00
|
|
|
).schema()
|
|
|
|
),
|
|
|
|
(
|
2019-07-29 10:49:15 +00:00
|
|
|
"allow-existing-dirs",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
|
|
|
&BooleanSchema::new("Do not fail if directories already exists.")
|
|
|
|
.default(false)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("keyfile", true, &StringSchema::new("Path to encryption key.").schema()),
|
|
|
|
(
|
2019-07-03 13:45:12 +00:00
|
|
|
"verbose",
|
2019-12-06 12:35:59 +00:00
|
|
|
true,
|
2019-11-21 08:36:41 +00:00
|
|
|
&BooleanSchema::new("Verbose output.")
|
|
|
|
.default(false)
|
|
|
|
.schema()
|
|
|
|
),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let restore_cmd_def = CliCommand::new(&API_METHOD_RESTORE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["snapshot", "archive-name", "target"])
|
2019-03-13 12:31:29 +00:00
|
|
|
.completion_cb("repository", complete_repository)
|
2019-03-14 07:09:35 +00:00
|
|
|
.completion_cb("snapshot", complete_group_or_snapshot)
|
|
|
|
.completion_cb("archive-name", complete_archive_name)
|
|
|
|
.completion_cb("target", tools::complete_file_name);
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_LIST_SNAPSHOT_FILES: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&list_snapshot_files),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"List snapshot files.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("snapshot", false, &StringSchema::new("Snapshot path.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("output-format", true, &OUTPUT_FORMAT),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let files_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOT_FILES)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["snapshot"])
|
2019-07-31 09:23:53 +00:00
|
|
|
.completion_cb("repository", complete_repository)
|
2019-08-09 08:08:45 +00:00
|
|
|
.completion_cb("snapshot", complete_backup_snapshot);
|
2019-07-31 09:23:53 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&prune),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Prune backup repository.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&proxmox_backup::add_common_prune_prameters!([
|
2019-12-06 12:35:25 +00:00
|
|
|
("dry-run", true, &BooleanSchema::new(
|
|
|
|
"Just show what prune would do, but do not delete anything.")
|
|
|
|
.schema()),
|
2019-11-21 08:36:41 +00:00
|
|
|
("group", false, &StringSchema::new("Backup group.").schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
], [
|
2019-12-07 15:11:26 +00:00
|
|
|
("output-format", true, &OUTPUT_FORMAT),
|
2019-11-21 08:36:41 +00:00
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
])
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let prune_cmd_def = CliCommand::new(&API_METHOD_PRUNE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["group"])
|
2019-07-27 06:49:14 +00:00
|
|
|
.completion_cb("group", complete_backup_group)
|
2019-03-13 08:47:12 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_STATUS: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&status),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Get repository status.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("output-format", true, &OUTPUT_FORMAT),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let status_cmd_def = CliCommand::new(&API_METHOD_STATUS)
|
2019-07-16 11:35:25 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_API_LOGIN: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&api_login),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Try to login. If successful, store ticket.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([ ("repository", true, &REPO_URL_SCHEMA) ]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let login_cmd_def = CliCommand::new(&API_METHOD_API_LOGIN)
|
2019-08-10 07:12:17 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_API_LOGOUT: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&api_logout),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Logout (delete stored ticket).",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([ ("repository", true, &REPO_URL_SCHEMA) ]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let logout_cmd_def = CliCommand::new(&API_METHOD_API_LOGOUT)
|
2019-08-10 07:12:17 +00:00
|
|
|
.completion_cb("repository", complete_repository);
|
2019-09-20 06:12:31 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const API_METHOD_MOUNT: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&mount),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Mount pxar archive.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("snapshot", false, &StringSchema::new("Group/Snapshot path.").schema()),
|
|
|
|
("archive-name", false, &StringSchema::new("Backup archive name.").schema()),
|
|
|
|
("target", false, &StringSchema::new("Target directory path.").schema()),
|
|
|
|
("repository", true, &REPO_URL_SCHEMA),
|
|
|
|
("keyfile", true, &StringSchema::new("Path to encryption key.").schema()),
|
|
|
|
("verbose", true, &BooleanSchema::new("Verbose output.").default(false).schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-06 12:35:59 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let mount_cmd_def = CliCommand::new(&API_METHOD_MOUNT)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["snapshot", "archive-name", "target"])
|
2019-09-19 14:33:09 +00:00
|
|
|
.completion_cb("repository", complete_repository)
|
|
|
|
.completion_cb("snapshot", complete_group_or_snapshot)
|
2019-12-09 12:14:32 +00:00
|
|
|
.completion_cb("archive-name", complete_pxar_archive_name)
|
2019-09-19 14:33:09 +00:00
|
|
|
.completion_cb("target", tools::complete_file_name);
|
2019-08-10 07:12:17 +00:00
|
|
|
|
2019-11-21 14:00:01 +00:00
|
|
|
|
2019-01-21 17:58:14 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("backup", backup_cmd_def)
|
|
|
|
.insert("upload-log", upload_log_cmd_def)
|
|
|
|
.insert("forget", forget_cmd_def)
|
|
|
|
.insert("garbage-collect", garbage_collect_cmd_def)
|
|
|
|
.insert("list", list_cmd_def)
|
|
|
|
.insert("login", login_cmd_def)
|
|
|
|
.insert("logout", logout_cmd_def)
|
|
|
|
.insert("prune", prune_cmd_def)
|
|
|
|
.insert("restore", restore_cmd_def)
|
|
|
|
.insert("snapshots", snapshots_cmd_def)
|
|
|
|
.insert("files", files_cmd_def)
|
|
|
|
.insert("status", status_cmd_def)
|
|
|
|
.insert("key", key_mgmt_cli())
|
|
|
|
.insert("mount", mount_cmd_def)
|
2019-12-10 12:43:53 +00:00
|
|
|
.insert("catalog", catalog_mgmt_cli())
|
|
|
|
.insert("task", task_mgmt_cli());
|
2019-12-09 16:40:34 +00:00
|
|
|
|
|
|
|
run_cli_command(cmd_def);
|
2019-08-28 15:20:32 +00:00
|
|
|
}
|
2019-03-12 13:39:51 +00:00
|
|
|
|
2019-08-28 15:20:32 +00:00
|
|
|
fn async_main<F: Future>(fut: F) -> <F as Future>::Output {
|
|
|
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
let ret = rt.block_on(fut);
|
|
|
|
rt.shutdown_now();
|
|
|
|
ret
|
2018-12-14 07:28:56 +00:00
|
|
|
}
|