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