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