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-02-20 12:37:44 +00:00
|
|
|
use chrono::{Local, TimeZone};
|
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
|
|
|
|
2018-12-15 10:14:41 +00:00
|
|
|
use serde_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-19 14:19:12 +00:00
|
|
|
fn backup_directory(
|
|
|
|
repo: &BackupRepository,
|
|
|
|
body: Body,
|
|
|
|
archive_name: &str,
|
|
|
|
chunk_size: Option<u64>,
|
|
|
|
) -> Result<(), Error> {
|
2019-02-13 11:30:52 +00:00
|
|
|
|
2019-02-13 13:31:43 +00:00
|
|
|
let client = HttpClient::new(&repo.host, &repo.user);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-01-18 11:01:37 +00:00
|
|
|
let epoch = std::time::SystemTime::now().duration_since(
|
|
|
|
std::time::SystemTime::UNIX_EPOCH)?.as_secs();
|
|
|
|
|
2019-02-19 14:19:12 +00:00
|
|
|
let mut query = url::form_urlencoded::Serializer::new(String::new());
|
|
|
|
|
|
|
|
query
|
2019-01-18 11:01:37 +00:00
|
|
|
.append_pair("archive_name", archive_name)
|
|
|
|
.append_pair("type", "host")
|
|
|
|
.append_pair("id", &tools::nodename())
|
2019-02-19 14:19:12 +00:00
|
|
|
.append_pair("time", &epoch.to_string());
|
|
|
|
|
|
|
|
if let Some(size) = chunk_size {
|
|
|
|
query.append_pair("chunk-size", &size.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let query = query.finish();
|
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-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-13 13:31:43 +00:00
|
|
|
let 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 {
|
|
|
|
|
|
|
|
let id = item["backup_id"].as_str(). unwrap();
|
|
|
|
let btype = item["backup_type"].as_str(). unwrap();
|
|
|
|
let epoch = item["backup_time"].as_i64(). unwrap();
|
|
|
|
|
|
|
|
let time_str = Local.timestamp(epoch, 0).format("%c");
|
|
|
|
|
|
|
|
let files = item["files"].as_array().unwrap();
|
|
|
|
|
|
|
|
for file in files {
|
|
|
|
let filename = file.as_str().unwrap();
|
|
|
|
println!("| {} | {} | {} | {}", btype, id, time_str, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ok(result)
|
|
|
|
Ok(Value::Null)
|
2019-01-21 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
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)?;
|
|
|
|
|
|
|
|
let client = HttpClient::new(&repo.host, &repo.user);
|
|
|
|
|
|
|
|
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-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-01-17 11:14:02 +00:00
|
|
|
let filename = tools::required_string_param(¶m, "filename")?;
|
2019-02-13 11:30:52 +00:00
|
|
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
2019-01-17 11:14:02 +00:00
|
|
|
let target = tools::required_string_param(¶m, "target")?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-02-13 11:30:52 +00:00
|
|
|
let repo = BackupRepository::parse(repo_url)?;
|
|
|
|
|
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-01-17 10:38:22 +00:00
|
|
|
let stat = match nix::sys::stat::stat(filename) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(err) => bail!("unable to access '{}' - {}", filename, err),
|
|
|
|
};
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
if (stat.st_mode & libc::S_IFDIR) != 0 {
|
2019-02-13 11:30:52 +00:00
|
|
|
println!("Backup directory '{}' to '{:?}'", filename, repo);
|
2018-12-27 09:11:11 +00:00
|
|
|
|
2019-01-17 10:38:22 +00:00
|
|
|
let stream = CaTarBackupStream::open(filename)?;
|
|
|
|
|
|
|
|
let body = Body::wrap_stream(stream);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-02-19 14:19:12 +00:00
|
|
|
backup_directory(&repo, body, target, chunk_size_opt)?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
} else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
|
2019-02-13 11:30:52 +00:00
|
|
|
println!("Backup image '{}' to '{:?}'", filename, repo);
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2018-12-16 13:44:44 +00:00
|
|
|
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
|
2019-01-18 15:50:15 +00:00
|
|
|
let _size = stat.st_size as usize;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2019-01-17 10:38:22 +00:00
|
|
|
panic!("implement me");
|
|
|
|
|
|
|
|
//backup_image(&datastore, &file, size, &target, chunk_size)?;
|
2018-12-21 07:36:57 +00:00
|
|
|
|
2019-01-02 11:55:18 +00:00
|
|
|
// let idx = datastore.open_image_reader(target)?;
|
|
|
|
// idx.print_info();
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
} else {
|
|
|
|
bail!("unsupported file type (expected a directory, file or block device)");
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 08:24:55 +00:00
|
|
|
//datastore.garbage_collection()?;
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-21 17:58:14 +00:00
|
|
|
let create_cmd_def = CliCommand::new(
|
2018-12-14 07:28:56 +00:00
|
|
|
ApiMethod::new(
|
2018-12-27 09:11:11 +00:00
|
|
|
create_backup,
|
|
|
|
ObjectSchema::new("Create backup.")
|
2019-02-13 11:30:52 +00:00
|
|
|
.required("repository", repo_url_schema.clone())
|
2018-12-27 09:11:11 +00:00
|
|
|
.required("filename", StringSchema::new("Source name (file or directory name)"))
|
2018-12-20 13:09:31 +00:00
|
|
|
.required("target", StringSchema::new("Target name."))
|
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-13 11:30:52 +00:00
|
|
|
.arg_param(vec!["repository", "filename", "target"])
|
|
|
|
.completion_cb("filename", tools::complete_file_name);
|
2018-12-15 10:24:39 +00:00
|
|
|
|
2019-01-21 17:58:14 +00:00
|
|
|
let list_cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
list_backups,
|
|
|
|
ObjectSchema::new("List backups.")
|
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-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-01-21 17:58:14 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert("create".to_owned(), create_cmd_def.into())
|
2019-02-20 13:10:45 +00:00
|
|
|
.insert("garbage-collect".to_owned(), garbage_collect_cmd_def.into())
|
2019-01-21 17:58:14 +00:00
|
|
|
.insert("list".to_owned(), list_cmd_def.into());
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
if let Err(err) = run_cli_command(&cmd_def.into()) {
|
|
|
|
eprintln!("Error: {}", err);
|
2019-01-03 13:36:31 +00:00
|
|
|
if err.downcast::<UsageError>().is_ok() {
|
|
|
|
print_cli_usage();
|
|
|
|
}
|
2018-12-14 07:28:56 +00:00
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|