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