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