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
::client
::http_client
::*;
11 use proxmox_backup
::client
::catar_backup_stream
::*;
12 //use proxmox_backup::backup::chunk_store::*;
13 //use proxmox_backup::backup::image_index::*;
14 //use proxmox_backup::config::datastore;
15 //use proxmox_backup::catar::encoder::*;
16 //use proxmox_backup::backup::datastore::*;
18 use serde_json
::{Value}
;
21 fn backup_directory(body
: Body
, store
: &str, archive_name
: &str) -> Result
<(), Error
> {
23 let client
= HttpClient
::new("localhost");
25 let epoch
= std
::time
::SystemTime
::now().duration_since(
26 std
::time
::SystemTime
::UNIX_EPOCH
)?
.as_secs();
28 let query
= url
::form_urlencoded
::Serializer
::new(String
::new())
29 .append_pair("archive_name", archive_name
)
30 .append_pair("type", "host")
31 .append_pair("id", &tools
::nodename())
32 .append_pair("time", &epoch
.to_string())
35 let path
= format
!("api3/json/admin/datastore/{}/catar?{}", store
, query
);
37 client
.upload("application/x-proxmox-backup-catar", body
, &path
)?
;
43 fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
45 let mut target = PathBuf::from(target);
47 if let Some(ext) = target.extension() {
49 bail!("got wrong file extension - expected '.iidx'");
52 target.set_extension("iidx");
55 let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
57 tools::file_chunker(file, chunk_size, |pos, chunk| {
58 index.add_chunk(pos, chunk)?;
62 index.close()?; // commit changes
68 fn list_backups(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
70 let store
= tools
::required_string_param(¶m
, "store")?
;
72 let client
= HttpClient
::new("localhost");
74 let path
= format
!("api3/json/admin/datastore/{}/backups", store
);
76 let result
= client
.get(&path
)?
;
81 fn create_backup(param
: Value
, _info
: &ApiMethod
) -> Result
<Value
, Error
> {
83 let filename
= tools
::required_string_param(¶m
, "filename")?
;
84 let store
= tools
::required_string_param(¶m
, "store")?
;
85 let target
= tools
::required_string_param(¶m
, "target")?
;
87 let mut _chunk_size
= 4*1024*1024;
89 if let Some(size
) = param
["chunk-size"].as_u64() {
90 static SIZES
: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
92 if SIZES
.contains(&size
) {
93 _chunk_size
= (size
as usize) * 1024;
95 bail
!("Got unsupported chunk size '{}'", size
);
99 let stat
= match nix
::sys
::stat
::stat(filename
) {
101 Err(err
) => bail
!("unable to access '{}' - {}", filename
, err
),
104 if (stat
.st_mode
& libc
::S_IFDIR
) != 0 {
105 println
!("Backup directory '{}' to '{}'", filename
, store
);
107 let stream
= CaTarBackupStream
::open(filename
)?
;
109 let body
= Body
::wrap_stream(stream
);
111 backup_directory(body
, store
, target
)?
;
113 } else if (stat
.st_mode
& (libc
::S_IFREG
|libc
::S_IFBLK
)) != 0 {
114 println
!("Backup image '{}' to '{}'", filename
, store
);
116 if stat
.st_size
<= 0 { bail!("got strange file size '{}'
", stat.st_size); }
117 let _size = stat.st_size as usize;
119 panic!("implement me
");
121 //backup_image(&datastore, &file, size, &target, chunk_size)?;
123 // let idx = datastore.open_image_reader(target)?;
127 bail!("unsupported file
type (expected a directory
, file or block device
)");
130 //datastore.garbage_collection()?;
137 let create_cmd_def = CliCommand::new(
140 ObjectSchema::new("Create backup
.")
141 .required("filename
", StringSchema::new("Source
name (file or directory name
)"))
142 .required("store
", StringSchema::new("Datastore name
."))
143 .required("target
", StringSchema::new("Target name
."))
146 IntegerSchema::new("Chunk size
in KB
. Must be a power of
2.")
152 .arg_param(vec!["filename
", "target
"])
153 .completion_cb("filename
", tools::complete_file_name)
154 .completion_cb("store
", proxmox_backup::config::datastore::complete_datastore_name);
156 let list_cmd_def = CliCommand::new(
159 ObjectSchema::new("List backups
.")
160 .required("store
", StringSchema::new("Datastore name
."))
162 .arg_param(vec!["store
"])
163 .completion_cb("store
", proxmox_backup::config::datastore::complete_datastore_name);
166 let cmd_def = CliCommandMap::new()
167 .insert("create
".to_owned(), create_cmd_def.into())
168 .insert("list
".to_owned(), list_cmd_def.into());
170 if let Err(err) = run_cli_command(&cmd_def.into()) {
171 eprintln!("Error
: {}
", err);
172 if err.downcast::<UsageError>().is_ok() {
175 std::process::exit(-1);