2018-12-20 09:32:49 +00:00
|
|
|
extern crate proxmox_backup;
|
2018-12-14 07:28:56 +00:00
|
|
|
|
|
|
|
use failure::*;
|
2018-12-15 13:51:05 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2018-12-20 09:32:49 +00:00
|
|
|
use proxmox_backup::tools;
|
|
|
|
use proxmox_backup::cli::command::*;
|
|
|
|
use proxmox_backup::api::schema::*;
|
|
|
|
use proxmox_backup::api::router::*;
|
|
|
|
//use proxmox_backup::backup::chunk_store::*;
|
|
|
|
//use proxmox_backup::backup::image_index::*;
|
|
|
|
//use proxmox_backup::config::datastore;
|
|
|
|
use proxmox_backup::backup::datastore::*;
|
2018-12-15 10:14:41 +00:00
|
|
|
use serde_json::{Value};
|
2018-12-14 07:28:56 +00:00
|
|
|
|
2018-12-14 12:39:41 +00:00
|
|
|
fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
|
|
|
|
param[name].as_str().expect(&format!("missing parameter '{}'", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
|
|
|
|
2018-12-14 12:39:41 +00:00
|
|
|
let filename = required_string_param(¶m, "filename");
|
|
|
|
let store = required_string_param(¶m, "store");
|
2018-12-20 13:09:31 +00:00
|
|
|
let target = required_string_param(¶m, "target");
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-17 12:00:39 +00:00
|
|
|
let mut datastore = DataStore::open(store)?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
|
|
|
println!("Backup file '{}' to '{}'", filename, store);
|
|
|
|
|
2018-12-20 13:09:31 +00:00
|
|
|
let mut target = std::path::PathBuf::from(target);
|
|
|
|
if let Some(ext) = target.extension() {
|
|
|
|
if ext != "iidx" {
|
|
|
|
bail!("got wrong file extension - expected '.iidx'");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target.set_extension("iidx");
|
|
|
|
}
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2018-12-16 13:44:44 +00:00
|
|
|
{
|
|
|
|
let file = std::fs::File::open(filename)?;
|
|
|
|
let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
|
|
|
|
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
|
|
|
|
let size = stat.st_size as usize;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-20 13:09:31 +00:00
|
|
|
let mut index = datastore.create_image_writer(&target, size)?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-16 13:44:44 +00:00
|
|
|
tools::file_chunker(file, 64*1024, |pos, chunk| {
|
|
|
|
index.add_chunk(pos, chunk)?;
|
|
|
|
Ok(true)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
index.close()?; // commit changes
|
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
datastore.garbage_collection()?;
|
|
|
|
|
2018-12-17 12:00:39 +00:00
|
|
|
let idx = datastore.open_image_reader(target)?;
|
2018-12-16 13:44:44 +00:00
|
|
|
idx.print_info();
|
2018-12-16 12:39:21 +00:00
|
|
|
|
2018-12-14 07:28:56 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
|
|
|
let cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
backup_file,
|
|
|
|
ObjectSchema::new("Create backup from file.")
|
|
|
|
.required("filename", StringSchema::new("Source file name."))
|
|
|
|
.required("store", StringSchema::new("Datastore name."))
|
2018-12-20 13:09:31 +00:00
|
|
|
.required("target", StringSchema::new("Target name."))
|
2018-12-14 07:28:56 +00:00
|
|
|
))
|
2018-12-20 13:09:31 +00:00
|
|
|
.arg_param(vec!["filename", "target"])
|
2018-12-20 09:32:49 +00:00
|
|
|
.completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
|
2018-12-15 10:24:39 +00:00
|
|
|
|
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);
|
|
|
|
print_cli_usage();
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|