]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-client.rs
client/http_client.rs: new helper class
[proxmox-backup.git] / src / bin / proxmox-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;
fb8365b7 13use proxmox_backup::catar::encoder::*;
fe0e04c6 14use proxmox_backup::backup::datastore::*;
43eeef28 15use serde_json::{Value};
ff5d3707 16
a914a774 17fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
18 param[name].as_str().expect(&format!("missing parameter '{}'", name))
19}
20
fb8365b7
DM
21fn backup_dir(
22 datastore: &DataStore,
23 path: &str,
24 dir: &mut nix::dir::Dir,
25 target: &str,
26 chunk_size: usize,
27) -> Result<(), Error> {
bcd879cf
DM
28
29 let mut target = std::path::PathBuf::from(target);
30
31 if let Some(ext) = target.extension() {
32 if ext != "aidx" {
33 bail!("got wrong file extension - expected '.aidx'");
34 }
35 } else {
36 target.set_extension("aidx");
37 }
38
5e7a09be 39 let mut index = datastore.create_archive_writer(&target, chunk_size)?;
fb8365b7
DM
40
41 let path = std::path::PathBuf::from(path);
42
4c0fd487 43 CaTarEncoder::encode(path, dir, None, &mut index)?;
5e7a09be
DM
44
45 index.close()?; // commit changes
bcd879cf
DM
46
47 Ok(())
48}
49
50fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
51
52 let mut target = std::path::PathBuf::from(target);
53
54 if let Some(ext) = target.extension() {
55 if ext != "iidx" {
56 bail!("got wrong file extension - expected '.iidx'");
57 }
58 } else {
59 target.set_extension("iidx");
60 }
61
62 let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
63
64 tools::file_chunker(file, chunk_size, |pos, chunk| {
65 index.add_chunk(pos, chunk)?;
66 Ok(true)
67 })?;
68
69 index.close()?; // commit changes
70
71 Ok(())
72}
73
74fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
ff5d3707 75
a914a774 76 let filename = required_string_param(&param, "filename");
77 let store = required_string_param(&param, "store");
c34eb166 78 let target = required_string_param(&param, "target");
a914a774 79
2d9d143a
DM
80 let mut chunk_size = 4*1024*1024;
81
82 if let Some(size) = param["chunk-size"].as_u64() {
83 static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
84
85 if SIZES.contains(&size) {
86 chunk_size = (size as usize) * 1024;
87 } else {
88 bail!("Got unsupported chunk size '{}'", size);
89 }
90 }
91
bcd879cf 92 let datastore = DataStore::open(store)?;
a914a774 93
bcd879cf 94 let file = std::fs::File::open(filename)?;
fb8365b7
DM
95 let rawfd = file.as_raw_fd();
96 let stat = nix::sys::stat::fstat(rawfd)?;
a914a774 97
bcd879cf
DM
98 if (stat.st_mode & libc::S_IFDIR) != 0 {
99 println!("Backup directory '{}' to '{}'", filename, store);
100
fb8365b7
DM
101 let mut dir = nix::dir::Dir::from_fd(rawfd)?;
102
103 backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?;
bcd879cf
DM
104
105 } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
106 println!("Backup file '{}' to '{}'", filename, store);
606ce64b 107
4818c8b6
DM
108 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
109 let size = stat.st_size as usize;
a914a774 110
bcd879cf 111 backup_image(&datastore, &file, size, &target, chunk_size)?;
d62e6e22 112
594fa520
DM
113 // let idx = datastore.open_image_reader(target)?;
114 // idx.print_info();
4818c8b6 115
bcd879cf
DM
116 } else {
117 bail!("unsupported file type (expected a directory, file or block device)");
4818c8b6
DM
118 }
119
f0819fe5 120 //datastore.garbage_collection()?;
3d5c11e5 121
ff5d3707 122 Ok(Value::Null)
123}
124
125
126fn main() {
127
128 let cmd_def = CliCommand::new(
129 ApiMethod::new(
bcd879cf
DM
130 create_backup,
131 ObjectSchema::new("Create backup.")
132 .required("filename", StringSchema::new("Source name (file or directory name)"))
ff5d3707 133 .required("store", StringSchema::new("Datastore name."))
c34eb166 134 .required("target", StringSchema::new("Target name."))
2d9d143a
DM
135 .optional(
136 "chunk-size",
137 IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
138 .minimum(64)
139 .maximum(4096)
140 .default(4096)
141 )
ff5d3707 142 ))
c34eb166 143 .arg_param(vec!["filename", "target"])
fe0e04c6 144 .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
f8838fe9 145
a914a774 146
ff5d3707 147 if let Err(err) = run_cli_command(&cmd_def.into()) {
148 eprintln!("Error: {}", err);
4968bc3a
WB
149 if err.downcast::<UsageError>().is_ok() {
150 print_cli_usage();
151 }
ff5d3707 152 std::process::exit(-1);
153 }
154
155}