]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/backup-client.rs
start impl garbage collection
[proxmox-backup.git] / src / bin / backup-client.rs
1 extern crate apitest;
2
3 use failure::*;
4 use std::os::unix::io::AsRawFd;
5
6 use apitest::tools;
7 use apitest::cli::command::*;
8 use apitest::api::schema::*;
9 use apitest::api::router::*;
10 use apitest::backup::chunk_store::*;
11 use apitest::backup::image_index::*;
12 use apitest::backup::datastore::*;
13 use serde_json::{Value};
14
15 use apitest::config::datastore;
16
17 fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
18 param[name].as_str().expect(&format!("missing parameter '{}'", name))
19 }
20
21
22 fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
23
24 let filename = required_string_param(&param, "filename");
25 let store = required_string_param(&param, "store");
26
27 let mut datastore = DataStore::open(store)?;
28
29 println!("Backup file '{}' to '{}'", filename, store);
30
31 let target = "test1.idx";
32
33 {
34 let file = std::fs::File::open(filename)?;
35 let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
36 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
37 let size = stat.st_size as usize;
38
39 let mut index = datastore.create_image_writer(target, size)?;
40
41 tools::file_chunker(file, 64*1024, |pos, chunk| {
42 index.add_chunk(pos, chunk)?;
43 Ok(true)
44 })?;
45
46 index.close()?; // commit changes
47 }
48
49 datastore.garbage_collection()?;
50
51 let idx = datastore.open_image_reader(target)?;
52 idx.print_info();
53
54 Ok(Value::Null)
55 }
56
57
58 fn main() {
59
60 let cmd_def = CliCommand::new(
61 ApiMethod::new(
62 backup_file,
63 ObjectSchema::new("Create backup from file.")
64 .required("filename", StringSchema::new("Source file name."))
65 .required("store", StringSchema::new("Datastore name."))
66 ))
67 .arg_param(vec!["filename"])
68 .completion_cb("store", apitest::config::datastore::complete_datastore_name);
69
70
71 if let Err(err) = run_cli_command(&cmd_def.into()) {
72 eprintln!("Error: {}", err);
73 print_cli_usage();
74 std::process::exit(-1);
75 }
76
77 }