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