1 extern crate proxmox_backup
;
4 use std
::os
::unix
::io
::AsRawFd
;
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
::*;
14 use serde_json
::{Value}
;
16 fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> &'a
str {
17 param
[name
].as_str().expect(&format
!("missing parameter '{}'", name
))
21 fn backup_file(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
23 let filename
= required_string_param(¶m
, "filename");
24 let store
= required_string_param(¶m
, "store");
25 let target
= required_string_param(¶m
, "target");
27 let mut datastore
= DataStore
::open(store
)?
;
29 println
!("Backup file '{}' to '{}'", filename
, store
);
31 let mut target
= std
::path
::PathBuf
::from(target
);
32 if let Some(ext
) = target
.extension() {
34 bail
!("got wrong file extension - expected '.iidx'");
37 target
.set_extension("iidx");
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;
46 let mut index = datastore.create_image_writer(&target, size)?;
48 tools::file_chunker(file, 64*1024, |pos, chunk| {
49 index.add_chunk(pos, chunk)?;
53 index.close()?; // commit changes
56 datastore.garbage_collection()?;
58 let idx = datastore.open_image_reader(target)?;
67 let cmd_def = CliCommand::new(
70 ObjectSchema::new("Create backup from file
.")
71 .required("filename
", StringSchema::new("Source file name
."))
72 .required("store
", StringSchema::new("Datastore name
."))
73 .required("target
", StringSchema::new("Target name
."))
75 .arg_param(vec!["filename
", "target
"])
76 .completion_cb("store
", proxmox_backup::config::datastore::complete_datastore_name);
79 if let Err(err) = run_cli_command(&cmd_def.into()) {
80 eprintln!("Error
: {}
", err);
82 std::process::exit(-1);