]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-client.rs
client: log archive upload duration more accurate, fix grammar
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
CommitLineData
2eeaacb9 1use std::collections::{HashSet, HashMap};
0351f23b
WB
2use std::convert::TryFrom;
3use std::io::{self, Read, Write, Seek, SeekFrom};
4use std::os::unix::io::{FromRawFd, RawFd};
c443f58b
WB
5use std::path::{Path, PathBuf};
6use std::pin::Pin;
7use std::sync::{Arc, Mutex};
a6f87283 8use std::task::Context;
c443f58b
WB
9
10use anyhow::{bail, format_err, Error};
11use chrono::{Local, DateTime, Utc, TimeZone};
12use futures::future::FutureExt;
c443f58b 13use futures::stream::{StreamExt, TryStreamExt};
c443f58b 14use serde_json::{json, Value};
c443f58b
WB
15use tokio::sync::mpsc;
16use xdg::BaseDirectories;
2761d6a4 17
c443f58b 18use pathpatterns::{MatchEntry, MatchType, PatternFlag};
feaa1ad3 19use proxmox::tools::fs::{file_get_contents, file_get_json, replace_file, CreateOptions, image_size};
a47a02ae 20use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment};
3d482025 21use proxmox::api::schema::*;
7eea56ca 22use proxmox::api::cli::*;
5830c205 23use proxmox::api::api;
a6f87283 24use pxar::accessor::{MaybeReady, ReadAt, ReadAtOperation};
ff5d3707 25
fe0e04c6 26use proxmox_backup::tools;
bbf9e7e9 27use proxmox_backup::api2::types::*;
e39974af 28use proxmox_backup::api2::version;
151c6ce2 29use proxmox_backup::client::*;
c443f58b 30use proxmox_backup::pxar::catalog::*;
4d16badf
WB
31use proxmox_backup::backup::{
32 archive_type,
0351f23b 33 decrypt_key,
4d16badf
WB
34 verify_chunk_size,
35 ArchiveType,
8e6e18b7 36 AsyncReadChunk,
4d16badf
WB
37 BackupDir,
38 BackupGroup,
39 BackupManifest,
40 BufferedDynamicReader,
f28d9088 41 CATALOG_NAME,
4d16badf
WB
42 CatalogReader,
43 CatalogWriter,
4d16badf
WB
44 ChunkStream,
45 CryptConfig,
f28d9088 46 CryptMode,
4d16badf
WB
47 DataBlob,
48 DynamicIndexReader,
49 FixedChunkStream,
50 FixedIndexReader,
51 IndexFile,
4d16badf 52 MANIFEST_BLOB_NAME,
4d16badf
WB
53 Shell,
54};
ae0be2dd 55
caea8d61
DM
56mod proxmox_backup_client;
57use proxmox_backup_client::*;
58
a05c0c6f 59const ENV_VAR_PBS_FINGERPRINT: &str = "PBS_FINGERPRINT";
d1c65727 60const ENV_VAR_PBS_PASSWORD: &str = "PBS_PASSWORD";
a05c0c6f 61
33d64b81 62
caea8d61 63pub const REPO_URL_SCHEMA: Schema = StringSchema::new("Repository URL.")
255f378a
DM
64 .format(&BACKUP_REPO_URL)
65 .max_length(256)
66 .schema();
d0a03d40 67
caea8d61 68pub const KEYFILE_SCHEMA: Schema = StringSchema::new(
a47a02ae
DM
69 "Path to encryption key. All data will be encrypted using this key.")
70 .schema();
71
0351f23b
WB
72pub const KEYFD_SCHEMA: Schema = IntegerSchema::new(
73 "Pass an encryption key via an already opened file descriptor.")
74 .minimum(0)
75 .schema();
76
a47a02ae
DM
77const 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
2665cef7
DM
84fn get_default_repository() -> Option<String> {
85 std::env::var("PBS_REPOSITORY").ok()
86}
87
caea8d61 88pub fn extract_repository_from_value(
2665cef7
DM
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
103fn 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
d0a03d40
DM
113fn 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
11377a47 126 let mut data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
d0a03d40
DM
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
feaa1ad3 158 let _ = replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new());
d0a03d40
DM
159}
160
43abba4b 161pub fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
d0a03d40
DM
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
11377a47 176 let data = file_get_json(&path, None).unwrap_or_else(|_| json!({}));
d0a03d40
DM
177
178 if let Some(map) = data.as_object() {
49811347 179 for (repo, _count) in map {
d0a03d40
DM
180 result.push(repo.to_owned());
181 }
182 }
183
184 result
185}
186
d59dbeca
DM
187fn connect(server: &str, userid: &str) -> Result<HttpClient, Error> {
188
a05c0c6f
DM
189 let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
190
d1c65727
DM
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
d59dbeca 198 let options = HttpClientOptions::new()
5030b7ce 199 .prefix(Some("proxmox-backup".to_string()))
d1c65727 200 .password(password)
d59dbeca 201 .interactive(true)
a05c0c6f 202 .fingerprint(fingerprint)
5a74756c 203 .fingerprint_cache(true)
d59dbeca
DM
204 .ticket_cache(true);
205
206 HttpClient::new(server, userid, options)
207}
208
d105176f
DM
209async 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
42af4b8f
DM
226async fn api_datastore_list_snapshots(
227 client: &HttpClient,
228 store: &str,
229 group: Option<BackupGroup>,
f24fc116 230) -> Result<Value, Error> {
42af4b8f
DM
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
f24fc116 242 Ok(result["data"].take())
42af4b8f
DM
243}
244
43abba4b 245pub async fn api_datastore_latest_snapshot(
27c9affb
DM
246 client: &HttpClient,
247 store: &str,
248 group: BackupGroup,
249) -> Result<(String, String, DateTime<Utc>), Error> {
250
f24fc116
DM
251 let list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?;
252 let mut list: Vec<SnapshotListItem> = serde_json::from_value(list)?;
27c9affb
DM
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
e9722f8b 265async fn backup_directory<P: AsRef<Path>>(
cf9271e2 266 client: &BackupWriter,
b957aa81 267 previous_manifest: Option<Arc<BackupManifest>>,
17d6979a 268 dir_path: P,
247cdbce 269 archive_name: &str,
36898ffc 270 chunk_size: Option<usize>,
2eeaacb9 271 device_set: Option<HashSet<u64>>,
219ef0e6 272 verbose: bool,
5b72c9b4 273 skip_lost_and_found: bool,
f1d99e3f 274 catalog: Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
c443f58b 275 exclude_pattern: Vec<MatchEntry>,
6fc053ed 276 entries_max: usize,
3638341a
DM
277 compress: bool,
278 encrypt: bool,
2c3891d1 279) -> Result<BackupStats, Error> {
33d64b81 280
6fc053ed
CE
281 let pxar_stream = PxarBackupStream::open(
282 dir_path.as_ref(),
283 device_set,
284 verbose,
285 skip_lost_and_found,
286 catalog,
189996cf 287 exclude_pattern,
6fc053ed
CE
288 entries_max,
289 )?;
e9722f8b 290 let mut chunk_stream = ChunkStream::new(pxar_stream, chunk_size);
ff3d3100 291
e9722f8b 292 let (mut tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks
5e7a09be 293
c4ff3dce 294 let stream = rx
e9722f8b 295 .map_err(Error::from);
17d6979a 296
c4ff3dce 297 // spawn chunker inside a separate task so that it can run parallel
e9722f8b 298 tokio::spawn(async move {
db0cb9ce
WB
299 while let Some(v) = chunk_stream.next().await {
300 let _ = tx.send(v).await;
301 }
e9722f8b 302 });
17d6979a 303
e9722f8b 304 let stats = client
3638341a 305 .upload_stream(previous_manifest, archive_name, stream, "dynamic", None, compress, encrypt)
e9722f8b 306 .await?;
bcd879cf 307
2c3891d1 308 Ok(stats)
bcd879cf
DM
309}
310
e9722f8b 311async fn backup_image<P: AsRef<Path>>(
cf9271e2 312 client: &BackupWriter,
b957aa81 313 previous_manifest: Option<Arc<BackupManifest>>,
6af905c1
DM
314 image_path: P,
315 archive_name: &str,
316 image_size: u64,
36898ffc 317 chunk_size: Option<usize>,
3638341a
DM
318 compress: bool,
319 encrypt: bool,
1c0472e8 320 _verbose: bool,
2c3891d1 321) -> Result<BackupStats, Error> {
6af905c1 322
6af905c1
DM
323 let path = image_path.as_ref().to_owned();
324
e9722f8b 325 let file = tokio::fs::File::open(path).await?;
6af905c1 326
db0cb9ce 327 let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
6af905c1
DM
328 .map_err(Error::from);
329
36898ffc 330 let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024));
6af905c1 331
e9722f8b 332 let stats = client
3638341a 333 .upload_stream(previous_manifest, archive_name, stream, "fixed", Some(image_size), compress, encrypt)
e9722f8b 334 .await?;
6af905c1 335
2c3891d1 336 Ok(stats)
6af905c1
DM
337}
338
a47a02ae
DM
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.
354async fn list_backup_groups(param: Value) -> Result<Value, Error> {
812c6f87 355
c81b2b7c
DM
356 let output_format = get_output_format(&param);
357
2665cef7 358 let repo = extract_repository_from_value(&param)?;
812c6f87 359
d59dbeca 360 let client = connect(repo.host(), repo.user())?;
812c6f87 361
d0a03d40 362 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
812c6f87 363
8a8a4703 364 let mut result = client.get(&path, None).await?;
812c6f87 365
d0a03d40
DM
366 record_repository(&repo);
367
c81b2b7c
DM
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 };
812c6f87 373
18deda40
DM
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())
c81b2b7c 378 };
812c6f87 379
c81b2b7c
DM
380 let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
381 let item: GroupListItem = serde_json::from_value(record.to_owned())?;
4939255f 382 Ok(tools::format::render_backup_file_list(&item.files))
c81b2b7c 383 };
812c6f87 384
c81b2b7c
DM
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"))
18deda40
DM
389 .column(
390 ColumnConfig::new("last-backup")
391 .renderer(render_last_backup)
392 .header("last snapshot")
393 .right_align(false)
394 )
c81b2b7c
DM
395 .column(ColumnConfig::new("backup-count"))
396 .column(ColumnConfig::new("files").renderer(render_files));
ad20d198 397
c81b2b7c 398 let mut data: Value = result["data"].take();
ad20d198 399
c81b2b7c 400 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_GROUPS;
812c6f87 401
c81b2b7c 402 format_and_print_result_full(&mut data, info, &output_format, &options);
34a816cc 403
812c6f87
DM
404 Ok(Value::Null)
405}
406
a47a02ae
DM
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.
427async fn list_snapshots(param: Value) -> Result<Value, Error> {
184f17af 428
2665cef7 429 let repo = extract_repository_from_value(&param)?;
184f17af 430
c2043614 431 let output_format = get_output_format(&param);
34a816cc 432
d59dbeca 433 let client = connect(repo.host(), repo.user())?;
184f17af 434
d6d3b353
DM
435 let group: Option<BackupGroup> = if let Some(path) = param["group"].as_str() {
436 Some(path.parse()?)
42af4b8f
DM
437 } else {
438 None
439 };
184f17af 440
f24fc116 441 let mut data = api_datastore_list_snapshots(&client, repo.store(), group).await?;
184f17af 442
d0a03d40
DM
443 record_repository(&repo);
444
f24fc116
DM
445 let render_snapshot_path = |_v: &Value, record: &Value| -> Result<String, Error> {
446 let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
af9d4afc 447 let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
f24fc116
DM
448 Ok(snapshot.relative_path().to_str().unwrap().to_owned())
449 };
184f17af 450
f24fc116
DM
451 let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
452 let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
1c090810
DC
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[..]))
f24fc116
DM
458 };
459
c2043614 460 let options = default_table_format_options()
f24fc116
DM
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);
184f17af
DM
472
473 Ok(Value::Null)
474}
475
a47a02ae
DM
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.
491async fn forget_snapshots(param: Value) -> Result<Value, Error> {
6f62c924 492
2665cef7 493 let repo = extract_repository_from_value(&param)?;
6f62c924
DM
494
495 let path = tools::required_string_param(&param, "snapshot")?;
a67f7d0a 496 let snapshot: BackupDir = path.parse()?;
6f62c924 497
d59dbeca 498 let mut client = connect(repo.host(), repo.user())?;
6f62c924 499
9e391bb7 500 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
6f62c924 501
8a8a4703
DM
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?;
6f62c924 507
d0a03d40
DM
508 record_repository(&repo);
509
6f62c924
DM
510 Ok(result)
511}
512
a47a02ae
DM
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.
524async fn api_login(param: Value) -> Result<Value, Error> {
e240d8be
DM
525
526 let repo = extract_repository_from_value(&param)?;
527
d59dbeca 528 let client = connect(repo.host(), repo.user())?;
8a8a4703 529 client.login().await?;
e240d8be
DM
530
531 record_repository(&repo);
532
533 Ok(Value::Null)
534}
535
a47a02ae
DM
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).
547fn api_logout(param: Value) -> Result<Value, Error> {
e240d8be
DM
548
549 let repo = extract_repository_from_value(&param)?;
550
5030b7ce 551 delete_ticket_info("proxmox-backup", repo.host(), repo.user())?;
e240d8be
DM
552
553 Ok(Value::Null)
554}
555
e39974af
TL
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
571async 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
9049a8cf 606
a47a02ae
DM
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.
626async fn list_snapshot_files(param: Value) -> Result<Value, Error> {
52c171e4
DM
627
628 let repo = extract_repository_from_value(&param)?;
629
630 let path = tools::required_string_param(&param, "snapshot")?;
a67f7d0a 631 let snapshot: BackupDir = path.parse()?;
52c171e4 632
c2043614 633 let output_format = get_output_format(&param);
52c171e4 634
d59dbeca 635 let client = connect(repo.host(), repo.user())?;
52c171e4
DM
636
637 let path = format!("api2/json/admin/datastore/{}/files", repo.store());
638
8a8a4703
DM
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?;
52c171e4
DM
644
645 record_repository(&repo);
646
ea5f547f 647 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_SNAPSHOT_FILES;
52c171e4 648
ea5f547f
DM
649 let mut data: Value = result["data"].take();
650
c2043614 651 let options = default_table_format_options();
ea5f547f
DM
652
653 format_and_print_result_full(&mut data, info, &output_format, &options);
52c171e4
DM
654
655 Ok(Value::Null)
656}
657
a47a02ae 658#[api(
94913f35 659 input: {
a47a02ae
DM
660 properties: {
661 repository: {
662 schema: REPO_URL_SCHEMA,
663 optional: true,
664 },
94913f35
DM
665 "output-format": {
666 schema: OUTPUT_FORMAT,
667 optional: true,
668 },
669 },
670 },
a47a02ae
DM
671)]
672/// Start garbage collection for a specific repository.
673async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
8cc0d6af 674
2665cef7 675 let repo = extract_repository_from_value(&param)?;
c2043614
DM
676
677 let output_format = get_output_format(&param);
8cc0d6af 678
d59dbeca 679 let mut client = connect(repo.host(), repo.user())?;
8cc0d6af 680
d0a03d40 681 let path = format!("api2/json/admin/datastore/{}/gc", repo.store());
8cc0d6af 682
8a8a4703 683 let result = client.post(&path, None).await?;
8cc0d6af 684
8a8a4703 685 record_repository(&repo);
d0a03d40 686
8a8a4703 687 view_task_result(client, result, &output_format).await?;
e5f7def4 688
e5f7def4 689 Ok(Value::Null)
8cc0d6af 690}
33d64b81 691
bf6e3217 692fn spawn_catalog_upload(
3bad3e6e 693 client: Arc<BackupWriter>,
3638341a 694 encrypt: bool,
bf6e3217
DM
695) -> Result<
696 (
f1d99e3f 697 Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
bf6e3217
DM
698 tokio::sync::oneshot::Receiver<Result<BackupStats, Error>>
699 ), Error>
700{
f1d99e3f
DM
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);
bf6e3217
DM
703 let catalog_chunk_size = 512*1024;
704 let catalog_chunk_stream = ChunkStream::new(catalog_stream, Some(catalog_chunk_size));
705
f1d99e3f 706 let catalog = Arc::new(Mutex::new(CatalogWriter::new(crate::tools::StdChannelWriter::new(catalog_tx))?));
bf6e3217
DM
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
3638341a 712 .upload_stream(None, CATALOG_NAME, catalog_chunk_stream, "dynamic", None, true, encrypt)
bf6e3217
DM
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
0351f23b 726fn keyfile_parameters(param: &Value) -> Result<(Option<Vec<u8>>, CryptMode), Error> {
f28d9088
WB
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
0351f23b
WB
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
f28d9088
WB
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
0351f23b
WB
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) {
96ee8577 766 // no parameters:
0351f23b 767 (None, None) => match key::read_optional_default_encryption_key()? {
05389a01
WB
768 Some(key) => (Some(key), CryptMode::Encrypt),
769 None => (None, CryptMode::None),
770 },
96ee8577 771
f28d9088
WB
772 // just --crypt-mode=none
773 (None, Some(CryptMode::None)) => (None, CryptMode::None),
96ee8577 774
f28d9088 775 // just --crypt-mode other than none
0351f23b 776 (None, Some(crypt_mode)) => match key::read_optional_default_encryption_key()? {
f28d9088 777 None => bail!("--crypt-mode without --keyfile and no default key file available"),
0351f23b 778 Some(key) => (Some(key), crypt_mode),
96ee8577
WB
779 }
780
781 // just --keyfile
0351f23b 782 (Some(key), None) => (Some(key), CryptMode::Encrypt),
96ee8577 783
f28d9088
WB
784 // --keyfile and --crypt-mode=none
785 (Some(_), Some(CryptMode::None)) => {
0351f23b 786 bail!("--keyfile/--keyfd and --crypt-mode=none are mutually exclusive");
96ee8577
WB
787 }
788
f28d9088 789 // --keyfile and --crypt-mode other than none
0351f23b 790 (Some(key), Some(crypt_mode)) => (Some(key), crypt_mode),
96ee8577
WB
791 })
792}
793
a47a02ae
DM
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 },
0351f23b
WB
820 "keyfd": {
821 schema: KEYFD_SCHEMA,
822 optional: true,
823 },
24be37e3
WB
824 "crypt-mode": {
825 type: CryptMode,
96ee8577
WB
826 optional: true,
827 },
a47a02ae
DM
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 },
189996cf
CE
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 },
6fc053ed
CE
858 "entries-max": {
859 type: Integer,
860 description: "Max number of entries to hold in memory.",
861 optional: true,
c443f58b 862 default: proxmox_backup::pxar::ENCODER_MAX_ENTRIES as isize,
6fc053ed 863 },
e02c3d46
DM
864 "verbose": {
865 type: Boolean,
866 description: "Verbose output.",
867 optional: true,
868 },
a47a02ae
DM
869 }
870 }
871)]
872/// Create (host) backup.
873async fn create_backup(
6049b71f
DM
874 param: Value,
875 _info: &ApiMethod,
dd5495d6 876 _rpcenv: &mut dyn RpcEnvironment,
6049b71f 877) -> Result<Value, Error> {
ff5d3707 878
2665cef7 879 let repo = extract_repository_from_value(&param)?;
ae0be2dd
DM
880
881 let backupspec_list = tools::required_array_param(&param, "backupspec")?;
a914a774 882
eed6db39
DM
883 let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
884
5b72c9b4
DM
885 let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false);
886
219ef0e6
DM
887 let verbose = param["verbose"].as_bool().unwrap_or(false);
888
ca5d0b61
DM
889 let backup_time_opt = param["backup-time"].as_i64();
890
36898ffc 891 let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize);
2d9d143a 892
247cdbce
DM
893 if let Some(size) = chunk_size_opt {
894 verify_chunk_size(size)?;
2d9d143a
DM
895 }
896
0351f23b 897 let (keydata, crypt_mode) = keyfile_parameters(&param)?;
6d0983db 898
f69adc81 899 let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename());
fba30411 900
bbf9e7e9 901 let backup_type = param["backup-type"].as_str().unwrap_or("host");
ca5d0b61 902
2eeaacb9
DM
903 let include_dev = param["include-dev"].as_array();
904
c443f58b
WB
905 let entries_max = param["entries-max"].as_u64()
906 .unwrap_or(proxmox_backup::pxar::ENCODER_MAX_ENTRIES as u64);
6fc053ed 907
189996cf 908 let empty = Vec::new();
c443f58b
WB
909 let exclude_args = param["exclude"].as_array().unwrap_or(&empty);
910
239e49f9 911 let mut pattern_list = Vec::with_capacity(exclude_args.len());
c443f58b
WB
912 for entry in exclude_args {
913 let entry = entry.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
239e49f9 914 pattern_list.push(
c443f58b
WB
915 MatchEntry::parse_pattern(entry, PatternFlag::PATH_NAME, MatchType::Exclude)
916 .map_err(|err| format_err!("invalid exclude pattern entry: {}", err))?
917 );
189996cf
CE
918 }
919
2eeaacb9
DM
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
ae0be2dd 937 let mut upload_list = vec![];
a914a774 938
ae0be2dd 939 for backupspec in backupspec_list {
7cc3473a
DM
940 let spec = parse_backup_specification(backupspec.as_str().unwrap())?;
941 let filename = &spec.config_string;
942 let target = &spec.archive_name;
bcd879cf 943
eb1804c5
DM
944 use std::os::unix::fs::FileTypeExt;
945
3fa71727
CE
946 let metadata = std::fs::metadata(filename)
947 .map_err(|err| format_err!("unable to access '{}' - {}", filename, err))?;
eb1804c5 948 let file_type = metadata.file_type();
23bb8780 949
7cc3473a
DM
950 match spec.spec_type {
951 BackupSpecificationType::PXAR => {
ec8a9bb9
DM
952 if !file_type.is_dir() {
953 bail!("got unexpected file type (expected directory)");
954 }
7cc3473a 955 upload_list.push((BackupSpecificationType::PXAR, filename.to_owned(), format!("{}.didx", target), 0));
ec8a9bb9 956 }
7cc3473a 957 BackupSpecificationType::IMAGE => {
ec8a9bb9
DM
958 if !(file_type.is_file() || file_type.is_block_device()) {
959 bail!("got unexpected file type (expected file or block device)");
960 }
eb1804c5 961
e18a6c9e 962 let size = image_size(&PathBuf::from(filename))?;
23bb8780 963
ec8a9bb9 964 if size == 0 { bail!("got zero-sized file '{}'", filename); }
ae0be2dd 965
7cc3473a 966 upload_list.push((BackupSpecificationType::IMAGE, filename.to_owned(), format!("{}.fidx", target), size));
ec8a9bb9 967 }
7cc3473a 968 BackupSpecificationType::CONFIG => {
ec8a9bb9
DM
969 if !file_type.is_file() {
970 bail!("got unexpected file type (expected regular file)");
971 }
7cc3473a 972 upload_list.push((BackupSpecificationType::CONFIG, filename.to_owned(), format!("{}.blob", target), metadata.len()));
ec8a9bb9 973 }
7cc3473a 974 BackupSpecificationType::LOGFILE => {
79679c2d
DM
975 if !file_type.is_file() {
976 bail!("got unexpected file type (expected regular file)");
977 }
7cc3473a 978 upload_list.push((BackupSpecificationType::LOGFILE, filename.to_owned(), format!("{}.blob", target), metadata.len()));
ec8a9bb9 979 }
ae0be2dd
DM
980 }
981 }
982
11377a47 983 let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0);
ae0be2dd 984
d59dbeca 985 let client = connect(repo.host(), repo.user())?;
d0a03d40
DM
986 record_repository(&repo);
987
ca5d0b61
DM
988 println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time));
989
f69adc81 990 println!("Client name: {}", proxmox::tools::nodename());
ca5d0b61
DM
991
992 let start_time = Local::now();
993
7a6cfbd9 994 println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
51144821 995
0351f23b 996 let (crypt_config, rsa_encrypted_key) = match keydata {
bb823140 997 None => (None, None),
0351f23b
WB
998 Some(key) => {
999 let (key, created) = decrypt_key(&key, &key::get_encryption_key_password)?;
bb823140
DM
1000
1001 let crypt_config = CryptConfig::new(key)?;
1002
05389a01
WB
1003 match key::find_master_pubkey()? {
1004 Some(ref path) if path.exists() => {
1005 let pem_data = file_get_contents(path)?;
1006 let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
1007 let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?;
1008 (Some(Arc::new(crypt_config)), Some(enc_key))
1009 }
1010 _ => (Some(Arc::new(crypt_config)), None),
bb823140 1011 }
6d0983db
DM
1012 }
1013 };
f98ac774 1014
8a8a4703
DM
1015 let client = BackupWriter::start(
1016 client,
b957aa81 1017 crypt_config.clone(),
8a8a4703
DM
1018 repo.store(),
1019 backup_type,
1020 &backup_id,
1021 backup_time,
1022 verbose,
1023 ).await?;
1024
b957aa81
DM
1025 let previous_manifest = if let Ok(previous_manifest) = client.download_previous_manifest().await {
1026 Some(Arc::new(previous_manifest))
1027 } else {
1028 None
1029 };
1030
8a8a4703
DM
1031 let snapshot = BackupDir::new(backup_type, backup_id, backup_time.timestamp());
1032 let mut manifest = BackupManifest::new(snapshot);
1033
5d85847f
DC
1034 let mut catalog = None;
1035 let mut catalog_result_tx = None;
8a8a4703
DM
1036
1037 for (backup_type, filename, target, size) in upload_list {
1038 match backup_type {
7cc3473a 1039 BackupSpecificationType::CONFIG => {
5b32820e 1040 println!("Upload config file '{}' to '{}' as {}", filename, repo, target);
8a8a4703 1041 let stats = client
3638341a 1042 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
8a8a4703 1043 .await?;
f28d9088 1044 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
8a8a4703 1045 }
7cc3473a 1046 BackupSpecificationType::LOGFILE => { // fixme: remove - not needed anymore ?
5b32820e 1047 println!("Upload log file '{}' to '{}' as {}", filename, repo, target);
8a8a4703 1048 let stats = client
3638341a 1049 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
8a8a4703 1050 .await?;
f28d9088 1051 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
8a8a4703 1052 }
7cc3473a 1053 BackupSpecificationType::PXAR => {
5d85847f
DC
1054 // start catalog upload on first use
1055 if catalog.is_none() {
3638341a 1056 let (cat, res) = spawn_catalog_upload(client.clone(), crypt_mode == CryptMode::Encrypt)?;
5d85847f
DC
1057 catalog = Some(cat);
1058 catalog_result_tx = Some(res);
1059 }
1060 let catalog = catalog.as_ref().unwrap();
1061
5b32820e 1062 println!("Upload directory '{}' to '{}' as {}", filename, repo, target);
8a8a4703
DM
1063 catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?;
1064 let stats = backup_directory(
1065 &client,
b957aa81 1066 previous_manifest.clone(),
8a8a4703
DM
1067 &filename,
1068 &target,
1069 chunk_size_opt,
1070 devices.clone(),
1071 verbose,
1072 skip_lost_and_found,
8a8a4703 1073 catalog.clone(),
239e49f9 1074 pattern_list.clone(),
6fc053ed 1075 entries_max as usize,
3638341a
DM
1076 true,
1077 crypt_mode == CryptMode::Encrypt,
8a8a4703 1078 ).await?;
f28d9088 1079 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
8a8a4703
DM
1080 catalog.lock().unwrap().end_directory()?;
1081 }
7cc3473a 1082 BackupSpecificationType::IMAGE => {
8a8a4703
DM
1083 println!("Upload image '{}' to '{:?}' as {}", filename, repo, target);
1084 let stats = backup_image(
1085 &client,
b957aa81
DM
1086 previous_manifest.clone(),
1087 &filename,
8a8a4703
DM
1088 &target,
1089 size,
1090 chunk_size_opt,
3638341a
DM
1091 true,
1092 crypt_mode == CryptMode::Encrypt,
8a8a4703 1093 verbose,
8a8a4703 1094 ).await?;
f28d9088 1095 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
6af905c1
DM
1096 }
1097 }
8a8a4703 1098 }
4818c8b6 1099
8a8a4703 1100 // finalize and upload catalog
5d85847f 1101 if let Some(catalog) = catalog {
8a8a4703
DM
1102 let mutex = Arc::try_unwrap(catalog)
1103 .map_err(|_| format_err!("unable to get catalog (still used)"))?;
1104 let mut catalog = mutex.into_inner().unwrap();
bf6e3217 1105
8a8a4703 1106 catalog.finish()?;
2761d6a4 1107
8a8a4703 1108 drop(catalog); // close upload stream
2761d6a4 1109
5d85847f
DC
1110 if let Some(catalog_result_rx) = catalog_result_tx {
1111 let stats = catalog_result_rx.await??;
f28d9088 1112 manifest.add_file(CATALOG_NAME.to_owned(), stats.size, stats.csum, crypt_mode)?;
5d85847f 1113 }
8a8a4703 1114 }
2761d6a4 1115
8a8a4703
DM
1116 if let Some(rsa_encrypted_key) = rsa_encrypted_key {
1117 let target = "rsa-encrypted.key";
1118 println!("Upload RSA encoded key to '{:?}' as {}", repo, target);
1119 let stats = client
3638341a 1120 .upload_blob_from_data(rsa_encrypted_key, target, false, false)
8a8a4703 1121 .await?;
f28d9088 1122 manifest.add_file(format!("{}.blob", target), stats.size, stats.csum, crypt_mode)?;
8a8a4703
DM
1123
1124 // openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t
1125 /*
1126 let mut buffer2 = vec![0u8; rsa.size() as usize];
1127 let pem_data = file_get_contents("master-private.pem")?;
1128 let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?;
1129 let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?;
1130 println!("TEST {} {:?}", len, buffer2);
1131 */
1132 }
9f46c7de 1133
8a8a4703 1134 // create manifest (index.json)
3638341a 1135 // manifests are never encrypted, but include a signature
dfa517ad 1136 let manifest = manifest.to_string(crypt_config.as_ref().map(Arc::as_ref))
b53f6379 1137 .map_err(|err| format_err!("unable to format manifest - {}", err))?;
3638341a 1138
b53f6379 1139
9688f6de 1140 if verbose { println!("Upload index.json to '{}'", repo) };
8a8a4703 1141 client
b53f6379 1142 .upload_blob_from_data(manifest.into_bytes(), MANIFEST_BLOB_NAME, true, false)
8a8a4703 1143 .await?;
2c3891d1 1144
8a8a4703 1145 client.finish().await?;
c4ff3dce 1146
8a8a4703
DM
1147 let end_time = Local::now();
1148 let elapsed = end_time.signed_duration_since(start_time);
1149 println!("Duration: {}", elapsed);
3ec3ec3f 1150
8a8a4703 1151 println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
3d5c11e5 1152
8a8a4703 1153 Ok(Value::Null)
f98ea63d
DM
1154}
1155
d0a03d40 1156fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
f98ea63d
DM
1157
1158 let mut result = vec![];
1159
1160 let data: Vec<&str> = arg.splitn(2, ':').collect();
1161
bff11030 1162 if data.len() != 2 {
8968258b
DM
1163 result.push(String::from("root.pxar:/"));
1164 result.push(String::from("etc.pxar:/etc"));
bff11030
DM
1165 return result;
1166 }
f98ea63d 1167
496a6784 1168 let files = tools::complete_file_name(data[1], param);
f98ea63d
DM
1169
1170 for file in files {
1171 result.push(format!("{}:{}", data[0], file));
1172 }
1173
1174 result
ff5d3707 1175}
1176
8e6e18b7 1177async fn dump_image<W: Write>(
88892ea8
DM
1178 client: Arc<BackupReader>,
1179 crypt_config: Option<Arc<CryptConfig>>,
1180 index: FixedIndexReader,
1181 mut writer: W,
fd04ca7a 1182 verbose: bool,
88892ea8
DM
1183) -> Result<(), Error> {
1184
1185 let most_used = index.find_most_used_chunks(8);
1186
e9764238 1187 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
88892ea8
DM
1188
1189 // Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy
1190 // and thus slows down reading. Instead, directly use RemoteChunkReader
fd04ca7a
DM
1191 let mut per = 0;
1192 let mut bytes = 0;
1193 let start_time = std::time::Instant::now();
1194
88892ea8
DM
1195 for pos in 0..index.index_count() {
1196 let digest = index.index_digest(pos).unwrap();
8e6e18b7 1197 let raw_data = chunk_reader.read_chunk(&digest).await?;
88892ea8 1198 writer.write_all(&raw_data)?;
fd04ca7a
DM
1199 bytes += raw_data.len();
1200 if verbose {
1201 let next_per = ((pos+1)*100)/index.index_count();
1202 if per != next_per {
1203 eprintln!("progress {}% (read {} bytes, duration {} sec)",
1204 next_per, bytes, start_time.elapsed().as_secs());
1205 per = next_per;
1206 }
1207 }
88892ea8
DM
1208 }
1209
fd04ca7a
DM
1210 let end_time = std::time::Instant::now();
1211 let elapsed = end_time.duration_since(start_time);
1212 eprintln!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)",
1213 bytes,
1214 elapsed.as_secs_f64(),
1215 bytes as f64/(1024.0*1024.0*elapsed.as_secs_f64())
1216 );
1217
1218
88892ea8
DM
1219 Ok(())
1220}
1221
dc155e9b 1222fn parse_archive_type(name: &str) -> (String, ArchiveType) {
2d32fe2c
TL
1223 if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
1224 (name.into(), archive_type(name).unwrap())
1225 } else if name.ends_with(".pxar") {
dc155e9b
TL
1226 (format!("{}.didx", name), ArchiveType::DynamicIndex)
1227 } else if name.ends_with(".img") {
1228 (format!("{}.fidx", name), ArchiveType::FixedIndex)
1229 } else {
1230 (format!("{}.blob", name), ArchiveType::Blob)
1231 }
1232}
1233
a47a02ae
DM
1234#[api(
1235 input: {
1236 properties: {
1237 repository: {
1238 schema: REPO_URL_SCHEMA,
1239 optional: true,
1240 },
1241 snapshot: {
1242 type: String,
1243 description: "Group/Snapshot path.",
1244 },
1245 "archive-name": {
1246 description: "Backup archive name.",
1247 type: String,
1248 },
1249 target: {
1250 type: String,
90c815bf 1251 description: r###"Target directory path. Use '-' to write to standard output.
8a8a4703 1252
5eee6d89 1253We do not extraxt '.pxar' archives when writing to standard output.
8a8a4703 1254
a47a02ae
DM
1255"###
1256 },
1257 "allow-existing-dirs": {
1258 type: Boolean,
1259 description: "Do not fail if directories already exists.",
1260 optional: true,
1261 },
1262 keyfile: {
1263 schema: KEYFILE_SCHEMA,
1264 optional: true,
1265 },
0351f23b
WB
1266 "keyfd": {
1267 schema: KEYFD_SCHEMA,
1268 optional: true,
1269 },
24be37e3
WB
1270 "crypt-mode": {
1271 type: CryptMode,
96ee8577
WB
1272 optional: true,
1273 },
a47a02ae
DM
1274 }
1275 }
1276)]
1277/// Restore backup repository.
1278async fn restore(param: Value) -> Result<Value, Error> {
2665cef7 1279 let repo = extract_repository_from_value(&param)?;
9f912493 1280
86eda3eb
DM
1281 let verbose = param["verbose"].as_bool().unwrap_or(false);
1282
46d5aa0a
DM
1283 let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
1284
d5c34d98
DM
1285 let archive_name = tools::required_string_param(&param, "archive-name")?;
1286
d59dbeca 1287 let client = connect(repo.host(), repo.user())?;
d0a03d40 1288
d0a03d40 1289 record_repository(&repo);
d5c34d98 1290
9f912493 1291 let path = tools::required_string_param(&param, "snapshot")?;
9f912493 1292
86eda3eb 1293 let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
d6d3b353 1294 let group: BackupGroup = path.parse()?;
27c9affb 1295 api_datastore_latest_snapshot(&client, repo.store(), group).await?
d5c34d98 1296 } else {
a67f7d0a 1297 let snapshot: BackupDir = path.parse()?;
86eda3eb
DM
1298 (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
1299 };
9f912493 1300
d5c34d98 1301 let target = tools::required_string_param(&param, "target")?;
bf125261 1302 let target = if target == "-" { None } else { Some(target) };
2ae7d196 1303
0351f23b 1304 let (keydata, _crypt_mode) = keyfile_parameters(&param)?;
2ae7d196 1305
0351f23b 1306 let crypt_config = match keydata {
86eda3eb 1307 None => None,
0351f23b
WB
1308 Some(key) => {
1309 let (key, _) = decrypt_key(&key, &key::get_encryption_key_password)?;
86eda3eb
DM
1310 Some(Arc::new(CryptConfig::new(key)?))
1311 }
1312 };
d5c34d98 1313
296c50ba
DM
1314 let client = BackupReader::start(
1315 client,
1316 crypt_config.clone(),
1317 repo.store(),
1318 &backup_type,
1319 &backup_id,
1320 backup_time,
1321 true,
1322 ).await?;
86eda3eb 1323
2107a5ae 1324 let (manifest, backup_index_data) = client.download_manifest().await?;
02fcf372 1325
dc155e9b
TL
1326 let (archive_name, archive_type) = parse_archive_type(archive_name);
1327
1328 if archive_name == MANIFEST_BLOB_NAME {
02fcf372 1329 if let Some(target) = target {
2107a5ae 1330 replace_file(target, &backup_index_data, CreateOptions::new())?;
02fcf372
DM
1331 } else {
1332 let stdout = std::io::stdout();
1333 let mut writer = stdout.lock();
2107a5ae 1334 writer.write_all(&backup_index_data)
02fcf372
DM
1335 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1336 }
1337
dc155e9b 1338 } else if archive_type == ArchiveType::Blob {
d2267b11 1339
dc155e9b 1340 let mut reader = client.download_blob(&manifest, &archive_name).await?;
f8100e96 1341
bf125261 1342 if let Some(target) = target {
0d986280
DM
1343 let mut writer = std::fs::OpenOptions::new()
1344 .write(true)
1345 .create(true)
1346 .create_new(true)
1347 .open(target)
1348 .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?;
1349 std::io::copy(&mut reader, &mut writer)?;
bf125261
DM
1350 } else {
1351 let stdout = std::io::stdout();
1352 let mut writer = stdout.lock();
0d986280 1353 std::io::copy(&mut reader, &mut writer)
bf125261
DM
1354 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1355 }
f8100e96 1356
dc155e9b 1357 } else if archive_type == ArchiveType::DynamicIndex {
86eda3eb 1358
dc155e9b 1359 let index = client.download_dynamic_index(&manifest, &archive_name).await?;
df65bd3d 1360
f4bf7dfc
DM
1361 let most_used = index.find_most_used_chunks(8);
1362
1363 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
1364
afb4cd28 1365 let mut reader = BufferedDynamicReader::new(index, chunk_reader);
86eda3eb 1366
bf125261 1367 if let Some(target) = target {
c443f58b
WB
1368 proxmox_backup::pxar::extract_archive(
1369 pxar::decoder::Decoder::from_std(reader)?,
1370 Path::new(target),
1371 &[],
5444fa94 1372 proxmox_backup::pxar::Flags::DEFAULT,
c443f58b
WB
1373 allow_existing_dirs,
1374 |path| {
1375 if verbose {
1376 println!("{:?}", path);
1377 }
1378 },
1379 )
1380 .map_err(|err| format_err!("error extracting archive - {}", err))?;
bf125261 1381 } else {
88892ea8
DM
1382 let mut writer = std::fs::OpenOptions::new()
1383 .write(true)
1384 .open("/dev/stdout")
1385 .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?;
afb4cd28 1386
bf125261
DM
1387 std::io::copy(&mut reader, &mut writer)
1388 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
1389 }
dc155e9b 1390 } else if archive_type == ArchiveType::FixedIndex {
afb4cd28 1391
dc155e9b 1392 let index = client.download_fixed_index(&manifest, &archive_name).await?;
df65bd3d 1393
88892ea8
DM
1394 let mut writer = if let Some(target) = target {
1395 std::fs::OpenOptions::new()
bf125261
DM
1396 .write(true)
1397 .create(true)
1398 .create_new(true)
1399 .open(target)
88892ea8 1400 .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?
bf125261 1401 } else {
88892ea8
DM
1402 std::fs::OpenOptions::new()
1403 .write(true)
1404 .open("/dev/stdout")
1405 .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?
1406 };
afb4cd28 1407
8e6e18b7 1408 dump_image(client.clone(), crypt_config.clone(), index, &mut writer, verbose).await?;
3031e44c 1409 }
fef44d4f
DM
1410
1411 Ok(Value::Null)
45db6f89
DM
1412}
1413
a47a02ae
DM
1414#[api(
1415 input: {
1416 properties: {
1417 repository: {
1418 schema: REPO_URL_SCHEMA,
1419 optional: true,
1420 },
1421 snapshot: {
1422 type: String,
1423 description: "Group/Snapshot path.",
1424 },
1425 logfile: {
1426 type: String,
1427 description: "The path to the log file you want to upload.",
1428 },
1429 keyfile: {
1430 schema: KEYFILE_SCHEMA,
1431 optional: true,
1432 },
0351f23b
WB
1433 "keyfd": {
1434 schema: KEYFD_SCHEMA,
1435 optional: true,
1436 },
24be37e3
WB
1437 "crypt-mode": {
1438 type: CryptMode,
96ee8577
WB
1439 optional: true,
1440 },
a47a02ae
DM
1441 }
1442 }
1443)]
1444/// Upload backup log file.
1445async fn upload_log(param: Value) -> Result<Value, Error> {
ec34f7eb
DM
1446
1447 let logfile = tools::required_string_param(&param, "logfile")?;
1448 let repo = extract_repository_from_value(&param)?;
1449
1450 let snapshot = tools::required_string_param(&param, "snapshot")?;
a67f7d0a 1451 let snapshot: BackupDir = snapshot.parse()?;
ec34f7eb 1452
d59dbeca 1453 let mut client = connect(repo.host(), repo.user())?;
ec34f7eb 1454
0351f23b 1455 let (keydata, crypt_mode) = keyfile_parameters(&param)?;
ec34f7eb 1456
0351f23b 1457 let crypt_config = match keydata {
ec34f7eb 1458 None => None,
0351f23b
WB
1459 Some(key) => {
1460 let (key, _created) = decrypt_key(&key, &key::get_encryption_key_password)?;
ec34f7eb 1461 let crypt_config = CryptConfig::new(key)?;
9025312a 1462 Some(Arc::new(crypt_config))
ec34f7eb
DM
1463 }
1464 };
1465
e18a6c9e 1466 let data = file_get_contents(logfile)?;
ec34f7eb 1467
3638341a 1468 // fixme: howto sign log?
f28d9088 1469 let blob = match crypt_mode {
3638341a
DM
1470 CryptMode::None | CryptMode::SignOnly => DataBlob::encode(&data, None, true)?,
1471 CryptMode::Encrypt => DataBlob::encode(&data, crypt_config.as_ref().map(Arc::as_ref), true)?,
f28d9088 1472 };
ec34f7eb
DM
1473
1474 let raw_data = blob.into_inner();
1475
1476 let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store());
1477
1478 let args = json!({
1479 "backup-type": snapshot.group().backup_type(),
1480 "backup-id": snapshot.group().backup_id(),
1481 "backup-time": snapshot.backup_time().timestamp(),
1482 });
1483
1484 let body = hyper::Body::from(raw_data);
1485
8a8a4703 1486 client.upload("application/octet-stream", body, &path, Some(args)).await
ec34f7eb
DM
1487}
1488
032d3ad8
DM
1489const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
1490 &ApiHandler::Async(&prune),
1491 &ObjectSchema::new(
1492 "Prune a backup repository.",
1493 &proxmox_backup::add_common_prune_prameters!([
1494 ("dry-run", true, &BooleanSchema::new(
1495 "Just show what prune would do, but do not delete anything.")
1496 .schema()),
1497 ("group", false, &StringSchema::new("Backup group.").schema()),
1498 ], [
1499 ("output-format", true, &OUTPUT_FORMAT),
c48aa39f
DM
1500 (
1501 "quiet",
1502 true,
1503 &BooleanSchema::new("Minimal output - only show removals.")
1504 .schema()
1505 ),
032d3ad8
DM
1506 ("repository", true, &REPO_URL_SCHEMA),
1507 ])
1508 )
1509);
1510
1511fn prune<'a>(
1512 param: Value,
1513 _info: &ApiMethod,
1514 _rpcenv: &'a mut dyn RpcEnvironment,
1515) -> proxmox::api::ApiFuture<'a> {
1516 async move {
1517 prune_async(param).await
1518 }.boxed()
1519}
83b7db02 1520
032d3ad8 1521async fn prune_async(mut param: Value) -> Result<Value, Error> {
2665cef7 1522 let repo = extract_repository_from_value(&param)?;
83b7db02 1523
d59dbeca 1524 let mut client = connect(repo.host(), repo.user())?;
83b7db02 1525
d0a03d40 1526 let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
83b7db02 1527
9fdc3ef4 1528 let group = tools::required_string_param(&param, "group")?;
d6d3b353 1529 let group: BackupGroup = group.parse()?;
c2043614
DM
1530
1531 let output_format = get_output_format(&param);
9fdc3ef4 1532
c48aa39f
DM
1533 let quiet = param["quiet"].as_bool().unwrap_or(false);
1534
ea7a7ef2
DM
1535 param.as_object_mut().unwrap().remove("repository");
1536 param.as_object_mut().unwrap().remove("group");
163e9bbe 1537 param.as_object_mut().unwrap().remove("output-format");
c48aa39f 1538 param.as_object_mut().unwrap().remove("quiet");
ea7a7ef2
DM
1539
1540 param["backup-type"] = group.backup_type().into();
1541 param["backup-id"] = group.backup_id().into();
83b7db02 1542
db1e061d 1543 let mut result = client.post(&path, Some(param)).await?;
74fa81b8 1544
87c42375 1545 record_repository(&repo);
3b03abfe 1546
db1e061d
DM
1547 let render_snapshot_path = |_v: &Value, record: &Value| -> Result<String, Error> {
1548 let item: PruneListItem = serde_json::from_value(record.to_owned())?;
1549 let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
1550 Ok(snapshot.relative_path().to_str().unwrap().to_owned())
1551 };
1552
c48aa39f
DM
1553 let render_prune_action = |v: &Value, _record: &Value| -> Result<String, Error> {
1554 Ok(match v.as_bool() {
1555 Some(true) => "keep",
1556 Some(false) => "remove",
1557 None => "unknown",
1558 }.to_string())
1559 };
1560
db1e061d
DM
1561 let options = default_table_format_options()
1562 .sortby("backup-type", false)
1563 .sortby("backup-id", false)
1564 .sortby("backup-time", false)
1565 .column(ColumnConfig::new("backup-id").renderer(render_snapshot_path).header("snapshot"))
74f7240b 1566 .column(ColumnConfig::new("backup-time").renderer(tools::format::render_epoch).header("date"))
c48aa39f 1567 .column(ColumnConfig::new("keep").renderer(render_prune_action).header("action"))
db1e061d
DM
1568 ;
1569
1570 let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_PRUNE;
1571
1572 let mut data = result["data"].take();
1573
c48aa39f
DM
1574 if quiet {
1575 let list: Vec<Value> = data.as_array().unwrap().iter().filter(|item| {
1576 item["keep"].as_bool() == Some(false)
1577 }).map(|v| v.clone()).collect();
1578 data = list.into();
1579 }
1580
db1e061d 1581 format_and_print_result_full(&mut data, info, &output_format, &options);
d0a03d40 1582
43a406fd 1583 Ok(Value::Null)
83b7db02
DM
1584}
1585
a47a02ae
DM
1586#[api(
1587 input: {
1588 properties: {
1589 repository: {
1590 schema: REPO_URL_SCHEMA,
1591 optional: true,
1592 },
1593 "output-format": {
1594 schema: OUTPUT_FORMAT,
1595 optional: true,
1596 },
1597 }
1598 }
1599)]
1600/// Get repository status.
1601async fn status(param: Value) -> Result<Value, Error> {
34a816cc
DM
1602
1603 let repo = extract_repository_from_value(&param)?;
1604
c2043614 1605 let output_format = get_output_format(&param);
34a816cc 1606
d59dbeca 1607 let client = connect(repo.host(), repo.user())?;
34a816cc
DM
1608
1609 let path = format!("api2/json/admin/datastore/{}/status", repo.store());
1610
1dc117bb 1611 let mut result = client.get(&path, None).await?;
390c5bdd 1612 let mut data = result["data"].take();
34a816cc
DM
1613
1614 record_repository(&repo);
1615
390c5bdd
DM
1616 let render_total_percentage = |v: &Value, record: &Value| -> Result<String, Error> {
1617 let v = v.as_u64().unwrap();
1618 let total = record["total"].as_u64().unwrap();
1619 let roundup = total/200;
1620 let per = ((v+roundup)*100)/total;
e23f5863
DM
1621 let info = format!(" ({} %)", per);
1622 Ok(format!("{} {:>8}", v, info))
390c5bdd 1623 };
1dc117bb 1624
c2043614 1625 let options = default_table_format_options()
be2425ff 1626 .noheader(true)
e23f5863 1627 .column(ColumnConfig::new("total").renderer(render_total_percentage))
390c5bdd
DM
1628 .column(ColumnConfig::new("used").renderer(render_total_percentage))
1629 .column(ColumnConfig::new("avail").renderer(render_total_percentage));
34a816cc 1630
ea5f547f 1631 let schema = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_STATUS;
390c5bdd
DM
1632
1633 format_and_print_result_full(&mut data, schema, &output_format, &options);
34a816cc
DM
1634
1635 Ok(Value::Null)
1636}
1637
5a2df000 1638// like get, but simply ignore errors and return Null instead
e9722f8b 1639async fn try_get(repo: &BackupRepository, url: &str) -> Value {
024f11bb 1640
a05c0c6f 1641 let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
d1c65727 1642 let password = std::env::var(ENV_VAR_PBS_PASSWORD).ok();
a05c0c6f 1643
d59dbeca 1644 let options = HttpClientOptions::new()
5030b7ce 1645 .prefix(Some("proxmox-backup".to_string()))
d1c65727 1646 .password(password)
d59dbeca 1647 .interactive(false)
a05c0c6f 1648 .fingerprint(fingerprint)
5a74756c 1649 .fingerprint_cache(true)
d59dbeca
DM
1650 .ticket_cache(true);
1651
1652 let client = match HttpClient::new(repo.host(), repo.user(), options) {
45cdce06
DM
1653 Ok(v) => v,
1654 _ => return Value::Null,
1655 };
b2388518 1656
e9722f8b 1657 let mut resp = match client.get(url, None).await {
b2388518
DM
1658 Ok(v) => v,
1659 _ => return Value::Null,
1660 };
1661
1662 if let Some(map) = resp.as_object_mut() {
1663 if let Some(data) = map.remove("data") {
1664 return data;
1665 }
1666 }
1667 Value::Null
1668}
1669
b2388518 1670fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
3f06d6fb 1671 proxmox_backup::tools::runtime::main(async { complete_backup_group_do(param).await })
e9722f8b
WB
1672}
1673
1674async fn complete_backup_group_do(param: &HashMap<String, String>) -> Vec<String> {
024f11bb 1675
b2388518
DM
1676 let mut result = vec![];
1677
2665cef7 1678 let repo = match extract_repository_from_map(param) {
b2388518 1679 Some(v) => v,
024f11bb
DM
1680 _ => return result,
1681 };
1682
b2388518
DM
1683 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
1684
e9722f8b 1685 let data = try_get(&repo, &path).await;
b2388518
DM
1686
1687 if let Some(list) = data.as_array() {
024f11bb 1688 for item in list {
98f0b972
DM
1689 if let (Some(backup_id), Some(backup_type)) =
1690 (item["backup-id"].as_str(), item["backup-type"].as_str())
1691 {
1692 result.push(format!("{}/{}", backup_type, backup_id));
024f11bb
DM
1693 }
1694 }
1695 }
1696
1697 result
1698}
1699
43abba4b 1700pub fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
3f06d6fb 1701 proxmox_backup::tools::runtime::main(async { complete_group_or_snapshot_do(arg, param).await })
e9722f8b
WB
1702}
1703
1704async fn complete_group_or_snapshot_do(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
b2388518 1705
b2388518 1706 if arg.matches('/').count() < 2 {
e9722f8b 1707 let groups = complete_backup_group_do(param).await;
543a260f 1708 let mut result = vec![];
b2388518
DM
1709 for group in groups {
1710 result.push(group.to_string());
1711 result.push(format!("{}/", group));
1712 }
1713 return result;
1714 }
1715
e9722f8b 1716 complete_backup_snapshot_do(param).await
543a260f 1717}
b2388518 1718
3fb53e07 1719fn complete_backup_snapshot(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
3f06d6fb 1720 proxmox_backup::tools::runtime::main(async { complete_backup_snapshot_do(param).await })
e9722f8b
WB
1721}
1722
1723async fn complete_backup_snapshot_do(param: &HashMap<String, String>) -> Vec<String> {
543a260f
DM
1724
1725 let mut result = vec![];
1726
1727 let repo = match extract_repository_from_map(param) {
1728 Some(v) => v,
1729 _ => return result,
1730 };
1731
1732 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
b2388518 1733
e9722f8b 1734 let data = try_get(&repo, &path).await;
b2388518
DM
1735
1736 if let Some(list) = data.as_array() {
1737 for item in list {
1738 if let (Some(backup_id), Some(backup_type), Some(backup_time)) =
1739 (item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64())
1740 {
1741 let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
1742 result.push(snapshot.relative_path().to_str().unwrap().to_owned());
1743 }
1744 }
1745 }
1746
1747 result
1748}
1749
45db6f89 1750fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
3f06d6fb 1751 proxmox_backup::tools::runtime::main(async { complete_server_file_name_do(param).await })
e9722f8b
WB
1752}
1753
1754async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<String> {
08dc340a
DM
1755
1756 let mut result = vec![];
1757
2665cef7 1758 let repo = match extract_repository_from_map(param) {
08dc340a
DM
1759 Some(v) => v,
1760 _ => return result,
1761 };
1762
a67f7d0a 1763 let snapshot: BackupDir = match param.get("snapshot") {
08dc340a 1764 Some(path) => {
a67f7d0a 1765 match path.parse() {
08dc340a
DM
1766 Ok(v) => v,
1767 _ => return result,
1768 }
1769 }
1770 _ => return result,
1771 };
1772
1773 let query = tools::json_object_to_query(json!({
1774 "backup-type": snapshot.group().backup_type(),
1775 "backup-id": snapshot.group().backup_id(),
1776 "backup-time": snapshot.backup_time().timestamp(),
1777 })).unwrap();
1778
1779 let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query);
1780
e9722f8b 1781 let data = try_get(&repo, &path).await;
08dc340a
DM
1782
1783 if let Some(list) = data.as_array() {
1784 for item in list {
c4f025eb 1785 if let Some(filename) = item["filename"].as_str() {
08dc340a
DM
1786 result.push(filename.to_owned());
1787 }
1788 }
1789 }
1790
45db6f89
DM
1791 result
1792}
1793
1794fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
52c171e4 1795 complete_server_file_name(arg, param)
e9722f8b 1796 .iter()
4939255f 1797 .map(|v| tools::format::strip_server_file_expenstion(&v))
e9722f8b 1798 .collect()
08dc340a
DM
1799}
1800
43abba4b 1801pub fn complete_pxar_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
0ec9e1b0
DM
1802 complete_server_file_name(arg, param)
1803 .iter()
1804 .filter_map(|v| {
4939255f 1805 let name = tools::format::strip_server_file_expenstion(&v);
0ec9e1b0
DM
1806 if name.ends_with(".pxar") {
1807 Some(name)
1808 } else {
1809 None
1810 }
1811 })
1812 .collect()
1813}
1814
49811347
DM
1815fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
1816
1817 let mut result = vec![];
1818
1819 let mut size = 64;
1820 loop {
1821 result.push(size.to_string());
11377a47 1822 size *= 2;
49811347
DM
1823 if size > 4096 { break; }
1824 }
1825
1826 result
1827}
1828
c443f58b
WB
1829use proxmox_backup::client::RemoteChunkReader;
1830/// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better
1831/// async use!
1832///
1833/// Ideally BufferedDynamicReader gets replaced so the LruCache maps to `BroadcastFuture<Chunk>`,
1834/// so that we can properly access it from multiple threads simultaneously while not issuing
1835/// duplicate simultaneous reads over http.
43abba4b 1836pub struct BufferedDynamicReadAt {
c443f58b
WB
1837 inner: Mutex<BufferedDynamicReader<RemoteChunkReader>>,
1838}
1839
1840impl BufferedDynamicReadAt {
1841 fn new(inner: BufferedDynamicReader<RemoteChunkReader>) -> Self {
1842 Self {
1843 inner: Mutex::new(inner),
1844 }
1845 }
1846}
1847
a6f87283
WB
1848impl ReadAt for BufferedDynamicReadAt {
1849 fn start_read_at<'a>(
1850 self: Pin<&'a Self>,
c443f58b 1851 _cx: &mut Context,
a6f87283 1852 buf: &'a mut [u8],
c443f58b 1853 offset: u64,
a6f87283 1854 ) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
a6f87283 1855 MaybeReady::Ready(tokio::task::block_in_place(move || {
c443f58b
WB
1856 let mut reader = self.inner.lock().unwrap();
1857 reader.seek(SeekFrom::Start(offset))?;
a6f87283
WB
1858 Ok(reader.read(buf)?)
1859 }))
1860 }
1861
1862 fn poll_complete<'a>(
1863 self: Pin<&'a Self>,
1864 _op: ReadAtOperation<'a>,
1865 ) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
1866 panic!("LocalDynamicReadAt::start_read_at returned Pending");
c443f58b
WB
1867 }
1868}
1869
f2401311 1870fn main() {
33d64b81 1871
255f378a 1872 let backup_cmd_def = CliCommand::new(&API_METHOD_CREATE_BACKUP)
49fddd98 1873 .arg_param(&["backupspec"])
d0a03d40 1874 .completion_cb("repository", complete_repository)
49811347 1875 .completion_cb("backupspec", complete_backup_source)
6d0983db 1876 .completion_cb("keyfile", tools::complete_file_name)
49811347 1877 .completion_cb("chunk-size", complete_chunk_size);
f8838fe9 1878
caea8d61
DM
1879 let benchmark_cmd_def = CliCommand::new(&API_METHOD_BENCHMARK)
1880 .completion_cb("repository", complete_repository)
1881 .completion_cb("keyfile", tools::complete_file_name);
1882
255f378a 1883 let upload_log_cmd_def = CliCommand::new(&API_METHOD_UPLOAD_LOG)
49fddd98 1884 .arg_param(&["snapshot", "logfile"])
543a260f 1885 .completion_cb("snapshot", complete_backup_snapshot)
ec34f7eb
DM
1886 .completion_cb("logfile", tools::complete_file_name)
1887 .completion_cb("keyfile", tools::complete_file_name)
1888 .completion_cb("repository", complete_repository);
1889
255f378a 1890 let list_cmd_def = CliCommand::new(&API_METHOD_LIST_BACKUP_GROUPS)
d0a03d40 1891 .completion_cb("repository", complete_repository);
41c039e1 1892
255f378a 1893 let snapshots_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOTS)
49fddd98 1894 .arg_param(&["group"])
024f11bb 1895 .completion_cb("group", complete_backup_group)
d0a03d40 1896 .completion_cb("repository", complete_repository);
184f17af 1897
255f378a 1898 let forget_cmd_def = CliCommand::new(&API_METHOD_FORGET_SNAPSHOTS)
49fddd98 1899 .arg_param(&["snapshot"])
b2388518 1900 .completion_cb("repository", complete_repository)
543a260f 1901 .completion_cb("snapshot", complete_backup_snapshot);
6f62c924 1902
255f378a 1903 let garbage_collect_cmd_def = CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
d0a03d40 1904 .completion_cb("repository", complete_repository);
8cc0d6af 1905
255f378a 1906 let restore_cmd_def = CliCommand::new(&API_METHOD_RESTORE)
49fddd98 1907 .arg_param(&["snapshot", "archive-name", "target"])
b2388518 1908 .completion_cb("repository", complete_repository)
08dc340a
DM
1909 .completion_cb("snapshot", complete_group_or_snapshot)
1910 .completion_cb("archive-name", complete_archive_name)
1911 .completion_cb("target", tools::complete_file_name);
9f912493 1912
255f378a 1913 let files_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOT_FILES)
49fddd98 1914 .arg_param(&["snapshot"])
52c171e4 1915 .completion_cb("repository", complete_repository)
543a260f 1916 .completion_cb("snapshot", complete_backup_snapshot);
52c171e4 1917
255f378a 1918 let prune_cmd_def = CliCommand::new(&API_METHOD_PRUNE)
49fddd98 1919 .arg_param(&["group"])
9fdc3ef4 1920 .completion_cb("group", complete_backup_group)
d0a03d40 1921 .completion_cb("repository", complete_repository);
9f912493 1922
255f378a 1923 let status_cmd_def = CliCommand::new(&API_METHOD_STATUS)
34a816cc
DM
1924 .completion_cb("repository", complete_repository);
1925
255f378a 1926 let login_cmd_def = CliCommand::new(&API_METHOD_API_LOGIN)
e240d8be
DM
1927 .completion_cb("repository", complete_repository);
1928
255f378a 1929 let logout_cmd_def = CliCommand::new(&API_METHOD_API_LOGOUT)
e240d8be 1930 .completion_cb("repository", complete_repository);
32efac1c 1931
e39974af
TL
1932 let version_cmd_def = CliCommand::new(&API_METHOD_API_VERSION)
1933 .completion_cb("repository", complete_repository);
1934
41c039e1 1935 let cmd_def = CliCommandMap::new()
48ef3c33
DM
1936 .insert("backup", backup_cmd_def)
1937 .insert("upload-log", upload_log_cmd_def)
1938 .insert("forget", forget_cmd_def)
1939 .insert("garbage-collect", garbage_collect_cmd_def)
1940 .insert("list", list_cmd_def)
1941 .insert("login", login_cmd_def)
1942 .insert("logout", logout_cmd_def)
1943 .insert("prune", prune_cmd_def)
1944 .insert("restore", restore_cmd_def)
1945 .insert("snapshots", snapshots_cmd_def)
1946 .insert("files", files_cmd_def)
1947 .insert("status", status_cmd_def)
9696f519 1948 .insert("key", key::cli())
43abba4b 1949 .insert("mount", mount_cmd_def())
5830c205 1950 .insert("catalog", catalog_mgmt_cli())
caea8d61 1951 .insert("task", task_mgmt_cli())
e39974af 1952 .insert("version", version_cmd_def)
caea8d61 1953 .insert("benchmark", benchmark_cmd_def);
48ef3c33 1954
7b22acd0
DM
1955 let rpcenv = CliEnvironment::new();
1956 run_cli_command(cmd_def, rpcenv, Some(|future| {
d08bc483
DM
1957 proxmox_backup::tools::runtime::main(future)
1958 }));
ff5d3707 1959}