]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/backup-client.rs
add test code to access static global state
[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
20
ff5d3707 21fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
22
a914a774 23 let filename = required_string_param(&param, "filename");
24 let store = required_string_param(&param, "store");
25
529de6c7 26 let mut datastore = DataStore::open(store)?;
a914a774 27
28 println!("Backup file '{}' to '{}'", filename, store);
29
4818c8b6 30 let target = "test1.idx";
606ce64b 31
4818c8b6
DM
32 {
33 let file = std::fs::File::open(filename)?;
34 let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
35 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
36 let size = stat.st_size as usize;
a914a774 37
529de6c7 38 let mut index = datastore.create_image_writer(target, size)?;
a914a774 39
4818c8b6
DM
40 tools::file_chunker(file, 64*1024, |pos, chunk| {
41 index.add_chunk(pos, chunk)?;
42 Ok(true)
43 })?;
44
45 index.close()?; // commit changes
46 }
47
3d5c11e5
DM
48 datastore.garbage_collection()?;
49
529de6c7 50 let idx = datastore.open_image_reader(target)?;
4818c8b6 51 idx.print_info();
4fbb72a8 52
ff5d3707 53 Ok(Value::Null)
54}
55
56
57fn main() {
58
59 let cmd_def = CliCommand::new(
60 ApiMethod::new(
61 backup_file,
62 ObjectSchema::new("Create backup from file.")
63 .required("filename", StringSchema::new("Source file name."))
64 .required("store", StringSchema::new("Datastore name."))
65 ))
f8838fe9 66 .arg_param(vec!["filename"])
fe0e04c6 67 .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
f8838fe9 68
a914a774 69
ff5d3707 70 if let Err(err) = run_cli_command(&cmd_def.into()) {
71 eprintln!("Error: {}", err);
72 print_cli_usage();
73 std::process::exit(-1);
74 }
75
76}