]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/backup-client.rs
start implementing catar
[proxmox-backup.git] / src / bin / backup-client.rs
CommitLineData
fe0e04c6 1extern crate proxmox_backup;
ff5d3707 2
3use failure::*;
606ce64b 4use std::os::unix::io::AsRawFd;
ff5d3707 5
fe0e04c6
DM
6use proxmox_backup::tools;
7use proxmox_backup::cli::command::*;
8use proxmox_backup::api::schema::*;
9use proxmox_backup::api::router::*;
10//use proxmox_backup::backup::chunk_store::*;
11//use proxmox_backup::backup::image_index::*;
12//use proxmox_backup::config::datastore;
13use proxmox_backup::backup::datastore::*;
43eeef28 14use serde_json::{Value};
ff5d3707 15
a914a774 16fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
17 param[name].as_str().expect(&format!("missing parameter '{}'", name))
18}
19
ff5d3707 20fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
21
a914a774 22 let filename = required_string_param(&param, "filename");
23 let store = required_string_param(&param, "store");
c34eb166 24 let target = required_string_param(&param, "target");
a914a774 25
2d9d143a
DM
26 let mut chunk_size = 4*1024*1024;
27
28 if let Some(size) = param["chunk-size"].as_u64() {
29 static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
30
31 if SIZES.contains(&size) {
32 chunk_size = (size as usize) * 1024;
33 } else {
34 bail!("Got unsupported chunk size '{}'", size);
35 }
36 }
37
529de6c7 38 let mut datastore = DataStore::open(store)?;
a914a774 39
40 println!("Backup file '{}' to '{}'", filename, store);
41
c34eb166
DM
42 let mut target = std::path::PathBuf::from(target);
43 if let Some(ext) = target.extension() {
44 if ext != "iidx" {
45 bail!("got wrong file extension - expected '.iidx'");
46 }
47 } else {
48 target.set_extension("iidx");
49 }
606ce64b 50
4818c8b6
DM
51 {
52 let file = std::fs::File::open(filename)?;
53 let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
54 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
55 let size = stat.st_size as usize;
a914a774 56
d62e6e22
DM
57 let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
58
59 tools::file_chunker(file, chunk_size, |pos, chunk| {
4818c8b6
DM
60 index.add_chunk(pos, chunk)?;
61 Ok(true)
62 })?;
63
64 index.close()?; // commit changes
65 }
66
f0819fe5 67 //datastore.garbage_collection()?;
3d5c11e5 68
529de6c7 69 let idx = datastore.open_image_reader(target)?;
4818c8b6 70 idx.print_info();
4fbb72a8 71
ff5d3707 72 Ok(Value::Null)
73}
74
75
76fn main() {
77
78 let cmd_def = CliCommand::new(
79 ApiMethod::new(
80 backup_file,
81 ObjectSchema::new("Create backup from file.")
82 .required("filename", StringSchema::new("Source file name."))
83 .required("store", StringSchema::new("Datastore name."))
c34eb166 84 .required("target", StringSchema::new("Target name."))
2d9d143a
DM
85 .optional(
86 "chunk-size",
87 IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
88 .minimum(64)
89 .maximum(4096)
90 .default(4096)
91 )
ff5d3707 92 ))
c34eb166 93 .arg_param(vec!["filename", "target"])
fe0e04c6 94 .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
f8838fe9 95
a914a774 96
ff5d3707 97 if let Err(err) = run_cli_command(&cmd_def.into()) {
98 eprintln!("Error: {}", err);
99 print_cli_usage();
100 std::process::exit(-1);
101 }
102
103}