]>
Commit | Line | Data |
---|---|---|
826f309b | 1 | //#[macro_use] |
fe0e04c6 | 2 | extern crate proxmox_backup; |
ff5d3707 | 3 | |
4 | use failure::*; | |
70235f72 CE |
5 | use nix::unistd::{fork, ForkResult, pipe}; |
6 | use std::os::unix::io::RawFd; | |
fa5d6977 | 7 | use chrono::{Local, Utc, TimeZone}; |
e9c9409a | 8 | use std::path::{Path, PathBuf}; |
2eeaacb9 | 9 | use std::collections::{HashSet, HashMap}; |
70235f72 | 10 | use std::ffi::OsStr; |
d2267b11 | 11 | use std::io::{BufReader, Read, Write, Seek, SeekFrom}; |
2761d6a4 DM |
12 | use std::os::unix::fs::OpenOptionsExt; |
13 | ||
e18a6c9e | 14 | use proxmox::tools::fs::{file_get_contents, file_get_json, file_set_contents, image_size}; |
ff5d3707 | 15 | |
fe0e04c6 | 16 | use proxmox_backup::tools; |
4de0e142 | 17 | use proxmox_backup::cli::*; |
bbf9e7e9 | 18 | use proxmox_backup::api2::types::*; |
ef2f2efb | 19 | use proxmox_backup::api_schema::*; |
dc9a007b | 20 | use proxmox_backup::api_schema::router::*; |
151c6ce2 | 21 | use proxmox_backup::client::*; |
247cdbce | 22 | use proxmox_backup::backup::*; |
7926a3a1 | 23 | use proxmox_backup::pxar::{ self, catalog::* }; |
86eda3eb | 24 | |
fe0e04c6 DM |
25 | //use proxmox_backup::backup::image_index::*; |
26 | //use proxmox_backup::config::datastore; | |
8968258b | 27 | //use proxmox_backup::pxar::encoder::*; |
728797d0 | 28 | //use proxmox_backup::backup::datastore::*; |
23bb8780 | 29 | |
f5f13ebc | 30 | use serde_json::{json, Value}; |
1c0472e8 | 31 | //use hyper::Body; |
2761d6a4 | 32 | use std::sync::{Arc, Mutex}; |
ae0be2dd | 33 | use regex::Regex; |
d0a03d40 | 34 | use xdg::BaseDirectories; |
ae0be2dd DM |
35 | |
36 | use lazy_static::lazy_static; | |
5a2df000 | 37 | use futures::*; |
c4ff3dce | 38 | use tokio::sync::mpsc; |
ae0be2dd DM |
39 | |
40 | lazy_static! { | |
79679c2d | 41 | static ref BACKUPSPEC_REGEX: Regex = Regex::new(r"^([a-zA-Z0-9_-]+\.(?:pxar|img|conf|log)):(.+)$").unwrap(); |
f2401311 DM |
42 | |
43 | static ref REPO_URL_SCHEMA: Arc<Schema> = Arc::new( | |
44 | StringSchema::new("Repository URL.") | |
45 | .format(BACKUP_REPO_URL.clone()) | |
46 | .max_length(256) | |
47 | .into() | |
48 | ); | |
ae0be2dd | 49 | } |
33d64b81 | 50 | |
d0a03d40 | 51 | |
2665cef7 DM |
52 | fn get_default_repository() -> Option<String> { |
53 | std::env::var("PBS_REPOSITORY").ok() | |
54 | } | |
55 | ||
56 | fn extract_repository_from_value( | |
57 | param: &Value, | |
58 | ) -> Result<BackupRepository, Error> { | |
59 | ||
60 | let repo_url = param["repository"] | |
61 | .as_str() | |
62 | .map(String::from) | |
63 | .or_else(get_default_repository) | |
64 | .ok_or_else(|| format_err!("unable to get (default) repository"))?; | |
65 | ||
66 | let repo: BackupRepository = repo_url.parse()?; | |
67 | ||
68 | Ok(repo) | |
69 | } | |
70 | ||
71 | fn extract_repository_from_map( | |
72 | param: &HashMap<String, String>, | |
73 | ) -> Option<BackupRepository> { | |
74 | ||
75 | param.get("repository") | |
76 | .map(String::from) | |
77 | .or_else(get_default_repository) | |
78 | .and_then(|repo_url| repo_url.parse::<BackupRepository>().ok()) | |
79 | } | |
80 | ||
d0a03d40 DM |
81 | fn record_repository(repo: &BackupRepository) { |
82 | ||
83 | let base = match BaseDirectories::with_prefix("proxmox-backup") { | |
84 | Ok(v) => v, | |
85 | _ => return, | |
86 | }; | |
87 | ||
88 | // usually $HOME/.cache/proxmox-backup/repo-list | |
89 | let path = match base.place_cache_file("repo-list") { | |
90 | Ok(v) => v, | |
91 | _ => return, | |
92 | }; | |
93 | ||
e18a6c9e | 94 | let mut data = file_get_json(&path, None).unwrap_or(json!({})); |
d0a03d40 DM |
95 | |
96 | let repo = repo.to_string(); | |
97 | ||
98 | data[&repo] = json!{ data[&repo].as_i64().unwrap_or(0) + 1 }; | |
99 | ||
100 | let mut map = serde_json::map::Map::new(); | |
101 | ||
102 | loop { | |
103 | let mut max_used = 0; | |
104 | let mut max_repo = None; | |
105 | for (repo, count) in data.as_object().unwrap() { | |
106 | if map.contains_key(repo) { continue; } | |
107 | if let Some(count) = count.as_i64() { | |
108 | if count > max_used { | |
109 | max_used = count; | |
110 | max_repo = Some(repo); | |
111 | } | |
112 | } | |
113 | } | |
114 | if let Some(repo) = max_repo { | |
115 | map.insert(repo.to_owned(), json!(max_used)); | |
116 | } else { | |
117 | break; | |
118 | } | |
119 | if map.len() > 10 { // store max. 10 repos | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | let new_data = json!(map); | |
125 | ||
e18a6c9e | 126 | let _ = file_set_contents(path, new_data.to_string().as_bytes(), None); |
d0a03d40 DM |
127 | } |
128 | ||
49811347 | 129 | fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> { |
d0a03d40 DM |
130 | |
131 | let mut result = vec![]; | |
132 | ||
133 | let base = match BaseDirectories::with_prefix("proxmox-backup") { | |
134 | Ok(v) => v, | |
135 | _ => return result, | |
136 | }; | |
137 | ||
138 | // usually $HOME/.cache/proxmox-backup/repo-list | |
139 | let path = match base.place_cache_file("repo-list") { | |
140 | Ok(v) => v, | |
141 | _ => return result, | |
142 | }; | |
143 | ||
e18a6c9e | 144 | let data = file_get_json(&path, None).unwrap_or(json!({})); |
d0a03d40 DM |
145 | |
146 | if let Some(map) = data.as_object() { | |
49811347 | 147 | for (repo, _count) in map { |
d0a03d40 DM |
148 | result.push(repo.to_owned()); |
149 | } | |
150 | } | |
151 | ||
152 | result | |
153 | } | |
154 | ||
d2267b11 DM |
155 | fn compute_file_csum(file: &mut std::fs::File) -> Result<([u8; 32], u64), Error> { |
156 | ||
157 | file.seek(SeekFrom::Start(0))?; | |
158 | ||
159 | let mut hasher = openssl::sha::Sha256::new(); | |
160 | let mut buffer = proxmox::tools::vec::undefined(256*1024); | |
161 | let mut size: u64 = 0; | |
162 | ||
163 | loop { | |
164 | let count = match file.read(&mut buffer) { | |
165 | Ok(count) => count, | |
166 | Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => { continue; } | |
167 | Err(err) => return Err(err.into()), | |
168 | }; | |
169 | if count == 0 { | |
170 | break; | |
171 | } | |
172 | size += count as u64; | |
173 | hasher.update(&buffer[..count]); | |
174 | } | |
175 | ||
176 | let csum = hasher.finish(); | |
177 | ||
178 | Ok((csum, size)) | |
179 | } | |
180 | ||
181 | ||
e9722f8b | 182 | async fn backup_directory<P: AsRef<Path>>( |
cf9271e2 | 183 | client: &BackupWriter, |
17d6979a | 184 | dir_path: P, |
247cdbce | 185 | archive_name: &str, |
36898ffc | 186 | chunk_size: Option<usize>, |
2eeaacb9 | 187 | device_set: Option<HashSet<u64>>, |
219ef0e6 | 188 | verbose: bool, |
5b72c9b4 | 189 | skip_lost_and_found: bool, |
f98ac774 | 190 | crypt_config: Option<Arc<CryptConfig>>, |
9d135fe6 | 191 | catalog: Arc<Mutex<CatalogBlobWriter<std::fs::File>>>, |
2c3891d1 | 192 | ) -> Result<BackupStats, Error> { |
33d64b81 | 193 | |
2761d6a4 | 194 | let pxar_stream = PxarBackupStream::open(dir_path.as_ref(), device_set, verbose, skip_lost_and_found, catalog)?; |
e9722f8b | 195 | let mut chunk_stream = ChunkStream::new(pxar_stream, chunk_size); |
ff3d3100 | 196 | |
e9722f8b | 197 | let (mut tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks |
5e7a09be | 198 | |
c4ff3dce | 199 | let stream = rx |
e9722f8b | 200 | .map_err(Error::from); |
17d6979a | 201 | |
c4ff3dce | 202 | // spawn chunker inside a separate task so that it can run parallel |
e9722f8b WB |
203 | tokio::spawn(async move { |
204 | let _ = tx.send_all(&mut chunk_stream).await; | |
205 | }); | |
17d6979a | 206 | |
e9722f8b WB |
207 | let stats = client |
208 | .upload_stream(archive_name, stream, "dynamic", None, crypt_config) | |
209 | .await?; | |
bcd879cf | 210 | |
2c3891d1 | 211 | Ok(stats) |
bcd879cf DM |
212 | } |
213 | ||
e9722f8b | 214 | async fn backup_image<P: AsRef<Path>>( |
cf9271e2 | 215 | client: &BackupWriter, |
6af905c1 DM |
216 | image_path: P, |
217 | archive_name: &str, | |
218 | image_size: u64, | |
36898ffc | 219 | chunk_size: Option<usize>, |
1c0472e8 | 220 | _verbose: bool, |
f98ac774 | 221 | crypt_config: Option<Arc<CryptConfig>>, |
2c3891d1 | 222 | ) -> Result<BackupStats, Error> { |
6af905c1 | 223 | |
6af905c1 DM |
224 | let path = image_path.as_ref().to_owned(); |
225 | ||
e9722f8b | 226 | let file = tokio::fs::File::open(path).await?; |
6af905c1 DM |
227 | |
228 | let stream = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new()) | |
229 | .map_err(Error::from); | |
230 | ||
36898ffc | 231 | let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024)); |
6af905c1 | 232 | |
e9722f8b WB |
233 | let stats = client |
234 | .upload_stream(archive_name, stream, "fixed", Some(image_size), crypt_config) | |
235 | .await?; | |
6af905c1 | 236 | |
2c3891d1 | 237 | Ok(stats) |
6af905c1 DM |
238 | } |
239 | ||
52c171e4 DM |
240 | fn strip_server_file_expenstion(name: &str) -> String { |
241 | ||
242 | if name.ends_with(".didx") { | |
243 | return name[..name.len()-5].to_owned(); | |
244 | } else if name.ends_with(".fidx") { | |
245 | return name[..name.len()-5].to_owned(); | |
246 | } else if name.ends_with(".blob") { | |
247 | return name[..name.len()-5].to_owned(); | |
248 | } else { | |
249 | return name.to_owned(); // should not happen | |
8e39232a | 250 | } |
8e39232a DM |
251 | } |
252 | ||
812c6f87 DM |
253 | fn list_backup_groups( |
254 | param: Value, | |
255 | _info: &ApiMethod, | |
dd5495d6 | 256 | _rpcenv: &mut dyn RpcEnvironment, |
812c6f87 DM |
257 | ) -> Result<Value, Error> { |
258 | ||
2665cef7 | 259 | let repo = extract_repository_from_value(¶m)?; |
812c6f87 | 260 | |
cc2ce4a9 | 261 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
812c6f87 | 262 | |
d0a03d40 | 263 | let path = format!("api2/json/admin/datastore/{}/groups", repo.store()); |
812c6f87 | 264 | |
e9722f8b WB |
265 | let mut result = async_main(async move { |
266 | client.get(&path, None).await | |
267 | })?; | |
812c6f87 | 268 | |
d0a03d40 DM |
269 | record_repository(&repo); |
270 | ||
812c6f87 | 271 | // fixme: implement and use output formatter instead .. |
80822b95 DM |
272 | let list = result["data"].as_array_mut().unwrap(); |
273 | ||
274 | list.sort_unstable_by(|a, b| { | |
275 | let a_id = a["backup-id"].as_str().unwrap(); | |
276 | let a_backup_type = a["backup-type"].as_str().unwrap(); | |
277 | let b_id = b["backup-id"].as_str().unwrap(); | |
278 | let b_backup_type = b["backup-type"].as_str().unwrap(); | |
279 | ||
280 | let type_order = a_backup_type.cmp(b_backup_type); | |
281 | if type_order == std::cmp::Ordering::Equal { | |
282 | a_id.cmp(b_id) | |
283 | } else { | |
284 | type_order | |
285 | } | |
286 | }); | |
812c6f87 | 287 | |
34a816cc DM |
288 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
289 | ||
290 | let mut result = vec![]; | |
291 | ||
812c6f87 DM |
292 | for item in list { |
293 | ||
ad20d198 DM |
294 | let id = item["backup-id"].as_str().unwrap(); |
295 | let btype = item["backup-type"].as_str().unwrap(); | |
296 | let epoch = item["last-backup"].as_i64().unwrap(); | |
fa5d6977 | 297 | let last_backup = Utc.timestamp(epoch, 0); |
ad20d198 | 298 | let backup_count = item["backup-count"].as_u64().unwrap(); |
812c6f87 | 299 | |
1e9a94e5 | 300 | let group = BackupGroup::new(btype, id); |
812c6f87 DM |
301 | |
302 | let path = group.group_path().to_str().unwrap().to_owned(); | |
ad20d198 | 303 | |
52c171e4 DM |
304 | let files = item["files"].as_array().unwrap().iter() |
305 | .map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect(); | |
ad20d198 | 306 | |
34a816cc | 307 | if output_format == "text" { |
fa5d6977 DM |
308 | println!( |
309 | "{:20} | {} | {:5} | {}", | |
310 | path, | |
311 | BackupDir::backup_time_to_string(last_backup), | |
312 | backup_count, | |
313 | tools::join(&files, ' '), | |
314 | ); | |
34a816cc DM |
315 | } else { |
316 | result.push(json!({ | |
317 | "backup-type": btype, | |
318 | "backup-id": id, | |
319 | "last-backup": epoch, | |
320 | "backup-count": backup_count, | |
321 | "files": files, | |
322 | })); | |
323 | } | |
812c6f87 DM |
324 | } |
325 | ||
9aa3f682 | 326 | if output_format != "text" { format_and_print_result(&result.into(), &output_format); } |
34a816cc | 327 | |
812c6f87 DM |
328 | Ok(Value::Null) |
329 | } | |
330 | ||
184f17af DM |
331 | fn list_snapshots( |
332 | param: Value, | |
333 | _info: &ApiMethod, | |
dd5495d6 | 334 | _rpcenv: &mut dyn RpcEnvironment, |
184f17af DM |
335 | ) -> Result<Value, Error> { |
336 | ||
2665cef7 | 337 | let repo = extract_repository_from_value(¶m)?; |
184f17af | 338 | |
34a816cc DM |
339 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
340 | ||
cc2ce4a9 | 341 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
184f17af | 342 | |
9e391bb7 | 343 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); |
184f17af | 344 | |
15c847f1 DM |
345 | let mut args = json!({}); |
346 | if let Some(path) = param["group"].as_str() { | |
347 | let group = BackupGroup::parse(path)?; | |
348 | args["backup-type"] = group.backup_type().into(); | |
349 | args["backup-id"] = group.backup_id().into(); | |
350 | } | |
351 | ||
e9722f8b WB |
352 | let result = async_main(async move { |
353 | client.get(&path, Some(args)).await | |
354 | })?; | |
184f17af | 355 | |
d0a03d40 DM |
356 | record_repository(&repo); |
357 | ||
184f17af DM |
358 | let list = result["data"].as_array().unwrap(); |
359 | ||
34a816cc DM |
360 | let mut result = vec![]; |
361 | ||
184f17af DM |
362 | for item in list { |
363 | ||
364 | let id = item["backup-id"].as_str().unwrap(); | |
365 | let btype = item["backup-type"].as_str().unwrap(); | |
366 | let epoch = item["backup-time"].as_i64().unwrap(); | |
184f17af | 367 | |
391d3107 | 368 | let snapshot = BackupDir::new(btype, id, epoch); |
184f17af DM |
369 | |
370 | let path = snapshot.relative_path().to_str().unwrap().to_owned(); | |
371 | ||
52c171e4 DM |
372 | let files = item["files"].as_array().unwrap().iter() |
373 | .map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect(); | |
184f17af | 374 | |
34a816cc | 375 | if output_format == "text" { |
a17a0e7a DM |
376 | let size_str = if let Some(size) = item["size"].as_u64() { |
377 | size.to_string() | |
378 | } else { | |
379 | String::from("-") | |
380 | }; | |
381 | println!("{} | {} | {}", path, size_str, tools::join(&files, ' ')); | |
34a816cc | 382 | } else { |
a17a0e7a | 383 | let mut data = json!({ |
34a816cc DM |
384 | "backup-type": btype, |
385 | "backup-id": id, | |
386 | "backup-time": epoch, | |
387 | "files": files, | |
a17a0e7a DM |
388 | }); |
389 | if let Some(size) = item["size"].as_u64() { | |
390 | data["size"] = size.into(); | |
391 | } | |
392 | result.push(data); | |
34a816cc | 393 | } |
184f17af DM |
394 | } |
395 | ||
f6ede796 | 396 | if output_format != "text" { format_and_print_result(&result.into(), &output_format); } |
34a816cc | 397 | |
184f17af DM |
398 | Ok(Value::Null) |
399 | } | |
400 | ||
6f62c924 DM |
401 | fn forget_snapshots( |
402 | param: Value, | |
403 | _info: &ApiMethod, | |
dd5495d6 | 404 | _rpcenv: &mut dyn RpcEnvironment, |
6f62c924 DM |
405 | ) -> Result<Value, Error> { |
406 | ||
2665cef7 | 407 | let repo = extract_repository_from_value(¶m)?; |
6f62c924 DM |
408 | |
409 | let path = tools::required_string_param(¶m, "snapshot")?; | |
410 | let snapshot = BackupDir::parse(path)?; | |
411 | ||
cc2ce4a9 | 412 | let mut client = HttpClient::new(repo.host(), repo.user(), None)?; |
6f62c924 | 413 | |
9e391bb7 | 414 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); |
6f62c924 | 415 | |
e9722f8b WB |
416 | let result = async_main(async move { |
417 | client.delete(&path, Some(json!({ | |
418 | "backup-type": snapshot.group().backup_type(), | |
419 | "backup-id": snapshot.group().backup_id(), | |
420 | "backup-time": snapshot.backup_time().timestamp(), | |
421 | }))).await | |
422 | })?; | |
6f62c924 | 423 | |
d0a03d40 DM |
424 | record_repository(&repo); |
425 | ||
6f62c924 DM |
426 | Ok(result) |
427 | } | |
428 | ||
e240d8be DM |
429 | fn api_login( |
430 | param: Value, | |
431 | _info: &ApiMethod, | |
432 | _rpcenv: &mut dyn RpcEnvironment, | |
433 | ) -> Result<Value, Error> { | |
434 | ||
435 | let repo = extract_repository_from_value(¶m)?; | |
436 | ||
cc2ce4a9 | 437 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
e9722f8b | 438 | async_main(async move { client.login().await })?; |
e240d8be DM |
439 | |
440 | record_repository(&repo); | |
441 | ||
442 | Ok(Value::Null) | |
443 | } | |
444 | ||
445 | fn api_logout( | |
446 | param: Value, | |
447 | _info: &ApiMethod, | |
448 | _rpcenv: &mut dyn RpcEnvironment, | |
449 | ) -> Result<Value, Error> { | |
450 | ||
451 | let repo = extract_repository_from_value(¶m)?; | |
452 | ||
453 | delete_ticket_info(repo.host(), repo.user())?; | |
454 | ||
455 | Ok(Value::Null) | |
456 | } | |
457 | ||
9049a8cf DM |
458 | fn dump_catalog( |
459 | param: Value, | |
460 | _info: &ApiMethod, | |
461 | _rpcenv: &mut dyn RpcEnvironment, | |
462 | ) -> Result<Value, Error> { | |
463 | ||
464 | let repo = extract_repository_from_value(¶m)?; | |
465 | ||
466 | let path = tools::required_string_param(¶m, "snapshot")?; | |
467 | let snapshot = BackupDir::parse(path)?; | |
468 | ||
469 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); | |
470 | ||
471 | let crypt_config = match keyfile { | |
472 | None => None, | |
473 | Some(path) => { | |
474 | let (key, _) = load_and_decrtypt_key(&path, get_encryption_key_password)?; | |
9025312a | 475 | Some(Arc::new(CryptConfig::new(key)?)) |
9049a8cf DM |
476 | } |
477 | }; | |
478 | ||
cc2ce4a9 | 479 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
9049a8cf | 480 | |
e9722f8b | 481 | async_main(async move { |
9e490a74 DM |
482 | let client = BackupReader::start( |
483 | client, | |
e9722f8b WB |
484 | repo.store(), |
485 | &snapshot.group().backup_type(), | |
486 | &snapshot.group().backup_id(), | |
9e490a74 DM |
487 | snapshot.backup_time(), |
488 | true, | |
489 | ).await?; | |
9049a8cf | 490 | |
d2267b11 DM |
491 | let backup_index_data = download_index_blob(client.clone(), crypt_config.clone()).await?; |
492 | let backup_index: Value = serde_json::from_slice(&backup_index_data[..])?; | |
493 | ||
e9722f8b WB |
494 | let blob_file = std::fs::OpenOptions::new() |
495 | .read(true) | |
496 | .write(true) | |
497 | .custom_flags(libc::O_TMPFILE) | |
498 | .open("/tmp")?; | |
9049a8cf | 499 | |
d2267b11 DM |
500 | let mut blob_file = client.download(CATALOG_BLOB_NAME, blob_file).await?; |
501 | ||
502 | let (csum, size) = compute_file_csum(&mut blob_file)?; | |
503 | verify_index_file(&backup_index, CATALOG_BLOB_NAME, &csum, size)?; | |
a84ef4c2 | 504 | |
e9722f8b | 505 | blob_file.seek(SeekFrom::Start(0))?; |
a84ef4c2 | 506 | |
e9722f8b WB |
507 | let reader = BufReader::new(blob_file); |
508 | let mut catalog_reader = CatalogBlobReader::new(reader, crypt_config)?; | |
9049a8cf | 509 | |
e9722f8b | 510 | catalog_reader.dump()?; |
9049a8cf | 511 | |
e9722f8b WB |
512 | record_repository(&repo); |
513 | ||
514 | Ok::<(), Error>(()) | |
515 | })?; | |
9049a8cf DM |
516 | |
517 | Ok(Value::Null) | |
518 | } | |
519 | ||
52c171e4 DM |
520 | fn list_snapshot_files( |
521 | param: Value, | |
522 | _info: &ApiMethod, | |
523 | _rpcenv: &mut dyn RpcEnvironment, | |
524 | ) -> Result<Value, Error> { | |
525 | ||
526 | let repo = extract_repository_from_value(¶m)?; | |
527 | ||
528 | let path = tools::required_string_param(¶m, "snapshot")?; | |
529 | let snapshot = BackupDir::parse(path)?; | |
530 | ||
531 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); | |
532 | ||
cc2ce4a9 | 533 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
52c171e4 DM |
534 | |
535 | let path = format!("api2/json/admin/datastore/{}/files", repo.store()); | |
536 | ||
e9722f8b WB |
537 | let mut result = async_main(async move { |
538 | client.get(&path, Some(json!({ | |
539 | "backup-type": snapshot.group().backup_type(), | |
540 | "backup-id": snapshot.group().backup_id(), | |
541 | "backup-time": snapshot.backup_time().timestamp(), | |
542 | }))).await | |
543 | })?; | |
52c171e4 DM |
544 | |
545 | record_repository(&repo); | |
546 | ||
8c70e3eb | 547 | let list: Value = result["data"].take(); |
52c171e4 DM |
548 | |
549 | if output_format == "text" { | |
8c70e3eb DM |
550 | for item in list.as_array().unwrap().iter() { |
551 | println!( | |
552 | "{} {}", | |
553 | strip_server_file_expenstion(item["filename"].as_str().unwrap()), | |
554 | item["size"].as_u64().unwrap_or(0), | |
555 | ); | |
52c171e4 DM |
556 | } |
557 | } else { | |
8c70e3eb | 558 | format_and_print_result(&list, &output_format); |
52c171e4 DM |
559 | } |
560 | ||
561 | Ok(Value::Null) | |
562 | } | |
563 | ||
8cc0d6af DM |
564 | fn start_garbage_collection( |
565 | param: Value, | |
566 | _info: &ApiMethod, | |
dd5495d6 | 567 | _rpcenv: &mut dyn RpcEnvironment, |
8cc0d6af DM |
568 | ) -> Result<Value, Error> { |
569 | ||
2665cef7 | 570 | let repo = extract_repository_from_value(¶m)?; |
8cc0d6af | 571 | |
cc2ce4a9 | 572 | let mut client = HttpClient::new(repo.host(), repo.user(), None)?; |
8cc0d6af | 573 | |
d0a03d40 | 574 | let path = format!("api2/json/admin/datastore/{}/gc", repo.store()); |
8cc0d6af | 575 | |
e9722f8b | 576 | let result = async_main(async move { client.post(&path, None).await })?; |
8cc0d6af | 577 | |
d0a03d40 DM |
578 | record_repository(&repo); |
579 | ||
8cc0d6af DM |
580 | Ok(result) |
581 | } | |
33d64b81 | 582 | |
ae0be2dd DM |
583 | fn parse_backupspec(value: &str) -> Result<(&str, &str), Error> { |
584 | ||
585 | if let Some(caps) = BACKUPSPEC_REGEX.captures(value) { | |
586 | return Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str())); | |
587 | } | |
588 | bail!("unable to parse directory specification '{}'", value); | |
589 | } | |
590 | ||
6049b71f DM |
591 | fn create_backup( |
592 | param: Value, | |
593 | _info: &ApiMethod, | |
dd5495d6 | 594 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 595 | ) -> Result<Value, Error> { |
ff5d3707 | 596 | |
2665cef7 | 597 | let repo = extract_repository_from_value(¶m)?; |
ae0be2dd DM |
598 | |
599 | let backupspec_list = tools::required_array_param(¶m, "backupspec")?; | |
a914a774 | 600 | |
eed6db39 DM |
601 | let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false); |
602 | ||
5b72c9b4 DM |
603 | let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false); |
604 | ||
219ef0e6 DM |
605 | let verbose = param["verbose"].as_bool().unwrap_or(false); |
606 | ||
ca5d0b61 DM |
607 | let backup_time_opt = param["backup-time"].as_i64(); |
608 | ||
36898ffc | 609 | let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize); |
2d9d143a | 610 | |
247cdbce DM |
611 | if let Some(size) = chunk_size_opt { |
612 | verify_chunk_size(size)?; | |
2d9d143a DM |
613 | } |
614 | ||
6d0983db DM |
615 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); |
616 | ||
f69adc81 | 617 | let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename()); |
fba30411 | 618 | |
bbf9e7e9 | 619 | let backup_type = param["backup-type"].as_str().unwrap_or("host"); |
ca5d0b61 | 620 | |
2eeaacb9 DM |
621 | let include_dev = param["include-dev"].as_array(); |
622 | ||
623 | let mut devices = if all_file_systems { None } else { Some(HashSet::new()) }; | |
624 | ||
625 | if let Some(include_dev) = include_dev { | |
626 | if all_file_systems { | |
627 | bail!("option 'all-file-systems' conflicts with option 'include-dev'"); | |
628 | } | |
629 | ||
630 | let mut set = HashSet::new(); | |
631 | for path in include_dev { | |
632 | let path = path.as_str().unwrap(); | |
633 | let stat = nix::sys::stat::stat(path) | |
634 | .map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?; | |
635 | set.insert(stat.st_dev); | |
636 | } | |
637 | devices = Some(set); | |
638 | } | |
639 | ||
ae0be2dd | 640 | let mut upload_list = vec![]; |
a914a774 | 641 | |
79679c2d | 642 | enum BackupType { PXAR, IMAGE, CONFIG, LOGFILE }; |
6af905c1 | 643 | |
ae0be2dd DM |
644 | for backupspec in backupspec_list { |
645 | let (target, filename) = parse_backupspec(backupspec.as_str().unwrap())?; | |
bcd879cf | 646 | |
eb1804c5 DM |
647 | use std::os::unix::fs::FileTypeExt; |
648 | ||
3fa71727 CE |
649 | let metadata = std::fs::metadata(filename) |
650 | .map_err(|err| format_err!("unable to access '{}' - {}", filename, err))?; | |
eb1804c5 | 651 | let file_type = metadata.file_type(); |
23bb8780 | 652 | |
4af0ee05 DM |
653 | let extension = target.rsplit('.').next() |
654 | .ok_or(format_err!("missing target file extenion '{}'", target))?; | |
bcd879cf | 655 | |
ec8a9bb9 DM |
656 | match extension { |
657 | "pxar" => { | |
658 | if !file_type.is_dir() { | |
659 | bail!("got unexpected file type (expected directory)"); | |
660 | } | |
4af0ee05 | 661 | upload_list.push((BackupType::PXAR, filename.to_owned(), format!("{}.didx", target), 0)); |
ec8a9bb9 DM |
662 | } |
663 | "img" => { | |
eb1804c5 | 664 | |
ec8a9bb9 DM |
665 | if !(file_type.is_file() || file_type.is_block_device()) { |
666 | bail!("got unexpected file type (expected file or block device)"); | |
667 | } | |
eb1804c5 | 668 | |
e18a6c9e | 669 | let size = image_size(&PathBuf::from(filename))?; |
23bb8780 | 670 | |
ec8a9bb9 | 671 | if size == 0 { bail!("got zero-sized file '{}'", filename); } |
ae0be2dd | 672 | |
4af0ee05 | 673 | upload_list.push((BackupType::IMAGE, filename.to_owned(), format!("{}.fidx", target), size)); |
ec8a9bb9 DM |
674 | } |
675 | "conf" => { | |
676 | if !file_type.is_file() { | |
677 | bail!("got unexpected file type (expected regular file)"); | |
678 | } | |
4af0ee05 | 679 | upload_list.push((BackupType::CONFIG, filename.to_owned(), format!("{}.blob", target), metadata.len())); |
ec8a9bb9 | 680 | } |
79679c2d DM |
681 | "log" => { |
682 | if !file_type.is_file() { | |
683 | bail!("got unexpected file type (expected regular file)"); | |
684 | } | |
4af0ee05 | 685 | upload_list.push((BackupType::LOGFILE, filename.to_owned(), format!("{}.blob", target), metadata.len())); |
79679c2d | 686 | } |
ec8a9bb9 DM |
687 | _ => { |
688 | bail!("got unknown archive extension '{}'", extension); | |
689 | } | |
ae0be2dd DM |
690 | } |
691 | } | |
692 | ||
ca5d0b61 | 693 | let backup_time = Utc.timestamp(backup_time_opt.unwrap_or(Utc::now().timestamp()), 0); |
ae0be2dd | 694 | |
cc2ce4a9 | 695 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
d0a03d40 DM |
696 | record_repository(&repo); |
697 | ||
ca5d0b61 DM |
698 | println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time)); |
699 | ||
f69adc81 | 700 | println!("Client name: {}", proxmox::tools::nodename()); |
ca5d0b61 DM |
701 | |
702 | let start_time = Local::now(); | |
703 | ||
7a6cfbd9 | 704 | println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)); |
51144821 | 705 | |
bb823140 DM |
706 | let (crypt_config, rsa_encrypted_key) = match keyfile { |
707 | None => (None, None), | |
6d0983db | 708 | Some(path) => { |
bb823140 DM |
709 | let (key, created) = load_and_decrtypt_key(&path, get_encryption_key_password)?; |
710 | ||
711 | let crypt_config = CryptConfig::new(key)?; | |
712 | ||
713 | let path = master_pubkey_path()?; | |
714 | if path.exists() { | |
e18a6c9e | 715 | let pem_data = file_get_contents(&path)?; |
bb823140 DM |
716 | let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?; |
717 | let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?; | |
718 | (Some(Arc::new(crypt_config)), Some(enc_key)) | |
719 | } else { | |
720 | (Some(Arc::new(crypt_config)), None) | |
721 | } | |
6d0983db DM |
722 | } |
723 | }; | |
f98ac774 | 724 | |
e9722f8b | 725 | async_main(async move { |
cf9271e2 DM |
726 | let client = BackupWriter::start( |
727 | client, | |
728 | repo.store(), | |
729 | backup_type, | |
730 | &backup_id, | |
731 | backup_time, | |
732 | verbose, | |
733 | ).await?; | |
e9722f8b WB |
734 | |
735 | let mut file_list = vec![]; | |
736 | ||
737 | // fixme: encrypt/sign catalog? | |
738 | let catalog_file = std::fs::OpenOptions::new() | |
739 | .write(true) | |
740 | .read(true) | |
741 | .custom_flags(libc::O_TMPFILE) | |
742 | .open("/tmp")?; | |
743 | ||
744 | let catalog = Arc::new(Mutex::new(CatalogBlobWriter::new_compressed(catalog_file)?)); | |
745 | let mut upload_catalog = false; | |
746 | ||
747 | for (backup_type, filename, target, size) in upload_list { | |
748 | match backup_type { | |
749 | BackupType::CONFIG => { | |
750 | println!("Upload config file '{}' to '{:?}' as {}", filename, repo, target); | |
751 | let stats = client | |
752 | .upload_blob_from_file(&filename, &target, crypt_config.clone(), true) | |
753 | .await?; | |
754 | file_list.push((target, stats)); | |
755 | } | |
756 | BackupType::LOGFILE => { // fixme: remove - not needed anymore ? | |
757 | println!("Upload log file '{}' to '{:?}' as {}", filename, repo, target); | |
758 | let stats = client | |
759 | .upload_blob_from_file(&filename, &target, crypt_config.clone(), true) | |
760 | .await?; | |
761 | file_list.push((target, stats)); | |
762 | } | |
763 | BackupType::PXAR => { | |
764 | upload_catalog = true; | |
765 | println!("Upload directory '{}' to '{:?}' as {}", filename, repo, target); | |
766 | catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?; | |
767 | let stats = backup_directory( | |
768 | &client, | |
769 | &filename, | |
770 | &target, | |
771 | chunk_size_opt, | |
772 | devices.clone(), | |
773 | verbose, | |
774 | skip_lost_and_found, | |
775 | crypt_config.clone(), | |
776 | catalog.clone(), | |
777 | ).await?; | |
778 | file_list.push((target, stats)); | |
779 | catalog.lock().unwrap().end_directory()?; | |
780 | } | |
781 | BackupType::IMAGE => { | |
782 | println!("Upload image '{}' to '{:?}' as {}", filename, repo, target); | |
783 | let stats = backup_image( | |
784 | &client, | |
785 | &filename, | |
786 | &target, | |
787 | size, | |
788 | chunk_size_opt, | |
789 | verbose, | |
790 | crypt_config.clone(), | |
791 | ).await?; | |
792 | file_list.push((target, stats)); | |
793 | } | |
6af905c1 DM |
794 | } |
795 | } | |
4818c8b6 | 796 | |
e9722f8b WB |
797 | // finalize and upload catalog |
798 | if upload_catalog { | |
799 | let mutex = Arc::try_unwrap(catalog) | |
800 | .map_err(|_| format_err!("unable to get catalog (still used)"))?; | |
801 | let mut catalog_file = mutex.into_inner().unwrap().finish()?; | |
2761d6a4 | 802 | |
d2267b11 | 803 | let target = CATALOG_BLOB_NAME; |
2761d6a4 | 804 | |
e9722f8b | 805 | catalog_file.seek(SeekFrom::Start(0))?; |
9d135fe6 | 806 | |
e9722f8b WB |
807 | let stats = client.upload_blob(catalog_file, target).await?; |
808 | file_list.push((target.to_owned(), stats)); | |
809 | } | |
2761d6a4 | 810 | |
e9722f8b WB |
811 | if let Some(rsa_encrypted_key) = rsa_encrypted_key { |
812 | let target = "rsa-encrypted.key"; | |
813 | println!("Upload RSA encoded key to '{:?}' as {}", repo, target); | |
814 | let stats = client | |
815 | .upload_blob_from_data(rsa_encrypted_key, target, None, false, false) | |
816 | .await?; | |
817 | file_list.push((format!("{}.blob", target), stats)); | |
818 | ||
819 | // openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t | |
820 | /* | |
821 | let mut buffer2 = vec![0u8; rsa.size() as usize]; | |
822 | let pem_data = file_get_contents("master-private.pem")?; | |
823 | let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?; | |
824 | let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?; | |
825 | println!("TEST {} {:?}", len, buffer2); | |
826 | */ | |
827 | } | |
9f46c7de | 828 | |
e9722f8b WB |
829 | // create index.json |
830 | let file_list = file_list.iter() | |
831 | .fold(vec![], |mut acc, (filename, stats)| { | |
832 | acc.push(json!({ | |
833 | "filename": filename, | |
834 | "size": stats.size, | |
835 | "csum": proxmox::tools::digest_to_hex(&stats.csum), | |
836 | })); | |
837 | acc | |
838 | }); | |
2c3891d1 | 839 | |
e9722f8b WB |
840 | let index = json!({ |
841 | "backup-type": backup_type, | |
842 | "backup-id": backup_id, | |
843 | "backup-time": backup_time.timestamp(), | |
844 | "files": file_list, | |
845 | }); | |
2c3891d1 | 846 | |
e9722f8b WB |
847 | println!("Upload index.json to '{:?}'", repo); |
848 | let index_data = serde_json::to_string_pretty(&index)?.into(); | |
849 | client | |
850 | .upload_blob_from_data(index_data, "index.json.blob", crypt_config.clone(), true, true) | |
851 | .await?; | |
2c3891d1 | 852 | |
e9722f8b | 853 | client.finish().await?; |
c4ff3dce | 854 | |
e9722f8b WB |
855 | let end_time = Local::now(); |
856 | let elapsed = end_time.signed_duration_since(start_time); | |
857 | println!("Duration: {}", elapsed); | |
3ec3ec3f | 858 | |
e9722f8b | 859 | println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)); |
3d5c11e5 | 860 | |
e9722f8b WB |
861 | Ok(Value::Null) |
862 | }) | |
f98ea63d DM |
863 | } |
864 | ||
d0a03d40 | 865 | fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
f98ea63d DM |
866 | |
867 | let mut result = vec![]; | |
868 | ||
869 | let data: Vec<&str> = arg.splitn(2, ':').collect(); | |
870 | ||
bff11030 | 871 | if data.len() != 2 { |
8968258b DM |
872 | result.push(String::from("root.pxar:/")); |
873 | result.push(String::from("etc.pxar:/etc")); | |
bff11030 DM |
874 | return result; |
875 | } | |
f98ea63d | 876 | |
496a6784 | 877 | let files = tools::complete_file_name(data[1], param); |
f98ea63d DM |
878 | |
879 | for file in files { | |
880 | result.push(format!("{}:{}", data[0], file)); | |
881 | } | |
882 | ||
883 | result | |
ff5d3707 | 884 | } |
885 | ||
9f912493 DM |
886 | fn restore( |
887 | param: Value, | |
888 | _info: &ApiMethod, | |
dd5495d6 | 889 | _rpcenv: &mut dyn RpcEnvironment, |
9f912493 | 890 | ) -> Result<Value, Error> { |
e9722f8b WB |
891 | async_main(restore_do(param)) |
892 | } | |
9f912493 | 893 | |
a40220c0 DM |
894 | async fn download_index_blob(client: Arc<BackupReader>, crypt_config: Option<Arc<CryptConfig>>) -> Result<Vec<u8>, Error> { |
895 | ||
896 | let index_data = client.download(INDEX_BLOB_NAME, Vec::with_capacity(64*1024)).await?; | |
897 | let blob = DataBlob::from_raw(index_data)?; | |
898 | blob.verify_crc()?; | |
7123ff7d | 899 | blob.decode(crypt_config.as_ref().map(Arc::as_ref)) |
a40220c0 DM |
900 | } |
901 | ||
902 | fn verify_index_file(backup_index: &Value, name: &str, csum: &[u8; 32], size: u64) -> Result<(), Error> { | |
903 | ||
904 | let files = backup_index["files"] | |
905 | .as_array() | |
906 | .ok_or_else(|| format_err!("mailformed index - missing 'files' property"))?; | |
907 | ||
908 | let info = files.iter().find(|v| { | |
909 | match v["filename"].as_str() { | |
910 | Some(filename) => filename == name, | |
911 | None => false, | |
912 | } | |
913 | }); | |
914 | ||
915 | let info = match info { | |
916 | None => bail!("index does not contain file '{}'", name), | |
917 | Some(info) => info, | |
918 | }; | |
919 | ||
920 | match info["size"].as_u64() { | |
921 | None => bail!("index does not contain property 'size' for file '{}'", name), | |
922 | Some(expected_size) => { | |
923 | if expected_size != size { | |
ff01c1e3 | 924 | bail!("verify index failed - wrong size for file '{}' ({} != {}", name, expected_size, size); |
a40220c0 DM |
925 | } |
926 | } | |
927 | }; | |
928 | ||
929 | match info["csum"].as_str() { | |
930 | None => bail!("index does not contain property 'csum' for file '{}'", name), | |
931 | Some(expected_csum) => { | |
932 | let expected_csum = &proxmox::tools::hex_to_digest(expected_csum)?; | |
933 | if expected_csum != csum { | |
934 | bail!("verify index failed - wrong checksum for file '{}'", name); | |
935 | } | |
936 | } | |
937 | }; | |
938 | ||
939 | Ok(()) | |
940 | } | |
941 | ||
88892ea8 DM |
942 | fn dump_image<W: Write>( |
943 | client: Arc<BackupReader>, | |
944 | crypt_config: Option<Arc<CryptConfig>>, | |
945 | index: FixedIndexReader, | |
946 | mut writer: W, | |
fd04ca7a | 947 | verbose: bool, |
88892ea8 DM |
948 | ) -> Result<(), Error> { |
949 | ||
950 | let most_used = index.find_most_used_chunks(8); | |
951 | ||
952 | let mut chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
953 | ||
954 | // Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy | |
955 | // and thus slows down reading. Instead, directly use RemoteChunkReader | |
fd04ca7a DM |
956 | let mut per = 0; |
957 | let mut bytes = 0; | |
958 | let start_time = std::time::Instant::now(); | |
959 | ||
88892ea8 DM |
960 | for pos in 0..index.index_count() { |
961 | let digest = index.index_digest(pos).unwrap(); | |
962 | let raw_data = chunk_reader.read_chunk(&digest)?; | |
963 | writer.write_all(&raw_data)?; | |
fd04ca7a DM |
964 | bytes += raw_data.len(); |
965 | if verbose { | |
966 | let next_per = ((pos+1)*100)/index.index_count(); | |
967 | if per != next_per { | |
968 | eprintln!("progress {}% (read {} bytes, duration {} sec)", | |
969 | next_per, bytes, start_time.elapsed().as_secs()); | |
970 | per = next_per; | |
971 | } | |
972 | } | |
88892ea8 DM |
973 | } |
974 | ||
fd04ca7a DM |
975 | let end_time = std::time::Instant::now(); |
976 | let elapsed = end_time.duration_since(start_time); | |
977 | eprintln!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)", | |
978 | bytes, | |
979 | elapsed.as_secs_f64(), | |
980 | bytes as f64/(1024.0*1024.0*elapsed.as_secs_f64()) | |
981 | ); | |
982 | ||
983 | ||
88892ea8 DM |
984 | Ok(()) |
985 | } | |
986 | ||
e9722f8b | 987 | async fn restore_do(param: Value) -> Result<Value, Error> { |
2665cef7 | 988 | let repo = extract_repository_from_value(¶m)?; |
9f912493 | 989 | |
86eda3eb DM |
990 | let verbose = param["verbose"].as_bool().unwrap_or(false); |
991 | ||
46d5aa0a DM |
992 | let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false); |
993 | ||
d5c34d98 DM |
994 | let archive_name = tools::required_string_param(¶m, "archive-name")?; |
995 | ||
cc2ce4a9 | 996 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
d0a03d40 | 997 | |
d0a03d40 | 998 | record_repository(&repo); |
d5c34d98 | 999 | |
9f912493 | 1000 | let path = tools::required_string_param(¶m, "snapshot")?; |
9f912493 | 1001 | |
86eda3eb | 1002 | let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { |
d5c34d98 | 1003 | let group = BackupGroup::parse(path)?; |
9f912493 | 1004 | |
9e391bb7 DM |
1005 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); |
1006 | let result = client.get(&path, Some(json!({ | |
d5c34d98 DM |
1007 | "backup-type": group.backup_type(), |
1008 | "backup-id": group.backup_id(), | |
e9722f8b | 1009 | }))).await?; |
9f912493 | 1010 | |
d5c34d98 DM |
1011 | let list = result["data"].as_array().unwrap(); |
1012 | if list.len() == 0 { | |
1013 | bail!("backup group '{}' does not contain any snapshots:", path); | |
1014 | } | |
9f912493 | 1015 | |
86eda3eb | 1016 | let epoch = list[0]["backup-time"].as_i64().unwrap(); |
fa5d6977 | 1017 | let backup_time = Utc.timestamp(epoch, 0); |
86eda3eb | 1018 | (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time) |
d5c34d98 DM |
1019 | } else { |
1020 | let snapshot = BackupDir::parse(path)?; | |
86eda3eb DM |
1021 | (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) |
1022 | }; | |
9f912493 | 1023 | |
d5c34d98 | 1024 | let target = tools::required_string_param(¶m, "target")?; |
bf125261 | 1025 | let target = if target == "-" { None } else { Some(target) }; |
2ae7d196 | 1026 | |
86eda3eb | 1027 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); |
2ae7d196 | 1028 | |
86eda3eb DM |
1029 | let crypt_config = match keyfile { |
1030 | None => None, | |
1031 | Some(path) => { | |
1032 | let (key, _) = load_and_decrtypt_key(&path, get_encryption_key_password)?; | |
1033 | Some(Arc::new(CryptConfig::new(key)?)) | |
1034 | } | |
1035 | }; | |
d5c34d98 | 1036 | |
afb4cd28 DM |
1037 | let server_archive_name = if archive_name.ends_with(".pxar") { |
1038 | format!("{}.didx", archive_name) | |
1039 | } else if archive_name.ends_with(".img") { | |
1040 | format!("{}.fidx", archive_name) | |
1041 | } else { | |
f8100e96 | 1042 | format!("{}.blob", archive_name) |
afb4cd28 | 1043 | }; |
9f912493 | 1044 | |
9e490a74 | 1045 | let client = BackupReader::start(client, repo.store(), &backup_type, &backup_id, backup_time, true).await?; |
86eda3eb | 1046 | |
86eda3eb DM |
1047 | let tmpfile = std::fs::OpenOptions::new() |
1048 | .write(true) | |
1049 | .read(true) | |
1050 | .custom_flags(libc::O_TMPFILE) | |
1051 | .open("/tmp")?; | |
1052 | ||
df65bd3d | 1053 | |
a40220c0 DM |
1054 | let backup_index_data = download_index_blob(client.clone(), crypt_config.clone()).await?; |
1055 | let backup_index: Value = serde_json::from_slice(&backup_index_data[..])?; | |
02fcf372 DM |
1056 | |
1057 | if server_archive_name == INDEX_BLOB_NAME { | |
1058 | if let Some(target) = target { | |
1059 | file_set_contents(target, &backup_index_data, None)?; | |
1060 | } else { | |
1061 | let stdout = std::io::stdout(); | |
1062 | let mut writer = stdout.lock(); | |
1063 | writer.write_all(&backup_index_data) | |
1064 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; | |
1065 | } | |
1066 | ||
1067 | } else if server_archive_name.ends_with(".blob") { | |
2b92971f | 1068 | let mut tmpfile = client.download(&server_archive_name, tmpfile).await?; |
d2267b11 DM |
1069 | |
1070 | let (csum, size) = compute_file_csum(&mut tmpfile)?; | |
1071 | verify_index_file(&backup_index, &server_archive_name, &csum, size)?; | |
1072 | ||
0d986280 DM |
1073 | tmpfile.seek(SeekFrom::Start(0))?; |
1074 | let mut reader = DataBlobReader::new(tmpfile, crypt_config)?; | |
f8100e96 | 1075 | |
bf125261 | 1076 | if let Some(target) = target { |
0d986280 DM |
1077 | let mut writer = std::fs::OpenOptions::new() |
1078 | .write(true) | |
1079 | .create(true) | |
1080 | .create_new(true) | |
1081 | .open(target) | |
1082 | .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?; | |
1083 | std::io::copy(&mut reader, &mut writer)?; | |
bf125261 DM |
1084 | } else { |
1085 | let stdout = std::io::stdout(); | |
1086 | let mut writer = stdout.lock(); | |
0d986280 | 1087 | std::io::copy(&mut reader, &mut writer) |
bf125261 DM |
1088 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; |
1089 | } | |
f8100e96 DM |
1090 | |
1091 | } else if server_archive_name.ends_with(".didx") { | |
e9722f8b | 1092 | let tmpfile = client.download(&server_archive_name, tmpfile).await?; |
86eda3eb | 1093 | |
afb4cd28 DM |
1094 | let index = DynamicIndexReader::new(tmpfile) |
1095 | .map_err(|err| format_err!("unable to read dynamic index '{}' - {}", archive_name, err))?; | |
86eda3eb | 1096 | |
df65bd3d DM |
1097 | // Note: do not use values stored in index (not trusted) - instead, computed them again |
1098 | let (csum, size) = index.compute_csum(); | |
1099 | ||
a40220c0 | 1100 | verify_index_file(&backup_index, &server_archive_name, &csum, size)?; |
df65bd3d | 1101 | |
f4bf7dfc DM |
1102 | let most_used = index.find_most_used_chunks(8); |
1103 | ||
1104 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
1105 | ||
afb4cd28 | 1106 | let mut reader = BufferedDynamicReader::new(index, chunk_reader); |
86eda3eb | 1107 | |
bf125261 | 1108 | if let Some(target) = target { |
86eda3eb | 1109 | |
47651f95 | 1110 | let feature_flags = pxar::flags::DEFAULT; |
bf125261 DM |
1111 | let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags, |path| { |
1112 | if verbose { | |
fd04ca7a | 1113 | eprintln!("{:?}", path); |
bf125261 DM |
1114 | } |
1115 | Ok(()) | |
1116 | }); | |
6a879109 CE |
1117 | decoder.set_allow_existing_dirs(allow_existing_dirs); |
1118 | ||
fa7e957c | 1119 | decoder.restore(Path::new(target), &Vec::new())?; |
bf125261 | 1120 | } else { |
88892ea8 DM |
1121 | let mut writer = std::fs::OpenOptions::new() |
1122 | .write(true) | |
1123 | .open("/dev/stdout") | |
1124 | .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?; | |
afb4cd28 | 1125 | |
bf125261 DM |
1126 | std::io::copy(&mut reader, &mut writer) |
1127 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; | |
1128 | } | |
afb4cd28 | 1129 | } else if server_archive_name.ends_with(".fidx") { |
e9722f8b | 1130 | let tmpfile = client.download(&server_archive_name, tmpfile).await?; |
afb4cd28 DM |
1131 | |
1132 | let index = FixedIndexReader::new(tmpfile) | |
1133 | .map_err(|err| format_err!("unable to read fixed index '{}' - {}", archive_name, err))?; | |
7dcbe051 | 1134 | |
df65bd3d DM |
1135 | // Note: do not use values stored in index (not trusted) - instead, computed them again |
1136 | let (csum, size) = index.compute_csum(); | |
1137 | ||
a40220c0 | 1138 | verify_index_file(&backup_index, &server_archive_name, &csum, size)?; |
df65bd3d | 1139 | |
88892ea8 DM |
1140 | let mut writer = if let Some(target) = target { |
1141 | std::fs::OpenOptions::new() | |
bf125261 DM |
1142 | .write(true) |
1143 | .create(true) | |
1144 | .create_new(true) | |
1145 | .open(target) | |
88892ea8 | 1146 | .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))? |
bf125261 | 1147 | } else { |
88892ea8 DM |
1148 | std::fs::OpenOptions::new() |
1149 | .write(true) | |
1150 | .open("/dev/stdout") | |
1151 | .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))? | |
1152 | }; | |
afb4cd28 | 1153 | |
fd04ca7a | 1154 | dump_image(client.clone(), crypt_config.clone(), index, &mut writer, verbose)?; |
88892ea8 DM |
1155 | |
1156 | } else { | |
f8100e96 | 1157 | bail!("unknown archive file extension (expected .pxar of .img)"); |
3031e44c | 1158 | } |
fef44d4f DM |
1159 | |
1160 | Ok(Value::Null) | |
45db6f89 DM |
1161 | } |
1162 | ||
ec34f7eb DM |
1163 | fn upload_log( |
1164 | param: Value, | |
1165 | _info: &ApiMethod, | |
1166 | _rpcenv: &mut dyn RpcEnvironment, | |
1167 | ) -> Result<Value, Error> { | |
1168 | ||
1169 | let logfile = tools::required_string_param(¶m, "logfile")?; | |
1170 | let repo = extract_repository_from_value(¶m)?; | |
1171 | ||
1172 | let snapshot = tools::required_string_param(¶m, "snapshot")?; | |
1173 | let snapshot = BackupDir::parse(snapshot)?; | |
1174 | ||
cc2ce4a9 | 1175 | let mut client = HttpClient::new(repo.host(), repo.user(), None)?; |
ec34f7eb DM |
1176 | |
1177 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); | |
1178 | ||
1179 | let crypt_config = match keyfile { | |
1180 | None => None, | |
1181 | Some(path) => { | |
1182 | let (key, _created) = load_and_decrtypt_key(&path, get_encryption_key_password)?; | |
1183 | let crypt_config = CryptConfig::new(key)?; | |
9025312a | 1184 | Some(Arc::new(crypt_config)) |
ec34f7eb DM |
1185 | } |
1186 | }; | |
1187 | ||
e18a6c9e | 1188 | let data = file_get_contents(logfile)?; |
ec34f7eb | 1189 | |
7123ff7d | 1190 | let blob = DataBlob::encode(&data, crypt_config.as_ref().map(Arc::as_ref), true)?; |
ec34f7eb DM |
1191 | |
1192 | let raw_data = blob.into_inner(); | |
1193 | ||
1194 | let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store()); | |
1195 | ||
1196 | let args = json!({ | |
1197 | "backup-type": snapshot.group().backup_type(), | |
1198 | "backup-id": snapshot.group().backup_id(), | |
1199 | "backup-time": snapshot.backup_time().timestamp(), | |
1200 | }); | |
1201 | ||
1202 | let body = hyper::Body::from(raw_data); | |
1203 | ||
e9722f8b WB |
1204 | async_main(async move { |
1205 | client.upload("application/octet-stream", body, &path, Some(args)).await | |
1206 | }) | |
ec34f7eb DM |
1207 | } |
1208 | ||
83b7db02 | 1209 | fn prune( |
ea7a7ef2 | 1210 | mut param: Value, |
83b7db02 | 1211 | _info: &ApiMethod, |
dd5495d6 | 1212 | _rpcenv: &mut dyn RpcEnvironment, |
83b7db02 DM |
1213 | ) -> Result<Value, Error> { |
1214 | ||
2665cef7 | 1215 | let repo = extract_repository_from_value(¶m)?; |
83b7db02 | 1216 | |
cc2ce4a9 | 1217 | let mut client = HttpClient::new(repo.host(), repo.user(), None)?; |
83b7db02 | 1218 | |
d0a03d40 | 1219 | let path = format!("api2/json/admin/datastore/{}/prune", repo.store()); |
83b7db02 | 1220 | |
9fdc3ef4 DM |
1221 | let group = tools::required_string_param(¶m, "group")?; |
1222 | let group = BackupGroup::parse(group)?; | |
1223 | ||
ea7a7ef2 DM |
1224 | param.as_object_mut().unwrap().remove("repository"); |
1225 | param.as_object_mut().unwrap().remove("group"); | |
1226 | ||
1227 | param["backup-type"] = group.backup_type().into(); | |
1228 | param["backup-id"] = group.backup_id().into(); | |
83b7db02 | 1229 | |
e9722f8b | 1230 | let _result = async_main(async move { client.post(&path, Some(param)).await })?; |
83b7db02 | 1231 | |
d0a03d40 DM |
1232 | record_repository(&repo); |
1233 | ||
43a406fd | 1234 | Ok(Value::Null) |
83b7db02 DM |
1235 | } |
1236 | ||
34a816cc DM |
1237 | fn status( |
1238 | param: Value, | |
1239 | _info: &ApiMethod, | |
1240 | _rpcenv: &mut dyn RpcEnvironment, | |
1241 | ) -> Result<Value, Error> { | |
1242 | ||
1243 | let repo = extract_repository_from_value(¶m)?; | |
1244 | ||
1245 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); | |
1246 | ||
cc2ce4a9 | 1247 | let client = HttpClient::new(repo.host(), repo.user(), None)?; |
34a816cc DM |
1248 | |
1249 | let path = format!("api2/json/admin/datastore/{}/status", repo.store()); | |
1250 | ||
e9722f8b | 1251 | let result = async_main(async move { client.get(&path, None).await })?; |
34a816cc DM |
1252 | let data = &result["data"]; |
1253 | ||
1254 | record_repository(&repo); | |
1255 | ||
1256 | if output_format == "text" { | |
1257 | let total = data["total"].as_u64().unwrap(); | |
1258 | let used = data["used"].as_u64().unwrap(); | |
1259 | let avail = data["avail"].as_u64().unwrap(); | |
1260 | let roundup = total/200; | |
1261 | ||
1262 | println!( | |
1263 | "total: {} used: {} ({} %) available: {}", | |
1264 | total, | |
1265 | used, | |
1266 | ((used+roundup)*100)/total, | |
1267 | avail, | |
1268 | ); | |
1269 | } else { | |
f6ede796 | 1270 | format_and_print_result(data, &output_format); |
34a816cc DM |
1271 | } |
1272 | ||
1273 | Ok(Value::Null) | |
1274 | } | |
1275 | ||
5a2df000 | 1276 | // like get, but simply ignore errors and return Null instead |
e9722f8b | 1277 | async fn try_get(repo: &BackupRepository, url: &str) -> Value { |
024f11bb | 1278 | |
cc2ce4a9 | 1279 | let client = match HttpClient::new(repo.host(), repo.user(), None) { |
45cdce06 DM |
1280 | Ok(v) => v, |
1281 | _ => return Value::Null, | |
1282 | }; | |
b2388518 | 1283 | |
e9722f8b | 1284 | let mut resp = match client.get(url, None).await { |
b2388518 DM |
1285 | Ok(v) => v, |
1286 | _ => return Value::Null, | |
1287 | }; | |
1288 | ||
1289 | if let Some(map) = resp.as_object_mut() { | |
1290 | if let Some(data) = map.remove("data") { | |
1291 | return data; | |
1292 | } | |
1293 | } | |
1294 | Value::Null | |
1295 | } | |
1296 | ||
b2388518 | 1297 | fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
e9722f8b WB |
1298 | async_main(async { complete_backup_group_do(param).await }) |
1299 | } | |
1300 | ||
1301 | async fn complete_backup_group_do(param: &HashMap<String, String>) -> Vec<String> { | |
024f11bb | 1302 | |
b2388518 DM |
1303 | let mut result = vec![]; |
1304 | ||
2665cef7 | 1305 | let repo = match extract_repository_from_map(param) { |
b2388518 | 1306 | Some(v) => v, |
024f11bb DM |
1307 | _ => return result, |
1308 | }; | |
1309 | ||
b2388518 DM |
1310 | let path = format!("api2/json/admin/datastore/{}/groups", repo.store()); |
1311 | ||
e9722f8b | 1312 | let data = try_get(&repo, &path).await; |
b2388518 DM |
1313 | |
1314 | if let Some(list) = data.as_array() { | |
024f11bb | 1315 | for item in list { |
98f0b972 DM |
1316 | if let (Some(backup_id), Some(backup_type)) = |
1317 | (item["backup-id"].as_str(), item["backup-type"].as_str()) | |
1318 | { | |
1319 | result.push(format!("{}/{}", backup_type, backup_id)); | |
024f11bb DM |
1320 | } |
1321 | } | |
1322 | } | |
1323 | ||
1324 | result | |
1325 | } | |
1326 | ||
b2388518 | 1327 | fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
e9722f8b WB |
1328 | async_main(async { complete_group_or_snapshot_do(arg, param).await }) |
1329 | } | |
1330 | ||
1331 | async fn complete_group_or_snapshot_do(arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
b2388518 | 1332 | |
b2388518 | 1333 | if arg.matches('/').count() < 2 { |
e9722f8b | 1334 | let groups = complete_backup_group_do(param).await; |
543a260f | 1335 | let mut result = vec![]; |
b2388518 DM |
1336 | for group in groups { |
1337 | result.push(group.to_string()); | |
1338 | result.push(format!("{}/", group)); | |
1339 | } | |
1340 | return result; | |
1341 | } | |
1342 | ||
e9722f8b | 1343 | complete_backup_snapshot_do(param).await |
543a260f | 1344 | } |
b2388518 | 1345 | |
3fb53e07 | 1346 | fn complete_backup_snapshot(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
e9722f8b WB |
1347 | async_main(async { complete_backup_snapshot_do(param).await }) |
1348 | } | |
1349 | ||
1350 | async fn complete_backup_snapshot_do(param: &HashMap<String, String>) -> Vec<String> { | |
543a260f DM |
1351 | |
1352 | let mut result = vec![]; | |
1353 | ||
1354 | let repo = match extract_repository_from_map(param) { | |
1355 | Some(v) => v, | |
1356 | _ => return result, | |
1357 | }; | |
1358 | ||
1359 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); | |
b2388518 | 1360 | |
e9722f8b | 1361 | let data = try_get(&repo, &path).await; |
b2388518 DM |
1362 | |
1363 | if let Some(list) = data.as_array() { | |
1364 | for item in list { | |
1365 | if let (Some(backup_id), Some(backup_type), Some(backup_time)) = | |
1366 | (item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64()) | |
1367 | { | |
1368 | let snapshot = BackupDir::new(backup_type, backup_id, backup_time); | |
1369 | result.push(snapshot.relative_path().to_str().unwrap().to_owned()); | |
1370 | } | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | result | |
1375 | } | |
1376 | ||
45db6f89 | 1377 | fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
e9722f8b WB |
1378 | async_main(async { complete_server_file_name_do(param).await }) |
1379 | } | |
1380 | ||
1381 | async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<String> { | |
08dc340a DM |
1382 | |
1383 | let mut result = vec![]; | |
1384 | ||
2665cef7 | 1385 | let repo = match extract_repository_from_map(param) { |
08dc340a DM |
1386 | Some(v) => v, |
1387 | _ => return result, | |
1388 | }; | |
1389 | ||
1390 | let snapshot = match param.get("snapshot") { | |
1391 | Some(path) => { | |
1392 | match BackupDir::parse(path) { | |
1393 | Ok(v) => v, | |
1394 | _ => return result, | |
1395 | } | |
1396 | } | |
1397 | _ => return result, | |
1398 | }; | |
1399 | ||
1400 | let query = tools::json_object_to_query(json!({ | |
1401 | "backup-type": snapshot.group().backup_type(), | |
1402 | "backup-id": snapshot.group().backup_id(), | |
1403 | "backup-time": snapshot.backup_time().timestamp(), | |
1404 | })).unwrap(); | |
1405 | ||
1406 | let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query); | |
1407 | ||
e9722f8b | 1408 | let data = try_get(&repo, &path).await; |
08dc340a DM |
1409 | |
1410 | if let Some(list) = data.as_array() { | |
1411 | for item in list { | |
c4f025eb | 1412 | if let Some(filename) = item["filename"].as_str() { |
08dc340a DM |
1413 | result.push(filename.to_owned()); |
1414 | } | |
1415 | } | |
1416 | } | |
1417 | ||
45db6f89 DM |
1418 | result |
1419 | } | |
1420 | ||
1421 | fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
52c171e4 | 1422 | complete_server_file_name(arg, param) |
e9722f8b WB |
1423 | .iter() |
1424 | .map(|v| strip_server_file_expenstion(&v)) | |
1425 | .collect() | |
08dc340a DM |
1426 | } |
1427 | ||
49811347 DM |
1428 | fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> { |
1429 | ||
1430 | let mut result = vec![]; | |
1431 | ||
1432 | let mut size = 64; | |
1433 | loop { | |
1434 | result.push(size.to_string()); | |
1435 | size = size * 2; | |
1436 | if size > 4096 { break; } | |
1437 | } | |
1438 | ||
1439 | result | |
1440 | } | |
1441 | ||
826f309b | 1442 | fn get_encryption_key_password() -> Result<Vec<u8>, Error> { |
ff5d3707 | 1443 | |
f2401311 DM |
1444 | // fixme: implement other input methods |
1445 | ||
1446 | use std::env::VarError::*; | |
1447 | match std::env::var("PBS_ENCRYPTION_PASSWORD") { | |
826f309b | 1448 | Ok(p) => return Ok(p.as_bytes().to_vec()), |
f2401311 DM |
1449 | Err(NotUnicode(_)) => bail!("PBS_ENCRYPTION_PASSWORD contains bad characters"), |
1450 | Err(NotPresent) => { | |
1451 | // Try another method | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | // If we're on a TTY, query the user for a password | |
1456 | if crate::tools::tty::stdin_isatty() { | |
826f309b | 1457 | return Ok(crate::tools::tty::read_password("Encryption Key Password: ")?); |
f2401311 DM |
1458 | } |
1459 | ||
1460 | bail!("no password input mechanism available"); | |
1461 | } | |
1462 | ||
ac716234 DM |
1463 | fn key_create( |
1464 | param: Value, | |
1465 | _info: &ApiMethod, | |
1466 | _rpcenv: &mut dyn RpcEnvironment, | |
1467 | ) -> Result<Value, Error> { | |
1468 | ||
9b06db45 DM |
1469 | let path = tools::required_string_param(¶m, "path")?; |
1470 | let path = PathBuf::from(path); | |
ac716234 | 1471 | |
181f097a | 1472 | let kdf = param["kdf"].as_str().unwrap_or("scrypt"); |
ac716234 DM |
1473 | |
1474 | let key = proxmox::sys::linux::random_data(32)?; | |
1475 | ||
181f097a DM |
1476 | if kdf == "scrypt" { |
1477 | // always read passphrase from tty | |
1478 | if !crate::tools::tty::stdin_isatty() { | |
1479 | bail!("unable to read passphrase - no tty"); | |
1480 | } | |
ac716234 | 1481 | |
181f097a DM |
1482 | let password = crate::tools::tty::read_password("Encryption Key Password: ")?; |
1483 | ||
ab44acff | 1484 | let key_config = encrypt_key_with_passphrase(&key, &password)?; |
37c5a175 | 1485 | |
ab44acff | 1486 | store_key_config(&path, false, key_config)?; |
181f097a DM |
1487 | |
1488 | Ok(Value::Null) | |
1489 | } else if kdf == "none" { | |
1490 | let created = Local.timestamp(Local::now().timestamp(), 0); | |
1491 | ||
1492 | store_key_config(&path, false, KeyConfig { | |
1493 | kdf: None, | |
1494 | created, | |
ab44acff | 1495 | modified: created, |
181f097a DM |
1496 | data: key, |
1497 | })?; | |
1498 | ||
1499 | Ok(Value::Null) | |
1500 | } else { | |
1501 | unreachable!(); | |
1502 | } | |
ac716234 DM |
1503 | } |
1504 | ||
9f46c7de DM |
1505 | fn master_pubkey_path() -> Result<PathBuf, Error> { |
1506 | let base = BaseDirectories::with_prefix("proxmox-backup")?; | |
1507 | ||
1508 | // usually $HOME/.config/proxmox-backup/master-public.pem | |
1509 | let path = base.place_config_file("master-public.pem")?; | |
1510 | ||
1511 | Ok(path) | |
1512 | } | |
1513 | ||
3ea8bfc9 DM |
1514 | fn key_import_master_pubkey( |
1515 | param: Value, | |
1516 | _info: &ApiMethod, | |
1517 | _rpcenv: &mut dyn RpcEnvironment, | |
1518 | ) -> Result<Value, Error> { | |
1519 | ||
1520 | let path = tools::required_string_param(¶m, "path")?; | |
1521 | let path = PathBuf::from(path); | |
1522 | ||
e18a6c9e | 1523 | let pem_data = file_get_contents(&path)?; |
3ea8bfc9 DM |
1524 | |
1525 | if let Err(err) = openssl::pkey::PKey::public_key_from_pem(&pem_data) { | |
1526 | bail!("Unable to decode PEM data - {}", err); | |
1527 | } | |
1528 | ||
9f46c7de | 1529 | let target_path = master_pubkey_path()?; |
3ea8bfc9 | 1530 | |
e18a6c9e | 1531 | file_set_contents(&target_path, &pem_data, None)?; |
3ea8bfc9 DM |
1532 | |
1533 | println!("Imported public master key to {:?}", target_path); | |
1534 | ||
1535 | Ok(Value::Null) | |
1536 | } | |
1537 | ||
37c5a175 DM |
1538 | fn key_create_master_key( |
1539 | _param: Value, | |
1540 | _info: &ApiMethod, | |
1541 | _rpcenv: &mut dyn RpcEnvironment, | |
1542 | ) -> Result<Value, Error> { | |
1543 | ||
1544 | // we need a TTY to query the new password | |
1545 | if !crate::tools::tty::stdin_isatty() { | |
1546 | bail!("unable to create master key - no tty"); | |
1547 | } | |
1548 | ||
1549 | let rsa = openssl::rsa::Rsa::generate(4096)?; | |
1550 | let pkey = openssl::pkey::PKey::from_rsa(rsa)?; | |
1551 | ||
1552 | let new_pw = String::from_utf8(crate::tools::tty::read_password("Master Key Password: ")?)?; | |
1553 | let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?; | |
1554 | ||
1555 | if new_pw != verify_pw { | |
1556 | bail!("Password verification fail!"); | |
1557 | } | |
1558 | ||
1559 | if new_pw.len() < 5 { | |
1560 | bail!("Password is too short!"); | |
1561 | } | |
1562 | ||
1563 | let pub_key: Vec<u8> = pkey.public_key_to_pem()?; | |
1564 | let filename_pub = "master-public.pem"; | |
1565 | println!("Writing public master key to {}", filename_pub); | |
e18a6c9e | 1566 | file_set_contents(filename_pub, pub_key.as_slice(), None)?; |
37c5a175 DM |
1567 | |
1568 | let cipher = openssl::symm::Cipher::aes_256_cbc(); | |
1569 | let priv_key: Vec<u8> = pkey.private_key_to_pem_pkcs8_passphrase(cipher, new_pw.as_bytes())?; | |
1570 | ||
1571 | let filename_priv = "master-private.pem"; | |
1572 | println!("Writing private master key to {}", filename_priv); | |
e18a6c9e | 1573 | file_set_contents(filename_priv, priv_key.as_slice(), None)?; |
37c5a175 DM |
1574 | |
1575 | Ok(Value::Null) | |
1576 | } | |
ac716234 DM |
1577 | |
1578 | fn key_change_passphrase( | |
1579 | param: Value, | |
1580 | _info: &ApiMethod, | |
1581 | _rpcenv: &mut dyn RpcEnvironment, | |
1582 | ) -> Result<Value, Error> { | |
1583 | ||
9b06db45 DM |
1584 | let path = tools::required_string_param(¶m, "path")?; |
1585 | let path = PathBuf::from(path); | |
ac716234 | 1586 | |
181f097a DM |
1587 | let kdf = param["kdf"].as_str().unwrap_or("scrypt"); |
1588 | ||
ac716234 DM |
1589 | // we need a TTY to query the new password |
1590 | if !crate::tools::tty::stdin_isatty() { | |
1591 | bail!("unable to change passphrase - no tty"); | |
1592 | } | |
1593 | ||
ab44acff | 1594 | let (key, created) = load_and_decrtypt_key(&path, get_encryption_key_password)?; |
ac716234 | 1595 | |
181f097a | 1596 | if kdf == "scrypt" { |
ac716234 | 1597 | |
181f097a DM |
1598 | let new_pw = String::from_utf8(crate::tools::tty::read_password("New Password: ")?)?; |
1599 | let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?; | |
ac716234 | 1600 | |
181f097a DM |
1601 | if new_pw != verify_pw { |
1602 | bail!("Password verification fail!"); | |
1603 | } | |
1604 | ||
1605 | if new_pw.len() < 5 { | |
1606 | bail!("Password is too short!"); | |
1607 | } | |
ac716234 | 1608 | |
ab44acff DM |
1609 | let mut new_key_config = encrypt_key_with_passphrase(&key, new_pw.as_bytes())?; |
1610 | new_key_config.created = created; // keep original value | |
1611 | ||
1612 | store_key_config(&path, true, new_key_config)?; | |
ac716234 | 1613 | |
181f097a DM |
1614 | Ok(Value::Null) |
1615 | } else if kdf == "none" { | |
ab44acff | 1616 | let modified = Local.timestamp(Local::now().timestamp(), 0); |
181f097a DM |
1617 | |
1618 | store_key_config(&path, true, KeyConfig { | |
1619 | kdf: None, | |
ab44acff DM |
1620 | created, // keep original value |
1621 | modified, | |
6d0983db | 1622 | data: key.to_vec(), |
181f097a DM |
1623 | })?; |
1624 | ||
1625 | Ok(Value::Null) | |
1626 | } else { | |
1627 | unreachable!(); | |
1628 | } | |
f2401311 DM |
1629 | } |
1630 | ||
1631 | fn key_mgmt_cli() -> CliCommandMap { | |
1632 | ||
181f097a DM |
1633 | let kdf_schema: Arc<Schema> = Arc::new( |
1634 | StringSchema::new("Key derivation function. Choose 'none' to store the key unecrypted.") | |
1635 | .format(Arc::new(ApiStringFormat::Enum(&["scrypt", "none"]))) | |
1636 | .default("scrypt") | |
1637 | .into() | |
1638 | ); | |
1639 | ||
f2401311 DM |
1640 | let key_create_cmd_def = CliCommand::new( |
1641 | ApiMethod::new( | |
1642 | key_create, | |
1643 | ObjectSchema::new("Create a new encryption key.") | |
9b06db45 | 1644 | .required("path", StringSchema::new("File system path.")) |
181f097a | 1645 | .optional("kdf", kdf_schema.clone()) |
f2401311 | 1646 | )) |
9b06db45 DM |
1647 | .arg_param(vec!["path"]) |
1648 | .completion_cb("path", tools::complete_file_name); | |
f2401311 | 1649 | |
ac716234 DM |
1650 | let key_change_passphrase_cmd_def = CliCommand::new( |
1651 | ApiMethod::new( | |
1652 | key_change_passphrase, | |
1653 | ObjectSchema::new("Change the passphrase required to decrypt the key.") | |
9b06db45 | 1654 | .required("path", StringSchema::new("File system path.")) |
181f097a | 1655 | .optional("kdf", kdf_schema.clone()) |
9b06db45 DM |
1656 | )) |
1657 | .arg_param(vec!["path"]) | |
1658 | .completion_cb("path", tools::complete_file_name); | |
ac716234 | 1659 | |
37c5a175 DM |
1660 | let key_create_master_key_cmd_def = CliCommand::new( |
1661 | ApiMethod::new( | |
1662 | key_create_master_key, | |
1663 | ObjectSchema::new("Create a new 4096 bit RSA master pub/priv key pair.") | |
1664 | )); | |
1665 | ||
3ea8bfc9 DM |
1666 | let key_import_master_pubkey_cmd_def = CliCommand::new( |
1667 | ApiMethod::new( | |
1668 | key_import_master_pubkey, | |
1669 | ObjectSchema::new("Import a new RSA public key and use it as master key. The key is expected to be in '.pem' format.") | |
1670 | .required("path", StringSchema::new("File system path.")) | |
1671 | )) | |
1672 | .arg_param(vec!["path"]) | |
1673 | .completion_cb("path", tools::complete_file_name); | |
1674 | ||
f2401311 | 1675 | let cmd_def = CliCommandMap::new() |
ac716234 | 1676 | .insert("create".to_owned(), key_create_cmd_def.into()) |
37c5a175 | 1677 | .insert("create-master-key".to_owned(), key_create_master_key_cmd_def.into()) |
3ea8bfc9 | 1678 | .insert("import-master-pubkey".to_owned(), key_import_master_pubkey_cmd_def.into()) |
ac716234 | 1679 | .insert("change-passphrase".to_owned(), key_change_passphrase_cmd_def.into()); |
f2401311 DM |
1680 | |
1681 | cmd_def | |
1682 | } | |
1683 | ||
70235f72 CE |
1684 | |
1685 | fn mount( | |
1686 | param: Value, | |
1687 | _info: &ApiMethod, | |
1688 | _rpcenv: &mut dyn RpcEnvironment, | |
1689 | ) -> Result<Value, Error> { | |
1690 | let verbose = param["verbose"].as_bool().unwrap_or(false); | |
1691 | if verbose { | |
1692 | // This will stay in foreground with debug output enabled as None is | |
1693 | // passed for the RawFd. | |
1694 | return async_main(mount_do(param, None)); | |
1695 | } | |
1696 | ||
1697 | // Process should be deamonized. | |
1698 | // Make sure to fork before the async runtime is instantiated to avoid troubles. | |
1699 | let pipe = pipe()?; | |
1700 | match fork() { | |
1701 | Ok(ForkResult::Parent { child: _, .. }) => { | |
1702 | nix::unistd::close(pipe.1).unwrap(); | |
1703 | // Blocks the parent process until we are ready to go in the child | |
1704 | let _res = nix::unistd::read(pipe.0, &mut [0]).unwrap(); | |
1705 | Ok(Value::Null) | |
1706 | } | |
1707 | Ok(ForkResult::Child) => { | |
1708 | nix::unistd::close(pipe.0).unwrap(); | |
1709 | nix::unistd::setsid().unwrap(); | |
1710 | async_main(mount_do(param, Some(pipe.1))) | |
1711 | } | |
1712 | Err(_) => bail!("failed to daemonize process"), | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> { | |
1717 | let repo = extract_repository_from_value(¶m)?; | |
1718 | let archive_name = tools::required_string_param(¶m, "archive-name")?; | |
1719 | let target = tools::required_string_param(¶m, "target")?; | |
1720 | let client = HttpClient::new(repo.host(), repo.user(), None)?; | |
1721 | ||
1722 | record_repository(&repo); | |
1723 | ||
1724 | let path = tools::required_string_param(¶m, "snapshot")?; | |
1725 | let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { | |
1726 | let group = BackupGroup::parse(path)?; | |
1727 | ||
1728 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); | |
1729 | let result = client.get(&path, Some(json!({ | |
1730 | "backup-type": group.backup_type(), | |
1731 | "backup-id": group.backup_id(), | |
1732 | }))).await?; | |
1733 | ||
1734 | let list = result["data"].as_array().unwrap(); | |
1735 | if list.len() == 0 { | |
1736 | bail!("backup group '{}' does not contain any snapshots:", path); | |
1737 | } | |
1738 | ||
1739 | let epoch = list[0]["backup-time"].as_i64().unwrap(); | |
1740 | let backup_time = Utc.timestamp(epoch, 0); | |
1741 | (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time) | |
1742 | } else { | |
1743 | let snapshot = BackupDir::parse(path)?; | |
1744 | (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) | |
1745 | }; | |
1746 | ||
1747 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); | |
1748 | let crypt_config = match keyfile { | |
1749 | None => None, | |
1750 | Some(path) => { | |
1751 | let (key, _) = load_and_decrtypt_key(&path, get_encryption_key_password)?; | |
1752 | Some(Arc::new(CryptConfig::new(key)?)) | |
1753 | } | |
1754 | }; | |
1755 | ||
1756 | let server_archive_name = if archive_name.ends_with(".pxar") { | |
1757 | format!("{}.didx", archive_name) | |
1758 | } else { | |
1759 | bail!("Can only mount pxar archives."); | |
1760 | }; | |
1761 | ||
9e490a74 | 1762 | let client = BackupReader::start(client, repo.store(), &backup_type, &backup_id, backup_time, true).await?; |
70235f72 CE |
1763 | |
1764 | let tmpfile = std::fs::OpenOptions::new() | |
1765 | .write(true) | |
1766 | .read(true) | |
1767 | .custom_flags(libc::O_TMPFILE) | |
1768 | .open("/tmp")?; | |
1769 | ||
1770 | let backup_index_data = download_index_blob(client.clone(), crypt_config.clone()).await?; | |
1771 | let backup_index: Value = serde_json::from_slice(&backup_index_data[..])?; | |
1772 | if server_archive_name.ends_with(".didx") { | |
1773 | let tmpfile = client.download(&server_archive_name, tmpfile).await?; | |
1774 | let index = DynamicIndexReader::new(tmpfile) | |
1775 | .map_err(|err| format_err!("unable to read dynamic index '{}' - {}", archive_name, err))?; | |
1776 | ||
1777 | // Note: do not use values stored in index (not trusted) - instead, computed them again | |
1778 | let (csum, size) = index.compute_csum(); | |
1779 | verify_index_file(&backup_index, &server_archive_name, &csum, size)?; | |
1780 | ||
1781 | let most_used = index.find_most_used_chunks(8); | |
1782 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
1783 | let reader = BufferedDynamicReader::new(index, chunk_reader); | |
1784 | let decoder = | |
1785 | pxar::Decoder::<Box<dyn pxar::fuse::ReadSeek>, fn(&Path) -> Result<(), Error>>::new( | |
1786 | Box::new(reader), | |
1787 | |_| Ok(()), | |
1788 | )?; | |
1789 | let options = OsStr::new("ro,default_permissions"); | |
1790 | let mut session = pxar::fuse::Session::from_decoder(decoder, &options, pipe.is_none()) | |
1791 | .map_err(|err| format_err!("pxar mount failed: {}", err))?; | |
1792 | ||
1793 | // Mount the session but not call fuse deamonize as this will cause | |
1794 | // issues with the runtime after the fork | |
1795 | let deamonize = false; | |
1796 | session.mount(&Path::new(target), deamonize)?; | |
1797 | ||
1798 | if let Some(pipe) = pipe { | |
1799 | nix::unistd::chdir(Path::new("/")).unwrap(); | |
1800 | // Finish creation of deamon by redirecting filedescriptors. | |
1801 | let nullfd = nix::fcntl::open( | |
1802 | "/dev/null", | |
1803 | nix::fcntl::OFlag::O_RDWR, | |
1804 | nix::sys::stat::Mode::empty(), | |
1805 | ).unwrap(); | |
1806 | nix::unistd::dup2(nullfd, 0).unwrap(); | |
1807 | nix::unistd::dup2(nullfd, 1).unwrap(); | |
1808 | nix::unistd::dup2(nullfd, 2).unwrap(); | |
1809 | if nullfd > 2 { | |
1810 | nix::unistd::close(nullfd).unwrap(); | |
1811 | } | |
1812 | // Signal the parent process that we are done with the setup and it can | |
1813 | // terminate. | |
1814 | nix::unistd::write(pipe, &mut [0u8])?; | |
1815 | nix::unistd::close(pipe).unwrap(); | |
1816 | } | |
1817 | ||
1818 | let multithreaded = true; | |
1819 | session.run_loop(multithreaded)?; | |
1820 | } else { | |
1821 | bail!("unknown archive file extension (expected .pxar)"); | |
1822 | } | |
1823 | ||
1824 | Ok(Value::Null) | |
1825 | } | |
1826 | ||
f2401311 | 1827 | fn main() { |
33d64b81 | 1828 | |
25f1650b DM |
1829 | let backup_source_schema: Arc<Schema> = Arc::new( |
1830 | StringSchema::new("Backup source specification ([<label>:<path>]).") | |
1831 | .format(Arc::new(ApiStringFormat::Pattern(&BACKUPSPEC_REGEX))) | |
1832 | .into() | |
1833 | ); | |
1834 | ||
597a9203 | 1835 | let backup_cmd_def = CliCommand::new( |
ff5d3707 | 1836 | ApiMethod::new( |
bcd879cf | 1837 | create_backup, |
597a9203 | 1838 | ObjectSchema::new("Create (host) backup.") |
ae0be2dd DM |
1839 | .required( |
1840 | "backupspec", | |
1841 | ArraySchema::new( | |
74cdb521 | 1842 | "List of backup source specifications ([<label.ext>:<path>] ...)", |
25f1650b | 1843 | backup_source_schema, |
ae0be2dd DM |
1844 | ).min_length(1) |
1845 | ) | |
2665cef7 | 1846 | .optional("repository", REPO_URL_SCHEMA.clone()) |
2eeaacb9 DM |
1847 | .optional( |
1848 | "include-dev", | |
1849 | ArraySchema::new( | |
1850 | "Include mountpoints with same st_dev number (see ``man fstat``) as specified files.", | |
1851 | StringSchema::new("Path to file.").into() | |
1852 | ) | |
1853 | ) | |
6d0983db DM |
1854 | .optional( |
1855 | "keyfile", | |
1856 | StringSchema::new("Path to encryption key. All data will be encrypted using this key.")) | |
219ef0e6 DM |
1857 | .optional( |
1858 | "verbose", | |
1859 | BooleanSchema::new("Verbose output.").default(false)) | |
5b72c9b4 DM |
1860 | .optional( |
1861 | "skip-lost-and-found", | |
1862 | BooleanSchema::new("Skip lost+found directory").default(false)) | |
fba30411 | 1863 | .optional( |
bbf9e7e9 DM |
1864 | "backup-type", |
1865 | BACKUP_TYPE_SCHEMA.clone() | |
1866 | ) | |
1867 | .optional( | |
1868 | "backup-id", | |
1869 | BACKUP_ID_SCHEMA.clone() | |
1870 | ) | |
ca5d0b61 DM |
1871 | .optional( |
1872 | "backup-time", | |
bbf9e7e9 | 1873 | BACKUP_TIME_SCHEMA.clone() |
ca5d0b61 | 1874 | ) |
2d9d143a DM |
1875 | .optional( |
1876 | "chunk-size", | |
1877 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
1878 | .minimum(64) | |
1879 | .maximum(4096) | |
1880 | .default(4096) | |
1881 | ) | |
ff5d3707 | 1882 | )) |
2665cef7 | 1883 | .arg_param(vec!["backupspec"]) |
d0a03d40 | 1884 | .completion_cb("repository", complete_repository) |
49811347 | 1885 | .completion_cb("backupspec", complete_backup_source) |
6d0983db | 1886 | .completion_cb("keyfile", tools::complete_file_name) |
49811347 | 1887 | .completion_cb("chunk-size", complete_chunk_size); |
f8838fe9 | 1888 | |
ec34f7eb DM |
1889 | let upload_log_cmd_def = CliCommand::new( |
1890 | ApiMethod::new( | |
1891 | upload_log, | |
1892 | ObjectSchema::new("Upload backup log file.") | |
1893 | .required("snapshot", StringSchema::new("Snapshot path.")) | |
1894 | .required("logfile", StringSchema::new("The path to the log file you want to upload.")) | |
1895 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
1896 | .optional( | |
1897 | "keyfile", | |
1898 | StringSchema::new("Path to encryption key. All data will be encrypted using this key.")) | |
1899 | )) | |
1900 | .arg_param(vec!["snapshot", "logfile"]) | |
543a260f | 1901 | .completion_cb("snapshot", complete_backup_snapshot) |
ec34f7eb DM |
1902 | .completion_cb("logfile", tools::complete_file_name) |
1903 | .completion_cb("keyfile", tools::complete_file_name) | |
1904 | .completion_cb("repository", complete_repository); | |
1905 | ||
41c039e1 DM |
1906 | let list_cmd_def = CliCommand::new( |
1907 | ApiMethod::new( | |
812c6f87 DM |
1908 | list_backup_groups, |
1909 | ObjectSchema::new("List backup groups.") | |
2665cef7 | 1910 | .optional("repository", REPO_URL_SCHEMA.clone()) |
34a816cc | 1911 | .optional("output-format", OUTPUT_FORMAT.clone()) |
41c039e1 | 1912 | )) |
d0a03d40 | 1913 | .completion_cb("repository", complete_repository); |
41c039e1 | 1914 | |
184f17af DM |
1915 | let snapshots_cmd_def = CliCommand::new( |
1916 | ApiMethod::new( | |
1917 | list_snapshots, | |
1918 | ObjectSchema::new("List backup snapshots.") | |
15c847f1 | 1919 | .optional("group", StringSchema::new("Backup group.")) |
2665cef7 | 1920 | .optional("repository", REPO_URL_SCHEMA.clone()) |
34a816cc | 1921 | .optional("output-format", OUTPUT_FORMAT.clone()) |
184f17af | 1922 | )) |
2665cef7 | 1923 | .arg_param(vec!["group"]) |
024f11bb | 1924 | .completion_cb("group", complete_backup_group) |
d0a03d40 | 1925 | .completion_cb("repository", complete_repository); |
184f17af | 1926 | |
6f62c924 DM |
1927 | let forget_cmd_def = CliCommand::new( |
1928 | ApiMethod::new( | |
1929 | forget_snapshots, | |
1930 | ObjectSchema::new("Forget (remove) backup snapshots.") | |
6f62c924 | 1931 | .required("snapshot", StringSchema::new("Snapshot path.")) |
2665cef7 | 1932 | .optional("repository", REPO_URL_SCHEMA.clone()) |
6f62c924 | 1933 | )) |
2665cef7 | 1934 | .arg_param(vec!["snapshot"]) |
b2388518 | 1935 | .completion_cb("repository", complete_repository) |
543a260f | 1936 | .completion_cb("snapshot", complete_backup_snapshot); |
6f62c924 | 1937 | |
8cc0d6af DM |
1938 | let garbage_collect_cmd_def = CliCommand::new( |
1939 | ApiMethod::new( | |
1940 | start_garbage_collection, | |
1941 | ObjectSchema::new("Start garbage collection for a specific repository.") | |
2665cef7 | 1942 | .optional("repository", REPO_URL_SCHEMA.clone()) |
8cc0d6af | 1943 | )) |
d0a03d40 | 1944 | .completion_cb("repository", complete_repository); |
8cc0d6af | 1945 | |
9f912493 DM |
1946 | let restore_cmd_def = CliCommand::new( |
1947 | ApiMethod::new( | |
1948 | restore, | |
1949 | ObjectSchema::new("Restore backup repository.") | |
d5c34d98 DM |
1950 | .required("snapshot", StringSchema::new("Group/Snapshot path.")) |
1951 | .required("archive-name", StringSchema::new("Backup archive name.")) | |
bf125261 DM |
1952 | .required("target", StringSchema::new(r###"Target directory path. Use '-' to write to stdandard output. |
1953 | ||
1954 | We do not extraxt '.pxar' archives when writing to stdandard output. | |
1955 | ||
1956 | "### | |
1957 | )) | |
46d5aa0a DM |
1958 | .optional( |
1959 | "allow-existing-dirs", | |
1960 | BooleanSchema::new("Do not fail if directories already exists.").default(false)) | |
2665cef7 | 1961 | .optional("repository", REPO_URL_SCHEMA.clone()) |
86eda3eb DM |
1962 | .optional("keyfile", StringSchema::new("Path to encryption key.")) |
1963 | .optional( | |
1964 | "verbose", | |
1965 | BooleanSchema::new("Verbose output.").default(false) | |
1966 | ) | |
9f912493 | 1967 | )) |
2665cef7 | 1968 | .arg_param(vec!["snapshot", "archive-name", "target"]) |
b2388518 | 1969 | .completion_cb("repository", complete_repository) |
08dc340a DM |
1970 | .completion_cb("snapshot", complete_group_or_snapshot) |
1971 | .completion_cb("archive-name", complete_archive_name) | |
1972 | .completion_cb("target", tools::complete_file_name); | |
9f912493 | 1973 | |
52c171e4 DM |
1974 | let files_cmd_def = CliCommand::new( |
1975 | ApiMethod::new( | |
1976 | list_snapshot_files, | |
1977 | ObjectSchema::new("List snapshot files.") | |
1978 | .required("snapshot", StringSchema::new("Snapshot path.")) | |
cec17a3e | 1979 | .optional("repository", REPO_URL_SCHEMA.clone()) |
52c171e4 DM |
1980 | .optional("output-format", OUTPUT_FORMAT.clone()) |
1981 | )) | |
1982 | .arg_param(vec!["snapshot"]) | |
1983 | .completion_cb("repository", complete_repository) | |
543a260f | 1984 | .completion_cb("snapshot", complete_backup_snapshot); |
52c171e4 | 1985 | |
9049a8cf DM |
1986 | let catalog_cmd_def = CliCommand::new( |
1987 | ApiMethod::new( | |
1988 | dump_catalog, | |
1989 | ObjectSchema::new("Dump catalog.") | |
1990 | .required("snapshot", StringSchema::new("Snapshot path.")) | |
1991 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
1992 | )) | |
1993 | .arg_param(vec!["snapshot"]) | |
1994 | .completion_cb("repository", complete_repository) | |
1995 | .completion_cb("snapshot", complete_backup_snapshot); | |
1996 | ||
83b7db02 DM |
1997 | let prune_cmd_def = CliCommand::new( |
1998 | ApiMethod::new( | |
1999 | prune, | |
2000 | proxmox_backup::api2::admin::datastore::add_common_prune_prameters( | |
2001 | ObjectSchema::new("Prune backup repository.") | |
9fdc3ef4 | 2002 | .required("group", StringSchema::new("Backup group.")) |
2665cef7 | 2003 | .optional("repository", REPO_URL_SCHEMA.clone()) |
83b7db02 DM |
2004 | ) |
2005 | )) | |
9fdc3ef4 DM |
2006 | .arg_param(vec!["group"]) |
2007 | .completion_cb("group", complete_backup_group) | |
d0a03d40 | 2008 | .completion_cb("repository", complete_repository); |
9f912493 | 2009 | |
34a816cc DM |
2010 | let status_cmd_def = CliCommand::new( |
2011 | ApiMethod::new( | |
2012 | status, | |
2013 | ObjectSchema::new("Get repository status.") | |
2014 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
2015 | .optional("output-format", OUTPUT_FORMAT.clone()) | |
2016 | )) | |
2017 | .completion_cb("repository", complete_repository); | |
2018 | ||
e240d8be DM |
2019 | let login_cmd_def = CliCommand::new( |
2020 | ApiMethod::new( | |
2021 | api_login, | |
2022 | ObjectSchema::new("Try to login. If successful, store ticket.") | |
2023 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
2024 | )) | |
2025 | .completion_cb("repository", complete_repository); | |
2026 | ||
2027 | let logout_cmd_def = CliCommand::new( | |
2028 | ApiMethod::new( | |
2029 | api_logout, | |
2030 | ObjectSchema::new("Logout (delete stored ticket).") | |
2031 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
2032 | )) | |
2033 | .completion_cb("repository", complete_repository); | |
32efac1c | 2034 | |
70235f72 CE |
2035 | let mount_cmd_def = CliCommand::new( |
2036 | ApiMethod::new( | |
2037 | mount, | |
2038 | ObjectSchema::new("Mount pxar archive.") | |
2039 | .required("snapshot", StringSchema::new("Group/Snapshot path.")) | |
2040 | .required("archive-name", StringSchema::new("Backup archive name.")) | |
2041 | .required("target", StringSchema::new("Target directory path.")) | |
2042 | .optional("repository", REPO_URL_SCHEMA.clone()) | |
2043 | .optional("keyfile", StringSchema::new("Path to encryption key.")) | |
2044 | .optional("verbose", BooleanSchema::new("Verbose output.").default(false)) | |
2045 | )) | |
2046 | .arg_param(vec!["snapshot", "archive-name", "target"]) | |
2047 | .completion_cb("repository", complete_repository) | |
2048 | .completion_cb("snapshot", complete_group_or_snapshot) | |
2049 | .completion_cb("archive-name", complete_archive_name) | |
2050 | .completion_cb("target", tools::complete_file_name); | |
e240d8be | 2051 | |
41c039e1 | 2052 | let cmd_def = CliCommandMap::new() |
597a9203 | 2053 | .insert("backup".to_owned(), backup_cmd_def.into()) |
ec34f7eb | 2054 | .insert("upload-log".to_owned(), upload_log_cmd_def.into()) |
6f62c924 | 2055 | .insert("forget".to_owned(), forget_cmd_def.into()) |
9049a8cf | 2056 | .insert("catalog".to_owned(), catalog_cmd_def.into()) |
8cc0d6af | 2057 | .insert("garbage-collect".to_owned(), garbage_collect_cmd_def.into()) |
83b7db02 | 2058 | .insert("list".to_owned(), list_cmd_def.into()) |
e240d8be DM |
2059 | .insert("login".to_owned(), login_cmd_def.into()) |
2060 | .insert("logout".to_owned(), logout_cmd_def.into()) | |
184f17af | 2061 | .insert("prune".to_owned(), prune_cmd_def.into()) |
9f912493 | 2062 | .insert("restore".to_owned(), restore_cmd_def.into()) |
f2401311 | 2063 | .insert("snapshots".to_owned(), snapshots_cmd_def.into()) |
52c171e4 | 2064 | .insert("files".to_owned(), files_cmd_def.into()) |
34a816cc | 2065 | .insert("status".to_owned(), status_cmd_def.into()) |
70235f72 CE |
2066 | .insert("key".to_owned(), key_mgmt_cli().into()) |
2067 | .insert("mount".to_owned(), mount_cmd_def.into()); | |
a914a774 | 2068 | |
e9722f8b WB |
2069 | run_cli_command(cmd_def.into()); |
2070 | } | |
496a6784 | 2071 | |
e9722f8b WB |
2072 | fn async_main<F: Future>(fut: F) -> <F as Future>::Output { |
2073 | let rt = tokio::runtime::Runtime::new().unwrap(); | |
2074 | let ret = rt.block_on(fut); | |
2075 | rt.shutdown_now(); | |
2076 | ret | |
ff5d3707 | 2077 | } |