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;
|
2018-12-27 12:15:10 +00:00
|
|
|
use proxmox_backup::catar::encoder::*;
|
2018-12-20 09:32:49 +00:00
|
|
|
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-27 12:15:10 +00:00
|
|
|
fn backup_dir(
|
|
|
|
datastore: &DataStore,
|
|
|
|
path: &str,
|
|
|
|
dir: &mut nix::dir::Dir,
|
|
|
|
target: &str,
|
|
|
|
chunk_size: usize,
|
|
|
|
) -> Result<(), Error> {
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
let mut target = std::path::PathBuf::from(target);
|
|
|
|
|
|
|
|
if let Some(ext) = target.extension() {
|
|
|
|
if ext != "aidx" {
|
|
|
|
bail!("got wrong file extension - expected '.aidx'");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target.set_extension("aidx");
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
// fixme: implement chunked writer
|
2018-12-31 12:01:06 +00:00
|
|
|
// let writer = std::fs::OpenOptions::new()
|
|
|
|
// .create(true)
|
|
|
|
// .write(true)
|
|
|
|
// .truncate(true)
|
|
|
|
// .open("mytest.catar")?;
|
|
|
|
|
2018-12-31 17:01:07 +00:00
|
|
|
let index = datastore.create_archive_writer(&target, chunk_size)?;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
let path = std::path::PathBuf::from(path);
|
|
|
|
|
2018-12-31 16:30:08 +00:00
|
|
|
CaTarEncoder::encode(path, dir, index)?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
2018-12-14 07:28:56 +00:00
|
|
|
|
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-21 10:18:08 +00:00
|
|
|
let mut chunk_size = 4*1024*1024;
|
|
|
|
|
|
|
|
if let Some(size) = param["chunk-size"].as_u64() {
|
|
|
|
static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
|
|
|
|
|
|
|
|
if SIZES.contains(&size) {
|
|
|
|
chunk_size = (size as usize) * 1024;
|
|
|
|
} else {
|
|
|
|
bail!("Got unsupported chunk size '{}'", size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
let datastore = DataStore::open(store)?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
let file = std::fs::File::open(filename)?;
|
2018-12-27 12:15:10 +00:00
|
|
|
let rawfd = file.as_raw_fd();
|
|
|
|
let stat = nix::sys::stat::fstat(rawfd)?;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
if (stat.st_mode & libc::S_IFDIR) != 0 {
|
|
|
|
println!("Backup directory '{}' to '{}'", filename, store);
|
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
let mut dir = nix::dir::Dir::from_fd(rawfd)?;
|
|
|
|
|
|
|
|
backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?;
|
2018-12-27 09:11:11 +00:00
|
|
|
|
|
|
|
} else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
|
|
|
|
println!("Backup file '{}' to '{}'", filename, store);
|
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); }
|
|
|
|
let size = stat.st_size as usize;
|
2018-12-14 12:39:41 +00:00
|
|
|
|
2018-12-27 09:11:11 +00:00
|
|
|
backup_image(&datastore, &file, size, &target, chunk_size)?;
|
2018-12-21 07:36:57 +00:00
|
|
|
|
2018-12-27 09:11:11 +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() {
|
|
|
|
|
|
|
|
let cmd_def = CliCommand::new(
|
|
|
|
ApiMethod::new(
|
2018-12-27 09:11:11 +00:00
|
|
|
create_backup,
|
|
|
|
ObjectSchema::new("Create backup.")
|
|
|
|
.required("filename", StringSchema::new("Source name (file or directory name)"))
|
2018-12-14 07:28:56 +00:00
|
|
|
.required("store", StringSchema::new("Datastore 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
|
|
|
))
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|