]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox-backup-client.rs
16c6ec66062998806637cf7a2367789a227e8342
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
1 use std::collections::{HashSet, HashMap};
2 use std::convert::TryFrom;
3 use std::io::{self, Read, Write, Seek, SeekFrom};
4 use std::os::unix::io::{FromRawFd, RawFd};
5 use std::path::{Path, PathBuf};
6 use std::pin::Pin;
7 use std::sync::{Arc, Mutex};
8 use std::task::Context;
9
10 use anyhow::{bail, format_err, Error};
11 use chrono::{Local, DateTime, Utc, TimeZone};
12 use futures::future::FutureExt;
13 use futures::stream::{StreamExt, TryStreamExt};
14 use serde_json::{json, Value};
15 use tokio::sync::mpsc;
16 use xdg::BaseDirectories;
17
18 use pathpatterns::{MatchEntry, MatchType, PatternFlag};
19 use proxmox::tools::fs::{file_get_contents, file_get_json, replace_file, CreateOptions, image_size};
20 use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment};
21 use proxmox::api::schema::*;
22 use proxmox::api::cli::*;
23 use proxmox::api::api;
24 use pxar::accessor::{MaybeReady, ReadAt, ReadAtOperation};
25
26 use proxmox_backup::tools;
27 use proxmox_backup::api2::types::*;
28 use proxmox_backup::api2::version;
29 use proxmox_backup::client::*;
30 use proxmox_backup::pxar::catalog::*;
31 use proxmox_backup::backup::{
32 archive_type,
33 decrypt_key,
34 verify_chunk_size,
35 ArchiveType,
36 AsyncReadChunk,
37 BackupDir,
38 BackupGroup,
39 BackupManifest,
40 BufferedDynamicReader,
41 CATALOG_NAME,
42 CatalogReader,
43 CatalogWriter,
44 ChunkStream,
45 CryptConfig,
46 CryptMode,
47 DataBlob,
48 DynamicIndexReader,
49 FixedChunkStream,
50 FixedIndexReader,
51 IndexFile,
52 MANIFEST_BLOB_NAME,
53 Shell,
54 };
55
56 mod proxmox_backup_client;
57 use proxmox_backup_client::*;
58
59 const ENV_VAR_PBS_FINGERPRINT: &str = "PBS_FINGERPRINT";
60 const ENV_VAR_PBS_PASSWORD: &str = "PBS_PASSWORD";
61
62
63 pub const REPO_URL_SCHEMA: Schema = StringSchema::new("Repository URL.")
64 .format(&BACKUP_REPO_URL)
65 .max_length(256)
66 .schema();
67
68 pub const KEYFILE_SCHEMA: Schema = StringSchema::new(
69 "Path to encryption key. All data will be encrypted using this key.")
70 .schema();
71
72 pub const KEYFD_SCHEMA: Schema = IntegerSchema::new(
73 "Pass an encryption key via an already opened file descriptor.")
74 .minimum(0)
75 .schema();
76
77 const CHUNK_SIZE_SCHEMA: Schema = IntegerSchema::new(
78 "Chunk size in KB. Must be a power of 2.")
79 .minimum(64)
80 .maximum(4096)
81 .default(4096)
82 .schema();
83
84 fn get_default_repository() -> Option<String> {
85 std::env::var("PBS_REPOSITORY").ok()
86 }
87
88 pub fn extract_repository_from_value(
89 param: &Value,
90 ) -> Result<BackupRepository, Error> {
91
92 let repo_url = param["repository"]
93 .as_str()
94 .map(String::from)
95 .or_else(get_default_repository)
96 .ok_or_else(|| format_err!("unable to get (default) repository"))?;
97
98 let repo: BackupRepository = repo_url.parse()?;
99
100 Ok(repo)
101 }
102
103 fn extract_repository_from_map(
104 param: &HashMap<String, String>,
105 ) -> Option<BackupRepository> {
106
107 param.get("repository")
108 .map(String::from)
109 .or_else(get_default_repository)
110 .and_then(|repo_url| repo_url.parse::<BackupRepository>().ok())
111 }
112
113 fn record_repository(repo: &BackupRepository) {
114
115 let base = match BaseDirectories::with_prefix("proxmox-backup") {
116 Ok(v) => v,
117 _ => return,
118 };
119
120 // usually $HOME/.cache/proxmox-backup/repo-list
121 let path = match base.place_cache_file("repo-list") {
122 Ok(v) => v,
123 _ => return,
124 };
125
126 let mut data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
127
128 let repo = repo.to_string();
129
130 data[&repo] = json!{ data[&repo].as_i64().unwrap_or(0) + 1 };
131
132 let mut map = serde_json::map::Map::new();
133
134 loop {
135 let mut max_used = 0;
136 let mut max_repo = None;
137 for (repo, count) in data.as_object().unwrap() {
138 if map.contains_key(repo) { continue; }
139 if let Some(count) = count.as_i64() {
140 if count > max_used {
141 max_used = count;
142 max_repo = Some(repo);
143 }
144 }
145 }
146 if let Some(repo) = max_repo {
147 map.insert(repo.to_owned(), json!(max_used));
148 } else {
149 break;
150 }
151 if map.len() > 10 { // store max. 10 repos
152 break;
153 }
154 }
155
156 let new_data = json!(map);
157
158 let _ = replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new());
159 }
160
161 pub fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
162
163 let mut result = vec![];
164
165 let base = match BaseDirectories::with_prefix("proxmox-backup") {
166 Ok(v) => v,
167 _ => return result,
168 };
169
170 // usually $HOME/.cache/proxmox-backup/repo-list
171 let path = match base.place_cache_file("repo-list") {
172 Ok(v) => v,
173 _ => return result,
174 };
175
176 let data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
177
178 if let Some(map) = data.as_object() {
179 for (repo, _count) in map {
180 result.push(repo.to_owned());
181 }
182 }
183
184 result
185 }
186
187 fn connect(server: &str, userid: &str) -> Result<HttpClient, Error> {
188
189 let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
190
191 use std::env::VarError::*;
192 let password = match std::env::var(ENV_VAR_PBS_PASSWORD) {
193 Ok(p) => Some(p),
194 Err(NotUnicode(_)) => bail!(format!("{} contains bad characters", ENV_VAR_PBS_PASSWORD)),
195 Err(NotPresent) => None,
196 };
197
198 let options = HttpClientOptions::new()
199 .prefix(Some("proxmox-backup".to_string()))
200 .password(password)
201 .interactive(true)
202 .fingerprint(fingerprint)
203 .fingerprint_cache(true)
204 .ticket_cache(true);
205
206 HttpClient::new(server, userid, options)
207 }
208
209 async fn view_task_result(
210 client: HttpClient,
211 result: Value,
212 output_format: &str,
213 ) -> Result<(), Error> {
214 let data = &result["data"];
215 if output_format == "text" {
216 if let Some(upid) = data.as_str() {
217 display_task_log(client, upid, true).await?;
218 }
219 } else {
220 format_and_print_result(&data, &output_format);
221 }
222
223 Ok(())
224 }
225
226 async fn api_datastore_list_snapshots(
227 client: &HttpClient,
228 store: &str,
229 group: Option<BackupGroup>,
230 ) -> Result<Value, Error> {
231
232 let path = format!("api2/json/admin/datastore/{}/snapshots", store);
233
234 let mut args = json!({});
235 if let Some(group) = group {
236 args["backup-type"] = group.backup_type().into();
237 args["backup-id"] = group.backup_id().into();
238 }
239
240 let mut result = client.get(&path, Some(args)).await?;
241
242 Ok(result["data"].take())
243 }
244
245 pub async fn api_datastore_latest_snapshot(
246 client: &HttpClient,
247 store: &str,
248 group: BackupGroup,
249 ) -> Result<(String, String, DateTime<Utc>), Error> {
250
251 let list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?;
252 let mut list: Vec<SnapshotListItem> = serde_json::from_value(list)?;
253
254 if list.is_empty() {
255 bail!("backup group {:?} does not contain any snapshots.", group.group_path());
256 }
257
258 list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time));
259
260 let backup_time = Utc.timestamp(list[0].backup_time, 0);
261
262 Ok((group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time))
263 }
264
265 async fn backup_directory<P: AsRef<Path>>(
266 client: &BackupWriter,
267 previous_manifest: Option<Arc<BackupManifest>>,
268 dir_path: P,
269 archive_name: &str,
270 chunk_size: Option<usize>,
271 device_set: Option<HashSet<u64>>,
272 verbose: bool,
273 skip_lost_and_found: bool,
274 catalog: Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
275 exclude_pattern: Vec<MatchEntry>,
276 entries_max: usize,
277 compress: bool,
278 encrypt: bool,
279 ) -> Result<BackupStats, Error> {
280
281 let pxar_stream = PxarBackupStream::open(
282 dir_path.as_ref(),
283 device_set,
284 verbose,
285 skip_lost_and_found,
286 catalog,
287 exclude_pattern,
288 entries_max,
289 )?;
290 let mut chunk_stream = ChunkStream::new(pxar_stream, chunk_size);
291
292 let (mut tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks
293
294 let stream = rx
295 .map_err(Error::from);
296
297 // spawn chunker inside a separate task so that it can run parallel
298 tokio::spawn(async move {
299 while let Some(v) = chunk_stream.next().await {
300 let _ = tx.send(v).await;
301 }
302 });
303
304 let stats = client
305 .upload_stream(previous_manifest, archive_name, stream, "dynamic", None, compress, encrypt)
306 .await?;
307
308 Ok(stats)
309 }
310
311 async fn backup_image<P: AsRef<Path>>(
312 client: &BackupWriter,
313 previous_manifest: Option<Arc<BackupManifest>>,
314 image_path: P,
315 archive_name: &str,
316 image_size: u64,
317 chunk_size: Option<usize>,
318 compress: bool,
319 encrypt: bool,
320 _verbose: bool,
321 ) -> Result<BackupStats, Error> {
322
323 let path = image_path.as_ref().to_owned();
324
325 let file = tokio::fs::File::open(path).await?;
326
327 let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
328 .map_err(Error::from);
329
330 let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024));
331
332 let stats = client
333 .upload_stream(previous_manifest, archive_name, stream, "fixed", Some(image_size), compress, encrypt)
334 .await?;
335
336 Ok(stats)
337 }
338
339 #[api(
340 input: {
341 properties: {
342 repository: {
343 schema: REPO_URL_SCHEMA,
344 optional: true,
345 },
346 "output-format": {
347 schema: OUTPUT_FORMAT,
348 optional: true,
349 },
350 }
351 }
352 )]
353 /// List backup groups.
354 async fn list_backup_groups(param: Value) -> Result<Value, Error> {
355
356 let output_format = get_output_format(&param);
357
358 let repo = extract_repository_from_value(&param)?;
359
360 let client = connect(repo.host(), repo.user())?;
361
362 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
363
364 let mut result = client.get(&path, None).await?;
365
366 record_repository(&repo);
367
368 let render_group_path = |_v: &Value, record: &Value| -> Result<String, Error> {
369 let item: GroupListItem = serde_json::from_value(record.to_owned())?;
370 let group = BackupGroup::new(item.backup_type, item.backup_id);
371 Ok(group.group_path().to_str().unwrap().to_owned())
372 };
373
374 let render_last_backup = |_v: &Value, record: &Value| -> Result<String, Error> {
375 let item: GroupListItem = serde_json::from_value(record.to_owned())?;
376 let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.last_backup);
377 Ok(snapshot.relative_path().to_str().unwrap().to_owned())
378 };
379
380 let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
381 let item: GroupListItem = serde_json::from_value(record.to_owned())?;
382 Ok(tools::format::render_backup_file_list(&item.files))
383 };
384
385 let options = default_table_format_options()
386 .sortby("backup-type", false)
387 .sortby("backup-id", false)
388 .column(ColumnConfig::new("backup-id").renderer(render_group_path).header("group"))
389 .column(
390 ColumnConfig::new("last-backup")
391 .renderer(render_last_backup)
392 .header("last snapshot")
393 .right_align(false)
394 )
395 .column(ColumnConfig::new("backup-count"))
396 .column(ColumnConfig::new("files").renderer(render_files));
397
398 let mut data: Value = result["data"].take();
399
400 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_GROUPS;
401
402 format_and_print_result_full(&mut data, info, &output_format, &options);
403
404 Ok(Value::Null)
405 }
406
407 #[api(
408 input: {
409 properties: {
410 repository: {
411 schema: REPO_URL_SCHEMA,
412 optional: true,
413 },
414 group: {
415 type: String,
416 description: "Backup group.",
417 optional: true,
418 },
419 "output-format": {
420 schema: OUTPUT_FORMAT,
421 optional: true,
422 },
423 }
424 }
425 )]
426 /// List backup snapshots.
427 async fn list_snapshots(param: Value) -> Result<Value, Error> {
428
429 let repo = extract_repository_from_value(&param)?;
430
431 let output_format = get_output_format(&param);
432
433 let client = connect(repo.host(), repo.user())?;
434
435 let group: Option<BackupGroup> = if let Some(path) = param["group"].as_str() {
436 Some(path.parse()?)
437 } else {
438 None
439 };
440
441 let mut data = api_datastore_list_snapshots(&client, repo.store(), group).await?;
442
443 record_repository(&repo);
444
445 let render_snapshot_path = |_v: &Value, record: &Value| -> Result<String, Error> {
446 let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
447 let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
448 Ok(snapshot.relative_path().to_str().unwrap().to_owned())
449 };
450
451 let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
452 let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
453 let mut filenames = Vec::new();
454 for file in &item.files {
455 filenames.push(file.filename.to_string());
456 }
457 Ok(tools::format::render_backup_file_list(&filenames[..]))
458 };
459
460 let options = default_table_format_options()
461 .sortby("backup-type", false)
462 .sortby("backup-id", false)
463 .sortby("backup-time", false)
464 .column(ColumnConfig::new("backup-id").renderer(render_snapshot_path).header("snapshot"))
465 .column(ColumnConfig::new("size"))
466 .column(ColumnConfig::new("files").renderer(render_files))
467 ;
468
469 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_SNAPSHOTS;
470
471 format_and_print_result_full(&mut data, info, &output_format, &options);
472
473 Ok(Value::Null)
474 }
475
476 #[api(
477 input: {
478 properties: {
479 repository: {
480 schema: REPO_URL_SCHEMA,
481 optional: true,
482 },
483 snapshot: {
484 type: String,
485 description: "Snapshot path.",
486 },
487 }
488 }
489 )]
490 /// Forget (remove) backup snapshots.
491 async fn forget_snapshots(param: Value) -> Result<Value, Error> {
492
493 let repo = extract_repository_from_value(&param)?;
494
495 let path = tools::required_string_param(&param, "snapshot")?;
496 let snapshot: BackupDir = path.parse()?;
497
498 let mut client = connect(repo.host(), repo.user())?;
499
500 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
501
502 let result = client.delete(&path, Some(json!({
503 "backup-type": snapshot.group().backup_type(),
504 "backup-id": snapshot.group().backup_id(),
505 "backup-time": snapshot.backup_time().timestamp(),
506 }))).await?;
507
508 record_repository(&repo);
509
510 Ok(result)
511 }
512
513 #[api(
514 input: {
515 properties: {
516 repository: {
517 schema: REPO_URL_SCHEMA,
518 optional: true,
519 },
520 }
521 }
522 )]
523 /// Try to login. If successful, store ticket.
524 async fn api_login(param: Value) -> Result<Value, Error> {
525
526 let repo = extract_repository_from_value(&param)?;
527
528 let client = connect(repo.host(), repo.user())?;
529 client.login().await?;
530
531 record_repository(&repo);
532
533 Ok(Value::Null)
534 }
535
536 #[api(
537 input: {
538 properties: {
539 repository: {
540 schema: REPO_URL_SCHEMA,
541 optional: true,
542 },
543 }
544 }
545 )]
546 /// Logout (delete stored ticket).
547 fn api_logout(param: Value) -> Result<Value, Error> {
548
549 let repo = extract_repository_from_value(&param)?;
550
551 delete_ticket_info("proxmox-backup", repo.host(), repo.user())?;
552
553 Ok(Value::Null)
554 }
555
556 #[api(
557 input: {
558 properties: {
559 repository: {
560 schema: REPO_URL_SCHEMA,
561 optional: true,
562 },
563 "output-format": {
564 schema: OUTPUT_FORMAT,
565 optional: true,
566 },
567 }
568 }
569 )]
570 /// Show client and optional server version
571 async fn api_version(param: Value) -> Result<(), Error> {
572
573 let output_format = get_output_format(&param);
574
575 let mut version_info = json!({
576 "client": {
577 "version": version::PROXMOX_PKG_VERSION,
578 "release": version::PROXMOX_PKG_RELEASE,
579 "repoid": version::PROXMOX_PKG_REPOID,
580 }
581 });
582
583 let repo = extract_repository_from_value(&param);
584 if let Ok(repo) = repo {
585 let client = connect(repo.host(), repo.user())?;
586
587 match client.get("api2/json/version", None).await {
588 Ok(mut result) => version_info["server"] = result["data"].take(),
589 Err(e) => eprintln!("could not connect to server - {}", e),
590 }
591 }
592 if output_format == "text" {
593 println!("client version: {}.{}", version::PROXMOX_PKG_VERSION, version::PROXMOX_PKG_RELEASE);
594 if let Some(server) = version_info["server"].as_object() {
595 let server_version = server["version"].as_str().unwrap();
596 let server_release = server["release"].as_str().unwrap();
597 println!("server version: {}.{}", server_version, server_release);
598 }
599 } else {
600 format_and_print_result(&version_info, &output_format);
601 }
602
603 Ok(())
604 }
605
606
607 #[api(
608 input: {
609 properties: {
610 repository: {
611 schema: REPO_URL_SCHEMA,
612 optional: true,
613 },
614 snapshot: {
615 type: String,
616 description: "Snapshot path.",
617 },
618 "output-format": {
619 schema: OUTPUT_FORMAT,
620 optional: true,
621 },
622 }
623 }
624 )]
625 /// List snapshot files.
626 async fn list_snapshot_files(param: Value) -> Result<Value, Error> {
627
628 let repo = extract_repository_from_value(&param)?;
629
630 let path = tools::required_string_param(&param, "snapshot")?;
631 let snapshot: BackupDir = path.parse()?;
632
633 let output_format = get_output_format(&param);
634
635 let client = connect(repo.host(), repo.user())?;
636
637 let path = format!("api2/json/admin/datastore/{}/files", repo.store());
638
639 let mut result = client.get(&path, Some(json!({
640 "backup-type": snapshot.group().backup_type(),
641 "backup-id": snapshot.group().backup_id(),
642 "backup-time": snapshot.backup_time().timestamp(),
643 }))).await?;
644
645 record_repository(&repo);
646
647 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_SNAPSHOT_FILES;
648
649 let mut data: Value = result["data"].take();
650
651 let options = default_table_format_options();
652
653 format_and_print_result_full(&mut data, info, &output_format, &options);
654
655 Ok(Value::Null)
656 }
657
658 #[api(
659 input: {
660 properties: {
661 repository: {
662 schema: REPO_URL_SCHEMA,
663 optional: true,
664 },
665 "output-format": {
666 schema: OUTPUT_FORMAT,
667 optional: true,
668 },
669 },
670 },
671 )]
672 /// Start garbage collection for a specific repository.
673 async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
674
675 let repo = extract_repository_from_value(&param)?;
676
677 let output_format = get_output_format(&param);
678
679 let mut client = connect(repo.host(), repo.user())?;
680
681 let path = format!("api2/json/admin/datastore/{}/gc", repo.store());
682
683 let result = client.post(&path, None).await?;
684
685 record_repository(&repo);
686
687 view_task_result(client, result, &output_format).await?;
688
689 Ok(Value::Null)
690 }
691
692 fn spawn_catalog_upload(
693 client: Arc<BackupWriter>,
694 encrypt: bool,
695 ) -> Result<
696 (
697 Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
698 tokio::sync::oneshot::Receiver<Result<BackupStats, Error>>
699 ), Error>
700 {
701 let (catalog_tx, catalog_rx) = std::sync::mpsc::sync_channel(10); // allow to buffer 10 writes
702 let catalog_stream = crate::tools::StdChannelStream(catalog_rx);
703 let catalog_chunk_size = 512*1024;
704 let catalog_chunk_stream = ChunkStream::new(catalog_stream, Some(catalog_chunk_size));
705
706 let catalog = Arc::new(Mutex::new(CatalogWriter::new(crate::tools::StdChannelWriter::new(catalog_tx))?));
707
708 let (catalog_result_tx, catalog_result_rx) = tokio::sync::oneshot::channel();
709
710 tokio::spawn(async move {
711 let catalog_upload_result = client
712 .upload_stream(None, CATALOG_NAME, catalog_chunk_stream, "dynamic", None, true, encrypt)
713 .await;
714
715 if let Err(ref err) = catalog_upload_result {
716 eprintln!("catalog upload error - {}", err);
717 client.cancel();
718 }
719
720 let _ = catalog_result_tx.send(catalog_upload_result);
721 });
722
723 Ok((catalog, catalog_result_rx))
724 }
725
726 fn keyfile_parameters(param: &Value) -> Result<(Option<Vec<u8>>, CryptMode), Error> {
727 let keyfile = match param.get("keyfile") {
728 Some(Value::String(keyfile)) => Some(keyfile),
729 Some(_) => bail!("bad --keyfile parameter type"),
730 None => None,
731 };
732
733 let key_fd = match param.get("keyfd") {
734 Some(Value::Number(key_fd)) => Some(
735 RawFd::try_from(key_fd
736 .as_i64()
737 .ok_or_else(|| format_err!("bad key fd: {:?}", key_fd))?
738 )
739 .map_err(|err| format_err!("bad key fd: {:?}: {}", key_fd, err))?
740 ),
741 Some(_) => bail!("bad --keyfd parameter type"),
742 None => None,
743 };
744
745 let crypt_mode: Option<CryptMode> = match param.get("crypt-mode") {
746 Some(mode) => Some(serde_json::from_value(mode.clone())?),
747 None => None,
748 };
749
750 let keydata = match (keyfile, key_fd) {
751 (None, None) => None,
752 (Some(_), Some(_)) => bail!("--keyfile and --keyfd are mutually exclusive"),
753 (Some(keyfile), None) => Some(file_get_contents(keyfile)?),
754 (None, Some(fd)) => {
755 let input = unsafe { std::fs::File::from_raw_fd(fd) };
756 let mut data = Vec::new();
757 let _len: usize = { input }.read_to_end(&mut data)
758 .map_err(|err| {
759 format_err!("error reading encryption key from fd {}: {}", fd, err)
760 })?;
761 Some(data)
762 }
763 };
764
765 Ok(match (keydata, crypt_mode) {
766 // no parameters:
767 (None, None) => match key::read_optional_default_encryption_key()? {
768 Some(key) => (Some(key), CryptMode::Encrypt),
769 None => (None, CryptMode::None),
770 },
771
772 // just --crypt-mode=none
773 (None, Some(CryptMode::None)) => (None, CryptMode::None),
774
775 // just --crypt-mode other than none
776 (None, Some(crypt_mode)) => match key::read_optional_default_encryption_key()? {
777 None => bail!("--crypt-mode without --keyfile and no default key file available"),
778 Some(key) => (Some(key), crypt_mode),
779 }
780
781 // just --keyfile
782 (Some(key), None) => (Some(key), CryptMode::Encrypt),
783
784 // --keyfile and --crypt-mode=none
785 (Some(_), Some(CryptMode::None)) => {
786 bail!("--keyfile/--keyfd and --crypt-mode=none are mutually exclusive");
787 }
788
789 // --keyfile and --crypt-mode other than none
790 (Some(key), Some(crypt_mode)) => (Some(key), crypt_mode),
791 })
792 }
793
794 #[api(
795 input: {
796 properties: {
797 backupspec: {
798 type: Array,
799 description: "List of backup source specifications ([<label.ext>:<path>] ...)",
800 items: {
801 schema: BACKUP_SOURCE_SCHEMA,
802 }
803 },
804 repository: {
805 schema: REPO_URL_SCHEMA,
806 optional: true,
807 },
808 "include-dev": {
809 description: "Include mountpoints with same st_dev number (see ``man fstat``) as specified files.",
810 optional: true,
811 items: {
812 type: String,
813 description: "Path to file.",
814 }
815 },
816 keyfile: {
817 schema: KEYFILE_SCHEMA,
818 optional: true,
819 },
820 "keyfd": {
821 schema: KEYFD_SCHEMA,
822 optional: true,
823 },
824 "crypt-mode": {
825 type: CryptMode,
826 optional: true,
827 },
828 "skip-lost-and-found": {
829 type: Boolean,
830 description: "Skip lost+found directory.",
831 optional: true,
832 },
833 "backup-type": {
834 schema: BACKUP_TYPE_SCHEMA,
835 optional: true,
836 },
837 "backup-id": {
838 schema: BACKUP_ID_SCHEMA,
839 optional: true,
840 },
841 "backup-time": {
842 schema: BACKUP_TIME_SCHEMA,
843 optional: true,
844 },
845 "chunk-size": {
846 schema: CHUNK_SIZE_SCHEMA,
847 optional: true,
848 },
849 "exclude": {
850 type: Array,
851 description: "List of paths or patterns for matching files to exclude.",
852 optional: true,
853 items: {
854 type: String,
855 description: "Path or match pattern.",
856 }
857 },
858 "entries-max": {
859 type: Integer,
860 description: "Max number of entries to hold in memory.",
861 optional: true,
862 default: proxmox_backup::pxar::ENCODER_MAX_ENTRIES as isize,
863 },
864 "verbose": {
865 type: Boolean,
866 description: "Verbose output.",
867 optional: true,
868 },
869 }
870 }
871 )]
872 /// Create (host) backup.
873 async fn create_backup(
874 param: Value,
875 _info: &ApiMethod,
876 _rpcenv: &mut dyn RpcEnvironment,
877 ) -> Result<Value, Error> {
878
879 let repo = extract_repository_from_value(&param)?;
880
881 let backupspec_list = tools::required_array_param(&param, "backupspec")?;
882
883 let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
884
885 let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false);
886
887 let verbose = param["verbose"].as_bool().unwrap_or(false);
888
889 let backup_time_opt = param["backup-time"].as_i64();
890
891 let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize);
892
893 if let Some(size) = chunk_size_opt {
894 verify_chunk_size(size)?;
895 }
896
897 let (keydata, crypt_mode) = keyfile_parameters(&param)?;
898
899 let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename());
900
901 let backup_type = param["backup-type"].as_str().unwrap_or("host");
902
903 let include_dev = param["include-dev"].as_array();
904
905 let entries_max = param["entries-max"].as_u64()
906 .unwrap_or(proxmox_backup::pxar::ENCODER_MAX_ENTRIES as u64);
907
908 let empty = Vec::new();
909 let exclude_args = param["exclude"].as_array().unwrap_or(&empty);
910
911 let mut pattern_list = Vec::with_capacity(exclude_args.len());
912 for entry in exclude_args {
913 let entry = entry.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
914 pattern_list.push(
915 MatchEntry::parse_pattern(entry, PatternFlag::PATH_NAME, MatchType::Exclude)
916 .map_err(|err| format_err!("invalid exclude pattern entry: {}", err))?
917 );
918 }
919
920 let mut devices = if all_file_systems { None } else { Some(HashSet::new()) };
921
922 if let Some(include_dev) = include_dev {
923 if all_file_systems {
924 bail!("option 'all-file-systems' conflicts with option 'include-dev'");
925 }
926
927 let mut set = HashSet::new();
928 for path in include_dev {
929 let path = path.as_str().unwrap();
930 let stat = nix::sys::stat::stat(path)
931 .map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?;
932 set.insert(stat.st_dev);
933 }
934 devices = Some(set);
935 }
936
937 let mut upload_list = vec![];
938 let mut target_set = HashSet::new();
939
940 for backupspec in backupspec_list {
941 let spec = parse_backup_specification(backupspec.as_str().unwrap())?;
942 let filename = &spec.config_string;
943 let target = &spec.archive_name;
944
945 if target_set.contains(target) {
946 bail!("got target twice: '{}'", target);
947 }
948 target_set.insert(target.to_string());
949
950 use std::os::unix::fs::FileTypeExt;
951
952 let metadata = std::fs::metadata(filename)
953 .map_err(|err| format_err!("unable to access '{}' - {}", filename, err))?;
954 let file_type = metadata.file_type();
955
956 match spec.spec_type {
957 BackupSpecificationType::PXAR => {
958 if !file_type.is_dir() {
959 bail!("got unexpected file type (expected directory)");
960 }
961 upload_list.push((BackupSpecificationType::PXAR, filename.to_owned(), format!("{}.didx", target), 0));
962 }
963 BackupSpecificationType::IMAGE => {
964 if !(file_type.is_file() || file_type.is_block_device()) {
965 bail!("got unexpected file type (expected file or block device)");
966 }
967
968 let size = image_size(&PathBuf::from(filename))?;
969
970 if size == 0 { bail!("got zero-sized file '{}'", filename); }
971
972 upload_list.push((BackupSpecificationType::IMAGE, filename.to_owned(), format!("{}.fidx", target), size));
973 }
974 BackupSpecificationType::CONFIG => {
975 if !file_type.is_file() {
976 bail!("got unexpected file type (expected regular file)");
977 }
978 upload_list.push((BackupSpecificationType::CONFIG, filename.to_owned(), format!("{}.blob", target), metadata.len()));
979 }
980 BackupSpecificationType::LOGFILE => {
981 if !file_type.is_file() {
982 bail!("got unexpected file type (expected regular file)");
983 }
984 upload_list.push((BackupSpecificationType::LOGFILE, filename.to_owned(), format!("{}.blob", target), metadata.len()));
985 }
986 }
987 }
988
989 let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0);
990
991 let client = connect(repo.host(), repo.user())?;
992 record_repository(&repo);
993
994 println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time));
995
996 println!("Client name: {}", proxmox::tools::nodename());
997
998 let start_time = Local::now();
999
1000 println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
1001
1002 let (crypt_config, rsa_encrypted_key) = match keydata {
1003 None => (None, None),
1004 Some(key) => {
1005 let (key, created) = decrypt_key(&key, &key::get_encryption_key_password)?;
1006
1007 let crypt_config = CryptConfig::new(key)?;
1008
1009 match key::find_master_pubkey()? {
1010 Some(ref path) if path.exists() => {
1011 let pem_data = file_get_contents(path)?;
1012 let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
1013 let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?;
1014 (Some(Arc::new(crypt_config)), Some(enc_key))
1015 }
1016 _ => (Some(Arc::new(crypt_config)), None),
1017 }
1018 }
1019 };
1020
1021 let client = BackupWriter::start(
1022 client,
1023 crypt_config.clone(),
1024 repo.store(),
1025 backup_type,
1026 &backup_id,
1027 backup_time,
1028 verbose,
1029 ).await?;
1030
1031 let previous_manifest = if let Ok(previous_manifest) = client.download_previous_manifest().await {
1032 Some(Arc::new(previous_manifest))
1033 } else {
1034 None
1035 };
1036
1037 let snapshot = BackupDir::new(backup_type, backup_id, backup_time.timestamp());
1038 let mut manifest = BackupManifest::new(snapshot);
1039
1040 let mut catalog = None;
1041 let mut catalog_result_tx = None;
1042
1043 for (backup_type, filename, target, size) in upload_list {
1044 match backup_type {
1045 BackupSpecificationType::CONFIG => {
1046 println!("Upload config file '{}' to '{}' as {}", filename, repo, target);
1047 let stats = client
1048 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
1049 .await?;
1050 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1051 }
1052 BackupSpecificationType::LOGFILE => { // fixme: remove - not needed anymore ?
1053 println!("Upload log file '{}' to '{}' as {}", filename, repo, target);
1054 let stats = client
1055 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
1056 .await?;
1057 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1058 }
1059 BackupSpecificationType::PXAR => {
1060 // start catalog upload on first use
1061 if catalog.is_none() {
1062 let (cat, res) = spawn_catalog_upload(client.clone(), crypt_mode == CryptMode::Encrypt)?;
1063 catalog = Some(cat);
1064 catalog_result_tx = Some(res);
1065 }
1066 let catalog = catalog.as_ref().unwrap();
1067
1068 println!("Upload directory '{}' to '{}' as {}", filename, repo, target);
1069 catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?;
1070 let stats = backup_directory(
1071 &client,
1072 previous_manifest.clone(),
1073 &filename,
1074 &target,
1075 chunk_size_opt,
1076 devices.clone(),
1077 verbose,
1078 skip_lost_and_found,
1079 catalog.clone(),
1080 pattern_list.clone(),
1081 entries_max as usize,
1082 true,
1083 crypt_mode == CryptMode::Encrypt,
1084 ).await?;
1085 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1086 catalog.lock().unwrap().end_directory()?;
1087 }
1088 BackupSpecificationType::IMAGE => {
1089 println!("Upload image '{}' to '{:?}' as {}", filename, repo, target);
1090 let stats = backup_image(
1091 &client,
1092 previous_manifest.clone(),
1093 &filename,
1094 &target,
1095 size,
1096 chunk_size_opt,
1097 true,
1098 crypt_mode == CryptMode::Encrypt,
1099 verbose,
1100 ).await?;
1101 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1102 }
1103 }
1104 }
1105
1106 // finalize and upload catalog
1107 if let Some(catalog) = catalog {
1108 let mutex = Arc::try_unwrap(catalog)
1109 .map_err(|_| format_err!("unable to get catalog (still used)"))?;
1110 let mut catalog = mutex.into_inner().unwrap();
1111
1112 catalog.finish()?;
1113
1114 drop(catalog); // close upload stream
1115
1116 if let Some(catalog_result_rx) = catalog_result_tx {
1117 let stats = catalog_result_rx.await??;
1118 manifest.add_file(CATALOG_NAME.to_owned(), stats.size, stats.csum, crypt_mode)?;
1119 }
1120 }
1121
1122 if let Some(rsa_encrypted_key) = rsa_encrypted_key {
1123 let target = "rsa-encrypted.key";
1124 println!("Upload RSA encoded key to '{:?}' as {}", repo, target);
1125 let stats = client
1126 .upload_blob_from_data(rsa_encrypted_key, target, false, false)
1127 .await?;
1128 manifest.add_file(format!("{}.blob", target), stats.size, stats.csum, crypt_mode)?;
1129
1130 // openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t
1131 /*
1132 let mut buffer2 = vec![0u8; rsa.size() as usize];
1133 let pem_data = file_get_contents("master-private.pem")?;
1134 let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?;
1135 let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?;
1136 println!("TEST {} {:?}", len, buffer2);
1137 */
1138 }
1139
1140 // create manifest (index.json)
1141 // manifests are never encrypted, but include a signature
1142 let manifest = manifest.to_string(crypt_config.as_ref().map(Arc::as_ref))
1143 .map_err(|err| format_err!("unable to format manifest - {}", err))?;
1144
1145
1146 if verbose { println!("Upload index.json to '{}'", repo) };
1147 client
1148 .upload_blob_from_data(manifest.into_bytes(), MANIFEST_BLOB_NAME, true, false)
1149 .await?;
1150
1151 client.finish().await?;
1152
1153 let end_time = Local::now();
1154 let elapsed = end_time.signed_duration_since(start_time);
1155 println!("Duration: {}", elapsed);
1156
1157 println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
1158
1159 Ok(Value::Null)
1160 }
1161
1162 fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1163
1164 let mut result = vec![];
1165
1166 let data: Vec<&str> = arg.splitn(2, ':').collect();
1167
1168 if data.len() != 2 {
1169 result.push(String::from("root.pxar:/"));
1170 result.push(String::from("etc.pxar:/etc"));
1171 return result;
1172 }
1173
1174 let files = tools::complete_file_name(data[1], param);
1175
1176 for file in files {
1177 result.push(format!("{}:{}", data[0], file));
1178 }
1179
1180 result
1181 }
1182
1183 async fn dump_image<W: Write>(
1184 client: Arc<BackupReader>,
1185 crypt_config: Option<Arc<CryptConfig>>,
1186 index: FixedIndexReader,
1187 mut writer: W,
1188 verbose: bool,
1189 ) -> Result<(), Error> {
1190
1191 let most_used = index.find_most_used_chunks(8);
1192
1193 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
1194
1195 // Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy
1196 // and thus slows down reading. Instead, directly use RemoteChunkReader
1197 let mut per = 0;
1198 let mut bytes = 0;
1199 let start_time = std::time::Instant::now();
1200
1201 for pos in 0..index.index_count() {
1202 let digest = index.index_digest(pos).unwrap();
1203 let raw_data = chunk_reader.read_chunk(&digest).await?;
1204 writer.write_all(&raw_data)?;
1205 bytes += raw_data.len();
1206 if verbose {
1207 let next_per = ((pos+1)*100)/index.index_count();
1208 if per != next_per {
1209 eprintln!("progress {}% (read {} bytes, duration {} sec)",
1210 next_per, bytes, start_time.elapsed().as_secs());
1211 per = next_per;
1212 }
1213 }
1214 }
1215
1216 let end_time = std::time::Instant::now();
1217 let elapsed = end_time.duration_since(start_time);
1218 eprintln!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)",
1219 bytes,
1220 elapsed.as_secs_f64(),
1221 bytes as f64/(1024.0*1024.0*elapsed.as_secs_f64())
1222 );
1223
1224
1225 Ok(())
1226 }
1227
1228 fn parse_archive_type(name: &str) -> (String, ArchiveType) {
1229 if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
1230 (name.into(), archive_type(name).unwrap())
1231 } else if name.ends_with(".pxar") {
1232 (format!("{}.didx", name), ArchiveType::DynamicIndex)
1233 } else if name.ends_with(".img") {
1234 (format!("{}.fidx", name), ArchiveType::FixedIndex)
1235 } else {
1236 (format!("{}.blob", name), ArchiveType::Blob)
1237 }
1238 }
1239
1240 #[api(
1241 input: {
1242 properties: {
1243 repository: {
1244 schema: REPO_URL_SCHEMA,
1245 optional: true,
1246 },
1247 snapshot: {
1248 type: String,
1249 description: "Group/Snapshot path.",
1250 },
1251 "archive-name": {
1252 description: "Backup archive name.",
1253 type: String,
1254 },
1255 target: {
1256 type: String,
1257 description: r###"Target directory path. Use '-' to write to standard output.
1258
1259 We do not extraxt '.pxar' archives when writing to standard output.
1260
1261 "###
1262 },
1263 "allow-existing-dirs": {
1264 type: Boolean,
1265 description: "Do not fail if directories already exists.",
1266 optional: true,
1267 },
1268 keyfile: {
1269 schema: KEYFILE_SCHEMA,
1270 optional: true,
1271 },
1272 "keyfd": {
1273 schema: KEYFD_SCHEMA,
1274 optional: true,
1275 },
1276 "crypt-mode": {
1277 type: CryptMode,
1278 optional: true,
1279 },
1280 }
1281 }
1282 )]
1283 /// Restore backup repository.
1284 async fn restore(param: Value) -> Result<Value, Error> {
1285 let repo = extract_repository_from_value(&param)?;
1286
1287 let verbose = param["verbose"].as_bool().unwrap_or(false);
1288
1289 let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
1290
1291 let archive_name = tools::required_string_param(&param, "archive-name")?;
1292
1293 let client = connect(repo.host(), repo.user())?;
1294
1295 record_repository(&repo);
1296
1297 let path = tools::required_string_param(&param, "snapshot")?;
1298
1299 let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
1300 let group: BackupGroup = path.parse()?;
1301 api_datastore_latest_snapshot(&client, repo.store(), group).await?
1302 } else {
1303 let snapshot: BackupDir = path.parse()?;
1304 (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
1305 };
1306
1307 let target = tools::required_string_param(&param, "target")?;
1308 let target = if target == "-" { None } else { Some(target) };
1309
1310 let (keydata, _crypt_mode) = keyfile_parameters(&param)?;
1311
1312 let crypt_config = match keydata {
1313 None => None,
1314 Some(key) => {
1315 let (key, _) = decrypt_key(&key, &key::get_encryption_key_password)?;
1316 Some(Arc::new(CryptConfig::new(key)?))
1317 }
1318 };
1319
1320 let client = BackupReader::start(
1321 client,
1322 crypt_config.clone(),
1323 repo.store(),
1324 &backup_type,
1325 &backup_id,
1326 backup_time,
1327 true,
1328 ).await?;
1329
1330 let (manifest, backup_index_data) = client.download_manifest().await?;
1331
1332 let (archive_name, archive_type) = parse_archive_type(archive_name);
1333
1334 if archive_name == MANIFEST_BLOB_NAME {
1335 if let Some(target) = target {
1336 replace_file(target, &backup_index_data, CreateOptions::new())?;
1337 } else {
1338 let stdout = std::io::stdout();
1339 let mut writer = stdout.lock();
1340 writer.write_all(&backup_index_data)
1341 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1342 }
1343
1344 } else if archive_type == ArchiveType::Blob {
1345
1346 let mut reader = client.download_blob(&manifest, &archive_name).await?;
1347
1348 if let Some(target) = target {
1349 let mut writer = std::fs::OpenOptions::new()
1350 .write(true)
1351 .create(true)
1352 .create_new(true)
1353 .open(target)
1354 .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?;
1355 std::io::copy(&mut reader, &mut writer)?;
1356 } else {
1357 let stdout = std::io::stdout();
1358 let mut writer = stdout.lock();
1359 std::io::copy(&mut reader, &mut writer)
1360 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1361 }
1362
1363 } else if archive_type == ArchiveType::DynamicIndex {
1364
1365 let index = client.download_dynamic_index(&manifest, &archive_name).await?;
1366
1367 let most_used = index.find_most_used_chunks(8);
1368
1369 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
1370
1371 let mut reader = BufferedDynamicReader::new(index, chunk_reader);
1372
1373 if let Some(target) = target {
1374 proxmox_backup::pxar::extract_archive(
1375 pxar::decoder::Decoder::from_std(reader)?,
1376 Path::new(target),
1377 &[],
1378 true,
1379 proxmox_backup::pxar::Flags::DEFAULT,
1380 allow_existing_dirs,
1381 |path| {
1382 if verbose {
1383 println!("{:?}", path);
1384 }
1385 },
1386 None,
1387 )
1388 .map_err(|err| format_err!("error extracting archive - {}", err))?;
1389 } else {
1390 let mut writer = std::fs::OpenOptions::new()
1391 .write(true)
1392 .open("/dev/stdout")
1393 .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?;
1394
1395 std::io::copy(&mut reader, &mut writer)
1396 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1397 }
1398 } else if archive_type == ArchiveType::FixedIndex {
1399
1400 let index = client.download_fixed_index(&manifest, &archive_name).await?;
1401
1402 let mut writer = if let Some(target) = target {
1403 std::fs::OpenOptions::new()
1404 .write(true)
1405 .create(true)
1406 .create_new(true)
1407 .open(target)
1408 .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?
1409 } else {
1410 std::fs::OpenOptions::new()
1411 .write(true)
1412 .open("/dev/stdout")
1413 .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?
1414 };
1415
1416 dump_image(client.clone(), crypt_config.clone(), index, &mut writer, verbose).await?;
1417 }
1418
1419 Ok(Value::Null)
1420 }
1421
1422 #[api(
1423 input: {
1424 properties: {
1425 repository: {
1426 schema: REPO_URL_SCHEMA,
1427 optional: true,
1428 },
1429 snapshot: {
1430 type: String,
1431 description: "Group/Snapshot path.",
1432 },
1433 logfile: {
1434 type: String,
1435 description: "The path to the log file you want to upload.",
1436 },
1437 keyfile: {
1438 schema: KEYFILE_SCHEMA,
1439 optional: true,
1440 },
1441 "keyfd": {
1442 schema: KEYFD_SCHEMA,
1443 optional: true,
1444 },
1445 "crypt-mode": {
1446 type: CryptMode,
1447 optional: true,
1448 },
1449 }
1450 }
1451 )]
1452 /// Upload backup log file.
1453 async fn upload_log(param: Value) -> Result<Value, Error> {
1454
1455 let logfile = tools::required_string_param(&param, "logfile")?;
1456 let repo = extract_repository_from_value(&param)?;
1457
1458 let snapshot = tools::required_string_param(&param, "snapshot")?;
1459 let snapshot: BackupDir = snapshot.parse()?;
1460
1461 let mut client = connect(repo.host(), repo.user())?;
1462
1463 let (keydata, crypt_mode) = keyfile_parameters(&param)?;
1464
1465 let crypt_config = match keydata {
1466 None => None,
1467 Some(key) => {
1468 let (key, _created) = decrypt_key(&key, &key::get_encryption_key_password)?;
1469 let crypt_config = CryptConfig::new(key)?;
1470 Some(Arc::new(crypt_config))
1471 }
1472 };
1473
1474 let data = file_get_contents(logfile)?;
1475
1476 // fixme: howto sign log?
1477 let blob = match crypt_mode {
1478 CryptMode::None | CryptMode::SignOnly => DataBlob::encode(&data, None, true)?,
1479 CryptMode::Encrypt => DataBlob::encode(&data, crypt_config.as_ref().map(Arc::as_ref), true)?,
1480 };
1481
1482 let raw_data = blob.into_inner();
1483
1484 let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store());
1485
1486 let args = json!({
1487 "backup-type": snapshot.group().backup_type(),
1488 "backup-id": snapshot.group().backup_id(),
1489 "backup-time": snapshot.backup_time().timestamp(),
1490 });
1491
1492 let body = hyper::Body::from(raw_data);
1493
1494 client.upload("application/octet-stream", body, &path, Some(args)).await
1495 }
1496
1497 const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
1498 &ApiHandler::Async(&prune),
1499 &ObjectSchema::new(
1500 "Prune a backup repository.",
1501 &proxmox_backup::add_common_prune_prameters!([
1502 ("dry-run", true, &BooleanSchema::new(
1503 "Just show what prune would do, but do not delete anything.")
1504 .schema()),
1505 ("group", false, &StringSchema::new("Backup group.").schema()),
1506 ], [
1507 ("output-format", true, &OUTPUT_FORMAT),
1508 (
1509 "quiet",
1510 true,
1511 &BooleanSchema::new("Minimal output - only show removals.")
1512 .schema()
1513 ),
1514 ("repository", true, &REPO_URL_SCHEMA),
1515 ])
1516 )
1517 );
1518
1519 fn prune<'a>(
1520 param: Value,
1521 _info: &ApiMethod,
1522 _rpcenv: &'a mut dyn RpcEnvironment,
1523 ) -> proxmox::api::ApiFuture<'a> {
1524 async move {
1525 prune_async(param).await
1526 }.boxed()
1527 }
1528
1529 async fn prune_async(mut param: Value) -> Result<Value, Error> {
1530 let repo = extract_repository_from_value(&param)?;
1531
1532 let mut client = connect(repo.host(), repo.user())?;
1533
1534 let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
1535
1536 let group = tools::required_string_param(&param, "group")?;
1537 let group: BackupGroup = group.parse()?;
1538
1539 let output_format = get_output_format(&param);
1540
1541 let quiet = param["quiet"].as_bool().unwrap_or(false);
1542
1543 param.as_object_mut().unwrap().remove("repository");
1544 param.as_object_mut().unwrap().remove("group");
1545 param.as_object_mut().unwrap().remove("output-format");
1546 param.as_object_mut().unwrap().remove("quiet");
1547
1548 param["backup-type"] = group.backup_type().into();
1549 param["backup-id"] = group.backup_id().into();
1550
1551 let mut result = client.post(&path, Some(param)).await?;
1552
1553 record_repository(&repo);
1554
1555 let render_snapshot_path = |_v: &Value, record: &Value| -> Result<String, Error> {
1556 let item: PruneListItem = serde_json::from_value(record.to_owned())?;
1557 let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
1558 Ok(snapshot.relative_path().to_str().unwrap().to_owned())
1559 };
1560
1561 let render_prune_action = |v: &Value, _record: &Value| -> Result<String, Error> {
1562 Ok(match v.as_bool() {
1563 Some(true) => "keep",
1564 Some(false) => "remove",
1565 None => "unknown",
1566 }.to_string())
1567 };
1568
1569 let options = default_table_format_options()
1570 .sortby("backup-type", false)
1571 .sortby("backup-id", false)
1572 .sortby("backup-time", false)
1573 .column(ColumnConfig::new("backup-id").renderer(render_snapshot_path).header("snapshot"))
1574 .column(ColumnConfig::new("backup-time").renderer(tools::format::render_epoch).header("date"))
1575 .column(ColumnConfig::new("keep").renderer(render_prune_action).header("action"))
1576 ;
1577
1578 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_PRUNE;
1579
1580 let mut data = result["data"].take();
1581
1582 if quiet {
1583 let list: Vec<Value> = data.as_array().unwrap().iter().filter(|item| {
1584 item["keep"].as_bool() == Some(false)
1585 }).map(|v| v.clone()).collect();
1586 data = list.into();
1587 }
1588
1589 format_and_print_result_full(&mut data, info, &output_format, &options);
1590
1591 Ok(Value::Null)
1592 }
1593
1594 #[api(
1595 input: {
1596 properties: {
1597 repository: {
1598 schema: REPO_URL_SCHEMA,
1599 optional: true,
1600 },
1601 "output-format": {
1602 schema: OUTPUT_FORMAT,
1603 optional: true,
1604 },
1605 }
1606 }
1607 )]
1608 /// Get repository status.
1609 async fn status(param: Value) -> Result<Value, Error> {
1610
1611 let repo = extract_repository_from_value(&param)?;
1612
1613 let output_format = get_output_format(&param);
1614
1615 let client = connect(repo.host(), repo.user())?;
1616
1617 let path = format!("api2/json/admin/datastore/{}/status", repo.store());
1618
1619 let mut result = client.get(&path, None).await?;
1620 let mut data = result["data"].take();
1621
1622 record_repository(&repo);
1623
1624 let render_total_percentage = |v: &Value, record: &Value| -> Result<String, Error> {
1625 let v = v.as_u64().unwrap();
1626 let total = record["total"].as_u64().unwrap();
1627 let roundup = total/200;
1628 let per = ((v+roundup)*100)/total;
1629 let info = format!(" ({} %)", per);
1630 Ok(format!("{} {:>8}", v, info))
1631 };
1632
1633 let options = default_table_format_options()
1634 .noheader(true)
1635 .column(ColumnConfig::new("total").renderer(render_total_percentage))
1636 .column(ColumnConfig::new("used").renderer(render_total_percentage))
1637 .column(ColumnConfig::new("avail").renderer(render_total_percentage));
1638
1639 let schema = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_STATUS;
1640
1641 format_and_print_result_full(&mut data, schema, &output_format, &options);
1642
1643 Ok(Value::Null)
1644 }
1645
1646 // like get, but simply ignore errors and return Null instead
1647 async fn try_get(repo: &BackupRepository, url: &str) -> Value {
1648
1649 let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
1650 let password = std::env::var(ENV_VAR_PBS_PASSWORD).ok();
1651
1652 let options = HttpClientOptions::new()
1653 .prefix(Some("proxmox-backup".to_string()))
1654 .password(password)
1655 .interactive(false)
1656 .fingerprint(fingerprint)
1657 .fingerprint_cache(true)
1658 .ticket_cache(true);
1659
1660 let client = match HttpClient::new(repo.host(), repo.user(), options) {
1661 Ok(v) => v,
1662 _ => return Value::Null,
1663 };
1664
1665 let mut resp = match client.get(url, None).await {
1666 Ok(v) => v,
1667 _ => return Value::Null,
1668 };
1669
1670 if let Some(map) = resp.as_object_mut() {
1671 if let Some(data) = map.remove("data") {
1672 return data;
1673 }
1674 }
1675 Value::Null
1676 }
1677
1678 fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1679 proxmox_backup::tools::runtime::main(async { complete_backup_group_do(param).await })
1680 }
1681
1682 async fn complete_backup_group_do(param: &HashMap<String, String>) -> Vec<String> {
1683
1684 let mut result = vec![];
1685
1686 let repo = match extract_repository_from_map(param) {
1687 Some(v) => v,
1688 _ => return result,
1689 };
1690
1691 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
1692
1693 let data = try_get(&repo, &path).await;
1694
1695 if let Some(list) = data.as_array() {
1696 for item in list {
1697 if let (Some(backup_id), Some(backup_type)) =
1698 (item["backup-id"].as_str(), item["backup-type"].as_str())
1699 {
1700 result.push(format!("{}/{}", backup_type, backup_id));
1701 }
1702 }
1703 }
1704
1705 result
1706 }
1707
1708 pub fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1709 proxmox_backup::tools::runtime::main(async { complete_group_or_snapshot_do(arg, param).await })
1710 }
1711
1712 async fn complete_group_or_snapshot_do(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1713
1714 if arg.matches('/').count() < 2 {
1715 let groups = complete_backup_group_do(param).await;
1716 let mut result = vec![];
1717 for group in groups {
1718 result.push(group.to_string());
1719 result.push(format!("{}/", group));
1720 }
1721 return result;
1722 }
1723
1724 complete_backup_snapshot_do(param).await
1725 }
1726
1727 fn complete_backup_snapshot(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1728 proxmox_backup::tools::runtime::main(async { complete_backup_snapshot_do(param).await })
1729 }
1730
1731 async fn complete_backup_snapshot_do(param: &HashMap<String, String>) -> Vec<String> {
1732
1733 let mut result = vec![];
1734
1735 let repo = match extract_repository_from_map(param) {
1736 Some(v) => v,
1737 _ => return result,
1738 };
1739
1740 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
1741
1742 let data = try_get(&repo, &path).await;
1743
1744 if let Some(list) = data.as_array() {
1745 for item in list {
1746 if let (Some(backup_id), Some(backup_type), Some(backup_time)) =
1747 (item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64())
1748 {
1749 let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
1750 result.push(snapshot.relative_path().to_str().unwrap().to_owned());
1751 }
1752 }
1753 }
1754
1755 result
1756 }
1757
1758 fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1759 proxmox_backup::tools::runtime::main(async { complete_server_file_name_do(param).await })
1760 }
1761
1762 async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<String> {
1763
1764 let mut result = vec![];
1765
1766 let repo = match extract_repository_from_map(param) {
1767 Some(v) => v,
1768 _ => return result,
1769 };
1770
1771 let snapshot: BackupDir = match param.get("snapshot") {
1772 Some(path) => {
1773 match path.parse() {
1774 Ok(v) => v,
1775 _ => return result,
1776 }
1777 }
1778 _ => return result,
1779 };
1780
1781 let query = tools::json_object_to_query(json!({
1782 "backup-type": snapshot.group().backup_type(),
1783 "backup-id": snapshot.group().backup_id(),
1784 "backup-time": snapshot.backup_time().timestamp(),
1785 })).unwrap();
1786
1787 let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query);
1788
1789 let data = try_get(&repo, &path).await;
1790
1791 if let Some(list) = data.as_array() {
1792 for item in list {
1793 if let Some(filename) = item["filename"].as_str() {
1794 result.push(filename.to_owned());
1795 }
1796 }
1797 }
1798
1799 result
1800 }
1801
1802 fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1803 complete_server_file_name(arg, param)
1804 .iter()
1805 .map(|v| tools::format::strip_server_file_expenstion(&v))
1806 .collect()
1807 }
1808
1809 pub fn complete_pxar_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1810 complete_server_file_name(arg, param)
1811 .iter()
1812 .filter_map(|v| {
1813 let name = tools::format::strip_server_file_expenstion(&v);
1814 if name.ends_with(".pxar") {
1815 Some(name)
1816 } else {
1817 None
1818 }
1819 })
1820 .collect()
1821 }
1822
1823 fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
1824
1825 let mut result = vec![];
1826
1827 let mut size = 64;
1828 loop {
1829 result.push(size.to_string());
1830 size *= 2;
1831 if size > 4096 { break; }
1832 }
1833
1834 result
1835 }
1836
1837 use proxmox_backup::client::RemoteChunkReader;
1838 /// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better
1839 /// async use!
1840 ///
1841 /// Ideally BufferedDynamicReader gets replaced so the LruCache maps to `BroadcastFuture<Chunk>`,
1842 /// so that we can properly access it from multiple threads simultaneously while not issuing
1843 /// duplicate simultaneous reads over http.
1844 pub struct BufferedDynamicReadAt {
1845 inner: Mutex<BufferedDynamicReader<RemoteChunkReader>>,
1846 }
1847
1848 impl BufferedDynamicReadAt {
1849 fn new(inner: BufferedDynamicReader<RemoteChunkReader>) -> Self {
1850 Self {
1851 inner: Mutex::new(inner),
1852 }
1853 }
1854 }
1855
1856 impl ReadAt for BufferedDynamicReadAt {
1857 fn start_read_at<'a>(
1858 self: Pin<&'a Self>,
1859 _cx: &mut Context,
1860 buf: &'a mut [u8],
1861 offset: u64,
1862 ) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
1863 MaybeReady::Ready(tokio::task::block_in_place(move || {
1864 let mut reader = self.inner.lock().unwrap();
1865 reader.seek(SeekFrom::Start(offset))?;
1866 Ok(reader.read(buf)?)
1867 }))
1868 }
1869
1870 fn poll_complete<'a>(
1871 self: Pin<&'a Self>,
1872 _op: ReadAtOperation<'a>,
1873 ) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
1874 panic!("LocalDynamicReadAt::start_read_at returned Pending");
1875 }
1876 }
1877
1878 fn main() {
1879
1880 let backup_cmd_def = CliCommand::new(&API_METHOD_CREATE_BACKUP)
1881 .arg_param(&["backupspec"])
1882 .completion_cb("repository", complete_repository)
1883 .completion_cb("backupspec", complete_backup_source)
1884 .completion_cb("keyfile", tools::complete_file_name)
1885 .completion_cb("chunk-size", complete_chunk_size);
1886
1887 let benchmark_cmd_def = CliCommand::new(&API_METHOD_BENCHMARK)
1888 .completion_cb("repository", complete_repository)
1889 .completion_cb("keyfile", tools::complete_file_name);
1890
1891 let upload_log_cmd_def = CliCommand::new(&API_METHOD_UPLOAD_LOG)
1892 .arg_param(&["snapshot", "logfile"])
1893 .completion_cb("snapshot", complete_backup_snapshot)
1894 .completion_cb("logfile", tools::complete_file_name)
1895 .completion_cb("keyfile", tools::complete_file_name)
1896 .completion_cb("repository", complete_repository);
1897
1898 let list_cmd_def = CliCommand::new(&API_METHOD_LIST_BACKUP_GROUPS)
1899 .completion_cb("repository", complete_repository);
1900
1901 let snapshots_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOTS)
1902 .arg_param(&["group"])
1903 .completion_cb("group", complete_backup_group)
1904 .completion_cb("repository", complete_repository);
1905
1906 let forget_cmd_def = CliCommand::new(&API_METHOD_FORGET_SNAPSHOTS)
1907 .arg_param(&["snapshot"])
1908 .completion_cb("repository", complete_repository)
1909 .completion_cb("snapshot", complete_backup_snapshot);
1910
1911 let garbage_collect_cmd_def = CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
1912 .completion_cb("repository", complete_repository);
1913
1914 let restore_cmd_def = CliCommand::new(&API_METHOD_RESTORE)
1915 .arg_param(&["snapshot", "archive-name", "target"])
1916 .completion_cb("repository", complete_repository)
1917 .completion_cb("snapshot", complete_group_or_snapshot)
1918 .completion_cb("archive-name", complete_archive_name)
1919 .completion_cb("target", tools::complete_file_name);
1920
1921 let files_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOT_FILES)
1922 .arg_param(&["snapshot"])
1923 .completion_cb("repository", complete_repository)
1924 .completion_cb("snapshot", complete_backup_snapshot);
1925
1926 let prune_cmd_def = CliCommand::new(&API_METHOD_PRUNE)
1927 .arg_param(&["group"])
1928 .completion_cb("group", complete_backup_group)
1929 .completion_cb("repository", complete_repository);
1930
1931 let status_cmd_def = CliCommand::new(&API_METHOD_STATUS)
1932 .completion_cb("repository", complete_repository);
1933
1934 let login_cmd_def = CliCommand::new(&API_METHOD_API_LOGIN)
1935 .completion_cb("repository", complete_repository);
1936
1937 let logout_cmd_def = CliCommand::new(&API_METHOD_API_LOGOUT)
1938 .completion_cb("repository", complete_repository);
1939
1940 let version_cmd_def = CliCommand::new(&API_METHOD_API_VERSION)
1941 .completion_cb("repository", complete_repository);
1942
1943 let cmd_def = CliCommandMap::new()
1944 .insert("backup", backup_cmd_def)
1945 .insert("upload-log", upload_log_cmd_def)
1946 .insert("forget", forget_cmd_def)
1947 .insert("garbage-collect", garbage_collect_cmd_def)
1948 .insert("list", list_cmd_def)
1949 .insert("login", login_cmd_def)
1950 .insert("logout", logout_cmd_def)
1951 .insert("prune", prune_cmd_def)
1952 .insert("restore", restore_cmd_def)
1953 .insert("snapshots", snapshots_cmd_def)
1954 .insert("files", files_cmd_def)
1955 .insert("status", status_cmd_def)
1956 .insert("key", key::cli())
1957 .insert("mount", mount_cmd_def())
1958 .insert("catalog", catalog_mgmt_cli())
1959 .insert("task", task_mgmt_cli())
1960 .insert("version", version_cmd_def)
1961 .insert("benchmark", benchmark_cmd_def);
1962
1963 let rpcenv = CliEnvironment::new();
1964 run_cli_command(cmd_def, rpcenv, Some(|future| {
1965 proxmox_backup::tools::runtime::main(future)
1966 }));
1967 }