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