2018-12-20 09:32:49 +00:00
|
|
|
extern crate proxmox_backup;
|
2018-12-14 07:28:56 +00:00
|
|
|
|
|
|
|
use failure::*;
|
2019-01-18 15:50:15 +00:00
|
|
|
//use std::os::unix::io::AsRawFd;
|
2019-03-04 08:38:34 +00:00
|
|
|
use chrono::{DateTime, Local, TimeZone};
|
2019-03-01 08:35:41 +00:00
|
|
|
use std::path::Path;
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2018-12-20 09:32:49 +00:00
|
|
|
use proxmox_backup::tools;
|
2019-02-21 08:07:25 +00:00
|
|
|
use proxmox_backup::cli::*;
|
2019-02-17 09:16:33 +00:00
|
|
|
use proxmox_backup::api_schema::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
use proxmox_backup::api_schema::router::*;
|
2019-02-14 10:11:39 +00:00
|
|
|
use proxmox_backup::client::*;
|
2019-02-19 14:19:12 +00:00
|
|
|
use proxmox_backup::backup::*;
|
2018-12-20 09:32:49 +00:00
|
|
|
//use proxmox_backup::backup::image_index::*;
|
|
|
|
//use proxmox_backup::config::datastore;
|
2019-01-17 10:38:22 +00:00
|
|
|
//use proxmox_backup::catar::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-01-17 10:38:22 +00:00
|
|
|
use hyper::Body;
|
2019-02-13 11:30:52 +00:00
|
|
|
use std::sync::Arc;
|
2019-02-26 11:27:28 +00:00
|
|
|
use regex::Regex;
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref BACKUPSPEC_REGEX: Regex = Regex::new(r"^([a-zA-Z0-9_-]+):(.+)$").unwrap();
|
|
|
|
}
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-03-01 08:35:41 +00:00
|
|
|
fn backup_directory<P: AsRef<Path>>(
|
2019-02-26 11:27:28 +00:00
|
|
|
client: &mut HttpClient,
|
2019-02-19 14:19:12 +00:00
|
|
|
repo: &BackupRepository,
|
2019-03-01 08:35:41 +00:00
|
|
|
dir_path: P,
|
2019-02-19 14:19:12 +00:00
|
|
|
archive_name: &str,
|
2019-03-06 06:02:52 +00:00
|
|
|
backup_id: &str,
|
2019-03-04 08:38:34 +00:00
|
|
|
backup_time: DateTime<Local>,
|
2019-02-19 14:19:12 +00:00
|
|
|
chunk_size: Option<u64>,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-02-19 14:19:12 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-03-03 08:44:25 +00:00
|
|
|
let mut param = json!({
|
|
|
|
"archive-name": archive_name,
|
2019-03-03 08:48:32 +00:00
|
|
|
"backup-type": "host",
|
2019-03-06 06:02:52 +00:00
|
|
|
"backup-id": backup_id,
|
2019-03-03 08:48:32 +00:00
|
|
|
"backup-time": backup_time.timestamp(),
|
2019-03-03 08:44:25 +00:00
|
|
|
});
|
2019-02-19 14:19:12 +00:00
|
|
|
|
|
|
|
if let Some(size) = chunk_size {
|
2019-03-03 08:44:25 +00:00
|
|
|
param["chunk-size"] = size.into();
|
2019-02-19 14:19:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 08:44:25 +00:00
|
|
|
let query = tools::json_object_to_query(param)?;
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query);
|
2019-01-02 10:02:56 +00:00
|
|
|
|
2019-03-04 07:01:09 +00:00
|
|
|
let stream = CaTarBackupStream::open(dir_path.as_ref(), verbose)?;
|
2019-03-01 08:35:41 +00:00
|
|
|
|
|
|
|
let body = Body::wrap_stream(stream);
|
|
|
|
|
2019-01-17 11:43:29 +00:00
|
|
|
client.upload("application/x-proxmox-backup-catar", body, &path)?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:38:22 +00:00
|
|
|
/****
|
2018-12-27 09:11:11 +00:00
|
|
|
fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
|
|
|
|
|
2019-01-17 10:38:22 +00:00
|
|
|
let mut target = PathBuf::from(target);
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
if let Some(ext) = target.extension() {
|
2019-02-12 10:50:45 +00:00
|
|
|
if ext != "fidx" {
|
|
|
|
bail!("got wrong file extension - expected '.fidx'");
|
2018-12-27 09:11:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-12 10:50:45 +00:00
|
|
|
target.set_extension("fidx");
|
2018-12-27 09:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
|
|
|
|
|
|
|
|
tools::file_chunker(file, chunk_size, |pos, chunk| {
|
|
|
|
index.add_chunk(pos, chunk)?;
|
|
|
|
Ok(true)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
index.close()?; // commit changes
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-17 10:38:22 +00:00
|
|
|
*/
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn list_backups(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2019-01-21 17:58:14 +00:00
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
2019-01-21 17:58:14 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
2019-01-21 17:58:14 +00:00
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let path = format!("api2/json/admin/datastore/{}/backups", repo.store);
|
2019-01-21 17:58:14 +00:00
|
|
|
|
|
|
|
let result = client.get(&path)?;
|
|
|
|
|
2019-02-20 12:37:44 +00:00
|
|
|
// fixme: implement and use output formatter instead ..
|
|
|
|
let list = result["data"].as_array().unwrap();
|
|
|
|
|
|
|
|
for item in list {
|
|
|
|
|
2019-03-02 15:34:10 +00:00
|
|
|
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-01 16:36:40 +00:00
|
|
|
|
2019-03-05 08:16:54 +00:00
|
|
|
let backup_dir = BackupDir::new(btype, id, epoch);
|
2019-03-01 16:36:40 +00:00
|
|
|
|
|
|
|
let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
|
|
|
|
|
|
|
|
let info = BackupInfo { backup_dir, files };
|
|
|
|
|
|
|
|
for filename in info.files {
|
|
|
|
let path = info.backup_dir.relative_path().to_str().unwrap().to_owned();
|
2019-03-04 16:58:22 +00:00
|
|
|
println!("{} | {}/{}", info.backup_dir.backup_time().format("%c"), path, filename);
|
2019-02-20 12:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ok(result)
|
|
|
|
Ok(Value::Null)
|
2019-01-21 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 10:29:05 +00:00
|
|
|
fn list_backup_groups(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/groups", repo.store);
|
|
|
|
|
2019-03-06 06:20:51 +00:00
|
|
|
let mut result = client.get(&path)?;
|
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
|
|
|
|
|
|
|
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-03-02 10:29:05 +00:00
|
|
|
let last_backup = Local.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
|
|
|
|
|
|
|
let files = item["files"].as_array().unwrap().iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.as_str().unwrap().to_owned()
|
|
|
|
}).collect();
|
|
|
|
|
2019-03-06 06:20:51 +00:00
|
|
|
println!("{:20} | {} | {:5} | {}", path, last_backup.format("%c"),
|
2019-03-02 15:28:36 +00:00
|
|
|
backup_count, tools::join(&files, ' '));
|
2019-03-02 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Ok(result)
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
fn list_snapshots(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "group")?;
|
|
|
|
let group = BackupGroup::parse(path)?;
|
|
|
|
|
2019-03-03 07:51:37 +00:00
|
|
|
let query = tools::json_object_to_query(json!({
|
2019-03-04 12:38:23 +00:00
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
2019-03-03 07:51:37 +00:00
|
|
|
}))?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store, query);
|
|
|
|
|
|
|
|
// fixme: params
|
|
|
|
let result = client.get(&path)?;
|
|
|
|
|
|
|
|
// fixme: implement and use output formatter instead ..
|
|
|
|
let list = result["data"].as_array().unwrap();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let files = item["files"].as_array().unwrap().iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.as_str().unwrap().to_owned()
|
|
|
|
}).collect();
|
|
|
|
|
2019-03-04 16:58:22 +00:00
|
|
|
println!("{} | {} | {}", path, snapshot.backup_time().format("%c"), tools::join(&files, ' '));
|
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,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
|
|
|
|
let query = tools::json_object_to_query(json!({
|
2019-03-04 12:51:36 +00:00
|
|
|
"backup-type": snapshot.group().backup_type(),
|
|
|
|
"backup-id": snapshot.group().backup_id(),
|
|
|
|
"backup-time": snapshot.backup_time().timestamp(),
|
2019-03-03 10:29:00 +00:00
|
|
|
}))?;
|
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store, query);
|
|
|
|
|
|
|
|
let result = client.delete(&path)?;
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:10:45 +00:00
|
|
|
fn start_garbage_collection(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
2019-02-20 13:10:45 +00:00
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/gc", repo.store);
|
|
|
|
|
|
|
|
let result = client.post(&path)?;
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
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> {
|
|
|
|
|
|
|
|
if let Some(caps) = BACKUPSPEC_REGEX.captures(value) {
|
|
|
|
return Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()));
|
|
|
|
}
|
|
|
|
bail!("unable to parse directory specification '{}'", value);
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn create_backup(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
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-02-13 11:30:52 +00:00
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
2019-03-04 07:01:09 +00:00
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-02-19 14:19:12 +00:00
|
|
|
let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024);
|
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-03-06 06:02:52 +00:00
|
|
|
let backup_id = param["host-id"].as_str().unwrap_or(&tools::nodename());
|
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
let mut upload_list = vec![];
|
2018-12-14 12:39:41 +00:00
|
|
|
|
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-02-26 11:27:28 +00:00
|
|
|
let stat = match nix::sys::stat::stat(filename) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(err) => bail!("unable to access '{}' - {}", filename, err),
|
|
|
|
};
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
if (stat.st_mode & libc::S_IFDIR) != 0 {
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
let target = format!("{}.catar", target);
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-03-01 05:48:41 +00:00
|
|
|
upload_list.push((filename.to_owned(), target));
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
} else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
|
|
|
|
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
|
|
|
|
let _size = stat.st_size as usize;
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
panic!("implement me");
|
2018-12-21 07:36:57 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
//backup_image(&datastore, &file, size, &target, chunk_size)?;
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-02-26 11:27:28 +00:00
|
|
|
// let idx = datastore.open_image_reader(target)?;
|
|
|
|
// idx.print_info();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
bail!("unsupported file type (expected a directory, file or block device)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 08:38:34 +00:00
|
|
|
let backup_time = Local.timestamp(Local::now().timestamp(), 0);
|
2019-02-26 11:27:28 +00:00
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
2019-03-01 05:48:41 +00:00
|
|
|
client.login()?; // login before starting backup
|
|
|
|
|
2019-03-04 08:38:34 +00:00
|
|
|
println!("Starting backup");
|
|
|
|
println!("Client name: {}", tools::nodename());
|
|
|
|
println!("Start Time: {}", backup_time.to_rfc3339());
|
2019-03-01 05:48:41 +00:00
|
|
|
|
2019-03-01 08:35:41 +00:00
|
|
|
for (filename, target) in upload_list {
|
2019-03-04 08:38:34 +00:00
|
|
|
println!("Upload '{}' to '{:?}' as {}", filename, repo, target);
|
2019-03-06 06:02:52 +00:00
|
|
|
backup_directory(&mut client, &repo, &filename, &target, backup_id, backup_time, chunk_size_opt, verbose)?;
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 08:38:34 +00:00
|
|
|
let end_time = Local.timestamp(Local::now().timestamp(), 0);
|
2019-03-05 07:11:40 +00:00
|
|
|
let elapsed = end_time.signed_duration_since(backup_time);
|
|
|
|
println!("Duration: {}", elapsed);
|
|
|
|
|
2019-03-04 08:38:34 +00:00
|
|
|
println!("End Time: {}", end_time.to_rfc3339());
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
Ok(Value::Null)
|
2019-02-27 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn complete_backup_source(arg: &str) -> Vec<String> {
|
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let data: Vec<&str> = arg.splitn(2, ':').collect();
|
|
|
|
|
|
|
|
if data.len() != 2 { return result; }
|
|
|
|
|
|
|
|
let files = tools::complete_file_name(data[1]);
|
|
|
|
|
|
|
|
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,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
|
|
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
|
|
|
let snapshot = BackupDir::parse(path)?;
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}))?;
|
|
|
|
|
|
|
|
let target_path = tools::required_string_param(¶m, "target")?;
|
|
|
|
if let Err(err) = std::fs::create_dir(target_path) {
|
|
|
|
bail!("unable to create target directory - {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store, query);
|
|
|
|
let result = client.get(&path)?;
|
|
|
|
|
|
|
|
let files = result["data"].as_array().unwrap();
|
|
|
|
|
|
|
|
for file in files {
|
|
|
|
let file = file.as_str().unwrap();
|
|
|
|
|
|
|
|
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(),
|
|
|
|
"archive-name": file,
|
|
|
|
}))?;
|
|
|
|
|
2019-03-06 10:18:46 +00:00
|
|
|
if file.ends_with(".catar.didx") {
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query);
|
|
|
|
let mut target = std::path::PathBuf::from(target_path);
|
|
|
|
target.push(file);
|
|
|
|
target.set_extension("");
|
|
|
|
|
|
|
|
let fh = std::fs::OpenOptions::new()
|
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
|
|
|
.open(&target)?;
|
|
|
|
|
|
|
|
println!("DOWNLOAD FILE {} to {:?}", path, target);
|
|
|
|
client.download(&path, Box::new(fh))?;
|
|
|
|
} else {
|
|
|
|
bail!("unknown file extensions - unable to download '{}'", file);
|
|
|
|
}
|
2019-03-06 09:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:53:17 +00:00
|
|
|
fn prune(
|
|
|
|
mut param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
|
|
|
let mut client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/prune", repo.store);
|
|
|
|
|
|
|
|
param.as_object_mut().unwrap().remove("repository");
|
|
|
|
|
|
|
|
let result = client.post_json(&path, param)?;
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
fn main() {
|
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let repo_url_schema: Arc<Schema> = Arc::new(
|
|
|
|
StringSchema::new("Repository URL.")
|
|
|
|
.format(BACKUP_REPO_URL.clone())
|
|
|
|
.max_length(256)
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
|
2019-02-27 06:21:57 +00:00
|
|
|
let backup_source_schema: Arc<Schema> = Arc::new(
|
|
|
|
StringSchema::new("Backup source specification ([<label>:<path>]).")
|
|
|
|
.format(Arc::new(ApiStringFormat::Pattern(&BACKUPSPEC_REGEX)))
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
|
2019-03-03 10:46:53 +00:00
|
|
|
let backup_cmd_def = CliCommand::new(
|
2018-12-14 07:28:56 +00:00
|
|
|
ApiMethod::new(
|
2018-12-27 09:11:11 +00:00
|
|
|
create_backup,
|
2019-03-03 10:46:53 +00:00
|
|
|
ObjectSchema::new("Create (host) backup.")
|
2019-02-13 11:30:52 +00:00
|
|
|
.required("repository", repo_url_schema.clone())
|
2019-02-26 11:27:28 +00:00
|
|
|
.required(
|
|
|
|
"backupspec",
|
|
|
|
ArraySchema::new(
|
|
|
|
"List of backup source specifications ([<label>:<path>] ...)",
|
2019-02-27 06:21:57 +00:00
|
|
|
backup_source_schema,
|
2019-02-26 11:27:28 +00:00
|
|
|
).min_length(1)
|
|
|
|
)
|
2019-03-04 07:01:09 +00:00
|
|
|
.optional(
|
|
|
|
"verbose",
|
|
|
|
BooleanSchema::new("Verbose output.").default(false))
|
2019-03-06 06:02:52 +00:00
|
|
|
.optional(
|
|
|
|
"host-id",
|
|
|
|
StringSchema::new("Use specified ID for the backup group name ('host/<id>'). The default is the system hostname."))
|
2018-12-21 10:18:08 +00:00
|
|
|
.optional(
|
|
|
|
"chunk-size",
|
|
|
|
IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
|
|
|
|
.minimum(64)
|
|
|
|
.maximum(4096)
|
|
|
|
.default(4096)
|
|
|
|
)
|
2018-12-14 07:28:56 +00:00
|
|
|
))
|
2019-02-26 11:27:28 +00:00
|
|
|
.arg_param(vec!["repository", "backupspec"])
|
2019-02-27 07:38:32 +00:00
|
|
|
.completion_cb("backupspec", complete_backup_source);
|
2018-12-15 10:24:39 +00:00
|
|
|
|
2019-01-21 17:58:14 +00:00
|
|
|
let list_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
2019-03-02 10:29:05 +00:00
|
|
|
list_backup_groups,
|
|
|
|
ObjectSchema::new("List backup groups.")
|
2019-02-13 11:30:52 +00:00
|
|
|
.required("repository", repo_url_schema.clone())
|
2019-01-21 17:58:14 +00:00
|
|
|
))
|
2019-02-13 11:30:52 +00:00
|
|
|
.arg_param(vec!["repository"]);
|
2019-01-21 17:58:14 +00:00
|
|
|
|
2019-03-02 15:20:50 +00:00
|
|
|
let snapshots_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
list_snapshots,
|
|
|
|
ObjectSchema::new("List backup snapshots.")
|
|
|
|
.required("repository", repo_url_schema.clone())
|
|
|
|
.required("group", StringSchema::new("Backup group."))
|
|
|
|
))
|
|
|
|
.arg_param(vec!["repository", "group"]);
|
|
|
|
|
2019-03-03 10:29:00 +00:00
|
|
|
let forget_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
forget_snapshots,
|
|
|
|
ObjectSchema::new("Forget (remove) backup snapshots.")
|
|
|
|
.required("repository", repo_url_schema.clone())
|
|
|
|
.required("snapshot", StringSchema::new("Snapshot path."))
|
|
|
|
))
|
|
|
|
.arg_param(vec!["repository", "snapshot"]);
|
|
|
|
|
2019-02-20 13:10:45 +00:00
|
|
|
let garbage_collect_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
start_garbage_collection,
|
|
|
|
ObjectSchema::new("Start garbage collection for a specific repository.")
|
|
|
|
.required("repository", repo_url_schema.clone())
|
|
|
|
))
|
|
|
|
.arg_param(vec!["repository"]);
|
|
|
|
|
2019-03-06 09:50:46 +00:00
|
|
|
let restore_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
restore,
|
|
|
|
ObjectSchema::new("Restore backup repository.")
|
|
|
|
.required("repository", repo_url_schema.clone())
|
|
|
|
.required("snapshot", StringSchema::new("Snapshot path."))
|
|
|
|
.required("target", StringSchema::new("Target directory path."))
|
|
|
|
))
|
|
|
|
.arg_param(vec!["repository", "snapshot", "target"]);
|
|
|
|
|
2019-02-27 15:53:17 +00:00
|
|
|
let prune_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
prune,
|
|
|
|
proxmox_backup::api2::admin::datastore::add_common_prune_prameters(
|
|
|
|
ObjectSchema::new("Prune backup repository.")
|
|
|
|
.required("repository", repo_url_schema.clone())
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.arg_param(vec!["repository"]);
|
2019-03-06 09:50:46 +00:00
|
|
|
|
2019-01-21 17:58:14 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-03-03 10:46:53 +00:00
|
|
|
.insert("backup".to_owned(), backup_cmd_def.into())
|
2019-03-03 10:29:00 +00:00
|
|
|
.insert("forget".to_owned(), forget_cmd_def.into())
|
2019-02-20 13:10:45 +00:00
|
|
|
.insert("garbage-collect".to_owned(), garbage_collect_cmd_def.into())
|
2019-02-27 15:53:17 +00:00
|
|
|
.insert("list".to_owned(), list_cmd_def.into())
|
2019-03-02 15:20:50 +00:00
|
|
|
.insert("prune".to_owned(), prune_cmd_def.into())
|
2019-03-06 09:50:46 +00:00
|
|
|
.insert("restore".to_owned(), restore_cmd_def.into())
|
2019-03-02 15:20:50 +00:00
|
|
|
.insert("snapshots".to_owned(), snapshots_cmd_def.into());
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
run_cli_command(cmd_def.into());
|
2018-12-14 07:28:56 +00:00
|
|
|
}
|