]>
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 WB |
11 | use proxmox::api::{http_err, list_subdirs_api_method}; |
12 | use proxmox::api::{ApiFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, RpcEnvironmentType}; | |
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 | ||
0eecf38f DM |
197 | fn status( |
198 | param: Value, | |
199 | _info: &ApiMethod, | |
200 | _rpcenv: &mut dyn RpcEnvironment, | |
201 | ) -> Result<Value, Error> { | |
202 | ||
203 | let store = param["store"].as_str().unwrap(); | |
204 | ||
205 | let datastore = DataStore::lookup_datastore(store)?; | |
206 | ||
207 | let base_path = datastore.base_path(); | |
208 | ||
209 | let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() }; | |
210 | ||
211 | use nix::NixPath; | |
212 | ||
213 | let res = base_path.with_nix_path(|cstr| unsafe { libc::statfs64(cstr.as_ptr(), &mut stat) })?; | |
214 | nix::errno::Errno::result(res)?; | |
215 | ||
216 | let bsize = stat.f_bsize as u64; | |
217 | Ok(json!({ | |
218 | "total": stat.f_blocks*bsize, | |
219 | "used": (stat.f_blocks-stat.f_bfree)*bsize, | |
220 | "avail": stat.f_bavail*bsize, | |
221 | })) | |
222 | } | |
223 | ||
255f378a DM |
224 | #[macro_export] |
225 | macro_rules! add_common_prune_prameters { | |
552c2259 DM |
226 | ( [ $( $list1:tt )* ] ) => { |
227 | add_common_prune_prameters!([$( $list1 )* ] , []) | |
228 | }; | |
229 | ( [ $( $list1:tt )* ] , [ $( $list2:tt )* ] ) => { | |
255f378a | 230 | [ |
552c2259 | 231 | $( $list1 )* |
255f378a | 232 | ( |
552c2259 | 233 | "keep-daily", |
255f378a | 234 | true, |
552c2259 | 235 | &IntegerSchema::new("Number of daily backups to keep.") |
255f378a DM |
236 | .minimum(1) |
237 | .schema() | |
238 | ), | |
239 | ( | |
552c2259 | 240 | "keep-last", |
255f378a | 241 | true, |
552c2259 | 242 | &IntegerSchema::new("Number of backups to keep.") |
255f378a DM |
243 | .minimum(1) |
244 | .schema() | |
245 | ), | |
246 | ( | |
552c2259 | 247 | "keep-monthly", |
255f378a | 248 | true, |
552c2259 | 249 | &IntegerSchema::new("Number of monthly backups to keep.") |
255f378a DM |
250 | .minimum(1) |
251 | .schema() | |
252 | ), | |
253 | ( | |
552c2259 | 254 | "keep-weekly", |
255f378a | 255 | true, |
552c2259 | 256 | &IntegerSchema::new("Number of weekly backups to keep.") |
255f378a DM |
257 | .minimum(1) |
258 | .schema() | |
259 | ), | |
260 | ( | |
261 | "keep-yearly", | |
262 | true, | |
263 | &IntegerSchema::new("Number of yearly backups to keep.") | |
264 | .minimum(1) | |
265 | .schema() | |
266 | ), | |
552c2259 | 267 | $( $list2 )* |
255f378a DM |
268 | ] |
269 | } | |
0eecf38f DM |
270 | } |
271 | ||
255f378a DM |
272 | const API_METHOD_STATUS: ApiMethod = ApiMethod::new( |
273 | &ApiHandler::Sync(&status), | |
274 | &ObjectSchema::new( | |
275 | "Get datastore status.", | |
552c2259 | 276 | &add_common_prune_prameters!([],[ |
255f378a | 277 | ("store", false, &StringSchema::new("Datastore name.").schema()), |
552c2259 | 278 | ]), |
255f378a DM |
279 | ) |
280 | ); | |
281 | ||
83b7db02 DM |
282 | fn prune( |
283 | param: Value, | |
284 | _info: &ApiMethod, | |
dd5495d6 | 285 | _rpcenv: &mut dyn RpcEnvironment, |
83b7db02 DM |
286 | ) -> Result<Value, Error> { |
287 | ||
288 | let store = param["store"].as_str().unwrap(); | |
289 | ||
9fdc3ef4 DM |
290 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
291 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
292 | ||
293 | let group = BackupGroup::new(backup_type, backup_id); | |
294 | ||
83b7db02 DM |
295 | let datastore = DataStore::lookup_datastore(store)?; |
296 | ||
9e3f0088 DM |
297 | let prune_options = PruneOptions { |
298 | keep_last: param["keep-last"].as_u64(), | |
299 | keep_daily: param["keep-daily"].as_u64(), | |
300 | keep_weekly: param["keep-weekly"].as_u64(), | |
301 | keep_monthly: param["keep-monthly"].as_u64(), | |
302 | keep_yearly: param["keep-yearly"].as_u64(), | |
303 | }; | |
8f579717 | 304 | |
dd8e744f DM |
305 | let worker = WorkerTask::new("prune", Some(store.to_owned()), "root@pam", true)?; |
306 | let result = try_block! { | |
9e3f0088 | 307 | if !prune_options.keeps_something() { |
9fdc3ef4 | 308 | worker.log("No prune selection - keeping all files."); |
dd8e744f DM |
309 | return Ok(()); |
310 | } else { | |
311 | worker.log(format!("Starting prune on store {}", store)); | |
312 | } | |
8f579717 | 313 | |
aeeac29b | 314 | let list = group.list_backups(&datastore.base_path())?; |
8f579717 | 315 | |
9b783521 | 316 | let mut prune_info = compute_prune_info(list, &prune_options)?; |
dd8e744f | 317 | |
8f0b4c1f DM |
318 | prune_info.reverse(); // delete older snapshots first |
319 | ||
320 | for (info, keep) in prune_info { | |
321 | if keep { | |
322 | worker.log(format!("keep {:?}", info.backup_dir.relative_path())); | |
323 | } else { | |
324 | worker.log(format!("remove {:?}", info.backup_dir.relative_path())); | |
325 | datastore.remove_backup_dir(&info.backup_dir)?; | |
326 | } | |
8f579717 | 327 | } |
dd8e744f DM |
328 | |
329 | Ok(()) | |
330 | }; | |
331 | ||
332 | worker.log_result(&result); | |
333 | ||
334 | if let Err(err) = result { | |
335 | bail!("prune failed - {}", err); | |
8f579717 | 336 | } |
83b7db02 DM |
337 | |
338 | Ok(json!(null)) | |
339 | } | |
340 | ||
255f378a DM |
341 | const API_METHOD_PRUNE: ApiMethod = ApiMethod::new( |
342 | &ApiHandler::Sync(&prune), | |
343 | &ObjectSchema::new( | |
344 | "Prune the datastore.", | |
552c2259 | 345 | &add_common_prune_prameters!([ |
255f378a | 346 | ("backup-id", false, &BACKUP_ID_SCHEMA), |
552c2259 DM |
347 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
348 | ],[ | |
349 | ("store", false, &StringSchema::new("Datastore name.").schema()), | |
350 | ]) | |
83b7db02 | 351 | ) |
255f378a | 352 | ); |
83b7db02 | 353 | |
6049b71f DM |
354 | fn start_garbage_collection( |
355 | param: Value, | |
356 | _info: &ApiMethod, | |
dd5495d6 | 357 | rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 358 | ) -> Result<Value, Error> { |
15e9b4ed | 359 | |
3e6a7dee | 360 | let store = param["store"].as_str().unwrap().to_string(); |
15e9b4ed | 361 | |
3e6a7dee | 362 | let datastore = DataStore::lookup_datastore(&store)?; |
15e9b4ed | 363 | |
5a778d92 | 364 | println!("Starting garbage collection on store {}", store); |
15e9b4ed | 365 | |
0f778e06 | 366 | let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false }; |
15e9b4ed | 367 | |
0f778e06 DM |
368 | let upid_str = WorkerTask::new_thread( |
369 | "garbage_collection", Some(store.clone()), "root@pam", to_stdout, move |worker| | |
370 | { | |
371 | worker.log(format!("starting garbage collection on store {}", store)); | |
d4b59ae0 | 372 | datastore.garbage_collection(worker) |
0f778e06 DM |
373 | })?; |
374 | ||
375 | Ok(json!(upid_str)) | |
15e9b4ed DM |
376 | } |
377 | ||
552c2259 | 378 | #[sortable] |
255f378a DM |
379 | pub const API_METHOD_START_GARBAGE_COLLECTION: ApiMethod = ApiMethod::new( |
380 | &ApiHandler::Sync(&start_garbage_collection), | |
381 | &ObjectSchema::new( | |
382 | "Start garbage collection.", | |
552c2259 DM |
383 | &sorted!([ |
384 | ("store", false, &StringSchema::new("Datastore name.").schema()), | |
385 | ]) | |
691c89a0 | 386 | ) |
255f378a | 387 | ); |
691c89a0 | 388 | |
6049b71f DM |
389 | fn garbage_collection_status( |
390 | param: Value, | |
391 | _info: &ApiMethod, | |
dd5495d6 | 392 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 393 | ) -> Result<Value, Error> { |
691c89a0 | 394 | |
5a778d92 | 395 | let store = param["store"].as_str().unwrap(); |
691c89a0 | 396 | |
f2b99c34 DM |
397 | let datastore = DataStore::lookup_datastore(&store)?; |
398 | ||
5a778d92 | 399 | println!("Garbage collection status on store {}", store); |
691c89a0 | 400 | |
f2b99c34 | 401 | let status = datastore.last_gc_status(); |
691c89a0 | 402 | |
f2b99c34 | 403 | Ok(serde_json::to_value(&status)?) |
691c89a0 DM |
404 | } |
405 | ||
552c2259 | 406 | #[sortable] |
255f378a DM |
407 | pub const API_METHOD_GARBAGE_COLLECTION_STATUS: ApiMethod = ApiMethod::new( |
408 | &ApiHandler::Sync(&garbage_collection_status), | |
409 | &ObjectSchema::new( | |
410 | "Garbage collection status.", | |
552c2259 DM |
411 | &sorted!([ |
412 | ("store", false, &StringSchema::new("Datastore name.").schema()), | |
413 | ]) | |
691c89a0 | 414 | ) |
255f378a | 415 | ); |
691c89a0 | 416 | |
6049b71f DM |
417 | fn get_datastore_list( |
418 | _param: Value, | |
419 | _info: &ApiMethod, | |
dd5495d6 | 420 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 421 | ) -> Result<Value, Error> { |
15e9b4ed DM |
422 | |
423 | let config = datastore::config()?; | |
424 | ||
5a778d92 | 425 | Ok(config.convert_to_array("store")) |
15e9b4ed DM |
426 | } |
427 | ||
691c89a0 | 428 | |
9e47c0a5 DM |
429 | fn download_file( |
430 | _parts: Parts, | |
431 | _req_body: Body, | |
432 | param: Value, | |
255f378a | 433 | _info: &ApiMethod, |
9e47c0a5 | 434 | _rpcenv: Box<dyn RpcEnvironment>, |
ad51d02a | 435 | ) -> ApiFuture { |
9e47c0a5 | 436 | |
ad51d02a DM |
437 | async move { |
438 | let store = tools::required_string_param(¶m, "store")?; | |
f14a8c9a | 439 | |
ad51d02a | 440 | let datastore = DataStore::lookup_datastore(store)?; |
f14a8c9a | 441 | |
ad51d02a | 442 | let file_name = tools::required_string_param(¶m, "file-name")?.to_owned(); |
9e47c0a5 | 443 | |
ad51d02a DM |
444 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
445 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
446 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
9e47c0a5 | 447 | |
ad51d02a DM |
448 | println!("Download {} from {} ({}/{}/{}/{})", file_name, store, |
449 | backup_type, backup_id, Local.timestamp(backup_time, 0), file_name); | |
9e47c0a5 | 450 | |
ad51d02a | 451 | let backup_dir = BackupDir::new(backup_type, backup_id, backup_time); |
9e47c0a5 | 452 | |
ad51d02a DM |
453 | let mut path = datastore.base_path(); |
454 | path.push(backup_dir.relative_path()); | |
455 | path.push(&file_name); | |
456 | ||
457 | let file = tokio::fs::File::open(path) | |
458 | .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err))) | |
459 | .await?; | |
460 | ||
461 | let payload = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new()) | |
462 | .map_ok(|bytes| hyper::Chunk::from(bytes.freeze())); | |
463 | let body = Body::wrap_stream(payload); | |
9e47c0a5 | 464 | |
ad51d02a DM |
465 | // fixme: set other headers ? |
466 | Ok(Response::builder() | |
467 | .status(StatusCode::OK) | |
468 | .header(header::CONTENT_TYPE, "application/octet-stream") | |
469 | .body(body) | |
470 | .unwrap()) | |
471 | }.boxed() | |
9e47c0a5 DM |
472 | } |
473 | ||
552c2259 | 474 | #[sortable] |
255f378a | 475 | pub const API_METHOD_DOWNLOAD_FILE: ApiMethod = ApiMethod::new( |
329d40b5 | 476 | &ApiHandler::AsyncHttp(&download_file), |
255f378a DM |
477 | &ObjectSchema::new( |
478 | "Download single raw file from backup snapshot.", | |
552c2259 | 479 | &sorted!([ |
255f378a DM |
480 | ("store", false, &StringSchema::new("Datastore name.").schema()), |
481 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), | |
482 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
483 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
484 | ("file-name", false, &StringSchema::new("Raw file name.") | |
485 | .format(&FILENAME_FORMAT) | |
486 | .schema() | |
487 | ), | |
552c2259 | 488 | ]), |
9e47c0a5 | 489 | ) |
255f378a | 490 | ); |
9e47c0a5 | 491 | |
07ee2235 DM |
492 | fn upload_backup_log( |
493 | _parts: Parts, | |
494 | req_body: Body, | |
495 | param: Value, | |
255f378a | 496 | _info: &ApiMethod, |
07ee2235 | 497 | _rpcenv: Box<dyn RpcEnvironment>, |
ad51d02a | 498 | ) -> ApiFuture { |
07ee2235 | 499 | |
ad51d02a DM |
500 | async move { |
501 | let store = tools::required_string_param(¶m, "store")?; | |
07ee2235 | 502 | |
ad51d02a | 503 | let datastore = DataStore::lookup_datastore(store)?; |
07ee2235 | 504 | |
ad51d02a | 505 | let file_name = "client.log.blob"; |
07ee2235 | 506 | |
ad51d02a DM |
507 | let backup_type = tools::required_string_param(¶m, "backup-type")?; |
508 | let backup_id = tools::required_string_param(¶m, "backup-id")?; | |
509 | let backup_time = tools::required_integer_param(¶m, "backup-time")?; | |
07ee2235 | 510 | |
ad51d02a | 511 | let backup_dir = BackupDir::new(backup_type, backup_id, backup_time); |
07ee2235 | 512 | |
ad51d02a DM |
513 | let mut path = datastore.base_path(); |
514 | path.push(backup_dir.relative_path()); | |
515 | path.push(&file_name); | |
07ee2235 | 516 | |
ad51d02a DM |
517 | if path.exists() { |
518 | bail!("backup already contains a log."); | |
519 | } | |
e128d4e8 | 520 | |
ad51d02a DM |
521 | println!("Upload backup log to {}/{}/{}/{}/{}", store, |
522 | backup_type, backup_id, BackupDir::backup_time_to_string(backup_dir.backup_time()), file_name); | |
523 | ||
524 | let data = req_body | |
525 | .map_err(Error::from) | |
526 | .try_fold(Vec::new(), |mut acc, chunk| { | |
527 | acc.extend_from_slice(&*chunk); | |
528 | future::ok::<_, Error>(acc) | |
529 | }) | |
530 | .await?; | |
531 | ||
532 | let blob = DataBlob::from_raw(data)?; | |
533 | // always verify CRC at server side | |
534 | blob.verify_crc()?; | |
535 | let raw_data = blob.raw_data(); | |
536 | file_set_contents(&path, raw_data, None)?; | |
537 | ||
538 | // fixme: use correct formatter | |
539 | Ok(crate::server::formatter::json_response(Ok(Value::Null))) | |
540 | }.boxed() | |
07ee2235 DM |
541 | } |
542 | ||
552c2259 | 543 | #[sortable] |
255f378a | 544 | pub const API_METHOD_UPLOAD_BACKUP_LOG: ApiMethod = ApiMethod::new( |
329d40b5 | 545 | &ApiHandler::AsyncHttp(&upload_backup_log), |
255f378a DM |
546 | &ObjectSchema::new( |
547 | "Download single raw file from backup snapshot.", | |
552c2259 | 548 | &sorted!([ |
255f378a DM |
549 | ("store", false, &StringSchema::new("Datastore name.").schema()), |
550 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), | |
551 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
552 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
552c2259 | 553 | ]), |
07ee2235 | 554 | ) |
255f378a DM |
555 | ); |
556 | ||
557 | const STORE_SCHEMA: Schema = StringSchema::new("Datastore name.").schema(); | |
558 | ||
552c2259 | 559 | #[sortable] |
255f378a DM |
560 | const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ |
561 | ( | |
562 | "download", | |
563 | &Router::new() | |
564 | .download(&API_METHOD_DOWNLOAD_FILE) | |
565 | ), | |
566 | ( | |
567 | "files", | |
568 | &Router::new() | |
569 | .get( | |
570 | &ApiMethod::new( | |
571 | &ApiHandler::Sync(&list_snapshot_files), | |
572 | &ObjectSchema::new( | |
573 | "List snapshot files.", | |
552c2259 | 574 | &sorted!([ |
255f378a DM |
575 | ("store", false, &STORE_SCHEMA), |
576 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), | |
577 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
578 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
552c2259 | 579 | ]), |
01a13423 DM |
580 | ) |
581 | ) | |
255f378a DM |
582 | ) |
583 | ), | |
584 | ( | |
585 | "gc", | |
586 | &Router::new() | |
587 | .get(&API_METHOD_GARBAGE_COLLECTION_STATUS) | |
588 | .post(&API_METHOD_START_GARBAGE_COLLECTION) | |
589 | ), | |
590 | ( | |
591 | "groups", | |
592 | &Router::new() | |
593 | .get( | |
594 | &ApiMethod::new( | |
595 | &ApiHandler::Sync(&list_groups), | |
596 | &ObjectSchema::new( | |
597 | "List backup groups.", | |
552c2259 | 598 | &sorted!([ ("store", false, &STORE_SCHEMA) ]), |
6f62c924 DM |
599 | ) |
600 | ) | |
255f378a DM |
601 | ) |
602 | ), | |
603 | ( | |
604 | "prune", | |
605 | &Router::new() | |
606 | .post(&API_METHOD_PRUNE) | |
607 | ), | |
608 | ( | |
609 | "snapshots", | |
610 | &Router::new() | |
611 | .get( | |
612 | &ApiMethod::new( | |
613 | &ApiHandler::Sync(&list_snapshots), | |
614 | &ObjectSchema::new( | |
615 | "List backup groups.", | |
552c2259 | 616 | &sorted!([ |
255f378a DM |
617 | ("store", false, &STORE_SCHEMA), |
618 | ("backup-type", true, &BACKUP_TYPE_SCHEMA), | |
619 | ("backup-id", true, &BACKUP_ID_SCHEMA), | |
552c2259 | 620 | ]), |
255f378a | 621 | ) |
6f62c924 | 622 | ) |
255f378a DM |
623 | ) |
624 | .delete( | |
625 | &ApiMethod::new( | |
626 | &ApiHandler::Sync(&delete_snapshots), | |
627 | &ObjectSchema::new( | |
628 | "Delete backup snapshot.", | |
552c2259 | 629 | &sorted!([ |
255f378a DM |
630 | ("store", false, &STORE_SCHEMA), |
631 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), | |
632 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
633 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
552c2259 | 634 | ]), |
255f378a DM |
635 | ) |
636 | ) | |
637 | ) | |
638 | ), | |
639 | ( | |
640 | "status", | |
641 | &Router::new() | |
642 | .get(&API_METHOD_STATUS) | |
643 | ), | |
644 | ( | |
645 | "upload-backup-log", | |
646 | &Router::new() | |
647 | .upload(&API_METHOD_UPLOAD_BACKUP_LOG) | |
648 | ), | |
649 | ]; | |
650 | ||
ad51d02a | 651 | const DATASTORE_INFO_ROUTER: Router = Router::new() |
255f378a DM |
652 | .get(&list_subdirs_api_method!(DATASTORE_INFO_SUBDIRS)) |
653 | .subdirs(DATASTORE_INFO_SUBDIRS); | |
654 | ||
655 | ||
656 | pub const ROUTER: Router = Router::new() | |
657 | .get( | |
658 | &ApiMethod::new( | |
659 | &ApiHandler::Sync(&get_datastore_list), | |
660 | &ObjectSchema::new("Directory index.", &[]) | |
6f62c924 | 661 | ) |
255f378a DM |
662 | ) |
663 | .match_all("store", &DATASTORE_INFO_ROUTER); |