]>
Commit | Line | Data |
---|---|---|
fe0e04c6 | 1 | extern crate proxmox_backup; |
ff5d3707 | 2 | |
3 | use failure::*; | |
606ce64b | 4 | use std::os::unix::io::AsRawFd; |
ff5d3707 | 5 | |
fe0e04c6 DM |
6 | use proxmox_backup::tools; |
7 | use proxmox_backup::cli::command::*; | |
8 | use proxmox_backup::api::schema::*; | |
9 | use proxmox_backup::api::router::*; | |
23bb8780 DM |
10 | use proxmox_backup::client::http_client::*; |
11 | use proxmox_backup::client::catar_backup_stream::*; | |
fe0e04c6 DM |
12 | //use proxmox_backup::backup::chunk_store::*; |
13 | //use proxmox_backup::backup::image_index::*; | |
14 | //use proxmox_backup::config::datastore; | |
23bb8780 | 15 | //use proxmox_backup::catar::encoder::*; |
fe0e04c6 | 16 | use proxmox_backup::backup::datastore::*; |
23bb8780 | 17 | |
43eeef28 | 18 | use serde_json::{Value}; |
23bb8780 DM |
19 | use hyper::Body; |
20 | ||
ff5d3707 | 21 | |
23bb8780 | 22 | fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), Error> { |
fb8365b7 | 23 | |
23bb8780 | 24 | let client = HttpClient::new("localhost"); |
fb8365b7 | 25 | |
23bb8780 | 26 | let path = format!("api3/json/admin/datastore/{}/upload_catar?archive_name={}", store, archive_name); |
5e7a09be | 27 | |
83bdac1e | 28 | client.upload("application/x-proxmox-backup-catar", body, &path)?; |
bcd879cf DM |
29 | |
30 | Ok(()) | |
31 | } | |
32 | ||
23bb8780 | 33 | /**** |
bcd879cf DM |
34 | fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { |
35 | ||
23bb8780 | 36 | let mut target = PathBuf::from(target); |
bcd879cf DM |
37 | |
38 | if let Some(ext) = target.extension() { | |
39 | if ext != "iidx" { | |
40 | bail!("got wrong file extension - expected '.iidx'"); | |
41 | } | |
42 | } else { | |
43 | target.set_extension("iidx"); | |
44 | } | |
45 | ||
46 | let mut index = datastore.create_image_writer(&target, size, chunk_size)?; | |
47 | ||
48 | tools::file_chunker(file, chunk_size, |pos, chunk| { | |
49 | index.add_chunk(pos, chunk)?; | |
50 | Ok(true) | |
51 | })?; | |
52 | ||
53 | index.close()?; // commit changes | |
54 | ||
55 | Ok(()) | |
56 | } | |
23bb8780 | 57 | */ |
bcd879cf DM |
58 | |
59 | fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> { | |
ff5d3707 | 60 | |
0fe5d605 DM |
61 | let filename = tools::required_string_param(¶m, "filename")?; |
62 | let store = tools::required_string_param(¶m, "store")?; | |
63 | let target = tools::required_string_param(¶m, "target")?; | |
a914a774 | 64 | |
2d9d143a DM |
65 | let mut chunk_size = 4*1024*1024; |
66 | ||
67 | if let Some(size) = param["chunk-size"].as_u64() { | |
68 | static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096]; | |
69 | ||
70 | if SIZES.contains(&size) { | |
71 | chunk_size = (size as usize) * 1024; | |
72 | } else { | |
73 | bail!("Got unsupported chunk size '{}'", size); | |
74 | } | |
75 | } | |
76 | ||
23bb8780 DM |
77 | let stat = match nix::sys::stat::stat(filename) { |
78 | Ok(s) => s, | |
79 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
80 | }; | |
a914a774 | 81 | |
bcd879cf DM |
82 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
83 | println!("Backup directory '{}' to '{}'", filename, store); | |
84 | ||
23bb8780 DM |
85 | let stream = CaTarBackupStream::open(filename)?; |
86 | ||
87 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 88 | |
23bb8780 | 89 | backup_directory(body, store, target)?; |
bcd879cf DM |
90 | |
91 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
23bb8780 | 92 | println!("Backup image '{}' to '{}'", filename, store); |
606ce64b | 93 | |
4818c8b6 DM |
94 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
95 | let size = stat.st_size as usize; | |
a914a774 | 96 | |
23bb8780 DM |
97 | panic!("implement me"); |
98 | ||
99 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 100 | |
594fa520 DM |
101 | // let idx = datastore.open_image_reader(target)?; |
102 | // idx.print_info(); | |
4818c8b6 | 103 | |
bcd879cf DM |
104 | } else { |
105 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
106 | } |
107 | ||
f0819fe5 | 108 | //datastore.garbage_collection()?; |
3d5c11e5 | 109 | |
ff5d3707 | 110 | Ok(Value::Null) |
111 | } | |
112 | ||
113 | ||
244d9b17 DM |
114 | pub fn complete_file_name(arg: &str) -> Vec<String> { |
115 | ||
116 | let mut result = vec![]; | |
117 | ||
118 | use nix::fcntl::OFlag; | |
119 | use nix::sys::stat::Mode; | |
120 | use nix::fcntl::AtFlags; | |
121 | ||
122 | let mut dirname = std::path::PathBuf::from(arg); | |
123 | ||
124 | if let Ok(stat) = nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) { | |
125 | ||
126 | } else { | |
127 | if let Some(parent) = dirname.parent() { | |
128 | dirname = parent.to_owned(); | |
129 | } | |
130 | } | |
131 | ||
132 | let mut dir = match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) { | |
133 | Ok(d) => d, | |
134 | Err(err) => { | |
135 | return result; | |
136 | } | |
137 | }; | |
138 | ||
139 | for item in dir.iter() { | |
140 | if let Ok(entry) = item { | |
141 | if let Ok(name) = entry.file_name().to_str() { | |
142 | if name == "." || name == ".." { continue; } | |
143 | let mut newpath = dirname.clone(); | |
144 | newpath.push(name); | |
145 | ||
146 | if let Ok(stat) = nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty()) { | |
147 | if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR { | |
148 | newpath.push(""); | |
149 | if let Some(newpath) = newpath.to_str() { | |
150 | result.push(newpath.to_owned()); | |
151 | } | |
152 | continue; | |
153 | } | |
154 | } | |
155 | if let Some(newpath) = newpath.to_str() { | |
156 | result.push(newpath.to_owned()); | |
157 | } | |
158 | ||
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | result | |
164 | } | |
165 | ||
ff5d3707 | 166 | fn main() { |
167 | ||
168 | let cmd_def = CliCommand::new( | |
169 | ApiMethod::new( | |
bcd879cf DM |
170 | create_backup, |
171 | ObjectSchema::new("Create backup.") | |
172 | .required("filename", StringSchema::new("Source name (file or directory name)")) | |
ff5d3707 | 173 | .required("store", StringSchema::new("Datastore name.")) |
c34eb166 | 174 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
175 | .optional( |
176 | "chunk-size", | |
177 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
178 | .minimum(64) | |
179 | .maximum(4096) | |
180 | .default(4096) | |
181 | ) | |
ff5d3707 | 182 | )) |
c34eb166 | 183 | .arg_param(vec!["filename", "target"]) |
244d9b17 | 184 | .completion_cb("filename", complete_file_name) |
fe0e04c6 | 185 | .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name); |
f8838fe9 | 186 | |
a914a774 | 187 | |
ff5d3707 | 188 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
189 | eprintln!("Error: {}", err); | |
4968bc3a WB |
190 | if err.downcast::<UsageError>().is_ok() { |
191 | print_cli_usage(); | |
192 | } | |
ff5d3707 | 193 | std::process::exit(-1); |
194 | } | |
195 | ||
196 | } |