]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/backup.rs
verify jobs: add permissions
[proxmox-backup.git] / src / api2 / backup.rs
1 use anyhow::{bail, format_err, Error};
2 use futures::*;
3 use hyper::header::{HeaderValue, UPGRADE};
4 use hyper::http::request::Parts;
5 use hyper::{Body, Response, StatusCode};
6 use serde_json::{json, Value};
7
8 use proxmox::{sortable, identity, list_subdirs_api_method};
9 use proxmox::api::{ApiResponseFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, Permission};
10 use proxmox::api::router::SubdirMap;
11 use proxmox::api::schema::*;
12
13 use crate::tools;
14 use crate::server::{WorkerTask, H2Service};
15 use crate::backup::*;
16 use crate::api2::types::*;
17 use crate::config::acl::PRIV_DATASTORE_BACKUP;
18 use crate::config::cached_user_info::CachedUserInfo;
19 use crate::tools::fs::lock_dir_noblock_shared;
20
21 mod environment;
22 use environment::*;
23
24 mod upload_chunk;
25 use upload_chunk::*;
26
27 pub const ROUTER: Router = Router::new()
28 .upgrade(&API_METHOD_UPGRADE_BACKUP);
29
30 #[sortable]
31 pub const API_METHOD_UPGRADE_BACKUP: ApiMethod = ApiMethod::new(
32 &ApiHandler::AsyncHttp(&upgrade_to_backup_protocol),
33 &ObjectSchema::new(
34 concat!("Upgraded to backup protocol ('", PROXMOX_BACKUP_PROTOCOL_ID_V1!(), "')."),
35 &sorted!([
36 ("store", false, &DATASTORE_SCHEMA),
37 ("backup-type", false, &BACKUP_TYPE_SCHEMA),
38 ("backup-id", false, &BACKUP_ID_SCHEMA),
39 ("backup-time", false, &BACKUP_TIME_SCHEMA),
40 ("debug", true, &BooleanSchema::new("Enable verbose debug logging.").schema()),
41 ("benchmark", true, &BooleanSchema::new("Job is a benchmark (do not keep data).").schema()),
42 ]),
43 )
44 ).access(
45 // Note: parameter 'store' is no uri parameter, so we need to test inside function body
46 Some("The user needs Datastore.Backup privilege on /datastore/{store} and needs to own the backup group."),
47 &Permission::Anybody
48 );
49
50 fn upgrade_to_backup_protocol(
51 parts: Parts,
52 req_body: Body,
53 param: Value,
54 _info: &ApiMethod,
55 rpcenv: Box<dyn RpcEnvironment>,
56 ) -> ApiResponseFuture {
57
58 async move {
59 let debug = param["debug"].as_bool().unwrap_or(false);
60 let benchmark = param["benchmark"].as_bool().unwrap_or(false);
61
62 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
63
64 let store = tools::required_string_param(&param, "store")?.to_owned();
65
66 let user_info = CachedUserInfo::new()?;
67 user_info.check_privs(&auth_id, &["datastore", &store], PRIV_DATASTORE_BACKUP, false)?;
68
69 let datastore = DataStore::lookup_datastore(&store)?;
70
71 let backup_type = tools::required_string_param(&param, "backup-type")?;
72 let backup_id = tools::required_string_param(&param, "backup-id")?;
73 let backup_time = tools::required_integer_param(&param, "backup-time")?;
74
75 let protocols = parts
76 .headers
77 .get("UPGRADE")
78 .ok_or_else(|| format_err!("missing Upgrade header"))?
79 .to_str()?;
80
81 if protocols != PROXMOX_BACKUP_PROTOCOL_ID_V1!() {
82 bail!("invalid protocol name");
83 }
84
85 if parts.version >= http::version::Version::HTTP_2 {
86 bail!("unexpected http version '{:?}' (expected version < 2)", parts.version);
87 }
88
89 let worker_id = format!("{}:{}/{}", store, backup_type, backup_id);
90
91 let env_type = rpcenv.env_type();
92
93 let backup_group = BackupGroup::new(backup_type, backup_id);
94
95 let worker_type = if backup_type == "host" && backup_id == "benchmark" {
96 if !benchmark {
97 bail!("unable to run benchmark without --benchmark flags");
98 }
99 "benchmark"
100 } else {
101 if benchmark {
102 bail!("benchmark flags is only allowed on 'host/benchmark'");
103 }
104 "backup"
105 };
106
107 // lock backup group to only allow one backup per group at a time
108 let (owner, _group_guard) = datastore.create_locked_backup_group(&backup_group, &auth_id)?;
109
110 // permission check
111 let correct_owner = owner == auth_id
112 || (owner.is_token()
113 && Authid::from(owner.user().clone()) == auth_id);
114 if !correct_owner && worker_type != "benchmark" {
115 // only the owner is allowed to create additional snapshots
116 bail!("backup owner check failed ({} != {})", auth_id, owner);
117 }
118
119 let last_backup = {
120 let info = BackupInfo::last_backup(&datastore.base_path(), &backup_group, true).unwrap_or(None);
121 if let Some(info) = info {
122 let (manifest, _) = datastore.load_manifest(&info.backup_dir)?;
123 let verify = manifest.unprotected["verify_state"].clone();
124 match serde_json::from_value::<SnapshotVerifyState>(verify) {
125 Ok(verify) => {
126 match verify.state {
127 VerifyState::Ok => Some(info),
128 VerifyState::Failed => None,
129 }
130 },
131 Err(_) => {
132 // no verify state found, treat as valid
133 Some(info)
134 }
135 }
136 } else {
137 None
138 }
139 };
140
141 let backup_dir = BackupDir::with_group(backup_group.clone(), backup_time)?;
142
143 let _last_guard = if let Some(last) = &last_backup {
144 if backup_dir.backup_time() <= last.backup_dir.backup_time() {
145 bail!("backup timestamp is older than last backup.");
146 }
147
148 // lock last snapshot to prevent forgetting/pruning it during backup
149 let full_path = datastore.snapshot_path(&last.backup_dir);
150 Some(lock_dir_noblock_shared(&full_path, "snapshot", "base snapshot is already locked by another operation")?)
151 } else {
152 None
153 };
154
155 let (path, is_new, snap_guard) = datastore.create_locked_backup_dir(&backup_dir)?;
156 if !is_new { bail!("backup directory already exists."); }
157
158
159 WorkerTask::spawn(worker_type, Some(worker_id), auth_id.clone(), true, move |worker| {
160 let mut env = BackupEnvironment::new(
161 env_type, auth_id, worker.clone(), datastore, backup_dir);
162
163 env.debug = debug;
164 env.last_backup = last_backup;
165
166 env.log(format!("starting new {} on datastore '{}': {:?}", worker_type, store, path));
167
168 let service = H2Service::new(env.clone(), worker.clone(), &BACKUP_API_ROUTER, debug);
169
170 let abort_future = worker.abort_future();
171
172 let env2 = env.clone();
173
174 let mut req_fut = req_body
175 .on_upgrade()
176 .map_err(Error::from)
177 .and_then(move |conn| {
178 env2.debug("protocol upgrade done");
179
180 let mut http = hyper::server::conn::Http::new();
181 http.http2_only(true);
182 // increase window size: todo - find optiomal size
183 let window_size = 32*1024*1024; // max = (1 << 31) - 2
184 http.http2_initial_stream_window_size(window_size);
185 http.http2_initial_connection_window_size(window_size);
186 http.http2_max_frame_size(4*1024*1024);
187
188 let env3 = env2.clone();
189 http.serve_connection(conn, service)
190 .map(move |result| {
191 match result {
192 Err(err) => {
193 // Avoid Transport endpoint is not connected (os error 107)
194 // fixme: find a better way to test for that error
195 if err.to_string().starts_with("connection error") && env3.finished() {
196 Ok(())
197 } else {
198 Err(Error::from(err))
199 }
200 }
201 Ok(()) => Ok(()),
202 }
203 })
204 });
205 let mut abort_future = abort_future
206 .map(|_| Err(format_err!("task aborted")));
207
208 async move {
209 // keep flock until task ends
210 let _group_guard = _group_guard;
211 let snap_guard = snap_guard;
212 let _last_guard = _last_guard;
213
214 let res = select!{
215 req = req_fut => req,
216 abrt = abort_future => abrt,
217 };
218 if benchmark {
219 env.log("benchmark finished successfully");
220 tools::runtime::block_in_place(|| env.remove_backup())?;
221 return Ok(());
222 }
223
224 let verify = |env: BackupEnvironment| {
225 if let Err(err) = env.verify_after_complete(snap_guard) {
226 env.log(format!(
227 "backup finished, but starting the requested verify task failed: {}",
228 err
229 ));
230 }
231 };
232
233 match (res, env.ensure_finished()) {
234 (Ok(_), Ok(())) => {
235 env.log("backup finished successfully");
236 verify(env);
237 Ok(())
238 },
239 (Err(err), Ok(())) => {
240 // ignore errors after finish
241 env.log(format!("backup had errors but finished: {}", err));
242 verify(env);
243 Ok(())
244 },
245 (Ok(_), Err(err)) => {
246 env.log(format!("backup ended and finish failed: {}", err));
247 env.log("removing unfinished backup");
248 tools::runtime::block_in_place(|| env.remove_backup())?;
249 Err(err)
250 },
251 (Err(err), Err(_)) => {
252 env.log(format!("backup failed: {}", err));
253 env.log("removing failed backup");
254 tools::runtime::block_in_place(|| env.remove_backup())?;
255 Err(err)
256 },
257 }
258 }
259 })?;
260
261 let response = Response::builder()
262 .status(StatusCode::SWITCHING_PROTOCOLS)
263 .header(UPGRADE, HeaderValue::from_static(PROXMOX_BACKUP_PROTOCOL_ID_V1!()))
264 .body(Body::empty())?;
265
266 Ok(response)
267 }.boxed()
268 }
269
270 pub const BACKUP_API_SUBDIRS: SubdirMap = &[
271 (
272 "blob", &Router::new()
273 .upload(&API_METHOD_UPLOAD_BLOB)
274 ),
275 (
276 "dynamic_chunk", &Router::new()
277 .upload(&API_METHOD_UPLOAD_DYNAMIC_CHUNK)
278 ),
279 (
280 "dynamic_close", &Router::new()
281 .post(&API_METHOD_CLOSE_DYNAMIC_INDEX)
282 ),
283 (
284 "dynamic_index", &Router::new()
285 .post(&API_METHOD_CREATE_DYNAMIC_INDEX)
286 .put(&API_METHOD_DYNAMIC_APPEND)
287 ),
288 (
289 "finish", &Router::new()
290 .post(
291 &ApiMethod::new(
292 &ApiHandler::Sync(&finish_backup),
293 &ObjectSchema::new("Mark backup as finished.", &[])
294 )
295 )
296 ),
297 (
298 "fixed_chunk", &Router::new()
299 .upload(&API_METHOD_UPLOAD_FIXED_CHUNK)
300 ),
301 (
302 "fixed_close", &Router::new()
303 .post(&API_METHOD_CLOSE_FIXED_INDEX)
304 ),
305 (
306 "fixed_index", &Router::new()
307 .post(&API_METHOD_CREATE_FIXED_INDEX)
308 .put(&API_METHOD_FIXED_APPEND)
309 ),
310 (
311 "previous", &Router::new()
312 .download(&API_METHOD_DOWNLOAD_PREVIOUS)
313 ),
314 (
315 "speedtest", &Router::new()
316 .upload(&API_METHOD_UPLOAD_SPEEDTEST)
317 ),
318 ];
319
320 pub const BACKUP_API_ROUTER: Router = Router::new()
321 .get(&list_subdirs_api_method!(BACKUP_API_SUBDIRS))
322 .subdirs(BACKUP_API_SUBDIRS);
323
324 #[sortable]
325 pub const API_METHOD_CREATE_DYNAMIC_INDEX: ApiMethod = ApiMethod::new(
326 &ApiHandler::Sync(&create_dynamic_index),
327 &ObjectSchema::new(
328 "Create dynamic chunk index file.",
329 &sorted!([
330 ("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA),
331 ]),
332 )
333 );
334
335 fn create_dynamic_index(
336 param: Value,
337 _info: &ApiMethod,
338 rpcenv: &mut dyn RpcEnvironment,
339 ) -> Result<Value, Error> {
340
341 let env: &BackupEnvironment = rpcenv.as_ref();
342
343 let name = tools::required_string_param(&param, "archive-name")?.to_owned();
344
345 let archive_name = name.clone();
346 if !archive_name.ends_with(".didx") {
347 bail!("wrong archive extension: '{}'", archive_name);
348 }
349
350 let mut path = env.backup_dir.relative_path();
351 path.push(archive_name);
352
353 let index = env.datastore.create_dynamic_writer(&path)?;
354 let wid = env.register_dynamic_writer(index, name)?;
355
356 env.log(format!("created new dynamic index {} ({:?})", wid, path));
357
358 Ok(json!(wid))
359 }
360
361 #[sortable]
362 pub const API_METHOD_CREATE_FIXED_INDEX: ApiMethod = ApiMethod::new(
363 &ApiHandler::Sync(&create_fixed_index),
364 &ObjectSchema::new(
365 "Create fixed chunk index file.",
366 &sorted!([
367 ("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA),
368 ("size", false, &IntegerSchema::new("File size.")
369 .minimum(1)
370 .schema()
371 ),
372 ("reuse-csum", true, &StringSchema::new("If set, compare last backup's \
373 csum and reuse index for incremental backup if it matches.").schema()),
374 ]),
375 )
376 );
377
378 fn create_fixed_index(
379 param: Value,
380 _info: &ApiMethod,
381 rpcenv: &mut dyn RpcEnvironment,
382 ) -> Result<Value, Error> {
383
384 let env: &BackupEnvironment = rpcenv.as_ref();
385
386 let name = tools::required_string_param(&param, "archive-name")?.to_owned();
387 let size = tools::required_integer_param(&param, "size")? as usize;
388 let reuse_csum = param["reuse-csum"].as_str();
389
390 let archive_name = name.clone();
391 if !archive_name.ends_with(".fidx") {
392 bail!("wrong archive extension: '{}'", archive_name);
393 }
394
395 let mut path = env.backup_dir.relative_path();
396 path.push(&archive_name);
397
398 let chunk_size = 4096*1024; // todo: ??
399
400 // do incremental backup if csum is set
401 let mut reader = None;
402 let mut incremental = false;
403 if let Some(csum) = reuse_csum {
404 incremental = true;
405 let last_backup = match &env.last_backup {
406 Some(info) => info,
407 None => {
408 bail!("cannot reuse index - no valid previous backup exists");
409 }
410 };
411
412 let mut last_path = last_backup.backup_dir.relative_path();
413 last_path.push(&archive_name);
414
415 let index = match env.datastore.open_fixed_reader(last_path) {
416 Ok(index) => index,
417 Err(_) => {
418 bail!("cannot reuse index - no previous backup exists for archive");
419 }
420 };
421
422 let (old_csum, _) = index.compute_csum();
423 let old_csum = proxmox::tools::digest_to_hex(&old_csum);
424 if old_csum != csum {
425 bail!("expected csum ({}) doesn't match last backup's ({}), cannot do incremental backup",
426 csum, old_csum);
427 }
428
429 reader = Some(index);
430 }
431
432 let mut writer = env.datastore.create_fixed_writer(&path, size, chunk_size)?;
433
434 if let Some(reader) = reader {
435 writer.clone_data_from(&reader)?;
436 }
437
438 let wid = env.register_fixed_writer(writer, name, size, chunk_size as u32, incremental)?;
439
440 env.log(format!("created new fixed index {} ({:?})", wid, path));
441
442 Ok(json!(wid))
443 }
444
445 #[sortable]
446 pub const API_METHOD_DYNAMIC_APPEND: ApiMethod = ApiMethod::new(
447 &ApiHandler::Sync(&dynamic_append),
448 &ObjectSchema::new(
449 "Append chunk to dynamic index writer.",
450 &sorted!([
451 (
452 "wid",
453 false,
454 &IntegerSchema::new("Dynamic writer ID.")
455 .minimum(1)
456 .maximum(256)
457 .schema()
458 ),
459 (
460 "digest-list",
461 false,
462 &ArraySchema::new("Chunk digest list.", &CHUNK_DIGEST_SCHEMA).schema()
463 ),
464 (
465 "offset-list",
466 false,
467 &ArraySchema::new(
468 "Chunk offset list.",
469 &IntegerSchema::new("Corresponding chunk offsets.")
470 .minimum(0)
471 .schema()
472 ).schema()
473 ),
474 ]),
475 )
476 );
477
478 fn dynamic_append (
479 param: Value,
480 _info: &ApiMethod,
481 rpcenv: &mut dyn RpcEnvironment,
482 ) -> Result<Value, Error> {
483
484 let wid = tools::required_integer_param(&param, "wid")? as usize;
485 let digest_list = tools::required_array_param(&param, "digest-list")?;
486 let offset_list = tools::required_array_param(&param, "offset-list")?;
487
488 if offset_list.len() != digest_list.len() {
489 bail!("offset list has wrong length ({} != {})", offset_list.len(), digest_list.len());
490 }
491
492 let env: &BackupEnvironment = rpcenv.as_ref();
493
494 env.debug(format!("dynamic_append {} chunks", digest_list.len()));
495
496 for (i, item) in digest_list.iter().enumerate() {
497 let digest_str = item.as_str().unwrap();
498 let digest = proxmox::tools::hex_to_digest(digest_str)?;
499 let offset = offset_list[i].as_u64().unwrap();
500 let size = env.lookup_chunk(&digest).ok_or_else(|| format_err!("no such chunk {}", digest_str))?;
501
502 env.dynamic_writer_append_chunk(wid, offset, size, &digest)?;
503
504 env.debug(format!("successfully added chunk {} to dynamic index {} (offset {}, size {})", digest_str, wid, offset, size));
505 }
506
507 Ok(Value::Null)
508 }
509
510 #[sortable]
511 pub const API_METHOD_FIXED_APPEND: ApiMethod = ApiMethod::new(
512 &ApiHandler::Sync(&fixed_append),
513 &ObjectSchema::new(
514 "Append chunk to fixed index writer.",
515 &sorted!([
516 (
517 "wid",
518 false,
519 &IntegerSchema::new("Fixed writer ID.")
520 .minimum(1)
521 .maximum(256)
522 .schema()
523 ),
524 (
525 "digest-list",
526 false,
527 &ArraySchema::new("Chunk digest list.", &CHUNK_DIGEST_SCHEMA).schema()
528 ),
529 (
530 "offset-list",
531 false,
532 &ArraySchema::new(
533 "Chunk offset list.",
534 &IntegerSchema::new("Corresponding chunk offsets.")
535 .minimum(0)
536 .schema()
537 ).schema()
538 )
539 ]),
540 )
541 );
542
543 fn fixed_append (
544 param: Value,
545 _info: &ApiMethod,
546 rpcenv: &mut dyn RpcEnvironment,
547 ) -> Result<Value, Error> {
548
549 let wid = tools::required_integer_param(&param, "wid")? as usize;
550 let digest_list = tools::required_array_param(&param, "digest-list")?;
551 let offset_list = tools::required_array_param(&param, "offset-list")?;
552
553 if offset_list.len() != digest_list.len() {
554 bail!("offset list has wrong length ({} != {})", offset_list.len(), digest_list.len());
555 }
556
557 let env: &BackupEnvironment = rpcenv.as_ref();
558
559 env.debug(format!("fixed_append {} chunks", digest_list.len()));
560
561 for (i, item) in digest_list.iter().enumerate() {
562 let digest_str = item.as_str().unwrap();
563 let digest = proxmox::tools::hex_to_digest(digest_str)?;
564 let offset = offset_list[i].as_u64().unwrap();
565 let size = env.lookup_chunk(&digest).ok_or_else(|| format_err!("no such chunk {}", digest_str))?;
566
567 env.fixed_writer_append_chunk(wid, offset, size, &digest)?;
568
569 env.debug(format!("successfully added chunk {} to fixed index {} (offset {}, size {})", digest_str, wid, offset, size));
570 }
571
572 Ok(Value::Null)
573 }
574
575 #[sortable]
576 pub const API_METHOD_CLOSE_DYNAMIC_INDEX: ApiMethod = ApiMethod::new(
577 &ApiHandler::Sync(&close_dynamic_index),
578 &ObjectSchema::new(
579 "Close dynamic index writer.",
580 &sorted!([
581 (
582 "wid",
583 false,
584 &IntegerSchema::new("Dynamic writer ID.")
585 .minimum(1)
586 .maximum(256)
587 .schema()
588 ),
589 (
590 "chunk-count",
591 false,
592 &IntegerSchema::new("Chunk count. This is used to verify that the server got all chunks.")
593 .minimum(1)
594 .schema()
595 ),
596 (
597 "size",
598 false,
599 &IntegerSchema::new("File size. This is used to verify that the server got all data.")
600 .minimum(1)
601 .schema()
602 ),
603 ("csum", false, &StringSchema::new("Digest list checksum.").schema()),
604 ]),
605 )
606 );
607
608 fn close_dynamic_index (
609 param: Value,
610 _info: &ApiMethod,
611 rpcenv: &mut dyn RpcEnvironment,
612 ) -> Result<Value, Error> {
613
614 let wid = tools::required_integer_param(&param, "wid")? as usize;
615 let chunk_count = tools::required_integer_param(&param, "chunk-count")? as u64;
616 let size = tools::required_integer_param(&param, "size")? as u64;
617 let csum_str = tools::required_string_param(&param, "csum")?;
618 let csum = proxmox::tools::hex_to_digest(csum_str)?;
619
620 let env: &BackupEnvironment = rpcenv.as_ref();
621
622 env.dynamic_writer_close(wid, chunk_count, size, csum)?;
623
624 env.log(format!("successfully closed dynamic index {}", wid));
625
626 Ok(Value::Null)
627 }
628
629 #[sortable]
630 pub const API_METHOD_CLOSE_FIXED_INDEX: ApiMethod = ApiMethod::new(
631 &ApiHandler::Sync(&close_fixed_index),
632 &ObjectSchema::new(
633 "Close fixed index writer.",
634 &sorted!([
635 (
636 "wid",
637 false,
638 &IntegerSchema::new("Fixed writer ID.")
639 .minimum(1)
640 .maximum(256)
641 .schema()
642 ),
643 (
644 "chunk-count",
645 false,
646 &IntegerSchema::new("Chunk count. This is used to verify that the server got all chunks. Ignored for incremental backups.")
647 .minimum(0)
648 .schema()
649 ),
650 (
651 "size",
652 false,
653 &IntegerSchema::new("File size. This is used to verify that the server got all data. Ignored for incremental backups.")
654 .minimum(0)
655 .schema()
656 ),
657 ("csum", false, &StringSchema::new("Digest list checksum.").schema()),
658 ]),
659 )
660 );
661
662 fn close_fixed_index (
663 param: Value,
664 _info: &ApiMethod,
665 rpcenv: &mut dyn RpcEnvironment,
666 ) -> Result<Value, Error> {
667
668 let wid = tools::required_integer_param(&param, "wid")? as usize;
669 let chunk_count = tools::required_integer_param(&param, "chunk-count")? as u64;
670 let size = tools::required_integer_param(&param, "size")? as u64;
671 let csum_str = tools::required_string_param(&param, "csum")?;
672 let csum = proxmox::tools::hex_to_digest(csum_str)?;
673
674 let env: &BackupEnvironment = rpcenv.as_ref();
675
676 env.fixed_writer_close(wid, chunk_count, size, csum)?;
677
678 env.log(format!("successfully closed fixed index {}", wid));
679
680 Ok(Value::Null)
681 }
682
683 fn finish_backup (
684 _param: Value,
685 _info: &ApiMethod,
686 rpcenv: &mut dyn RpcEnvironment,
687 ) -> Result<Value, Error> {
688
689 let env: &BackupEnvironment = rpcenv.as_ref();
690
691 env.finish_backup()?;
692 env.log("successfully finished backup");
693
694 Ok(Value::Null)
695 }
696
697 #[sortable]
698 pub const API_METHOD_DOWNLOAD_PREVIOUS: ApiMethod = ApiMethod::new(
699 &ApiHandler::AsyncHttp(&download_previous),
700 &ObjectSchema::new(
701 "Download archive from previous backup.",
702 &sorted!([
703 ("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA)
704 ]),
705 )
706 );
707
708 fn download_previous(
709 _parts: Parts,
710 _req_body: Body,
711 param: Value,
712 _info: &ApiMethod,
713 rpcenv: Box<dyn RpcEnvironment>,
714 ) -> ApiResponseFuture {
715
716 async move {
717 let env: &BackupEnvironment = rpcenv.as_ref();
718
719 let archive_name = tools::required_string_param(&param, "archive-name")?.to_owned();
720
721 let last_backup = match &env.last_backup {
722 Some(info) => info,
723 None => bail!("no valid previous backup"),
724 };
725
726 let mut path = env.datastore.snapshot_path(&last_backup.backup_dir);
727 path.push(&archive_name);
728
729 {
730 let index: Option<Box<dyn IndexFile>> = match archive_type(&archive_name)? {
731 ArchiveType::FixedIndex => {
732 let index = env.datastore.open_fixed_reader(&path)?;
733 Some(Box::new(index))
734 }
735 ArchiveType::DynamicIndex => {
736 let index = env.datastore.open_dynamic_reader(&path)?;
737 Some(Box::new(index))
738 }
739 _ => { None }
740 };
741 if let Some(index) = index {
742 env.log(format!("register chunks in '{}' from previous backup.", archive_name));
743
744 for pos in 0..index.index_count() {
745 let info = index.chunk_info(pos).unwrap();
746 let size = info.range.end - info.range.start;
747 env.register_chunk(info.digest, size as u32)?;
748 }
749 }
750 }
751
752 env.log(format!("download '{}' from previous backup.", archive_name));
753 crate::api2::helpers::create_download_response(path).await
754 }.boxed()
755 }