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