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
::catar
::encoder
::*;
14 use proxmox_backup
::backup
::datastore
::*;
15 use serde_json
::{Value}
;
17 fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> &'a
str {
18 param
[name
].as_str().expect(&format
!("missing parameter '{}'", name
))
22 datastore
: &DataStore
,
24 dir
: &mut nix
::dir
::Dir
,
27 ) -> Result
<(), Error
> {
29 let mut target
= std
::path
::PathBuf
::from(target
);
31 if let Some(ext
) = target
.extension() {
33 bail
!("got wrong file extension - expected '.aidx'");
36 target
.set_extension("aidx");
39 let mut index
= datastore
.create_archive_writer(&target
, chunk_size
)?
;
41 let path
= std
::path
::PathBuf
::from(path
);
43 CaTarEncoder
::encode(path
, dir
, &mut index
)?
;
45 index
.close()?
; // commit changes
50 fn backup_image(datastore
: &DataStore
, file
: &std
::fs
::File
, size
: usize, target
: &str, chunk_size
: usize) -> Result
<(), Error
> {
52 let mut target
= std
::path
::PathBuf
::from(target
);
54 if let Some(ext
) = target
.extension() {
56 bail
!("got wrong file extension - expected '.iidx'");
59 target
.set_extension("iidx");
62 let mut index
= datastore
.create_image_writer(&target
, size
, chunk_size
)?
;
64 tools
::file_chunker(file
, chunk_size
, |pos
, chunk
| {
65 index
.add_chunk(pos
, chunk
)?
;
69 index
.close()?
; // commit changes
74 fn create_backup(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
76 let filename
= required_string_param(¶m
, "filename");
77 let store
= required_string_param(¶m
, "store");
78 let target
= required_string_param(¶m
, "target");
80 let mut chunk_size
= 4*1024*1024;
82 if let Some(size
) = param
["chunk-size"].as_u64() {
83 static SIZES
: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
85 if SIZES
.contains(&size
) {
86 chunk_size
= (size
as usize) * 1024;
88 bail
!("Got unsupported chunk size '{}'", size
);
92 let datastore
= DataStore
::open(store
)?
;
94 let file
= std
::fs
::File
::open(filename
)?
;
95 let rawfd
= file
.as_raw_fd();
96 let stat
= nix
::sys
::stat
::fstat(rawfd
)?
;
98 if (stat
.st_mode
& libc
::S_IFDIR
) != 0 {
99 println
!("Backup directory '{}' to '{}'", filename
, store
);
101 let mut dir
= nix
::dir
::Dir
::from_fd(rawfd
)?
;
103 backup_dir(&datastore
, &filename
, &mut dir
, &target
, chunk_size
)?
;
105 } else if (stat
.st_mode
& (libc
::S_IFREG
|libc
::S_IFBLK
)) != 0 {
106 println
!("Backup file '{}' to '{}'", filename
, store
);
108 if stat
.st_size
<= 0 { bail!("got strange file size '{}'
", stat.st_size); }
109 let size = stat.st_size as usize;
111 backup_image(&datastore, &file, size, &target, chunk_size)?;
113 // let idx = datastore.open_image_reader(target)?;
117 bail!("unsupported file
type (expected a directory
, file or block device
)");
120 //datastore.garbage_collection()?;
128 let cmd_def = CliCommand::new(
131 ObjectSchema::new("Create backup
.")
132 .required("filename
", StringSchema::new("Source
name (file or directory name
)"))
133 .required("store
", StringSchema::new("Datastore name
."))
134 .required("target
", StringSchema::new("Target name
."))
137 IntegerSchema::new("Chunk size
in KB
. Must be a power of
2.")
143 .arg_param(vec!["filename
", "target
"])
144 .completion_cb("store
", proxmox_backup::config::datastore::complete_datastore_name);
147 if let Err(err) = run_cli_command(&cmd_def.into()) {
148 eprintln!("Error
: {}
", err);
149 if err.downcast::<UsageError>().is_ok() {
152 std::process::exit(-1);