]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox-backup-client.rs
rename backup-client to proxmox-backup-client
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
1 extern crate proxmox_backup;
2
3 use failure::*;
4 use std::os::unix::io::AsRawFd;
5
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};
16
17 fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
18 param[name].as_str().expect(&format!("missing parameter '{}'", name))
19 }
20
21 fn backup_dir(
22 datastore: &DataStore,
23 path: &str,
24 dir: &mut nix::dir::Dir,
25 target: &str,
26 chunk_size: usize,
27 ) -> Result<(), Error> {
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
39 let mut index = datastore.create_archive_writer(&target, chunk_size)?;
40
41 let path = std::path::PathBuf::from(path);
42
43 CaTarEncoder::encode(path, dir, &mut index)?;
44
45 index.close()?; // commit changes
46
47 Ok(())
48 }
49
50 fn 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
74 fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
75
76 let filename = required_string_param(&param, "filename");
77 let store = required_string_param(&param, "store");
78 let target = required_string_param(&param, "target");
79
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
92 let datastore = DataStore::open(store)?;
93
94 let file = std::fs::File::open(filename)?;
95 let rawfd = file.as_raw_fd();
96 let stat = nix::sys::stat::fstat(rawfd)?;
97
98 if (stat.st_mode & libc::S_IFDIR) != 0 {
99 println!("Backup directory '{}' to '{}'", filename, store);
100
101 let mut dir = nix::dir::Dir::from_fd(rawfd)?;
102
103 backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?;
104
105 } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
106 println!("Backup file '{}' to '{}'", filename, store);
107
108 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
109 let size = stat.st_size as usize;
110
111 backup_image(&datastore, &file, size, &target, chunk_size)?;
112
113 // let idx = datastore.open_image_reader(target)?;
114 // idx.print_info();
115
116 } else {
117 bail!("unsupported file type (expected a directory, file or block device)");
118 }
119
120 //datastore.garbage_collection()?;
121
122 Ok(Value::Null)
123 }
124
125
126 fn main() {
127
128 let cmd_def = CliCommand::new(
129 ApiMethod::new(
130 create_backup,
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."))
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 )
142 ))
143 .arg_param(vec!["filename", "target"])
144 .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
145
146
147 if let Err(err) = run_cli_command(&cmd_def.into()) {
148 eprintln!("Error: {}", err);
149 if err.downcast::<UsageError>().is_ok() {
150 print_cli_usage();
151 }
152 std::process::exit(-1);
153 }
154
155 }