]>
Commit | Line | Data |
---|---|---|
cad540e9 | 1 | use std::collections::{HashSet, HashMap}; |
cad540e9 | 2 | |
aeeac29b | 3 | use chrono::{TimeZone, Local}; |
15e9b4ed | 4 | use failure::*; |
9e47c0a5 | 5 | use futures::*; |
cad540e9 WB |
6 | use hyper::http::request::Parts; |
7 | use hyper::{header, Body, Response, StatusCode}; | |
15e9b4ed DM |
8 | use serde_json::{json, Value}; |
9 | ||
552c2259 | 10 | use proxmox::{sortable, identity}; |
cad540e9 | 11 | use proxmox::api::{http_err, list_subdirs_api_method}; |
bb084b9c | 12 | use proxmox::api::{ApiResponseFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, RpcEnvironmentType}; |
cad540e9 WB |
13 | use proxmox::api::router::SubdirMap; |
14 | use proxmox::api::schema::*; | |
8c70e3eb | 15 | use proxmox::tools::{try_block, fs::file_get_contents, fs::file_set_contents}; |
e18a6c9e | 16 | |
cad540e9 | 17 | use crate::api2::types::*; |
e5064ba6 | 18 | use crate::backup::*; |
cad540e9 | 19 | use crate::config::datastore; |
0f778e06 | 20 | use crate::server::WorkerTask; |
cad540e9 | 21 | use crate::tools; |
1629d2ad | 22 | |
8c70e3eb DM |
23 | fn read_backup_index(store: &DataStore, backup_dir: &BackupDir) -> Result<Value, Error> { |
24 | ||
25 | let mut path = store.base_path(); | |
26 | path.push(backup_dir.relative_path()); | |
27 | path.push("index.json.blob"); | |
28 | ||
29 | let raw_data = file_get_contents(&path)?; | |
30 | let data = DataBlob::from_raw(raw_data)?.decode(None)?; | |
4f1e40a2 | 31 | let index_size = data.len(); |
8c70e3eb DM |
32 | let mut result: Value = serde_json::from_reader(&mut &data[..])?; |
33 | ||
4f1e40a2 | 34 | let mut result = result["files"].take(); |
8c70e3eb DM |
35 | |
36 | if result == Value::Null { | |
37 | bail!("missing 'files' property in backup index {:?}", path); | |
38 | } | |
39 | ||
4f1e40a2 DM |
40 | result.as_array_mut().unwrap().push(json!({ |
41 | "filename": "index.json.blob", | |
42 | "size": index_size, | |
43 | })); | |
44 | ||
8c70e3eb DM |
45 | Ok(result) |
46 | } | |
47 | ||
8f579717 DM |
48 | fn group_backups(backup_list: Vec<BackupInfo>) -> HashMap<String, Vec<BackupInfo>> { |
49 | ||
50 | let mut group_hash = HashMap::new(); | |
51 | ||
52 | for info in backup_list { | |
9b492eb2 | 53 | let group_id = info.backup_dir.group().group_path().to_str().unwrap().to_owned(); |
8f579717 DM |
54 | let time_list = group_hash.entry(group_id).or_insert(vec![]); |
55 | time_list.push(info); | |
56 | } | |
57 | ||
58 | group_hash | |
59 | } | |
60 | ||
ad20d198 | 61 | fn list_groups( |
812c6f87 DM |
62 | param: Value, |
63 | _info: &ApiMethod, | |
dd5495d6 | 64 | _rpcenv: &mut dyn RpcEnvironment, |
812c6f87 DM |
65 | ) -> Result<Value, Error> { |
66 | ||
67 | let store = param["store"].as_str().unwrap(); | |
68 | ||
69 | let datastore = DataStore::lookup_datastore(store)?; | |
70 | ||
c0977501 | 71 | let backup_list = BackupInfo::list_backups(&datastore.base_path())?; |
812c6f87 DM |
72 | |
73 | let group_hash = group_backups(backup_list); | |
74 | ||
75 | let mut groups = vec![]; | |
76 | ||
77 | for (_group_id, mut list) in group_hash { | |
78 | ||
2b01a225 | 79 | BackupInfo::sort_list(&mut list, false); |
812c6f87 DM |
80 | |
81 | let info = &list[0]; | |
9b492eb2 | 82 | let group = info.backup_dir.group(); |
812c6f87 DM |
83 | |
84 | groups.push(json!({ | |
1e9a94e5 DM |
85 | "backup-type": group.backup_type(), |
86 | "backup-id": group.backup_id(), | |
9b492eb2 | 87 | "last-backup": info.backup_dir.backup_time().timestamp(), |
ad20d198 DM |
88 | "backup-count": list.len() as u64, |
89 | "files": info.files, | |
812c6f87 DM |
90 | })); |
91 | } | |
92 | ||
93 | Ok(json!(groups)) | |
94 | } | |
8f579717 | 95 | |
01a13423 DM |
96 | fn list_snapshot_files ( |
97 | param: Value, | |
98 | _info: &ApiMethod, | |
dd5495d6 | 99 | _rpcenv: &mut dyn RpcEnvironment, |
01a13423 DM |
100 | ) -> Result<Value, Error> { |
101 | ||
102 | let store = tools::required_string_param(¶m, "store")?; | |
103 | let backup_type = tools::required_string_param(¶m, "backup-type")?; | |
104 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
105 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
106 | ||
d7c24397 | 107 | let datastore = DataStore::lookup_datastore(store)?; |
01a13423 DM |
108 | let snapshot = BackupDir::new(backup_type, backup_id, backup_time); |
109 | ||
d7c24397 DM |
110 | let mut files = read_backup_index(&datastore, &snapshot)?; |
111 | ||
112 | let info = BackupInfo::new(&datastore.base_path(), snapshot)?; | |
01a13423 | 113 | |
d7c24397 DM |
114 | let file_set = files.as_array().unwrap().iter().fold(HashSet::new(), |mut acc, item| { |
115 | acc.insert(item["filename"].as_str().unwrap().to_owned()); | |
116 | acc | |
117 | }); | |
118 | ||
119 | for file in info.files { | |
120 | if file_set.contains(&file) { continue; } | |
121 | files.as_array_mut().unwrap().push(json!({ "filename": file })); | |
122 | } | |
01a13423 | 123 | |
8c70e3eb | 124 | Ok(files) |
01a13423 DM |
125 | } |
126 | ||
6f62c924 DM |
127 | fn delete_snapshots ( |
128 | param: Value, | |
129 | _info: &ApiMethod, | |
dd5495d6 | 130 | _rpcenv: &mut dyn RpcEnvironment, |
6f62c924 DM |
131 | ) -> Result<Value, Error> { |
132 | ||
133 | let store = tools::required_string_param(¶m, "store")?; | |
134 | let backup_type = tools::required_string_param(¶m, "backup-type")?; | |
135 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
136 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
6f62c924 | 137 | |
391d3107 | 138 | let snapshot = BackupDir::new(backup_type, backup_id, backup_time); |
6f62c924 DM |
139 | |
140 | let datastore = DataStore::lookup_datastore(store)?; | |
141 | ||
142 | datastore.remove_backup_dir(&snapshot)?; | |
143 | ||
144 | Ok(Value::Null) | |
145 | } | |
146 | ||
184f17af DM |
147 | fn list_snapshots ( |
148 | param: Value, | |
149 | _info: &ApiMethod, | |
dd5495d6 | 150 | _rpcenv: &mut dyn RpcEnvironment, |
184f17af DM |
151 | ) -> Result<Value, Error> { |
152 | ||
153 | let store = tools::required_string_param(¶m, "store")?; | |
15c847f1 DM |
154 | let backup_type = param["backup-type"].as_str(); |
155 | let backup_id = param["backup-id"].as_str(); | |
184f17af DM |
156 | |
157 | let datastore = DataStore::lookup_datastore(store)?; | |
158 | ||
c0977501 | 159 | let base_path = datastore.base_path(); |
184f17af | 160 | |
15c847f1 | 161 | let backup_list = BackupInfo::list_backups(&base_path)?; |
184f17af DM |
162 | |
163 | let mut snapshots = vec![]; | |
164 | ||
c0977501 | 165 | for info in backup_list { |
15c847f1 DM |
166 | let group = info.backup_dir.group(); |
167 | if let Some(backup_type) = backup_type { | |
168 | if backup_type != group.backup_type() { continue; } | |
169 | } | |
a17a0e7a | 170 | if let Some(backup_id) = backup_id { |
15c847f1 DM |
171 | if backup_id != group.backup_id() { continue; } |
172 | } | |
a17a0e7a DM |
173 | |
174 | let mut result_item = json!({ | |
1e9a94e5 DM |
175 | "backup-type": group.backup_type(), |
176 | "backup-id": group.backup_id(), | |
9b492eb2 | 177 | "backup-time": info.backup_dir.backup_time().timestamp(), |
184f17af | 178 | "files": info.files, |
a17a0e7a DM |
179 | }); |
180 | ||
181 | if let Ok(index) = read_backup_index(&datastore, &info.backup_dir) { | |
182 | let mut backup_size = 0; | |
183 | for item in index.as_array().unwrap().iter() { | |
184 | if let Some(item_size) = item["size"].as_u64() { | |
185 | backup_size += item_size; | |
186 | } | |
187 | } | |
188 | result_item["size"] = backup_size.into(); | |
189 | } | |
190 | ||
191 | snapshots.push(result_item); | |
184f17af DM |
192 | } |
193 | ||
194 | Ok(json!(snapshots)) | |
195 | } | |
196 | ||
0ab08ac9 DM |
197 | #[sortable] |
198 | const API_METHOD_STATUS: ApiMethod = ApiMethod::new( | |
199 | &ApiHandler::Sync(&status), | |
200 | &ObjectSchema::new( | |
201 | "Get datastore status.", | |
202 | &sorted!([ | |
66c49c21 | 203 | ("store", false, &DATASTORE_SCHEMA), |
0ab08ac9 DM |
204 | ]), |
205 | ) | |
206 | ); | |
207 | ||
0eecf38f DM |
208 | fn status( |
209 | param: Value, | |
210 | _info: &ApiMethod, | |
211 | _rpcenv: &mut dyn RpcEnvironment, | |
212 | ) -> Result<Value, Error> { | |
213 | ||
214 | let store = param["store"].as_str().unwrap(); | |
215 | ||
216 | let datastore = DataStore::lookup_datastore(store)?; | |
217 | ||
218 | let base_path = datastore.base_path(); | |
219 | ||
220 | let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() }; | |
221 | ||
222 | use nix::NixPath; | |
223 | ||
224 | let res = base_path.with_nix_path(|cstr| unsafe { libc::statfs64(cstr.as_ptr(), &mut stat) })?; | |
225 | nix::errno::Errno::result(res)?; | |
226 | ||
227 | let bsize = stat.f_bsize as u64; | |
228 | Ok(json!({ | |
229 | "total": stat.f_blocks*bsize, | |
230 | "used": (stat.f_blocks-stat.f_bfree)*bsize, | |
231 | "avail": stat.f_bavail*bsize, | |
232 | })) | |
233 | } | |
234 | ||
255f378a DM |
235 | #[macro_export] |
236 | macro_rules! add_common_prune_prameters { | |
552c2259 DM |
237 | ( [ $( $list1:tt )* ] ) => { |
238 | add_common_prune_prameters!([$( $list1 )* ] , []) | |
239 | }; | |
240 | ( [ $( $list1:tt )* ] , [ $( $list2:tt )* ] ) => { | |
255f378a | 241 | [ |
552c2259 | 242 | $( $list1 )* |
255f378a | 243 | ( |
552c2259 | 244 | "keep-daily", |
255f378a | 245 | true, |
552c2259 | 246 | &IntegerSchema::new("Number of daily backups to keep.") |
255f378a DM |
247 | .minimum(1) |
248 | .schema() | |
249 | ), | |
102d8d41 DM |
250 | ( |
251 | "keep-hourly", | |
252 | true, | |
253 | &IntegerSchema::new("Number of hourly backups to keep.") | |
254 | .minimum(1) | |
255 | .schema() | |
256 | ), | |
255f378a | 257 | ( |
552c2259 | 258 | "keep-last", |
255f378a | 259 | true, |
552c2259 | 260 | &IntegerSchema::new("Number of backups to keep.") |
255f378a DM |
261 | .minimum(1) |
262 | .schema() | |
263 | ), | |
264 | ( | |
552c2259 | 265 | "keep-monthly", |
255f378a | 266 | true, |
552c2259 | 267 | &IntegerSchema::new("Number of monthly backups to keep.") |
255f378a DM |
268 | .minimum(1) |
269 | .schema() | |
270 | ), | |
271 | ( | |
552c2259 | 272 | "keep-weekly", |
255f378a | 273 | true, |
552c2259 | 274 | &IntegerSchema::new("Number of weekly backups to keep.") |
255f378a DM |
275 | .minimum(1) |
276 | .schema() | |
277 | ), | |
278 | ( | |
279 | "keep-yearly", | |
280 | true, | |
281 | &IntegerSchema::new("Number of yearly backups to keep.") | |
282 | .minimum(1) | |
283 | .schema() | |
284 | ), | |
552c2259 | 285 | $( $list2 )* |
255f378a DM |
286 | ] |
287 | } | |
0eecf38f DM |
288 | } |
289 | ||
0ab08ac9 DM |
290 | const API_METHOD_PRUNE: ApiMethod = ApiMethod::new( |
291 | &ApiHandler::Sync(&prune), | |
255f378a | 292 | &ObjectSchema::new( |
0ab08ac9 DM |
293 | "Prune the datastore.", |
294 | &add_common_prune_prameters!([ | |
295 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
296 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), | |
3b03abfe DM |
297 | ("dry-run", true, &BooleanSchema::new( |
298 | "Just show what prune would do, but do not delete anything.") | |
299 | .schema() | |
300 | ), | |
0ab08ac9 | 301 | ],[ |
66c49c21 | 302 | ("store", false, &DATASTORE_SCHEMA), |
0ab08ac9 | 303 | ]) |
255f378a DM |
304 | ) |
305 | ); | |
306 | ||
83b7db02 DM |
307 | fn prune( |
308 | param: Value, | |
309 | _info: &ApiMethod, | |
dd5495d6 | 310 | _rpcenv: &mut dyn RpcEnvironment, |
83b7db02 DM |
311 | ) -> Result<Value, Error> { |
312 | ||
313 | let store = param["store"].as_str().unwrap(); | |
314 | ||
9fdc3ef4 DM |
315 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
316 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
317 | ||
3b03abfe DM |
318 | let dry_run = param["dry-run"].as_bool().unwrap_or(false); |
319 | ||
9fdc3ef4 DM |
320 | let group = BackupGroup::new(backup_type, backup_id); |
321 | ||
83b7db02 DM |
322 | let datastore = DataStore::lookup_datastore(store)?; |
323 | ||
9e3f0088 DM |
324 | let prune_options = PruneOptions { |
325 | keep_last: param["keep-last"].as_u64(), | |
102d8d41 | 326 | keep_hourly: param["keep-hourly"].as_u64(), |
9e3f0088 DM |
327 | keep_daily: param["keep-daily"].as_u64(), |
328 | keep_weekly: param["keep-weekly"].as_u64(), | |
329 | keep_monthly: param["keep-monthly"].as_u64(), | |
330 | keep_yearly: param["keep-yearly"].as_u64(), | |
331 | }; | |
8f579717 | 332 | |
503995c7 DM |
333 | let worker_id = format!("{}_{}_{}", store, backup_type, backup_id); |
334 | ||
163e9bbe | 335 | // We use a WorkerTask just to have a task log, but run synchrounously |
503995c7 | 336 | let worker = WorkerTask::new("prune", Some(worker_id), "root@pam", true)?; |
dd8e744f | 337 | let result = try_block! { |
9e3f0088 | 338 | if !prune_options.keeps_something() { |
9fdc3ef4 | 339 | worker.log("No prune selection - keeping all files."); |
dd8e744f DM |
340 | return Ok(()); |
341 | } else { | |
236a396a | 342 | worker.log(format!("retention options: {}", prune_options.cli_options_string())); |
3b03abfe | 343 | if dry_run { |
503995c7 DM |
344 | worker.log(format!("Testing prune on store \"{}\" group \"{}/{}\"", |
345 | store, backup_type, backup_id)); | |
3b03abfe | 346 | } else { |
503995c7 DM |
347 | worker.log(format!("Starting prune on store \"{}\" group \"{}/{}\"", |
348 | store, backup_type, backup_id)); | |
3b03abfe | 349 | } |
dd8e744f | 350 | } |
8f579717 | 351 | |
aeeac29b | 352 | let list = group.list_backups(&datastore.base_path())?; |
8f579717 | 353 | |
9b783521 | 354 | let mut prune_info = compute_prune_info(list, &prune_options)?; |
dd8e744f | 355 | |
8f0b4c1f DM |
356 | prune_info.reverse(); // delete older snapshots first |
357 | ||
358 | for (info, keep) in prune_info { | |
3b03abfe DM |
359 | let backup_time = info.backup_dir.backup_time(); |
360 | let timestamp = BackupDir::backup_time_to_string(backup_time); | |
361 | let group = info.backup_dir.group(); | |
362 | ||
363 | let msg = format!( | |
364 | "{}/{}/{} {}", | |
365 | group.backup_type(), | |
366 | group.backup_id(), | |
367 | timestamp, | |
368 | if keep { "keep" } else { "remove" }, | |
369 | ); | |
370 | ||
371 | worker.log(msg); | |
372 | ||
373 | if !(dry_run || keep) { | |
8f0b4c1f DM |
374 | datastore.remove_backup_dir(&info.backup_dir)?; |
375 | } | |
8f579717 | 376 | } |
dd8e744f DM |
377 | |
378 | Ok(()) | |
379 | }; | |
380 | ||
381 | worker.log_result(&result); | |
382 | ||
383 | if let Err(err) = result { | |
384 | bail!("prune failed - {}", err); | |
8f579717 | 385 | } |
83b7db02 | 386 | |
163e9bbe | 387 | Ok(json!(worker.to_string())) // return the UPID |
83b7db02 DM |
388 | } |
389 | ||
0ab08ac9 DM |
390 | #[sortable] |
391 | pub const API_METHOD_START_GARBAGE_COLLECTION: ApiMethod = ApiMethod::new( | |
392 | &ApiHandler::Sync(&start_garbage_collection), | |
255f378a | 393 | &ObjectSchema::new( |
0ab08ac9 DM |
394 | "Start garbage collection.", |
395 | &sorted!([ | |
66c49c21 | 396 | ("store", false, &DATASTORE_SCHEMA), |
552c2259 | 397 | ]) |
83b7db02 | 398 | ) |
255f378a | 399 | ); |
83b7db02 | 400 | |
6049b71f DM |
401 | fn start_garbage_collection( |
402 | param: Value, | |
403 | _info: &ApiMethod, | |
dd5495d6 | 404 | rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 405 | ) -> Result<Value, Error> { |
15e9b4ed | 406 | |
3e6a7dee | 407 | let store = param["store"].as_str().unwrap().to_string(); |
15e9b4ed | 408 | |
3e6a7dee | 409 | let datastore = DataStore::lookup_datastore(&store)?; |
15e9b4ed | 410 | |
5a778d92 | 411 | println!("Starting garbage collection on store {}", store); |
15e9b4ed | 412 | |
0f778e06 | 413 | let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false }; |
15e9b4ed | 414 | |
0f778e06 DM |
415 | let upid_str = WorkerTask::new_thread( |
416 | "garbage_collection", Some(store.clone()), "root@pam", to_stdout, move |worker| | |
417 | { | |
418 | worker.log(format!("starting garbage collection on store {}", store)); | |
d4b59ae0 | 419 | datastore.garbage_collection(worker) |
0f778e06 DM |
420 | })?; |
421 | ||
422 | Ok(json!(upid_str)) | |
15e9b4ed DM |
423 | } |
424 | ||
552c2259 | 425 | #[sortable] |
0ab08ac9 DM |
426 | pub const API_METHOD_GARBAGE_COLLECTION_STATUS: ApiMethod = ApiMethod::new( |
427 | &ApiHandler::Sync(&garbage_collection_status), | |
255f378a | 428 | &ObjectSchema::new( |
0ab08ac9 | 429 | "Garbage collection status.", |
552c2259 | 430 | &sorted!([ |
66c49c21 | 431 | ("store", false, &DATASTORE_SCHEMA), |
552c2259 | 432 | ]) |
691c89a0 | 433 | ) |
255f378a | 434 | ); |
691c89a0 | 435 | |
6049b71f DM |
436 | fn garbage_collection_status( |
437 | param: Value, | |
438 | _info: &ApiMethod, | |
dd5495d6 | 439 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 440 | ) -> Result<Value, Error> { |
691c89a0 | 441 | |
5a778d92 | 442 | let store = param["store"].as_str().unwrap(); |
691c89a0 | 443 | |
f2b99c34 DM |
444 | let datastore = DataStore::lookup_datastore(&store)?; |
445 | ||
5a778d92 | 446 | println!("Garbage collection status on store {}", store); |
691c89a0 | 447 | |
f2b99c34 | 448 | let status = datastore.last_gc_status(); |
691c89a0 | 449 | |
f2b99c34 | 450 | Ok(serde_json::to_value(&status)?) |
691c89a0 DM |
451 | } |
452 | ||
691c89a0 | 453 | |
6049b71f DM |
454 | fn get_datastore_list( |
455 | _param: Value, | |
456 | _info: &ApiMethod, | |
dd5495d6 | 457 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 458 | ) -> Result<Value, Error> { |
15e9b4ed DM |
459 | |
460 | let config = datastore::config()?; | |
461 | ||
5a778d92 | 462 | Ok(config.convert_to_array("store")) |
15e9b4ed DM |
463 | } |
464 | ||
0ab08ac9 DM |
465 | #[sortable] |
466 | pub const API_METHOD_DOWNLOAD_FILE: ApiMethod = ApiMethod::new( | |
467 | &ApiHandler::AsyncHttp(&download_file), | |
468 | &ObjectSchema::new( | |
469 | "Download single raw file from backup snapshot.", | |
470 | &sorted!([ | |
66c49c21 | 471 | ("store", false, &DATASTORE_SCHEMA), |
0ab08ac9 DM |
472 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
473 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
474 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
475 | ("file-name", false, &StringSchema::new("Raw file name.") | |
476 | .format(&FILENAME_FORMAT) | |
477 | .schema() | |
478 | ), | |
479 | ]), | |
480 | ) | |
481 | ); | |
691c89a0 | 482 | |
9e47c0a5 DM |
483 | fn download_file( |
484 | _parts: Parts, | |
485 | _req_body: Body, | |
486 | param: Value, | |
255f378a | 487 | _info: &ApiMethod, |
9e47c0a5 | 488 | _rpcenv: Box<dyn RpcEnvironment>, |
bb084b9c | 489 | ) -> ApiResponseFuture { |
9e47c0a5 | 490 | |
ad51d02a DM |
491 | async move { |
492 | let store = tools::required_string_param(¶m, "store")?; | |
f14a8c9a | 493 | |
ad51d02a | 494 | let datastore = DataStore::lookup_datastore(store)?; |
f14a8c9a | 495 | |
ad51d02a | 496 | let file_name = tools::required_string_param(¶m, "file-name")?.to_owned(); |
9e47c0a5 | 497 | |
ad51d02a DM |
498 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
499 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
500 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
9e47c0a5 | 501 | |
ad51d02a DM |
502 | println!("Download {} from {} ({}/{}/{}/{})", file_name, store, |
503 | backup_type, backup_id, Local.timestamp(backup_time, 0), file_name); | |
9e47c0a5 | 504 | |
ad51d02a | 505 | let backup_dir = BackupDir::new(backup_type, backup_id, backup_time); |
9e47c0a5 | 506 | |
ad51d02a DM |
507 | let mut path = datastore.base_path(); |
508 | path.push(backup_dir.relative_path()); | |
509 | path.push(&file_name); | |
510 | ||
511 | let file = tokio::fs::File::open(path) | |
512 | .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err))) | |
513 | .await?; | |
514 | ||
db0cb9ce WB |
515 | let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) |
516 | .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze())); | |
ad51d02a | 517 | let body = Body::wrap_stream(payload); |
9e47c0a5 | 518 | |
ad51d02a DM |
519 | // fixme: set other headers ? |
520 | Ok(Response::builder() | |
521 | .status(StatusCode::OK) | |
522 | .header(header::CONTENT_TYPE, "application/octet-stream") | |
523 | .body(body) | |
524 | .unwrap()) | |
525 | }.boxed() | |
9e47c0a5 DM |
526 | } |
527 | ||
552c2259 | 528 | #[sortable] |
0ab08ac9 DM |
529 | pub const API_METHOD_UPLOAD_BACKUP_LOG: ApiMethod = ApiMethod::new( |
530 | &ApiHandler::AsyncHttp(&upload_backup_log), | |
255f378a DM |
531 | &ObjectSchema::new( |
532 | "Download single raw file from backup snapshot.", | |
552c2259 | 533 | &sorted!([ |
66c49c21 | 534 | ("store", false, &DATASTORE_SCHEMA), |
255f378a | 535 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
0ab08ac9 | 536 | ("backup-id", false, &BACKUP_ID_SCHEMA), |
255f378a | 537 | ("backup-time", false, &BACKUP_TIME_SCHEMA), |
552c2259 | 538 | ]), |
9e47c0a5 | 539 | ) |
255f378a | 540 | ); |
9e47c0a5 | 541 | |
07ee2235 DM |
542 | fn upload_backup_log( |
543 | _parts: Parts, | |
544 | req_body: Body, | |
545 | param: Value, | |
255f378a | 546 | _info: &ApiMethod, |
07ee2235 | 547 | _rpcenv: Box<dyn RpcEnvironment>, |
bb084b9c | 548 | ) -> ApiResponseFuture { |
07ee2235 | 549 | |
ad51d02a DM |
550 | async move { |
551 | let store = tools::required_string_param(¶m, "store")?; | |
07ee2235 | 552 | |
ad51d02a | 553 | let datastore = DataStore::lookup_datastore(store)?; |
07ee2235 | 554 | |
ad51d02a | 555 | let file_name = "client.log.blob"; |
07ee2235 | 556 | |
ad51d02a DM |
557 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
558 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
559 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
07ee2235 | 560 | |
ad51d02a | 561 | let backup_dir = BackupDir::new(backup_type, backup_id, backup_time); |
07ee2235 | 562 | |
ad51d02a DM |
563 | let mut path = datastore.base_path(); |
564 | path.push(backup_dir.relative_path()); | |
565 | path.push(&file_name); | |
07ee2235 | 566 | |
ad51d02a DM |
567 | if path.exists() { |
568 | bail!("backup already contains a log."); | |
569 | } | |
e128d4e8 | 570 | |
ad51d02a DM |
571 | println!("Upload backup log to {}/{}/{}/{}/{}", store, |
572 | backup_type, backup_id, BackupDir::backup_time_to_string(backup_dir.backup_time()), file_name); | |
573 | ||
574 | let data = req_body | |
575 | .map_err(Error::from) | |
576 | .try_fold(Vec::new(), |mut acc, chunk| { | |
577 | acc.extend_from_slice(&*chunk); | |
578 | future::ok::<_, Error>(acc) | |
579 | }) | |
580 | .await?; | |
581 | ||
582 | let blob = DataBlob::from_raw(data)?; | |
583 | // always verify CRC at server side | |
584 | blob.verify_crc()?; | |
585 | let raw_data = blob.raw_data(); | |
586 | file_set_contents(&path, raw_data, None)?; | |
587 | ||
588 | // fixme: use correct formatter | |
589 | Ok(crate::server::formatter::json_response(Ok(Value::Null))) | |
590 | }.boxed() | |
07ee2235 DM |
591 | } |
592 | ||
552c2259 | 593 | #[sortable] |
255f378a DM |
594 | const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ |
595 | ( | |
596 | "download", | |
597 | &Router::new() | |
598 | .download(&API_METHOD_DOWNLOAD_FILE) | |
599 | ), | |
600 | ( | |
601 | "files", | |
602 | &Router::new() | |
603 | .get( | |
604 | &ApiMethod::new( | |
605 | &ApiHandler::Sync(&list_snapshot_files), | |
606 | &ObjectSchema::new( | |
607 | "List snapshot files.", | |
552c2259 | 608 | &sorted!([ |
66c49c21 | 609 | ("store", false, &DATASTORE_SCHEMA), |
255f378a DM |
610 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
611 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
612 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
552c2259 | 613 | ]), |
01a13423 DM |
614 | ) |
615 | ) | |
255f378a DM |
616 | ) |
617 | ), | |
618 | ( | |
619 | "gc", | |
620 | &Router::new() | |
621 | .get(&API_METHOD_GARBAGE_COLLECTION_STATUS) | |
622 | .post(&API_METHOD_START_GARBAGE_COLLECTION) | |
623 | ), | |
624 | ( | |
625 | "groups", | |
626 | &Router::new() | |
627 | .get( | |
628 | &ApiMethod::new( | |
629 | &ApiHandler::Sync(&list_groups), | |
630 | &ObjectSchema::new( | |
631 | "List backup groups.", | |
66c49c21 | 632 | &sorted!([ ("store", false, &DATASTORE_SCHEMA) ]), |
6f62c924 DM |
633 | ) |
634 | ) | |
255f378a DM |
635 | ) |
636 | ), | |
637 | ( | |
638 | "prune", | |
639 | &Router::new() | |
640 | .post(&API_METHOD_PRUNE) | |
641 | ), | |
642 | ( | |
643 | "snapshots", | |
644 | &Router::new() | |
645 | .get( | |
646 | &ApiMethod::new( | |
647 | &ApiHandler::Sync(&list_snapshots), | |
648 | &ObjectSchema::new( | |
649 | "List backup groups.", | |
552c2259 | 650 | &sorted!([ |
66c49c21 | 651 | ("store", false, &DATASTORE_SCHEMA), |
255f378a DM |
652 | ("backup-type", true, &BACKUP_TYPE_SCHEMA), |
653 | ("backup-id", true, &BACKUP_ID_SCHEMA), | |
552c2259 | 654 | ]), |
255f378a | 655 | ) |
6f62c924 | 656 | ) |
255f378a DM |
657 | ) |
658 | .delete( | |
659 | &ApiMethod::new( | |
660 | &ApiHandler::Sync(&delete_snapshots), | |
661 | &ObjectSchema::new( | |
662 | "Delete backup snapshot.", | |
552c2259 | 663 | &sorted!([ |
66c49c21 | 664 | ("store", false, &DATASTORE_SCHEMA), |
255f378a DM |
665 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
666 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
667 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
552c2259 | 668 | ]), |
255f378a DM |
669 | ) |
670 | ) | |
671 | ) | |
672 | ), | |
673 | ( | |
674 | "status", | |
675 | &Router::new() | |
676 | .get(&API_METHOD_STATUS) | |
677 | ), | |
678 | ( | |
679 | "upload-backup-log", | |
680 | &Router::new() | |
681 | .upload(&API_METHOD_UPLOAD_BACKUP_LOG) | |
682 | ), | |
683 | ]; | |
684 | ||
ad51d02a | 685 | const DATASTORE_INFO_ROUTER: Router = Router::new() |
255f378a DM |
686 | .get(&list_subdirs_api_method!(DATASTORE_INFO_SUBDIRS)) |
687 | .subdirs(DATASTORE_INFO_SUBDIRS); | |
688 | ||
689 | ||
690 | pub const ROUTER: Router = Router::new() | |
691 | .get( | |
692 | &ApiMethod::new( | |
693 | &ApiHandler::Sync(&get_datastore_list), | |
694 | &ObjectSchema::new("Directory index.", &[]) | |
6f62c924 | 695 | ) |
255f378a DM |
696 | ) |
697 | .match_all("store", &DATASTORE_INFO_ROUTER); |