]>
Commit | Line | Data |
---|---|---|
ff5d3707 | 1 | use failure::*; |
70235f72 CE |
2 | use nix::unistd::{fork, ForkResult, pipe}; |
3 | use std::os::unix::io::RawFd; | |
27c9affb | 4 | use chrono::{Local, DateTime, Utc, TimeZone}; |
e9c9409a | 5 | use std::path::{Path, PathBuf}; |
2eeaacb9 | 6 | use std::collections::{HashSet, HashMap}; |
70235f72 | 7 | use std::ffi::OsStr; |
bb19af73 | 8 | use std::io::{Write, Seek, SeekFrom}; |
2761d6a4 DM |
9 | use std::os::unix::fs::OpenOptionsExt; |
10 | ||
552c2259 | 11 | use proxmox::{sortable, identity}; |
feaa1ad3 | 12 | use proxmox::tools::fs::{file_get_contents, file_get_json, replace_file, CreateOptions, image_size}; |
501f4fa2 | 13 | use proxmox::sys::linux::tty; |
a47a02ae | 14 | use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment}; |
3d482025 | 15 | use proxmox::api::schema::*; |
7eea56ca | 16 | use proxmox::api::cli::*; |
5830c205 | 17 | use proxmox::api::api; |
ff5d3707 | 18 | |
fe0e04c6 | 19 | use proxmox_backup::tools; |
bbf9e7e9 | 20 | use proxmox_backup::api2::types::*; |
151c6ce2 | 21 | use proxmox_backup::client::*; |
247cdbce | 22 | use proxmox_backup::backup::*; |
7926a3a1 | 23 | use proxmox_backup::pxar::{ self, catalog::* }; |
86eda3eb | 24 | |
fe0e04c6 DM |
25 | //use proxmox_backup::backup::image_index::*; |
26 | //use proxmox_backup::config::datastore; | |
8968258b | 27 | //use proxmox_backup::pxar::encoder::*; |
728797d0 | 28 | //use proxmox_backup::backup::datastore::*; |
23bb8780 | 29 | |
f5f13ebc | 30 | use serde_json::{json, Value}; |
1c0472e8 | 31 | //use hyper::Body; |
2761d6a4 | 32 | use std::sync::{Arc, Mutex}; |
255f378a | 33 | //use regex::Regex; |
d0a03d40 | 34 | use xdg::BaseDirectories; |
ae0be2dd | 35 | |
5a2df000 | 36 | use futures::*; |
c4ff3dce | 37 | use tokio::sync::mpsc; |
ae0be2dd | 38 | |
a05c0c6f | 39 | const ENV_VAR_PBS_FINGERPRINT: &str = "PBS_FINGERPRINT"; |
d1c65727 | 40 | const ENV_VAR_PBS_PASSWORD: &str = "PBS_PASSWORD"; |
a05c0c6f | 41 | |
9ea4bce4 | 42 | proxmox::const_regex! { |
255f378a | 43 | BACKUPSPEC_REGEX = r"^([a-zA-Z0-9_-]+\.(?:pxar|img|conf|log)):(.+)$"; |
ae0be2dd | 44 | } |
33d64b81 | 45 | |
255f378a DM |
46 | const REPO_URL_SCHEMA: Schema = StringSchema::new("Repository URL.") |
47 | .format(&BACKUP_REPO_URL) | |
48 | .max_length(256) | |
49 | .schema(); | |
d0a03d40 | 50 | |
a47a02ae DM |
51 | const BACKUP_SOURCE_SCHEMA: Schema = StringSchema::new( |
52 | "Backup source specification ([<label>:<path>]).") | |
53 | .format(&ApiStringFormat::Pattern(&BACKUPSPEC_REGEX)) | |
54 | .schema(); | |
55 | ||
56 | const KEYFILE_SCHEMA: Schema = StringSchema::new( | |
57 | "Path to encryption key. All data will be encrypted using this key.") | |
58 | .schema(); | |
59 | ||
60 | const CHUNK_SIZE_SCHEMA: Schema = IntegerSchema::new( | |
61 | "Chunk size in KB. Must be a power of 2.") | |
62 | .minimum(64) | |
63 | .maximum(4096) | |
64 | .default(4096) | |
65 | .schema(); | |
66 | ||
2665cef7 DM |
67 | fn get_default_repository() -> Option<String> { |
68 | std::env::var("PBS_REPOSITORY").ok() | |
69 | } | |
70 | ||
71 | fn extract_repository_from_value( | |
72 | param: &Value, | |
73 | ) -> Result<BackupRepository, Error> { | |
74 | ||
75 | let repo_url = param["repository"] | |
76 | .as_str() | |
77 | .map(String::from) | |
78 | .or_else(get_default_repository) | |
79 | .ok_or_else(|| format_err!("unable to get (default) repository"))?; | |
80 | ||
81 | let repo: BackupRepository = repo_url.parse()?; | |
82 | ||
83 | Ok(repo) | |
84 | } | |
85 | ||
86 | fn extract_repository_from_map( | |
87 | param: &HashMap<String, String>, | |
88 | ) -> Option<BackupRepository> { | |
89 | ||
90 | param.get("repository") | |
91 | .map(String::from) | |
92 | .or_else(get_default_repository) | |
93 | .and_then(|repo_url| repo_url.parse::<BackupRepository>().ok()) | |
94 | } | |
95 | ||
d0a03d40 DM |
96 | fn record_repository(repo: &BackupRepository) { |
97 | ||
98 | let base = match BaseDirectories::with_prefix("proxmox-backup") { | |
99 | Ok(v) => v, | |
100 | _ => return, | |
101 | }; | |
102 | ||
103 | // usually $HOME/.cache/proxmox-backup/repo-list | |
104 | let path = match base.place_cache_file("repo-list") { | |
105 | Ok(v) => v, | |
106 | _ => return, | |
107 | }; | |
108 | ||
11377a47 | 109 | let mut data = file_get_json(&path, None).unwrap_or_else(|_| json!({})); |
d0a03d40 DM |
110 | |
111 | let repo = repo.to_string(); | |
112 | ||
113 | data[&repo] = json!{ data[&repo].as_i64().unwrap_or(0) + 1 }; | |
114 | ||
115 | let mut map = serde_json::map::Map::new(); | |
116 | ||
117 | loop { | |
118 | let mut max_used = 0; | |
119 | let mut max_repo = None; | |
120 | for (repo, count) in data.as_object().unwrap() { | |
121 | if map.contains_key(repo) { continue; } | |
122 | if let Some(count) = count.as_i64() { | |
123 | if count > max_used { | |
124 | max_used = count; | |
125 | max_repo = Some(repo); | |
126 | } | |
127 | } | |
128 | } | |
129 | if let Some(repo) = max_repo { | |
130 | map.insert(repo.to_owned(), json!(max_used)); | |
131 | } else { | |
132 | break; | |
133 | } | |
134 | if map.len() > 10 { // store max. 10 repos | |
135 | break; | |
136 | } | |
137 | } | |
138 | ||
139 | let new_data = json!(map); | |
140 | ||
feaa1ad3 | 141 | let _ = replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new()); |
d0a03d40 DM |
142 | } |
143 | ||
49811347 | 144 | fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> { |
d0a03d40 DM |
145 | |
146 | let mut result = vec![]; | |
147 | ||
148 | let base = match BaseDirectories::with_prefix("proxmox-backup") { | |
149 | Ok(v) => v, | |
150 | _ => return result, | |
151 | }; | |
152 | ||
153 | // usually $HOME/.cache/proxmox-backup/repo-list | |
154 | let path = match base.place_cache_file("repo-list") { | |
155 | Ok(v) => v, | |
156 | _ => return result, | |
157 | }; | |
158 | ||
11377a47 | 159 | let data = file_get_json(&path, None).unwrap_or_else(|_| json!({})); |
d0a03d40 DM |
160 | |
161 | if let Some(map) = data.as_object() { | |
49811347 | 162 | for (repo, _count) in map { |
d0a03d40 DM |
163 | result.push(repo.to_owned()); |
164 | } | |
165 | } | |
166 | ||
167 | result | |
168 | } | |
169 | ||
d59dbeca DM |
170 | fn connect(server: &str, userid: &str) -> Result<HttpClient, Error> { |
171 | ||
a05c0c6f DM |
172 | let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok(); |
173 | ||
d1c65727 DM |
174 | use std::env::VarError::*; |
175 | let password = match std::env::var(ENV_VAR_PBS_PASSWORD) { | |
176 | Ok(p) => Some(p), | |
177 | Err(NotUnicode(_)) => bail!(format!("{} contains bad characters", ENV_VAR_PBS_PASSWORD)), | |
178 | Err(NotPresent) => None, | |
179 | }; | |
180 | ||
d59dbeca | 181 | let options = HttpClientOptions::new() |
5030b7ce | 182 | .prefix(Some("proxmox-backup".to_string())) |
d1c65727 | 183 | .password(password) |
d59dbeca | 184 | .interactive(true) |
a05c0c6f | 185 | .fingerprint(fingerprint) |
5a74756c | 186 | .fingerprint_cache(true) |
d59dbeca DM |
187 | .ticket_cache(true); |
188 | ||
189 | HttpClient::new(server, userid, options) | |
190 | } | |
191 | ||
d105176f DM |
192 | async fn view_task_result( |
193 | client: HttpClient, | |
194 | result: Value, | |
195 | output_format: &str, | |
196 | ) -> Result<(), Error> { | |
197 | let data = &result["data"]; | |
198 | if output_format == "text" { | |
199 | if let Some(upid) = data.as_str() { | |
200 | display_task_log(client, upid, true).await?; | |
201 | } | |
202 | } else { | |
203 | format_and_print_result(&data, &output_format); | |
204 | } | |
205 | ||
206 | Ok(()) | |
207 | } | |
208 | ||
42af4b8f DM |
209 | async fn api_datastore_list_snapshots( |
210 | client: &HttpClient, | |
211 | store: &str, | |
212 | group: Option<BackupGroup>, | |
213 | ) -> Result<Vec<SnapshotListItem>, Error> { | |
214 | ||
215 | let path = format!("api2/json/admin/datastore/{}/snapshots", store); | |
216 | ||
217 | let mut args = json!({}); | |
218 | if let Some(group) = group { | |
219 | args["backup-type"] = group.backup_type().into(); | |
220 | args["backup-id"] = group.backup_id().into(); | |
221 | } | |
222 | ||
223 | let mut result = client.get(&path, Some(args)).await?; | |
224 | ||
225 | let list: Vec<SnapshotListItem> = serde_json::from_value(result["data"].take())?; | |
226 | ||
227 | Ok(list) | |
228 | } | |
229 | ||
27c9affb DM |
230 | async fn api_datastore_latest_snapshot( |
231 | client: &HttpClient, | |
232 | store: &str, | |
233 | group: BackupGroup, | |
234 | ) -> Result<(String, String, DateTime<Utc>), Error> { | |
235 | ||
236 | let mut list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?; | |
237 | ||
238 | if list.is_empty() { | |
239 | bail!("backup group {:?} does not contain any snapshots.", group.group_path()); | |
240 | } | |
241 | ||
242 | list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time)); | |
243 | ||
244 | let backup_time = Utc.timestamp(list[0].backup_time, 0); | |
245 | ||
246 | Ok((group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)) | |
247 | } | |
248 | ||
249 | ||
e9722f8b | 250 | async fn backup_directory<P: AsRef<Path>>( |
cf9271e2 | 251 | client: &BackupWriter, |
17d6979a | 252 | dir_path: P, |
247cdbce | 253 | archive_name: &str, |
36898ffc | 254 | chunk_size: Option<usize>, |
2eeaacb9 | 255 | device_set: Option<HashSet<u64>>, |
219ef0e6 | 256 | verbose: bool, |
5b72c9b4 | 257 | skip_lost_and_found: bool, |
f98ac774 | 258 | crypt_config: Option<Arc<CryptConfig>>, |
f1d99e3f | 259 | catalog: Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>, |
6fc053ed | 260 | entries_max: usize, |
2c3891d1 | 261 | ) -> Result<BackupStats, Error> { |
33d64b81 | 262 | |
6fc053ed CE |
263 | let pxar_stream = PxarBackupStream::open( |
264 | dir_path.as_ref(), | |
265 | device_set, | |
266 | verbose, | |
267 | skip_lost_and_found, | |
268 | catalog, | |
269 | entries_max, | |
270 | )?; | |
e9722f8b | 271 | let mut chunk_stream = ChunkStream::new(pxar_stream, chunk_size); |
ff3d3100 | 272 | |
e9722f8b | 273 | let (mut tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks |
5e7a09be | 274 | |
c4ff3dce | 275 | let stream = rx |
e9722f8b | 276 | .map_err(Error::from); |
17d6979a | 277 | |
c4ff3dce | 278 | // spawn chunker inside a separate task so that it can run parallel |
e9722f8b | 279 | tokio::spawn(async move { |
db0cb9ce WB |
280 | while let Some(v) = chunk_stream.next().await { |
281 | let _ = tx.send(v).await; | |
282 | } | |
e9722f8b | 283 | }); |
17d6979a | 284 | |
e9722f8b WB |
285 | let stats = client |
286 | .upload_stream(archive_name, stream, "dynamic", None, crypt_config) | |
287 | .await?; | |
bcd879cf | 288 | |
2c3891d1 | 289 | Ok(stats) |
bcd879cf DM |
290 | } |
291 | ||
e9722f8b | 292 | async fn backup_image<P: AsRef<Path>>( |
cf9271e2 | 293 | client: &BackupWriter, |
6af905c1 DM |
294 | image_path: P, |
295 | archive_name: &str, | |
296 | image_size: u64, | |
36898ffc | 297 | chunk_size: Option<usize>, |
1c0472e8 | 298 | _verbose: bool, |
f98ac774 | 299 | crypt_config: Option<Arc<CryptConfig>>, |
2c3891d1 | 300 | ) -> Result<BackupStats, Error> { |
6af905c1 | 301 | |
6af905c1 DM |
302 | let path = image_path.as_ref().to_owned(); |
303 | ||
e9722f8b | 304 | let file = tokio::fs::File::open(path).await?; |
6af905c1 | 305 | |
db0cb9ce | 306 | let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) |
6af905c1 DM |
307 | .map_err(Error::from); |
308 | ||
36898ffc | 309 | let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024)); |
6af905c1 | 310 | |
e9722f8b WB |
311 | let stats = client |
312 | .upload_stream(archive_name, stream, "fixed", Some(image_size), crypt_config) | |
313 | .await?; | |
6af905c1 | 314 | |
2c3891d1 | 315 | Ok(stats) |
6af905c1 DM |
316 | } |
317 | ||
52c171e4 DM |
318 | fn strip_server_file_expenstion(name: &str) -> String { |
319 | ||
11377a47 DM |
320 | if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") { |
321 | name[..name.len()-5].to_owned() | |
52c171e4 | 322 | } else { |
11377a47 | 323 | name.to_owned() // should not happen |
8e39232a | 324 | } |
8e39232a DM |
325 | } |
326 | ||
a47a02ae DM |
327 | #[api( |
328 | input: { | |
329 | properties: { | |
330 | repository: { | |
331 | schema: REPO_URL_SCHEMA, | |
332 | optional: true, | |
333 | }, | |
334 | "output-format": { | |
335 | schema: OUTPUT_FORMAT, | |
336 | optional: true, | |
337 | }, | |
338 | } | |
339 | } | |
340 | )] | |
341 | /// List backup groups. | |
342 | async fn list_backup_groups(param: Value) -> Result<Value, Error> { | |
812c6f87 | 343 | |
2665cef7 | 344 | let repo = extract_repository_from_value(¶m)?; |
812c6f87 | 345 | |
d59dbeca | 346 | let client = connect(repo.host(), repo.user())?; |
812c6f87 | 347 | |
d0a03d40 | 348 | let path = format!("api2/json/admin/datastore/{}/groups", repo.store()); |
812c6f87 | 349 | |
8a8a4703 | 350 | let mut result = client.get(&path, None).await?; |
812c6f87 | 351 | |
d0a03d40 DM |
352 | record_repository(&repo); |
353 | ||
812c6f87 | 354 | // fixme: implement and use output formatter instead .. |
80822b95 DM |
355 | let list = result["data"].as_array_mut().unwrap(); |
356 | ||
357 | list.sort_unstable_by(|a, b| { | |
358 | let a_id = a["backup-id"].as_str().unwrap(); | |
359 | let a_backup_type = a["backup-type"].as_str().unwrap(); | |
360 | let b_id = b["backup-id"].as_str().unwrap(); | |
361 | let b_backup_type = b["backup-type"].as_str().unwrap(); | |
362 | ||
363 | let type_order = a_backup_type.cmp(b_backup_type); | |
364 | if type_order == std::cmp::Ordering::Equal { | |
365 | a_id.cmp(b_id) | |
366 | } else { | |
367 | type_order | |
368 | } | |
369 | }); | |
812c6f87 | 370 | |
34a816cc DM |
371 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
372 | ||
373 | let mut result = vec![]; | |
374 | ||
812c6f87 DM |
375 | for item in list { |
376 | ||
ad20d198 DM |
377 | let id = item["backup-id"].as_str().unwrap(); |
378 | let btype = item["backup-type"].as_str().unwrap(); | |
379 | let epoch = item["last-backup"].as_i64().unwrap(); | |
fa5d6977 | 380 | let last_backup = Utc.timestamp(epoch, 0); |
ad20d198 | 381 | let backup_count = item["backup-count"].as_u64().unwrap(); |
812c6f87 | 382 | |
1e9a94e5 | 383 | let group = BackupGroup::new(btype, id); |
812c6f87 DM |
384 | |
385 | let path = group.group_path().to_str().unwrap().to_owned(); | |
ad20d198 | 386 | |
52c171e4 DM |
387 | let files = item["files"].as_array().unwrap().iter() |
388 | .map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect(); | |
ad20d198 | 389 | |
34a816cc | 390 | if output_format == "text" { |
fa5d6977 DM |
391 | println!( |
392 | "{:20} | {} | {:5} | {}", | |
393 | path, | |
394 | BackupDir::backup_time_to_string(last_backup), | |
395 | backup_count, | |
396 | tools::join(&files, ' '), | |
397 | ); | |
34a816cc DM |
398 | } else { |
399 | result.push(json!({ | |
400 | "backup-type": btype, | |
401 | "backup-id": id, | |
402 | "last-backup": epoch, | |
403 | "backup-count": backup_count, | |
404 | "files": files, | |
405 | })); | |
406 | } | |
812c6f87 DM |
407 | } |
408 | ||
9aa3f682 | 409 | if output_format != "text" { format_and_print_result(&result.into(), &output_format); } |
34a816cc | 410 | |
812c6f87 DM |
411 | Ok(Value::Null) |
412 | } | |
413 | ||
a47a02ae DM |
414 | #[api( |
415 | input: { | |
416 | properties: { | |
417 | repository: { | |
418 | schema: REPO_URL_SCHEMA, | |
419 | optional: true, | |
420 | }, | |
421 | group: { | |
422 | type: String, | |
423 | description: "Backup group.", | |
424 | optional: true, | |
425 | }, | |
426 | "output-format": { | |
427 | schema: OUTPUT_FORMAT, | |
428 | optional: true, | |
429 | }, | |
430 | } | |
431 | } | |
432 | )] | |
433 | /// List backup snapshots. | |
434 | async fn list_snapshots(param: Value) -> Result<Value, Error> { | |
184f17af | 435 | |
2665cef7 | 436 | let repo = extract_repository_from_value(¶m)?; |
184f17af | 437 | |
34a816cc DM |
438 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
439 | ||
d59dbeca | 440 | let client = connect(repo.host(), repo.user())?; |
184f17af | 441 | |
42af4b8f DM |
442 | let group = if let Some(path) = param["group"].as_str() { |
443 | Some(BackupGroup::parse(path)?) | |
444 | } else { | |
445 | None | |
446 | }; | |
184f17af | 447 | |
42af4b8f | 448 | let mut list = api_datastore_list_snapshots(&client, repo.store(), group).await?; |
15c847f1 | 449 | |
42af4b8f | 450 | list.sort_unstable_by(|a, b| a.backup_time.cmp(&b.backup_time)); |
184f17af | 451 | |
d0a03d40 DM |
452 | record_repository(&repo); |
453 | ||
af9d4afc | 454 | if output_format != "text" { |
42af4b8f | 455 | format_and_print_result(&serde_json::to_value(list)?, &output_format); |
af9d4afc DM |
456 | return Ok(Value::Null); |
457 | } | |
184f17af DM |
458 | |
459 | for item in list { | |
460 | ||
af9d4afc | 461 | let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time); |
184f17af DM |
462 | |
463 | let path = snapshot.relative_path().to_str().unwrap().to_owned(); | |
464 | ||
af9d4afc DM |
465 | let files = item.files.iter() |
466 | .map(|v| strip_server_file_expenstion(&v)) | |
467 | .collect(); | |
184f17af | 468 | |
af9d4afc DM |
469 | let size_str = if let Some(size) = item.size { |
470 | size.to_string() | |
34a816cc | 471 | } else { |
af9d4afc DM |
472 | String::from("-") |
473 | }; | |
474 | println!("{} | {} | {}", path, size_str, tools::join(&files, ' ')); | |
184f17af DM |
475 | } |
476 | ||
477 | Ok(Value::Null) | |
478 | } | |
479 | ||
a47a02ae DM |
480 | #[api( |
481 | input: { | |
482 | properties: { | |
483 | repository: { | |
484 | schema: REPO_URL_SCHEMA, | |
485 | optional: true, | |
486 | }, | |
487 | snapshot: { | |
488 | type: String, | |
489 | description: "Snapshot path.", | |
490 | }, | |
491 | } | |
492 | } | |
493 | )] | |
494 | /// Forget (remove) backup snapshots. | |
495 | async fn forget_snapshots(param: Value) -> Result<Value, Error> { | |
6f62c924 | 496 | |
2665cef7 | 497 | let repo = extract_repository_from_value(¶m)?; |
6f62c924 DM |
498 | |
499 | let path = tools::required_string_param(¶m, "snapshot")?; | |
500 | let snapshot = BackupDir::parse(path)?; | |
501 | ||
d59dbeca | 502 | let mut client = connect(repo.host(), repo.user())?; |
6f62c924 | 503 | |
9e391bb7 | 504 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); |
6f62c924 | 505 | |
8a8a4703 DM |
506 | let result = client.delete(&path, Some(json!({ |
507 | "backup-type": snapshot.group().backup_type(), | |
508 | "backup-id": snapshot.group().backup_id(), | |
509 | "backup-time": snapshot.backup_time().timestamp(), | |
510 | }))).await?; | |
6f62c924 | 511 | |
d0a03d40 DM |
512 | record_repository(&repo); |
513 | ||
6f62c924 DM |
514 | Ok(result) |
515 | } | |
516 | ||
a47a02ae DM |
517 | #[api( |
518 | input: { | |
519 | properties: { | |
520 | repository: { | |
521 | schema: REPO_URL_SCHEMA, | |
522 | optional: true, | |
523 | }, | |
524 | } | |
525 | } | |
526 | )] | |
527 | /// Try to login. If successful, store ticket. | |
528 | async fn api_login(param: Value) -> Result<Value, Error> { | |
e240d8be DM |
529 | |
530 | let repo = extract_repository_from_value(¶m)?; | |
531 | ||
d59dbeca | 532 | let client = connect(repo.host(), repo.user())?; |
8a8a4703 | 533 | client.login().await?; |
e240d8be DM |
534 | |
535 | record_repository(&repo); | |
536 | ||
537 | Ok(Value::Null) | |
538 | } | |
539 | ||
a47a02ae DM |
540 | #[api( |
541 | input: { | |
542 | properties: { | |
543 | repository: { | |
544 | schema: REPO_URL_SCHEMA, | |
545 | optional: true, | |
546 | }, | |
547 | } | |
548 | } | |
549 | )] | |
550 | /// Logout (delete stored ticket). | |
551 | fn api_logout(param: Value) -> Result<Value, Error> { | |
e240d8be DM |
552 | |
553 | let repo = extract_repository_from_value(¶m)?; | |
554 | ||
5030b7ce | 555 | delete_ticket_info("proxmox-backup", repo.host(), repo.user())?; |
e240d8be DM |
556 | |
557 | Ok(Value::Null) | |
558 | } | |
559 | ||
a47a02ae DM |
560 | #[api( |
561 | input: { | |
562 | properties: { | |
563 | repository: { | |
564 | schema: REPO_URL_SCHEMA, | |
565 | optional: true, | |
566 | }, | |
567 | snapshot: { | |
568 | type: String, | |
569 | description: "Snapshot path.", | |
570 | }, | |
571 | } | |
572 | } | |
573 | )] | |
574 | /// Dump catalog. | |
575 | async fn dump_catalog(param: Value) -> Result<Value, Error> { | |
9049a8cf DM |
576 | |
577 | let repo = extract_repository_from_value(¶m)?; | |
578 | ||
579 | let path = tools::required_string_param(¶m, "snapshot")?; | |
580 | let snapshot = BackupDir::parse(path)?; | |
581 | ||
11377a47 | 582 | let keyfile = param["keyfile"].as_str().map(PathBuf::from); |
9049a8cf DM |
583 | |
584 | let crypt_config = match keyfile { | |
585 | None => None, | |
586 | Some(path) => { | |
6d20a29d | 587 | let (key, _) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
9025312a | 588 | Some(Arc::new(CryptConfig::new(key)?)) |
9049a8cf DM |
589 | } |
590 | }; | |
591 | ||
d59dbeca | 592 | let client = connect(repo.host(), repo.user())?; |
9049a8cf | 593 | |
8a8a4703 DM |
594 | let client = BackupReader::start( |
595 | client, | |
596 | crypt_config.clone(), | |
597 | repo.store(), | |
598 | &snapshot.group().backup_type(), | |
599 | &snapshot.group().backup_id(), | |
600 | snapshot.backup_time(), | |
601 | true, | |
602 | ).await?; | |
9049a8cf | 603 | |
8a8a4703 | 604 | let manifest = client.download_manifest().await?; |
d2267b11 | 605 | |
8a8a4703 | 606 | let index = client.download_dynamic_index(&manifest, CATALOG_NAME).await?; |
bf6e3217 | 607 | |
8a8a4703 | 608 | let most_used = index.find_most_used_chunks(8); |
bf6e3217 | 609 | |
8a8a4703 | 610 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); |
bf6e3217 | 611 | |
8a8a4703 | 612 | let mut reader = BufferedDynamicReader::new(index, chunk_reader); |
9049a8cf | 613 | |
8a8a4703 DM |
614 | let mut catalogfile = std::fs::OpenOptions::new() |
615 | .write(true) | |
616 | .read(true) | |
617 | .custom_flags(libc::O_TMPFILE) | |
618 | .open("/tmp")?; | |
d2267b11 | 619 | |
8a8a4703 DM |
620 | std::io::copy(&mut reader, &mut catalogfile) |
621 | .map_err(|err| format_err!("unable to download catalog - {}", err))?; | |
a84ef4c2 | 622 | |
8a8a4703 | 623 | catalogfile.seek(SeekFrom::Start(0))?; |
9049a8cf | 624 | |
8a8a4703 | 625 | let mut catalog_reader = CatalogReader::new(catalogfile); |
9049a8cf | 626 | |
8a8a4703 | 627 | catalog_reader.dump()?; |
e9722f8b | 628 | |
8a8a4703 | 629 | record_repository(&repo); |
9049a8cf DM |
630 | |
631 | Ok(Value::Null) | |
632 | } | |
633 | ||
a47a02ae DM |
634 | #[api( |
635 | input: { | |
636 | properties: { | |
637 | repository: { | |
638 | schema: REPO_URL_SCHEMA, | |
639 | optional: true, | |
640 | }, | |
641 | snapshot: { | |
642 | type: String, | |
643 | description: "Snapshot path.", | |
644 | }, | |
645 | "output-format": { | |
646 | schema: OUTPUT_FORMAT, | |
647 | optional: true, | |
648 | }, | |
649 | } | |
650 | } | |
651 | )] | |
652 | /// List snapshot files. | |
653 | async fn list_snapshot_files(param: Value) -> Result<Value, Error> { | |
52c171e4 DM |
654 | |
655 | let repo = extract_repository_from_value(¶m)?; | |
656 | ||
657 | let path = tools::required_string_param(¶m, "snapshot")?; | |
658 | let snapshot = BackupDir::parse(path)?; | |
659 | ||
660 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); | |
661 | ||
d59dbeca | 662 | let client = connect(repo.host(), repo.user())?; |
52c171e4 DM |
663 | |
664 | let path = format!("api2/json/admin/datastore/{}/files", repo.store()); | |
665 | ||
8a8a4703 DM |
666 | let mut result = client.get(&path, Some(json!({ |
667 | "backup-type": snapshot.group().backup_type(), | |
668 | "backup-id": snapshot.group().backup_id(), | |
669 | "backup-time": snapshot.backup_time().timestamp(), | |
670 | }))).await?; | |
52c171e4 DM |
671 | |
672 | record_repository(&repo); | |
673 | ||
8c70e3eb | 674 | let list: Value = result["data"].take(); |
52c171e4 DM |
675 | |
676 | if output_format == "text" { | |
8c70e3eb DM |
677 | for item in list.as_array().unwrap().iter() { |
678 | println!( | |
679 | "{} {}", | |
680 | strip_server_file_expenstion(item["filename"].as_str().unwrap()), | |
681 | item["size"].as_u64().unwrap_or(0), | |
682 | ); | |
52c171e4 DM |
683 | } |
684 | } else { | |
8c70e3eb | 685 | format_and_print_result(&list, &output_format); |
52c171e4 DM |
686 | } |
687 | ||
688 | Ok(Value::Null) | |
689 | } | |
690 | ||
a47a02ae | 691 | #[api( |
94913f35 | 692 | input: { |
a47a02ae DM |
693 | properties: { |
694 | repository: { | |
695 | schema: REPO_URL_SCHEMA, | |
696 | optional: true, | |
697 | }, | |
94913f35 DM |
698 | "output-format": { |
699 | schema: OUTPUT_FORMAT, | |
700 | optional: true, | |
701 | }, | |
702 | }, | |
703 | }, | |
a47a02ae DM |
704 | )] |
705 | /// Start garbage collection for a specific repository. | |
706 | async fn start_garbage_collection(param: Value) -> Result<Value, Error> { | |
8cc0d6af | 707 | |
2665cef7 | 708 | let repo = extract_repository_from_value(¶m)?; |
e5f7def4 | 709 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
8cc0d6af | 710 | |
d59dbeca | 711 | let mut client = connect(repo.host(), repo.user())?; |
8cc0d6af | 712 | |
d0a03d40 | 713 | let path = format!("api2/json/admin/datastore/{}/gc", repo.store()); |
8cc0d6af | 714 | |
8a8a4703 | 715 | let result = client.post(&path, None).await?; |
8cc0d6af | 716 | |
8a8a4703 | 717 | record_repository(&repo); |
d0a03d40 | 718 | |
8a8a4703 | 719 | view_task_result(client, result, &output_format).await?; |
e5f7def4 | 720 | |
e5f7def4 | 721 | Ok(Value::Null) |
8cc0d6af | 722 | } |
33d64b81 | 723 | |
ae0be2dd DM |
724 | fn parse_backupspec(value: &str) -> Result<(&str, &str), Error> { |
725 | ||
255f378a | 726 | if let Some(caps) = (BACKUPSPEC_REGEX.regex_obj)().captures(value) { |
ae0be2dd DM |
727 | return Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str())); |
728 | } | |
729 | bail!("unable to parse directory specification '{}'", value); | |
730 | } | |
731 | ||
bf6e3217 DM |
732 | fn spawn_catalog_upload( |
733 | client: Arc<BackupWriter>, | |
734 | crypt_config: Option<Arc<CryptConfig>>, | |
735 | ) -> Result< | |
736 | ( | |
f1d99e3f | 737 | Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>, |
bf6e3217 DM |
738 | tokio::sync::oneshot::Receiver<Result<BackupStats, Error>> |
739 | ), Error> | |
740 | { | |
f1d99e3f DM |
741 | let (catalog_tx, catalog_rx) = std::sync::mpsc::sync_channel(10); // allow to buffer 10 writes |
742 | let catalog_stream = crate::tools::StdChannelStream(catalog_rx); | |
bf6e3217 DM |
743 | let catalog_chunk_size = 512*1024; |
744 | let catalog_chunk_stream = ChunkStream::new(catalog_stream, Some(catalog_chunk_size)); | |
745 | ||
f1d99e3f | 746 | let catalog = Arc::new(Mutex::new(CatalogWriter::new(crate::tools::StdChannelWriter::new(catalog_tx))?)); |
bf6e3217 DM |
747 | |
748 | let (catalog_result_tx, catalog_result_rx) = tokio::sync::oneshot::channel(); | |
749 | ||
750 | tokio::spawn(async move { | |
751 | let catalog_upload_result = client | |
752 | .upload_stream(CATALOG_NAME, catalog_chunk_stream, "dynamic", None, crypt_config) | |
753 | .await; | |
754 | ||
755 | if let Err(ref err) = catalog_upload_result { | |
756 | eprintln!("catalog upload error - {}", err); | |
757 | client.cancel(); | |
758 | } | |
759 | ||
760 | let _ = catalog_result_tx.send(catalog_upload_result); | |
761 | }); | |
762 | ||
763 | Ok((catalog, catalog_result_rx)) | |
764 | } | |
765 | ||
a47a02ae DM |
766 | #[api( |
767 | input: { | |
768 | properties: { | |
769 | backupspec: { | |
770 | type: Array, | |
771 | description: "List of backup source specifications ([<label.ext>:<path>] ...)", | |
772 | items: { | |
773 | schema: BACKUP_SOURCE_SCHEMA, | |
774 | } | |
775 | }, | |
776 | repository: { | |
777 | schema: REPO_URL_SCHEMA, | |
778 | optional: true, | |
779 | }, | |
780 | "include-dev": { | |
781 | description: "Include mountpoints with same st_dev number (see ``man fstat``) as specified files.", | |
782 | optional: true, | |
783 | items: { | |
784 | type: String, | |
785 | description: "Path to file.", | |
786 | } | |
787 | }, | |
788 | keyfile: { | |
789 | schema: KEYFILE_SCHEMA, | |
790 | optional: true, | |
791 | }, | |
792 | "skip-lost-and-found": { | |
793 | type: Boolean, | |
794 | description: "Skip lost+found directory.", | |
795 | optional: true, | |
796 | }, | |
797 | "backup-type": { | |
798 | schema: BACKUP_TYPE_SCHEMA, | |
799 | optional: true, | |
800 | }, | |
801 | "backup-id": { | |
802 | schema: BACKUP_ID_SCHEMA, | |
803 | optional: true, | |
804 | }, | |
805 | "backup-time": { | |
806 | schema: BACKUP_TIME_SCHEMA, | |
807 | optional: true, | |
808 | }, | |
809 | "chunk-size": { | |
810 | schema: CHUNK_SIZE_SCHEMA, | |
811 | optional: true, | |
812 | }, | |
6fc053ed CE |
813 | "entries-max": { |
814 | type: Integer, | |
815 | description: "Max number of entries to hold in memory.", | |
816 | optional: true, | |
817 | default: pxar::ENCODER_MAX_ENTRIES as isize, | |
818 | }, | |
e02c3d46 DM |
819 | "verbose": { |
820 | type: Boolean, | |
821 | description: "Verbose output.", | |
822 | optional: true, | |
823 | }, | |
a47a02ae DM |
824 | } |
825 | } | |
826 | )] | |
827 | /// Create (host) backup. | |
828 | async fn create_backup( | |
6049b71f DM |
829 | param: Value, |
830 | _info: &ApiMethod, | |
dd5495d6 | 831 | _rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 832 | ) -> Result<Value, Error> { |
ff5d3707 | 833 | |
2665cef7 | 834 | let repo = extract_repository_from_value(¶m)?; |
ae0be2dd DM |
835 | |
836 | let backupspec_list = tools::required_array_param(¶m, "backupspec")?; | |
a914a774 | 837 | |
eed6db39 DM |
838 | let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false); |
839 | ||
5b72c9b4 DM |
840 | let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false); |
841 | ||
219ef0e6 DM |
842 | let verbose = param["verbose"].as_bool().unwrap_or(false); |
843 | ||
ca5d0b61 DM |
844 | let backup_time_opt = param["backup-time"].as_i64(); |
845 | ||
36898ffc | 846 | let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize); |
2d9d143a | 847 | |
247cdbce DM |
848 | if let Some(size) = chunk_size_opt { |
849 | verify_chunk_size(size)?; | |
2d9d143a DM |
850 | } |
851 | ||
11377a47 | 852 | let keyfile = param["keyfile"].as_str().map(PathBuf::from); |
6d0983db | 853 | |
f69adc81 | 854 | let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename()); |
fba30411 | 855 | |
bbf9e7e9 | 856 | let backup_type = param["backup-type"].as_str().unwrap_or("host"); |
ca5d0b61 | 857 | |
2eeaacb9 DM |
858 | let include_dev = param["include-dev"].as_array(); |
859 | ||
6fc053ed CE |
860 | let entries_max = param["entries-max"].as_u64().unwrap_or(pxar::ENCODER_MAX_ENTRIES as u64); |
861 | ||
2eeaacb9 DM |
862 | let mut devices = if all_file_systems { None } else { Some(HashSet::new()) }; |
863 | ||
864 | if let Some(include_dev) = include_dev { | |
865 | if all_file_systems { | |
866 | bail!("option 'all-file-systems' conflicts with option 'include-dev'"); | |
867 | } | |
868 | ||
869 | let mut set = HashSet::new(); | |
870 | for path in include_dev { | |
871 | let path = path.as_str().unwrap(); | |
872 | let stat = nix::sys::stat::stat(path) | |
873 | .map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?; | |
874 | set.insert(stat.st_dev); | |
875 | } | |
876 | devices = Some(set); | |
877 | } | |
878 | ||
ae0be2dd | 879 | let mut upload_list = vec![]; |
a914a774 | 880 | |
79679c2d | 881 | enum BackupType { PXAR, IMAGE, CONFIG, LOGFILE }; |
6af905c1 | 882 | |
bf6e3217 DM |
883 | let mut upload_catalog = false; |
884 | ||
ae0be2dd DM |
885 | for backupspec in backupspec_list { |
886 | let (target, filename) = parse_backupspec(backupspec.as_str().unwrap())?; | |
bcd879cf | 887 | |
eb1804c5 DM |
888 | use std::os::unix::fs::FileTypeExt; |
889 | ||
3fa71727 CE |
890 | let metadata = std::fs::metadata(filename) |
891 | .map_err(|err| format_err!("unable to access '{}' - {}", filename, err))?; | |
eb1804c5 | 892 | let file_type = metadata.file_type(); |
23bb8780 | 893 | |
4af0ee05 | 894 | let extension = target.rsplit('.').next() |
11377a47 | 895 | .ok_or_else(|| format_err!("missing target file extenion '{}'", target))?; |
bcd879cf | 896 | |
ec8a9bb9 DM |
897 | match extension { |
898 | "pxar" => { | |
899 | if !file_type.is_dir() { | |
900 | bail!("got unexpected file type (expected directory)"); | |
901 | } | |
4af0ee05 | 902 | upload_list.push((BackupType::PXAR, filename.to_owned(), format!("{}.didx", target), 0)); |
bf6e3217 | 903 | upload_catalog = true; |
ec8a9bb9 DM |
904 | } |
905 | "img" => { | |
eb1804c5 | 906 | |
ec8a9bb9 DM |
907 | if !(file_type.is_file() || file_type.is_block_device()) { |
908 | bail!("got unexpected file type (expected file or block device)"); | |
909 | } | |
eb1804c5 | 910 | |
e18a6c9e | 911 | let size = image_size(&PathBuf::from(filename))?; |
23bb8780 | 912 | |
ec8a9bb9 | 913 | if size == 0 { bail!("got zero-sized file '{}'", filename); } |
ae0be2dd | 914 | |
4af0ee05 | 915 | upload_list.push((BackupType::IMAGE, filename.to_owned(), format!("{}.fidx", target), size)); |
ec8a9bb9 DM |
916 | } |
917 | "conf" => { | |
918 | if !file_type.is_file() { | |
919 | bail!("got unexpected file type (expected regular file)"); | |
920 | } | |
4af0ee05 | 921 | upload_list.push((BackupType::CONFIG, filename.to_owned(), format!("{}.blob", target), metadata.len())); |
ec8a9bb9 | 922 | } |
79679c2d DM |
923 | "log" => { |
924 | if !file_type.is_file() { | |
925 | bail!("got unexpected file type (expected regular file)"); | |
926 | } | |
4af0ee05 | 927 | upload_list.push((BackupType::LOGFILE, filename.to_owned(), format!("{}.blob", target), metadata.len())); |
79679c2d | 928 | } |
ec8a9bb9 DM |
929 | _ => { |
930 | bail!("got unknown archive extension '{}'", extension); | |
931 | } | |
ae0be2dd DM |
932 | } |
933 | } | |
934 | ||
11377a47 | 935 | let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0); |
ae0be2dd | 936 | |
d59dbeca | 937 | let client = connect(repo.host(), repo.user())?; |
d0a03d40 DM |
938 | record_repository(&repo); |
939 | ||
ca5d0b61 DM |
940 | println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time)); |
941 | ||
f69adc81 | 942 | println!("Client name: {}", proxmox::tools::nodename()); |
ca5d0b61 DM |
943 | |
944 | let start_time = Local::now(); | |
945 | ||
7a6cfbd9 | 946 | println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)); |
51144821 | 947 | |
bb823140 DM |
948 | let (crypt_config, rsa_encrypted_key) = match keyfile { |
949 | None => (None, None), | |
6d0983db | 950 | Some(path) => { |
6d20a29d | 951 | let (key, created) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
bb823140 DM |
952 | |
953 | let crypt_config = CryptConfig::new(key)?; | |
954 | ||
955 | let path = master_pubkey_path()?; | |
956 | if path.exists() { | |
e18a6c9e | 957 | let pem_data = file_get_contents(&path)?; |
bb823140 DM |
958 | let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?; |
959 | let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?; | |
960 | (Some(Arc::new(crypt_config)), Some(enc_key)) | |
961 | } else { | |
962 | (Some(Arc::new(crypt_config)), None) | |
963 | } | |
6d0983db DM |
964 | } |
965 | }; | |
f98ac774 | 966 | |
8a8a4703 DM |
967 | let client = BackupWriter::start( |
968 | client, | |
969 | repo.store(), | |
970 | backup_type, | |
971 | &backup_id, | |
972 | backup_time, | |
973 | verbose, | |
974 | ).await?; | |
975 | ||
976 | let snapshot = BackupDir::new(backup_type, backup_id, backup_time.timestamp()); | |
977 | let mut manifest = BackupManifest::new(snapshot); | |
978 | ||
979 | let (catalog, catalog_result_rx) = spawn_catalog_upload(client.clone(), crypt_config.clone())?; | |
980 | ||
981 | for (backup_type, filename, target, size) in upload_list { | |
982 | match backup_type { | |
983 | BackupType::CONFIG => { | |
984 | println!("Upload config file '{}' to '{:?}' as {}", filename, repo, target); | |
985 | let stats = client | |
986 | .upload_blob_from_file(&filename, &target, crypt_config.clone(), true) | |
987 | .await?; | |
1e8da0a7 | 988 | manifest.add_file(target, stats.size, stats.csum)?; |
8a8a4703 DM |
989 | } |
990 | BackupType::LOGFILE => { // fixme: remove - not needed anymore ? | |
991 | println!("Upload log file '{}' to '{:?}' as {}", filename, repo, target); | |
992 | let stats = client | |
993 | .upload_blob_from_file(&filename, &target, crypt_config.clone(), true) | |
994 | .await?; | |
1e8da0a7 | 995 | manifest.add_file(target, stats.size, stats.csum)?; |
8a8a4703 DM |
996 | } |
997 | BackupType::PXAR => { | |
998 | println!("Upload directory '{}' to '{:?}' as {}", filename, repo, target); | |
999 | catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?; | |
1000 | let stats = backup_directory( | |
1001 | &client, | |
1002 | &filename, | |
1003 | &target, | |
1004 | chunk_size_opt, | |
1005 | devices.clone(), | |
1006 | verbose, | |
1007 | skip_lost_and_found, | |
1008 | crypt_config.clone(), | |
1009 | catalog.clone(), | |
6fc053ed | 1010 | entries_max as usize, |
8a8a4703 | 1011 | ).await?; |
1e8da0a7 | 1012 | manifest.add_file(target, stats.size, stats.csum)?; |
8a8a4703 DM |
1013 | catalog.lock().unwrap().end_directory()?; |
1014 | } | |
1015 | BackupType::IMAGE => { | |
1016 | println!("Upload image '{}' to '{:?}' as {}", filename, repo, target); | |
1017 | let stats = backup_image( | |
1018 | &client, | |
1019 | &filename, | |
1020 | &target, | |
1021 | size, | |
1022 | chunk_size_opt, | |
1023 | verbose, | |
1024 | crypt_config.clone(), | |
1025 | ).await?; | |
1e8da0a7 | 1026 | manifest.add_file(target, stats.size, stats.csum)?; |
6af905c1 DM |
1027 | } |
1028 | } | |
8a8a4703 | 1029 | } |
4818c8b6 | 1030 | |
8a8a4703 DM |
1031 | // finalize and upload catalog |
1032 | if upload_catalog { | |
1033 | let mutex = Arc::try_unwrap(catalog) | |
1034 | .map_err(|_| format_err!("unable to get catalog (still used)"))?; | |
1035 | let mut catalog = mutex.into_inner().unwrap(); | |
bf6e3217 | 1036 | |
8a8a4703 | 1037 | catalog.finish()?; |
2761d6a4 | 1038 | |
8a8a4703 | 1039 | drop(catalog); // close upload stream |
2761d6a4 | 1040 | |
8a8a4703 | 1041 | let stats = catalog_result_rx.await??; |
9d135fe6 | 1042 | |
1e8da0a7 | 1043 | manifest.add_file(CATALOG_NAME.to_owned(), stats.size, stats.csum)?; |
8a8a4703 | 1044 | } |
2761d6a4 | 1045 | |
8a8a4703 DM |
1046 | if let Some(rsa_encrypted_key) = rsa_encrypted_key { |
1047 | let target = "rsa-encrypted.key"; | |
1048 | println!("Upload RSA encoded key to '{:?}' as {}", repo, target); | |
1049 | let stats = client | |
1050 | .upload_blob_from_data(rsa_encrypted_key, target, None, false, false) | |
1051 | .await?; | |
1e8da0a7 | 1052 | manifest.add_file(format!("{}.blob", target), stats.size, stats.csum)?; |
8a8a4703 DM |
1053 | |
1054 | // openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t | |
1055 | /* | |
1056 | let mut buffer2 = vec![0u8; rsa.size() as usize]; | |
1057 | let pem_data = file_get_contents("master-private.pem")?; | |
1058 | let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?; | |
1059 | let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?; | |
1060 | println!("TEST {} {:?}", len, buffer2); | |
1061 | */ | |
1062 | } | |
9f46c7de | 1063 | |
8a8a4703 DM |
1064 | // create manifest (index.json) |
1065 | let manifest = manifest.into_json(); | |
2c3891d1 | 1066 | |
8a8a4703 DM |
1067 | println!("Upload index.json to '{:?}'", repo); |
1068 | let manifest = serde_json::to_string_pretty(&manifest)?.into(); | |
1069 | client | |
1070 | .upload_blob_from_data(manifest, MANIFEST_BLOB_NAME, crypt_config.clone(), true, true) | |
1071 | .await?; | |
2c3891d1 | 1072 | |
8a8a4703 | 1073 | client.finish().await?; |
c4ff3dce | 1074 | |
8a8a4703 DM |
1075 | let end_time = Local::now(); |
1076 | let elapsed = end_time.signed_duration_since(start_time); | |
1077 | println!("Duration: {}", elapsed); | |
3ec3ec3f | 1078 | |
8a8a4703 | 1079 | println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)); |
3d5c11e5 | 1080 | |
8a8a4703 | 1081 | Ok(Value::Null) |
f98ea63d DM |
1082 | } |
1083 | ||
d0a03d40 | 1084 | fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
f98ea63d DM |
1085 | |
1086 | let mut result = vec![]; | |
1087 | ||
1088 | let data: Vec<&str> = arg.splitn(2, ':').collect(); | |
1089 | ||
bff11030 | 1090 | if data.len() != 2 { |
8968258b DM |
1091 | result.push(String::from("root.pxar:/")); |
1092 | result.push(String::from("etc.pxar:/etc")); | |
bff11030 DM |
1093 | return result; |
1094 | } | |
f98ea63d | 1095 | |
496a6784 | 1096 | let files = tools::complete_file_name(data[1], param); |
f98ea63d DM |
1097 | |
1098 | for file in files { | |
1099 | result.push(format!("{}:{}", data[0], file)); | |
1100 | } | |
1101 | ||
1102 | result | |
ff5d3707 | 1103 | } |
1104 | ||
88892ea8 DM |
1105 | fn dump_image<W: Write>( |
1106 | client: Arc<BackupReader>, | |
1107 | crypt_config: Option<Arc<CryptConfig>>, | |
1108 | index: FixedIndexReader, | |
1109 | mut writer: W, | |
fd04ca7a | 1110 | verbose: bool, |
88892ea8 DM |
1111 | ) -> Result<(), Error> { |
1112 | ||
1113 | let most_used = index.find_most_used_chunks(8); | |
1114 | ||
1115 | let mut chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
1116 | ||
1117 | // Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy | |
1118 | // and thus slows down reading. Instead, directly use RemoteChunkReader | |
fd04ca7a DM |
1119 | let mut per = 0; |
1120 | let mut bytes = 0; | |
1121 | let start_time = std::time::Instant::now(); | |
1122 | ||
88892ea8 DM |
1123 | for pos in 0..index.index_count() { |
1124 | let digest = index.index_digest(pos).unwrap(); | |
1125 | let raw_data = chunk_reader.read_chunk(&digest)?; | |
1126 | writer.write_all(&raw_data)?; | |
fd04ca7a DM |
1127 | bytes += raw_data.len(); |
1128 | if verbose { | |
1129 | let next_per = ((pos+1)*100)/index.index_count(); | |
1130 | if per != next_per { | |
1131 | eprintln!("progress {}% (read {} bytes, duration {} sec)", | |
1132 | next_per, bytes, start_time.elapsed().as_secs()); | |
1133 | per = next_per; | |
1134 | } | |
1135 | } | |
88892ea8 DM |
1136 | } |
1137 | ||
fd04ca7a DM |
1138 | let end_time = std::time::Instant::now(); |
1139 | let elapsed = end_time.duration_since(start_time); | |
1140 | eprintln!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)", | |
1141 | bytes, | |
1142 | elapsed.as_secs_f64(), | |
1143 | bytes as f64/(1024.0*1024.0*elapsed.as_secs_f64()) | |
1144 | ); | |
1145 | ||
1146 | ||
88892ea8 DM |
1147 | Ok(()) |
1148 | } | |
1149 | ||
a47a02ae DM |
1150 | #[api( |
1151 | input: { | |
1152 | properties: { | |
1153 | repository: { | |
1154 | schema: REPO_URL_SCHEMA, | |
1155 | optional: true, | |
1156 | }, | |
1157 | snapshot: { | |
1158 | type: String, | |
1159 | description: "Group/Snapshot path.", | |
1160 | }, | |
1161 | "archive-name": { | |
1162 | description: "Backup archive name.", | |
1163 | type: String, | |
1164 | }, | |
1165 | target: { | |
1166 | type: String, | |
90c815bf | 1167 | description: r###"Target directory path. Use '-' to write to standard output. |
8a8a4703 | 1168 | |
5eee6d89 | 1169 | We do not extraxt '.pxar' archives when writing to standard output. |
8a8a4703 | 1170 | |
a47a02ae DM |
1171 | "### |
1172 | }, | |
1173 | "allow-existing-dirs": { | |
1174 | type: Boolean, | |
1175 | description: "Do not fail if directories already exists.", | |
1176 | optional: true, | |
1177 | }, | |
1178 | keyfile: { | |
1179 | schema: KEYFILE_SCHEMA, | |
1180 | optional: true, | |
1181 | }, | |
1182 | } | |
1183 | } | |
1184 | )] | |
1185 | /// Restore backup repository. | |
1186 | async fn restore(param: Value) -> Result<Value, Error> { | |
2665cef7 | 1187 | let repo = extract_repository_from_value(¶m)?; |
9f912493 | 1188 | |
86eda3eb DM |
1189 | let verbose = param["verbose"].as_bool().unwrap_or(false); |
1190 | ||
46d5aa0a DM |
1191 | let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false); |
1192 | ||
d5c34d98 DM |
1193 | let archive_name = tools::required_string_param(¶m, "archive-name")?; |
1194 | ||
d59dbeca | 1195 | let client = connect(repo.host(), repo.user())?; |
d0a03d40 | 1196 | |
d0a03d40 | 1197 | record_repository(&repo); |
d5c34d98 | 1198 | |
9f912493 | 1199 | let path = tools::required_string_param(¶m, "snapshot")?; |
9f912493 | 1200 | |
86eda3eb | 1201 | let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { |
d5c34d98 | 1202 | let group = BackupGroup::parse(path)?; |
27c9affb | 1203 | api_datastore_latest_snapshot(&client, repo.store(), group).await? |
d5c34d98 DM |
1204 | } else { |
1205 | let snapshot = BackupDir::parse(path)?; | |
86eda3eb DM |
1206 | (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) |
1207 | }; | |
9f912493 | 1208 | |
d5c34d98 | 1209 | let target = tools::required_string_param(¶m, "target")?; |
bf125261 | 1210 | let target = if target == "-" { None } else { Some(target) }; |
2ae7d196 | 1211 | |
11377a47 | 1212 | let keyfile = param["keyfile"].as_str().map(PathBuf::from); |
2ae7d196 | 1213 | |
86eda3eb DM |
1214 | let crypt_config = match keyfile { |
1215 | None => None, | |
1216 | Some(path) => { | |
6d20a29d | 1217 | let (key, _) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
86eda3eb DM |
1218 | Some(Arc::new(CryptConfig::new(key)?)) |
1219 | } | |
1220 | }; | |
d5c34d98 | 1221 | |
afb4cd28 DM |
1222 | let server_archive_name = if archive_name.ends_with(".pxar") { |
1223 | format!("{}.didx", archive_name) | |
1224 | } else if archive_name.ends_with(".img") { | |
1225 | format!("{}.fidx", archive_name) | |
1226 | } else { | |
f8100e96 | 1227 | format!("{}.blob", archive_name) |
afb4cd28 | 1228 | }; |
9f912493 | 1229 | |
296c50ba DM |
1230 | let client = BackupReader::start( |
1231 | client, | |
1232 | crypt_config.clone(), | |
1233 | repo.store(), | |
1234 | &backup_type, | |
1235 | &backup_id, | |
1236 | backup_time, | |
1237 | true, | |
1238 | ).await?; | |
86eda3eb | 1239 | |
f06b820a | 1240 | let manifest = client.download_manifest().await?; |
02fcf372 | 1241 | |
ad6e5a6f | 1242 | if server_archive_name == MANIFEST_BLOB_NAME { |
f06b820a | 1243 | let backup_index_data = manifest.into_json().to_string(); |
02fcf372 | 1244 | if let Some(target) = target { |
feaa1ad3 | 1245 | replace_file(target, backup_index_data.as_bytes(), CreateOptions::new())?; |
02fcf372 DM |
1246 | } else { |
1247 | let stdout = std::io::stdout(); | |
1248 | let mut writer = stdout.lock(); | |
296c50ba | 1249 | writer.write_all(backup_index_data.as_bytes()) |
02fcf372 DM |
1250 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; |
1251 | } | |
1252 | ||
1253 | } else if server_archive_name.ends_with(".blob") { | |
d2267b11 | 1254 | |
bb19af73 | 1255 | let mut reader = client.download_blob(&manifest, &server_archive_name).await?; |
f8100e96 | 1256 | |
bf125261 | 1257 | if let Some(target) = target { |
0d986280 DM |
1258 | let mut writer = std::fs::OpenOptions::new() |
1259 | .write(true) | |
1260 | .create(true) | |
1261 | .create_new(true) | |
1262 | .open(target) | |
1263 | .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?; | |
1264 | std::io::copy(&mut reader, &mut writer)?; | |
bf125261 DM |
1265 | } else { |
1266 | let stdout = std::io::stdout(); | |
1267 | let mut writer = stdout.lock(); | |
0d986280 | 1268 | std::io::copy(&mut reader, &mut writer) |
bf125261 DM |
1269 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; |
1270 | } | |
f8100e96 DM |
1271 | |
1272 | } else if server_archive_name.ends_with(".didx") { | |
86eda3eb | 1273 | |
c3d84a22 | 1274 | let index = client.download_dynamic_index(&manifest, &server_archive_name).await?; |
df65bd3d | 1275 | |
f4bf7dfc DM |
1276 | let most_used = index.find_most_used_chunks(8); |
1277 | ||
1278 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
1279 | ||
afb4cd28 | 1280 | let mut reader = BufferedDynamicReader::new(index, chunk_reader); |
86eda3eb | 1281 | |
bf125261 | 1282 | if let Some(target) = target { |
86eda3eb | 1283 | |
47651f95 | 1284 | let feature_flags = pxar::flags::DEFAULT; |
f701d033 DM |
1285 | let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags); |
1286 | decoder.set_callback(move |path| { | |
bf125261 | 1287 | if verbose { |
fd04ca7a | 1288 | eprintln!("{:?}", path); |
bf125261 DM |
1289 | } |
1290 | Ok(()) | |
1291 | }); | |
6a879109 CE |
1292 | decoder.set_allow_existing_dirs(allow_existing_dirs); |
1293 | ||
fa7e957c | 1294 | decoder.restore(Path::new(target), &Vec::new())?; |
bf125261 | 1295 | } else { |
88892ea8 DM |
1296 | let mut writer = std::fs::OpenOptions::new() |
1297 | .write(true) | |
1298 | .open("/dev/stdout") | |
1299 | .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))?; | |
afb4cd28 | 1300 | |
bf125261 DM |
1301 | std::io::copy(&mut reader, &mut writer) |
1302 | .map_err(|err| format_err!("unable to pipe data - {}", err))?; | |
1303 | } | |
afb4cd28 | 1304 | } else if server_archive_name.ends_with(".fidx") { |
afb4cd28 | 1305 | |
72050500 | 1306 | let index = client.download_fixed_index(&manifest, &server_archive_name).await?; |
df65bd3d | 1307 | |
88892ea8 DM |
1308 | let mut writer = if let Some(target) = target { |
1309 | std::fs::OpenOptions::new() | |
bf125261 DM |
1310 | .write(true) |
1311 | .create(true) | |
1312 | .create_new(true) | |
1313 | .open(target) | |
88892ea8 | 1314 | .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))? |
bf125261 | 1315 | } else { |
88892ea8 DM |
1316 | std::fs::OpenOptions::new() |
1317 | .write(true) | |
1318 | .open("/dev/stdout") | |
1319 | .map_err(|err| format_err!("unable to open /dev/stdout - {}", err))? | |
1320 | }; | |
afb4cd28 | 1321 | |
fd04ca7a | 1322 | dump_image(client.clone(), crypt_config.clone(), index, &mut writer, verbose)?; |
88892ea8 DM |
1323 | |
1324 | } else { | |
f8100e96 | 1325 | bail!("unknown archive file extension (expected .pxar of .img)"); |
3031e44c | 1326 | } |
fef44d4f DM |
1327 | |
1328 | Ok(Value::Null) | |
45db6f89 DM |
1329 | } |
1330 | ||
a47a02ae DM |
1331 | #[api( |
1332 | input: { | |
1333 | properties: { | |
1334 | repository: { | |
1335 | schema: REPO_URL_SCHEMA, | |
1336 | optional: true, | |
1337 | }, | |
1338 | snapshot: { | |
1339 | type: String, | |
1340 | description: "Group/Snapshot path.", | |
1341 | }, | |
1342 | logfile: { | |
1343 | type: String, | |
1344 | description: "The path to the log file you want to upload.", | |
1345 | }, | |
1346 | keyfile: { | |
1347 | schema: KEYFILE_SCHEMA, | |
1348 | optional: true, | |
1349 | }, | |
1350 | } | |
1351 | } | |
1352 | )] | |
1353 | /// Upload backup log file. | |
1354 | async fn upload_log(param: Value) -> Result<Value, Error> { | |
ec34f7eb DM |
1355 | |
1356 | let logfile = tools::required_string_param(¶m, "logfile")?; | |
1357 | let repo = extract_repository_from_value(¶m)?; | |
1358 | ||
1359 | let snapshot = tools::required_string_param(¶m, "snapshot")?; | |
1360 | let snapshot = BackupDir::parse(snapshot)?; | |
1361 | ||
d59dbeca | 1362 | let mut client = connect(repo.host(), repo.user())?; |
ec34f7eb | 1363 | |
11377a47 | 1364 | let keyfile = param["keyfile"].as_str().map(PathBuf::from); |
ec34f7eb DM |
1365 | |
1366 | let crypt_config = match keyfile { | |
1367 | None => None, | |
1368 | Some(path) => { | |
6d20a29d | 1369 | let (key, _created) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
ec34f7eb | 1370 | let crypt_config = CryptConfig::new(key)?; |
9025312a | 1371 | Some(Arc::new(crypt_config)) |
ec34f7eb DM |
1372 | } |
1373 | }; | |
1374 | ||
e18a6c9e | 1375 | let data = file_get_contents(logfile)?; |
ec34f7eb | 1376 | |
7123ff7d | 1377 | let blob = DataBlob::encode(&data, crypt_config.as_ref().map(Arc::as_ref), true)?; |
ec34f7eb DM |
1378 | |
1379 | let raw_data = blob.into_inner(); | |
1380 | ||
1381 | let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store()); | |
1382 | ||
1383 | let args = json!({ | |
1384 | "backup-type": snapshot.group().backup_type(), | |
1385 | "backup-id": snapshot.group().backup_id(), | |
1386 | "backup-time": snapshot.backup_time().timestamp(), | |
1387 | }); | |
1388 | ||
1389 | let body = hyper::Body::from(raw_data); | |
1390 | ||
8a8a4703 | 1391 | client.upload("application/octet-stream", body, &path, Some(args)).await |
ec34f7eb DM |
1392 | } |
1393 | ||
032d3ad8 DM |
1394 | const API_METHOD_PRUNE: ApiMethod = ApiMethod::new( |
1395 | &ApiHandler::Async(&prune), | |
1396 | &ObjectSchema::new( | |
1397 | "Prune a backup repository.", | |
1398 | &proxmox_backup::add_common_prune_prameters!([ | |
1399 | ("dry-run", true, &BooleanSchema::new( | |
1400 | "Just show what prune would do, but do not delete anything.") | |
1401 | .schema()), | |
1402 | ("group", false, &StringSchema::new("Backup group.").schema()), | |
1403 | ], [ | |
1404 | ("output-format", true, &OUTPUT_FORMAT), | |
1405 | ("repository", true, &REPO_URL_SCHEMA), | |
1406 | ]) | |
1407 | ) | |
1408 | ); | |
1409 | ||
1410 | fn prune<'a>( | |
1411 | param: Value, | |
1412 | _info: &ApiMethod, | |
1413 | _rpcenv: &'a mut dyn RpcEnvironment, | |
1414 | ) -> proxmox::api::ApiFuture<'a> { | |
1415 | async move { | |
1416 | prune_async(param).await | |
1417 | }.boxed() | |
1418 | } | |
83b7db02 | 1419 | |
032d3ad8 | 1420 | async fn prune_async(mut param: Value) -> Result<Value, Error> { |
2665cef7 | 1421 | let repo = extract_repository_from_value(¶m)?; |
83b7db02 | 1422 | |
d59dbeca | 1423 | let mut client = connect(repo.host(), repo.user())?; |
83b7db02 | 1424 | |
d0a03d40 | 1425 | let path = format!("api2/json/admin/datastore/{}/prune", repo.store()); |
83b7db02 | 1426 | |
9fdc3ef4 DM |
1427 | let group = tools::required_string_param(¶m, "group")?; |
1428 | let group = BackupGroup::parse(group)?; | |
163e9bbe | 1429 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
9fdc3ef4 | 1430 | |
ea7a7ef2 DM |
1431 | param.as_object_mut().unwrap().remove("repository"); |
1432 | param.as_object_mut().unwrap().remove("group"); | |
163e9bbe | 1433 | param.as_object_mut().unwrap().remove("output-format"); |
ea7a7ef2 DM |
1434 | |
1435 | param["backup-type"] = group.backup_type().into(); | |
1436 | param["backup-id"] = group.backup_id().into(); | |
83b7db02 | 1437 | |
87c42375 | 1438 | let result = client.post(&path, Some(param)).await?; |
74fa81b8 | 1439 | |
87c42375 | 1440 | record_repository(&repo); |
3b03abfe | 1441 | |
87c42375 | 1442 | view_task_result(client, result, &output_format).await?; |
d0a03d40 | 1443 | |
43a406fd | 1444 | Ok(Value::Null) |
83b7db02 DM |
1445 | } |
1446 | ||
a47a02ae DM |
1447 | #[api( |
1448 | input: { | |
1449 | properties: { | |
1450 | repository: { | |
1451 | schema: REPO_URL_SCHEMA, | |
1452 | optional: true, | |
1453 | }, | |
1454 | "output-format": { | |
1455 | schema: OUTPUT_FORMAT, | |
1456 | optional: true, | |
1457 | }, | |
1458 | } | |
1459 | } | |
1460 | )] | |
1461 | /// Get repository status. | |
1462 | async fn status(param: Value) -> Result<Value, Error> { | |
34a816cc DM |
1463 | |
1464 | let repo = extract_repository_from_value(¶m)?; | |
1465 | ||
1466 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); | |
1467 | ||
d59dbeca | 1468 | let client = connect(repo.host(), repo.user())?; |
34a816cc DM |
1469 | |
1470 | let path = format!("api2/json/admin/datastore/{}/status", repo.store()); | |
1471 | ||
1dc117bb | 1472 | let mut result = client.get(&path, None).await?; |
34a816cc DM |
1473 | |
1474 | record_repository(&repo); | |
1475 | ||
1476 | if output_format == "text" { | |
1dc117bb DM |
1477 | let result: StorageStatus = serde_json::from_value(result["data"].take())?; |
1478 | ||
1479 | let roundup = result.total/200; | |
34a816cc DM |
1480 | |
1481 | println!( | |
1482 | "total: {} used: {} ({} %) available: {}", | |
1dc117bb DM |
1483 | result.total, |
1484 | result.used, | |
1485 | ((result.used+roundup)*100)/result.total, | |
1486 | result.avail, | |
34a816cc DM |
1487 | ); |
1488 | } else { | |
1dc117bb | 1489 | format_and_print_result(&result["data"], &output_format); |
34a816cc DM |
1490 | } |
1491 | ||
1492 | Ok(Value::Null) | |
1493 | } | |
1494 | ||
5a2df000 | 1495 | // like get, but simply ignore errors and return Null instead |
e9722f8b | 1496 | async fn try_get(repo: &BackupRepository, url: &str) -> Value { |
024f11bb | 1497 | |
a05c0c6f | 1498 | let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok(); |
d1c65727 | 1499 | let password = std::env::var(ENV_VAR_PBS_PASSWORD).ok(); |
a05c0c6f | 1500 | |
d59dbeca | 1501 | let options = HttpClientOptions::new() |
5030b7ce | 1502 | .prefix(Some("proxmox-backup".to_string())) |
d1c65727 | 1503 | .password(password) |
d59dbeca | 1504 | .interactive(false) |
a05c0c6f | 1505 | .fingerprint(fingerprint) |
5a74756c | 1506 | .fingerprint_cache(true) |
d59dbeca DM |
1507 | .ticket_cache(true); |
1508 | ||
1509 | let client = match HttpClient::new(repo.host(), repo.user(), options) { | |
45cdce06 DM |
1510 | Ok(v) => v, |
1511 | _ => return Value::Null, | |
1512 | }; | |
b2388518 | 1513 | |
e9722f8b | 1514 | let mut resp = match client.get(url, None).await { |
b2388518 DM |
1515 | Ok(v) => v, |
1516 | _ => return Value::Null, | |
1517 | }; | |
1518 | ||
1519 | if let Some(map) = resp.as_object_mut() { | |
1520 | if let Some(data) = map.remove("data") { | |
1521 | return data; | |
1522 | } | |
1523 | } | |
1524 | Value::Null | |
1525 | } | |
1526 | ||
b2388518 | 1527 | fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
3f06d6fb | 1528 | proxmox_backup::tools::runtime::main(async { complete_backup_group_do(param).await }) |
e9722f8b WB |
1529 | } |
1530 | ||
1531 | async fn complete_backup_group_do(param: &HashMap<String, String>) -> Vec<String> { | |
024f11bb | 1532 | |
b2388518 DM |
1533 | let mut result = vec![]; |
1534 | ||
2665cef7 | 1535 | let repo = match extract_repository_from_map(param) { |
b2388518 | 1536 | Some(v) => v, |
024f11bb DM |
1537 | _ => return result, |
1538 | }; | |
1539 | ||
b2388518 DM |
1540 | let path = format!("api2/json/admin/datastore/{}/groups", repo.store()); |
1541 | ||
e9722f8b | 1542 | let data = try_get(&repo, &path).await; |
b2388518 DM |
1543 | |
1544 | if let Some(list) = data.as_array() { | |
024f11bb | 1545 | for item in list { |
98f0b972 DM |
1546 | if let (Some(backup_id), Some(backup_type)) = |
1547 | (item["backup-id"].as_str(), item["backup-type"].as_str()) | |
1548 | { | |
1549 | result.push(format!("{}/{}", backup_type, backup_id)); | |
024f11bb DM |
1550 | } |
1551 | } | |
1552 | } | |
1553 | ||
1554 | result | |
1555 | } | |
1556 | ||
b2388518 | 1557 | fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
3f06d6fb | 1558 | proxmox_backup::tools::runtime::main(async { complete_group_or_snapshot_do(arg, param).await }) |
e9722f8b WB |
1559 | } |
1560 | ||
1561 | async fn complete_group_or_snapshot_do(arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
b2388518 | 1562 | |
b2388518 | 1563 | if arg.matches('/').count() < 2 { |
e9722f8b | 1564 | let groups = complete_backup_group_do(param).await; |
543a260f | 1565 | let mut result = vec![]; |
b2388518 DM |
1566 | for group in groups { |
1567 | result.push(group.to_string()); | |
1568 | result.push(format!("{}/", group)); | |
1569 | } | |
1570 | return result; | |
1571 | } | |
1572 | ||
e9722f8b | 1573 | complete_backup_snapshot_do(param).await |
543a260f | 1574 | } |
b2388518 | 1575 | |
3fb53e07 | 1576 | fn complete_backup_snapshot(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
3f06d6fb | 1577 | proxmox_backup::tools::runtime::main(async { complete_backup_snapshot_do(param).await }) |
e9722f8b WB |
1578 | } |
1579 | ||
1580 | async fn complete_backup_snapshot_do(param: &HashMap<String, String>) -> Vec<String> { | |
543a260f DM |
1581 | |
1582 | let mut result = vec![]; | |
1583 | ||
1584 | let repo = match extract_repository_from_map(param) { | |
1585 | Some(v) => v, | |
1586 | _ => return result, | |
1587 | }; | |
1588 | ||
1589 | let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store()); | |
b2388518 | 1590 | |
e9722f8b | 1591 | let data = try_get(&repo, &path).await; |
b2388518 DM |
1592 | |
1593 | if let Some(list) = data.as_array() { | |
1594 | for item in list { | |
1595 | if let (Some(backup_id), Some(backup_type), Some(backup_time)) = | |
1596 | (item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64()) | |
1597 | { | |
1598 | let snapshot = BackupDir::new(backup_type, backup_id, backup_time); | |
1599 | result.push(snapshot.relative_path().to_str().unwrap().to_owned()); | |
1600 | } | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | result | |
1605 | } | |
1606 | ||
45db6f89 | 1607 | fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
3f06d6fb | 1608 | proxmox_backup::tools::runtime::main(async { complete_server_file_name_do(param).await }) |
e9722f8b WB |
1609 | } |
1610 | ||
1611 | async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<String> { | |
08dc340a DM |
1612 | |
1613 | let mut result = vec![]; | |
1614 | ||
2665cef7 | 1615 | let repo = match extract_repository_from_map(param) { |
08dc340a DM |
1616 | Some(v) => v, |
1617 | _ => return result, | |
1618 | }; | |
1619 | ||
1620 | let snapshot = match param.get("snapshot") { | |
1621 | Some(path) => { | |
1622 | match BackupDir::parse(path) { | |
1623 | Ok(v) => v, | |
1624 | _ => return result, | |
1625 | } | |
1626 | } | |
1627 | _ => return result, | |
1628 | }; | |
1629 | ||
1630 | let query = tools::json_object_to_query(json!({ | |
1631 | "backup-type": snapshot.group().backup_type(), | |
1632 | "backup-id": snapshot.group().backup_id(), | |
1633 | "backup-time": snapshot.backup_time().timestamp(), | |
1634 | })).unwrap(); | |
1635 | ||
1636 | let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query); | |
1637 | ||
e9722f8b | 1638 | let data = try_get(&repo, &path).await; |
08dc340a DM |
1639 | |
1640 | if let Some(list) = data.as_array() { | |
1641 | for item in list { | |
c4f025eb | 1642 | if let Some(filename) = item["filename"].as_str() { |
08dc340a DM |
1643 | result.push(filename.to_owned()); |
1644 | } | |
1645 | } | |
1646 | } | |
1647 | ||
45db6f89 DM |
1648 | result |
1649 | } | |
1650 | ||
1651 | fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
52c171e4 | 1652 | complete_server_file_name(arg, param) |
e9722f8b WB |
1653 | .iter() |
1654 | .map(|v| strip_server_file_expenstion(&v)) | |
1655 | .collect() | |
08dc340a DM |
1656 | } |
1657 | ||
0ec9e1b0 DM |
1658 | fn complete_pxar_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> { |
1659 | complete_server_file_name(arg, param) | |
1660 | .iter() | |
1661 | .filter_map(|v| { | |
1662 | let name = strip_server_file_expenstion(&v); | |
1663 | if name.ends_with(".pxar") { | |
1664 | Some(name) | |
1665 | } else { | |
1666 | None | |
1667 | } | |
1668 | }) | |
1669 | .collect() | |
1670 | } | |
1671 | ||
49811347 DM |
1672 | fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> { |
1673 | ||
1674 | let mut result = vec![]; | |
1675 | ||
1676 | let mut size = 64; | |
1677 | loop { | |
1678 | result.push(size.to_string()); | |
11377a47 | 1679 | size *= 2; |
49811347 DM |
1680 | if size > 4096 { break; } |
1681 | } | |
1682 | ||
1683 | result | |
1684 | } | |
1685 | ||
826f309b | 1686 | fn get_encryption_key_password() -> Result<Vec<u8>, Error> { |
ff5d3707 | 1687 | |
f2401311 DM |
1688 | // fixme: implement other input methods |
1689 | ||
1690 | use std::env::VarError::*; | |
1691 | match std::env::var("PBS_ENCRYPTION_PASSWORD") { | |
826f309b | 1692 | Ok(p) => return Ok(p.as_bytes().to_vec()), |
f2401311 DM |
1693 | Err(NotUnicode(_)) => bail!("PBS_ENCRYPTION_PASSWORD contains bad characters"), |
1694 | Err(NotPresent) => { | |
1695 | // Try another method | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | // If we're on a TTY, query the user for a password | |
501f4fa2 DM |
1700 | if tty::stdin_isatty() { |
1701 | return Ok(tty::read_password("Encryption Key Password: ")?); | |
f2401311 DM |
1702 | } |
1703 | ||
1704 | bail!("no password input mechanism available"); | |
1705 | } | |
1706 | ||
ac716234 DM |
1707 | fn key_create( |
1708 | param: Value, | |
1709 | _info: &ApiMethod, | |
1710 | _rpcenv: &mut dyn RpcEnvironment, | |
1711 | ) -> Result<Value, Error> { | |
1712 | ||
9b06db45 DM |
1713 | let path = tools::required_string_param(¶m, "path")?; |
1714 | let path = PathBuf::from(path); | |
ac716234 | 1715 | |
181f097a | 1716 | let kdf = param["kdf"].as_str().unwrap_or("scrypt"); |
ac716234 DM |
1717 | |
1718 | let key = proxmox::sys::linux::random_data(32)?; | |
1719 | ||
181f097a DM |
1720 | if kdf == "scrypt" { |
1721 | // always read passphrase from tty | |
501f4fa2 | 1722 | if !tty::stdin_isatty() { |
181f097a DM |
1723 | bail!("unable to read passphrase - no tty"); |
1724 | } | |
ac716234 | 1725 | |
501f4fa2 | 1726 | let password = tty::read_and_verify_password("Encryption Key Password: ")?; |
181f097a | 1727 | |
ab44acff | 1728 | let key_config = encrypt_key_with_passphrase(&key, &password)?; |
37c5a175 | 1729 | |
ab44acff | 1730 | store_key_config(&path, false, key_config)?; |
181f097a DM |
1731 | |
1732 | Ok(Value::Null) | |
1733 | } else if kdf == "none" { | |
1734 | let created = Local.timestamp(Local::now().timestamp(), 0); | |
1735 | ||
1736 | store_key_config(&path, false, KeyConfig { | |
1737 | kdf: None, | |
1738 | created, | |
ab44acff | 1739 | modified: created, |
181f097a DM |
1740 | data: key, |
1741 | })?; | |
1742 | ||
1743 | Ok(Value::Null) | |
1744 | } else { | |
1745 | unreachable!(); | |
1746 | } | |
ac716234 DM |
1747 | } |
1748 | ||
9f46c7de DM |
1749 | fn master_pubkey_path() -> Result<PathBuf, Error> { |
1750 | let base = BaseDirectories::with_prefix("proxmox-backup")?; | |
1751 | ||
1752 | // usually $HOME/.config/proxmox-backup/master-public.pem | |
1753 | let path = base.place_config_file("master-public.pem")?; | |
1754 | ||
1755 | Ok(path) | |
1756 | } | |
1757 | ||
3ea8bfc9 DM |
1758 | fn key_import_master_pubkey( |
1759 | param: Value, | |
1760 | _info: &ApiMethod, | |
1761 | _rpcenv: &mut dyn RpcEnvironment, | |
1762 | ) -> Result<Value, Error> { | |
1763 | ||
1764 | let path = tools::required_string_param(¶m, "path")?; | |
1765 | let path = PathBuf::from(path); | |
1766 | ||
e18a6c9e | 1767 | let pem_data = file_get_contents(&path)?; |
3ea8bfc9 DM |
1768 | |
1769 | if let Err(err) = openssl::pkey::PKey::public_key_from_pem(&pem_data) { | |
1770 | bail!("Unable to decode PEM data - {}", err); | |
1771 | } | |
1772 | ||
9f46c7de | 1773 | let target_path = master_pubkey_path()?; |
3ea8bfc9 | 1774 | |
feaa1ad3 | 1775 | replace_file(&target_path, &pem_data, CreateOptions::new())?; |
3ea8bfc9 DM |
1776 | |
1777 | println!("Imported public master key to {:?}", target_path); | |
1778 | ||
1779 | Ok(Value::Null) | |
1780 | } | |
1781 | ||
37c5a175 DM |
1782 | fn key_create_master_key( |
1783 | _param: Value, | |
1784 | _info: &ApiMethod, | |
1785 | _rpcenv: &mut dyn RpcEnvironment, | |
1786 | ) -> Result<Value, Error> { | |
1787 | ||
1788 | // we need a TTY to query the new password | |
501f4fa2 | 1789 | if !tty::stdin_isatty() { |
37c5a175 DM |
1790 | bail!("unable to create master key - no tty"); |
1791 | } | |
1792 | ||
1793 | let rsa = openssl::rsa::Rsa::generate(4096)?; | |
1794 | let pkey = openssl::pkey::PKey::from_rsa(rsa)?; | |
1795 | ||
37c5a175 | 1796 | |
501f4fa2 | 1797 | let password = String::from_utf8(tty::read_and_verify_password("Master Key Password: ")?)?; |
37c5a175 DM |
1798 | |
1799 | let pub_key: Vec<u8> = pkey.public_key_to_pem()?; | |
1800 | let filename_pub = "master-public.pem"; | |
1801 | println!("Writing public master key to {}", filename_pub); | |
feaa1ad3 | 1802 | replace_file(filename_pub, pub_key.as_slice(), CreateOptions::new())?; |
37c5a175 DM |
1803 | |
1804 | let cipher = openssl::symm::Cipher::aes_256_cbc(); | |
cbe01dc5 | 1805 | let priv_key: Vec<u8> = pkey.private_key_to_pem_pkcs8_passphrase(cipher, password.as_bytes())?; |
37c5a175 DM |
1806 | |
1807 | let filename_priv = "master-private.pem"; | |
1808 | println!("Writing private master key to {}", filename_priv); | |
feaa1ad3 | 1809 | replace_file(filename_priv, priv_key.as_slice(), CreateOptions::new())?; |
37c5a175 DM |
1810 | |
1811 | Ok(Value::Null) | |
1812 | } | |
ac716234 DM |
1813 | |
1814 | fn key_change_passphrase( | |
1815 | param: Value, | |
1816 | _info: &ApiMethod, | |
1817 | _rpcenv: &mut dyn RpcEnvironment, | |
1818 | ) -> Result<Value, Error> { | |
1819 | ||
9b06db45 DM |
1820 | let path = tools::required_string_param(¶m, "path")?; |
1821 | let path = PathBuf::from(path); | |
ac716234 | 1822 | |
181f097a DM |
1823 | let kdf = param["kdf"].as_str().unwrap_or("scrypt"); |
1824 | ||
ac716234 | 1825 | // we need a TTY to query the new password |
501f4fa2 | 1826 | if !tty::stdin_isatty() { |
ac716234 DM |
1827 | bail!("unable to change passphrase - no tty"); |
1828 | } | |
1829 | ||
6d20a29d | 1830 | let (key, created) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
ac716234 | 1831 | |
181f097a | 1832 | if kdf == "scrypt" { |
ac716234 | 1833 | |
501f4fa2 | 1834 | let password = tty::read_and_verify_password("New Password: ")?; |
ac716234 | 1835 | |
cbe01dc5 | 1836 | let mut new_key_config = encrypt_key_with_passphrase(&key, &password)?; |
ab44acff DM |
1837 | new_key_config.created = created; // keep original value |
1838 | ||
1839 | store_key_config(&path, true, new_key_config)?; | |
ac716234 | 1840 | |
181f097a DM |
1841 | Ok(Value::Null) |
1842 | } else if kdf == "none" { | |
ab44acff | 1843 | let modified = Local.timestamp(Local::now().timestamp(), 0); |
181f097a DM |
1844 | |
1845 | store_key_config(&path, true, KeyConfig { | |
1846 | kdf: None, | |
ab44acff DM |
1847 | created, // keep original value |
1848 | modified, | |
6d0983db | 1849 | data: key.to_vec(), |
181f097a DM |
1850 | })?; |
1851 | ||
1852 | Ok(Value::Null) | |
1853 | } else { | |
1854 | unreachable!(); | |
1855 | } | |
f2401311 DM |
1856 | } |
1857 | ||
1858 | fn key_mgmt_cli() -> CliCommandMap { | |
1859 | ||
255f378a | 1860 | const KDF_SCHEMA: Schema = |
181f097a | 1861 | StringSchema::new("Key derivation function. Choose 'none' to store the key unecrypted.") |
255f378a DM |
1862 | .format(&ApiStringFormat::Enum(&["scrypt", "none"])) |
1863 | .default("scrypt") | |
1864 | .schema(); | |
1865 | ||
552c2259 | 1866 | #[sortable] |
255f378a DM |
1867 | const API_METHOD_KEY_CREATE: ApiMethod = ApiMethod::new( |
1868 | &ApiHandler::Sync(&key_create), | |
1869 | &ObjectSchema::new( | |
1870 | "Create a new encryption key.", | |
552c2259 | 1871 | &sorted!([ |
255f378a DM |
1872 | ("path", false, &StringSchema::new("File system path.").schema()), |
1873 | ("kdf", true, &KDF_SCHEMA), | |
552c2259 | 1874 | ]), |
255f378a | 1875 | ) |
181f097a | 1876 | ); |
7074a0b3 | 1877 | |
255f378a | 1878 | let key_create_cmd_def = CliCommand::new(&API_METHOD_KEY_CREATE) |
49fddd98 | 1879 | .arg_param(&["path"]) |
9b06db45 | 1880 | .completion_cb("path", tools::complete_file_name); |
f2401311 | 1881 | |
552c2259 | 1882 | #[sortable] |
255f378a DM |
1883 | const API_METHOD_KEY_CHANGE_PASSPHRASE: ApiMethod = ApiMethod::new( |
1884 | &ApiHandler::Sync(&key_change_passphrase), | |
1885 | &ObjectSchema::new( | |
1886 | "Change the passphrase required to decrypt the key.", | |
552c2259 | 1887 | &sorted!([ |
255f378a DM |
1888 | ("path", false, &StringSchema::new("File system path.").schema()), |
1889 | ("kdf", true, &KDF_SCHEMA), | |
552c2259 | 1890 | ]), |
255f378a DM |
1891 | ) |
1892 | ); | |
7074a0b3 | 1893 | |
255f378a | 1894 | let key_change_passphrase_cmd_def = CliCommand::new(&API_METHOD_KEY_CHANGE_PASSPHRASE) |
49fddd98 | 1895 | .arg_param(&["path"]) |
9b06db45 | 1896 | .completion_cb("path", tools::complete_file_name); |
ac716234 | 1897 | |
255f378a DM |
1898 | const API_METHOD_KEY_CREATE_MASTER_KEY: ApiMethod = ApiMethod::new( |
1899 | &ApiHandler::Sync(&key_create_master_key), | |
1900 | &ObjectSchema::new("Create a new 4096 bit RSA master pub/priv key pair.", &[]) | |
1901 | ); | |
7074a0b3 | 1902 | |
255f378a DM |
1903 | let key_create_master_key_cmd_def = CliCommand::new(&API_METHOD_KEY_CREATE_MASTER_KEY); |
1904 | ||
552c2259 | 1905 | #[sortable] |
255f378a DM |
1906 | const API_METHOD_KEY_IMPORT_MASTER_PUBKEY: ApiMethod = ApiMethod::new( |
1907 | &ApiHandler::Sync(&key_import_master_pubkey), | |
1908 | &ObjectSchema::new( | |
1909 | "Import a new RSA public key and use it as master key. The key is expected to be in '.pem' format.", | |
552c2259 | 1910 | &sorted!([ ("path", false, &StringSchema::new("File system path.").schema()) ]), |
255f378a DM |
1911 | ) |
1912 | ); | |
7074a0b3 | 1913 | |
255f378a | 1914 | let key_import_master_pubkey_cmd_def = CliCommand::new(&API_METHOD_KEY_IMPORT_MASTER_PUBKEY) |
49fddd98 | 1915 | .arg_param(&["path"]) |
3ea8bfc9 DM |
1916 | .completion_cb("path", tools::complete_file_name); |
1917 | ||
11377a47 | 1918 | CliCommandMap::new() |
48ef3c33 DM |
1919 | .insert("create", key_create_cmd_def) |
1920 | .insert("create-master-key", key_create_master_key_cmd_def) | |
1921 | .insert("import-master-pubkey", key_import_master_pubkey_cmd_def) | |
1922 | .insert("change-passphrase", key_change_passphrase_cmd_def) | |
f2401311 DM |
1923 | } |
1924 | ||
70235f72 CE |
1925 | fn mount( |
1926 | param: Value, | |
1927 | _info: &ApiMethod, | |
1928 | _rpcenv: &mut dyn RpcEnvironment, | |
1929 | ) -> Result<Value, Error> { | |
1930 | let verbose = param["verbose"].as_bool().unwrap_or(false); | |
1931 | if verbose { | |
1932 | // This will stay in foreground with debug output enabled as None is | |
1933 | // passed for the RawFd. | |
3f06d6fb | 1934 | return proxmox_backup::tools::runtime::main(mount_do(param, None)); |
70235f72 CE |
1935 | } |
1936 | ||
1937 | // Process should be deamonized. | |
1938 | // Make sure to fork before the async runtime is instantiated to avoid troubles. | |
1939 | let pipe = pipe()?; | |
1940 | match fork() { | |
11377a47 | 1941 | Ok(ForkResult::Parent { .. }) => { |
70235f72 CE |
1942 | nix::unistd::close(pipe.1).unwrap(); |
1943 | // Blocks the parent process until we are ready to go in the child | |
1944 | let _res = nix::unistd::read(pipe.0, &mut [0]).unwrap(); | |
1945 | Ok(Value::Null) | |
1946 | } | |
1947 | Ok(ForkResult::Child) => { | |
1948 | nix::unistd::close(pipe.0).unwrap(); | |
1949 | nix::unistd::setsid().unwrap(); | |
3f06d6fb | 1950 | proxmox_backup::tools::runtime::main(mount_do(param, Some(pipe.1))) |
70235f72 CE |
1951 | } |
1952 | Err(_) => bail!("failed to daemonize process"), | |
1953 | } | |
1954 | } | |
1955 | ||
1956 | async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> { | |
1957 | let repo = extract_repository_from_value(¶m)?; | |
1958 | let archive_name = tools::required_string_param(¶m, "archive-name")?; | |
1959 | let target = tools::required_string_param(¶m, "target")?; | |
d59dbeca | 1960 | let client = connect(repo.host(), repo.user())?; |
70235f72 CE |
1961 | |
1962 | record_repository(&repo); | |
1963 | ||
1964 | let path = tools::required_string_param(¶m, "snapshot")?; | |
1965 | let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { | |
1966 | let group = BackupGroup::parse(path)?; | |
27c9affb | 1967 | api_datastore_latest_snapshot(&client, repo.store(), group).await? |
70235f72 CE |
1968 | } else { |
1969 | let snapshot = BackupDir::parse(path)?; | |
1970 | (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) | |
1971 | }; | |
1972 | ||
11377a47 | 1973 | let keyfile = param["keyfile"].as_str().map(PathBuf::from); |
70235f72 CE |
1974 | let crypt_config = match keyfile { |
1975 | None => None, | |
1976 | Some(path) => { | |
6d20a29d | 1977 | let (key, _) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
70235f72 CE |
1978 | Some(Arc::new(CryptConfig::new(key)?)) |
1979 | } | |
1980 | }; | |
1981 | ||
1982 | let server_archive_name = if archive_name.ends_with(".pxar") { | |
1983 | format!("{}.didx", archive_name) | |
1984 | } else { | |
1985 | bail!("Can only mount pxar archives."); | |
1986 | }; | |
1987 | ||
296c50ba DM |
1988 | let client = BackupReader::start( |
1989 | client, | |
1990 | crypt_config.clone(), | |
1991 | repo.store(), | |
1992 | &backup_type, | |
1993 | &backup_id, | |
1994 | backup_time, | |
1995 | true, | |
1996 | ).await?; | |
70235f72 | 1997 | |
f06b820a | 1998 | let manifest = client.download_manifest().await?; |
296c50ba | 1999 | |
70235f72 | 2000 | if server_archive_name.ends_with(".didx") { |
c3d84a22 | 2001 | let index = client.download_dynamic_index(&manifest, &server_archive_name).await?; |
70235f72 CE |
2002 | let most_used = index.find_most_used_chunks(8); |
2003 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
2004 | let reader = BufferedDynamicReader::new(index, chunk_reader); | |
f701d033 | 2005 | let decoder = pxar::Decoder::new(reader)?; |
70235f72 | 2006 | let options = OsStr::new("ro,default_permissions"); |
2a111910 | 2007 | let mut session = pxar::fuse::Session::new(decoder, &options, pipe.is_none()) |
70235f72 CE |
2008 | .map_err(|err| format_err!("pxar mount failed: {}", err))?; |
2009 | ||
2010 | // Mount the session but not call fuse deamonize as this will cause | |
2011 | // issues with the runtime after the fork | |
2012 | let deamonize = false; | |
2013 | session.mount(&Path::new(target), deamonize)?; | |
2014 | ||
2015 | if let Some(pipe) = pipe { | |
2016 | nix::unistd::chdir(Path::new("/")).unwrap(); | |
2017 | // Finish creation of deamon by redirecting filedescriptors. | |
2018 | let nullfd = nix::fcntl::open( | |
2019 | "/dev/null", | |
2020 | nix::fcntl::OFlag::O_RDWR, | |
2021 | nix::sys::stat::Mode::empty(), | |
2022 | ).unwrap(); | |
2023 | nix::unistd::dup2(nullfd, 0).unwrap(); | |
2024 | nix::unistd::dup2(nullfd, 1).unwrap(); | |
2025 | nix::unistd::dup2(nullfd, 2).unwrap(); | |
2026 | if nullfd > 2 { | |
2027 | nix::unistd::close(nullfd).unwrap(); | |
2028 | } | |
2029 | // Signal the parent process that we are done with the setup and it can | |
2030 | // terminate. | |
11377a47 | 2031 | nix::unistd::write(pipe, &[0u8])?; |
70235f72 CE |
2032 | nix::unistd::close(pipe).unwrap(); |
2033 | } | |
2034 | ||
2035 | let multithreaded = true; | |
2036 | session.run_loop(multithreaded)?; | |
2037 | } else { | |
2038 | bail!("unknown archive file extension (expected .pxar)"); | |
2039 | } | |
2040 | ||
2041 | Ok(Value::Null) | |
2042 | } | |
2043 | ||
78d54360 WB |
2044 | #[api( |
2045 | input: { | |
2046 | properties: { | |
2047 | "snapshot": { | |
2048 | type: String, | |
2049 | description: "Group/Snapshot path.", | |
2050 | }, | |
2051 | "archive-name": { | |
2052 | type: String, | |
2053 | description: "Backup archive name.", | |
2054 | }, | |
2055 | "repository": { | |
2056 | optional: true, | |
2057 | schema: REPO_URL_SCHEMA, | |
2058 | }, | |
2059 | "keyfile": { | |
2060 | optional: true, | |
2061 | type: String, | |
2062 | description: "Path to encryption key.", | |
2063 | }, | |
2064 | }, | |
2065 | }, | |
2066 | )] | |
2067 | /// Shell to interactively inspect and restore snapshots. | |
2068 | async fn catalog_shell(param: Value) -> Result<(), Error> { | |
3cf73c4e | 2069 | let repo = extract_repository_from_value(¶m)?; |
d59dbeca | 2070 | let client = connect(repo.host(), repo.user())?; |
3cf73c4e CE |
2071 | let path = tools::required_string_param(¶m, "snapshot")?; |
2072 | let archive_name = tools::required_string_param(¶m, "archive-name")?; | |
2073 | ||
2074 | let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { | |
2075 | let group = BackupGroup::parse(path)?; | |
27c9affb | 2076 | api_datastore_latest_snapshot(&client, repo.store(), group).await? |
3cf73c4e CE |
2077 | } else { |
2078 | let snapshot = BackupDir::parse(path)?; | |
2079 | (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) | |
2080 | }; | |
2081 | ||
2082 | let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); | |
2083 | let crypt_config = match keyfile { | |
2084 | None => None, | |
2085 | Some(path) => { | |
6d20a29d | 2086 | let (key, _) = load_and_decrypt_key(&path, &get_encryption_key_password)?; |
3cf73c4e CE |
2087 | Some(Arc::new(CryptConfig::new(key)?)) |
2088 | } | |
2089 | }; | |
2090 | ||
2091 | let server_archive_name = if archive_name.ends_with(".pxar") { | |
2092 | format!("{}.didx", archive_name) | |
2093 | } else { | |
2094 | bail!("Can only mount pxar archives."); | |
2095 | }; | |
2096 | ||
2097 | let client = BackupReader::start( | |
2098 | client, | |
2099 | crypt_config.clone(), | |
2100 | repo.store(), | |
2101 | &backup_type, | |
2102 | &backup_id, | |
2103 | backup_time, | |
2104 | true, | |
2105 | ).await?; | |
2106 | ||
2107 | let tmpfile = std::fs::OpenOptions::new() | |
2108 | .write(true) | |
2109 | .read(true) | |
2110 | .custom_flags(libc::O_TMPFILE) | |
2111 | .open("/tmp")?; | |
2112 | ||
2113 | let manifest = client.download_manifest().await?; | |
2114 | ||
2115 | let index = client.download_dynamic_index(&manifest, &server_archive_name).await?; | |
2116 | let most_used = index.find_most_used_chunks(8); | |
2117 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config.clone(), most_used); | |
2118 | let reader = BufferedDynamicReader::new(index, chunk_reader); | |
f701d033 DM |
2119 | let mut decoder = pxar::Decoder::new(reader)?; |
2120 | decoder.set_callback(|path| { | |
2121 | println!("{:?}", path); | |
2122 | Ok(()) | |
2123 | }); | |
3cf73c4e CE |
2124 | |
2125 | let tmpfile = client.download(CATALOG_NAME, tmpfile).await?; | |
2126 | let index = DynamicIndexReader::new(tmpfile) | |
2127 | .map_err(|err| format_err!("unable to read catalog index - {}", err))?; | |
2128 | ||
2129 | // Note: do not use values stored in index (not trusted) - instead, computed them again | |
2130 | let (csum, size) = index.compute_csum(); | |
2131 | manifest.verify_file(CATALOG_NAME, &csum, size)?; | |
2132 | ||
2133 | let most_used = index.find_most_used_chunks(8); | |
2134 | let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); | |
2135 | let mut reader = BufferedDynamicReader::new(index, chunk_reader); | |
2136 | let mut catalogfile = std::fs::OpenOptions::new() | |
2137 | .write(true) | |
2138 | .read(true) | |
2139 | .custom_flags(libc::O_TMPFILE) | |
2140 | .open("/tmp")?; | |
2141 | ||
2142 | std::io::copy(&mut reader, &mut catalogfile) | |
2143 | .map_err(|err| format_err!("unable to download catalog - {}", err))?; | |
2144 | ||
2145 | catalogfile.seek(SeekFrom::Start(0))?; | |
2146 | let catalog_reader = CatalogReader::new(catalogfile); | |
2147 | let state = Shell::new( | |
2148 | catalog_reader, | |
2149 | &server_archive_name, | |
2150 | decoder, | |
2151 | )?; | |
2152 | ||
2153 | println!("Starting interactive shell"); | |
2154 | state.shell()?; | |
2155 | ||
2156 | record_repository(&repo); | |
2157 | ||
78d54360 | 2158 | Ok(()) |
3cf73c4e CE |
2159 | } |
2160 | ||
1c6ad6ef | 2161 | fn catalog_mgmt_cli() -> CliCommandMap { |
78d54360 | 2162 | let catalog_shell_cmd_def = CliCommand::new(&API_METHOD_CATALOG_SHELL) |
1c6ad6ef DM |
2163 | .arg_param(&["snapshot", "archive-name"]) |
2164 | .completion_cb("repository", complete_repository) | |
0ec9e1b0 | 2165 | .completion_cb("archive-name", complete_pxar_archive_name) |
1c6ad6ef DM |
2166 | .completion_cb("snapshot", complete_group_or_snapshot); |
2167 | ||
1c6ad6ef DM |
2168 | let catalog_dump_cmd_def = CliCommand::new(&API_METHOD_DUMP_CATALOG) |
2169 | .arg_param(&["snapshot"]) | |
2170 | .completion_cb("repository", complete_repository) | |
2171 | .completion_cb("snapshot", complete_backup_snapshot); | |
2172 | ||
2173 | CliCommandMap::new() | |
48ef3c33 DM |
2174 | .insert("dump", catalog_dump_cmd_def) |
2175 | .insert("shell", catalog_shell_cmd_def) | |
1c6ad6ef DM |
2176 | } |
2177 | ||
5830c205 DM |
2178 | #[api( |
2179 | input: { | |
2180 | properties: { | |
2181 | repository: { | |
2182 | schema: REPO_URL_SCHEMA, | |
2183 | optional: true, | |
2184 | }, | |
2185 | limit: { | |
2186 | description: "The maximal number of tasks to list.", | |
2187 | type: Integer, | |
2188 | optional: true, | |
2189 | minimum: 1, | |
2190 | maximum: 1000, | |
2191 | default: 50, | |
2192 | }, | |
2193 | "output-format": { | |
2194 | schema: OUTPUT_FORMAT, | |
2195 | optional: true, | |
2196 | }, | |
2197 | } | |
2198 | } | |
2199 | )] | |
2200 | /// List running server tasks for this repo user | |
d6c4a119 | 2201 | async fn task_list(param: Value) -> Result<Value, Error> { |
5830c205 | 2202 | |
d6c4a119 DM |
2203 | let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); |
2204 | let repo = extract_repository_from_value(¶m)?; | |
d59dbeca | 2205 | let client = connect(repo.host(), repo.user())?; |
5830c205 | 2206 | |
d6c4a119 | 2207 | let limit = param["limit"].as_u64().unwrap_or(50) as usize; |
5830c205 | 2208 | |
d6c4a119 DM |
2209 | let args = json!({ |
2210 | "running": true, | |
2211 | "start": 0, | |
2212 | "limit": limit, | |
2213 | "userfilter": repo.user(), | |
2214 | "store": repo.store(), | |
2215 | }); | |
2216 | let result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; | |
5830c205 | 2217 | |
d6c4a119 | 2218 | let data = &result["data"]; |
5830c205 | 2219 | |
d6c4a119 DM |
2220 | if output_format == "text" { |
2221 | for item in data.as_array().unwrap() { | |
2222 | println!( | |
2223 | "{} {}", | |
2224 | item["upid"].as_str().unwrap(), | |
2225 | item["status"].as_str().unwrap_or("running"), | |
2226 | ); | |
5830c205 | 2227 | } |
d6c4a119 DM |
2228 | } else { |
2229 | format_and_print_result(data, &output_format); | |
2230 | } | |
5830c205 DM |
2231 | |
2232 | Ok(Value::Null) | |
2233 | } | |
2234 | ||
2235 | #[api( | |
2236 | input: { | |
2237 | properties: { | |
2238 | repository: { | |
2239 | schema: REPO_URL_SCHEMA, | |
2240 | optional: true, | |
2241 | }, | |
2242 | upid: { | |
2243 | schema: UPID_SCHEMA, | |
2244 | }, | |
2245 | } | |
2246 | } | |
2247 | )] | |
2248 | /// Display the task log. | |
d6c4a119 | 2249 | async fn task_log(param: Value) -> Result<Value, Error> { |
5830c205 | 2250 | |
d6c4a119 DM |
2251 | let repo = extract_repository_from_value(¶m)?; |
2252 | let upid = tools::required_string_param(¶m, "upid")?; | |
5830c205 | 2253 | |
d59dbeca | 2254 | let client = connect(repo.host(), repo.user())?; |
5830c205 | 2255 | |
d6c4a119 | 2256 | display_task_log(client, upid, true).await?; |
5830c205 DM |
2257 | |
2258 | Ok(Value::Null) | |
2259 | } | |
2260 | ||
3f1020b7 DM |
2261 | #[api( |
2262 | input: { | |
2263 | properties: { | |
2264 | repository: { | |
2265 | schema: REPO_URL_SCHEMA, | |
2266 | optional: true, | |
2267 | }, | |
2268 | upid: { | |
2269 | schema: UPID_SCHEMA, | |
2270 | }, | |
2271 | } | |
2272 | } | |
2273 | )] | |
2274 | /// Try to stop a specific task. | |
d6c4a119 | 2275 | async fn task_stop(param: Value) -> Result<Value, Error> { |
3f1020b7 | 2276 | |
d6c4a119 DM |
2277 | let repo = extract_repository_from_value(¶m)?; |
2278 | let upid_str = tools::required_string_param(¶m, "upid")?; | |
3f1020b7 | 2279 | |
d59dbeca | 2280 | let mut client = connect(repo.host(), repo.user())?; |
3f1020b7 | 2281 | |
d6c4a119 DM |
2282 | let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); |
2283 | let _ = client.delete(&path, None).await?; | |
3f1020b7 DM |
2284 | |
2285 | Ok(Value::Null) | |
2286 | } | |
2287 | ||
5830c205 DM |
2288 | fn task_mgmt_cli() -> CliCommandMap { |
2289 | ||
2290 | let task_list_cmd_def = CliCommand::new(&API_METHOD_TASK_LIST) | |
2291 | .completion_cb("repository", complete_repository); | |
2292 | ||
2293 | let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) | |
2294 | .arg_param(&["upid"]); | |
2295 | ||
3f1020b7 DM |
2296 | let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) |
2297 | .arg_param(&["upid"]); | |
2298 | ||
5830c205 DM |
2299 | CliCommandMap::new() |
2300 | .insert("log", task_log_cmd_def) | |
2301 | .insert("list", task_list_cmd_def) | |
3f1020b7 | 2302 | .insert("stop", task_stop_cmd_def) |
5830c205 | 2303 | } |
1c6ad6ef | 2304 | |
f2401311 | 2305 | fn main() { |
33d64b81 | 2306 | |
255f378a | 2307 | let backup_cmd_def = CliCommand::new(&API_METHOD_CREATE_BACKUP) |
49fddd98 | 2308 | .arg_param(&["backupspec"]) |
d0a03d40 | 2309 | .completion_cb("repository", complete_repository) |
49811347 | 2310 | .completion_cb("backupspec", complete_backup_source) |
6d0983db | 2311 | .completion_cb("keyfile", tools::complete_file_name) |
49811347 | 2312 | .completion_cb("chunk-size", complete_chunk_size); |
f8838fe9 | 2313 | |
255f378a | 2314 | let upload_log_cmd_def = CliCommand::new(&API_METHOD_UPLOAD_LOG) |
49fddd98 | 2315 | .arg_param(&["snapshot", "logfile"]) |
543a260f | 2316 | .completion_cb("snapshot", complete_backup_snapshot) |
ec34f7eb DM |
2317 | .completion_cb("logfile", tools::complete_file_name) |
2318 | .completion_cb("keyfile", tools::complete_file_name) | |
2319 | .completion_cb("repository", complete_repository); | |
2320 | ||
255f378a | 2321 | let list_cmd_def = CliCommand::new(&API_METHOD_LIST_BACKUP_GROUPS) |
d0a03d40 | 2322 | .completion_cb("repository", complete_repository); |
41c039e1 | 2323 | |
255f378a | 2324 | let snapshots_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOTS) |
49fddd98 | 2325 | .arg_param(&["group"]) |
024f11bb | 2326 | .completion_cb("group", complete_backup_group) |
d0a03d40 | 2327 | .completion_cb("repository", complete_repository); |
184f17af | 2328 | |
255f378a | 2329 | let forget_cmd_def = CliCommand::new(&API_METHOD_FORGET_SNAPSHOTS) |
49fddd98 | 2330 | .arg_param(&["snapshot"]) |
b2388518 | 2331 | .completion_cb("repository", complete_repository) |
543a260f | 2332 | .completion_cb("snapshot", complete_backup_snapshot); |
6f62c924 | 2333 | |
255f378a | 2334 | let garbage_collect_cmd_def = CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION) |
d0a03d40 | 2335 | .completion_cb("repository", complete_repository); |
8cc0d6af | 2336 | |
255f378a | 2337 | let restore_cmd_def = CliCommand::new(&API_METHOD_RESTORE) |
49fddd98 | 2338 | .arg_param(&["snapshot", "archive-name", "target"]) |
b2388518 | 2339 | .completion_cb("repository", complete_repository) |
08dc340a DM |
2340 | .completion_cb("snapshot", complete_group_or_snapshot) |
2341 | .completion_cb("archive-name", complete_archive_name) | |
2342 | .completion_cb("target", tools::complete_file_name); | |
9f912493 | 2343 | |
255f378a | 2344 | let files_cmd_def = CliCommand::new(&API_METHOD_LIST_SNAPSHOT_FILES) |
49fddd98 | 2345 | .arg_param(&["snapshot"]) |
52c171e4 | 2346 | .completion_cb("repository", complete_repository) |
543a260f | 2347 | .completion_cb("snapshot", complete_backup_snapshot); |
52c171e4 | 2348 | |
255f378a | 2349 | let prune_cmd_def = CliCommand::new(&API_METHOD_PRUNE) |
49fddd98 | 2350 | .arg_param(&["group"]) |
9fdc3ef4 | 2351 | .completion_cb("group", complete_backup_group) |
d0a03d40 | 2352 | .completion_cb("repository", complete_repository); |
9f912493 | 2353 | |
255f378a | 2354 | let status_cmd_def = CliCommand::new(&API_METHOD_STATUS) |
34a816cc DM |
2355 | .completion_cb("repository", complete_repository); |
2356 | ||
255f378a | 2357 | let login_cmd_def = CliCommand::new(&API_METHOD_API_LOGIN) |
e240d8be DM |
2358 | .completion_cb("repository", complete_repository); |
2359 | ||
255f378a | 2360 | let logout_cmd_def = CliCommand::new(&API_METHOD_API_LOGOUT) |
e240d8be | 2361 | .completion_cb("repository", complete_repository); |
32efac1c | 2362 | |
552c2259 | 2363 | #[sortable] |
255f378a DM |
2364 | const API_METHOD_MOUNT: ApiMethod = ApiMethod::new( |
2365 | &ApiHandler::Sync(&mount), | |
2366 | &ObjectSchema::new( | |
2367 | "Mount pxar archive.", | |
552c2259 | 2368 | &sorted!([ |
255f378a DM |
2369 | ("snapshot", false, &StringSchema::new("Group/Snapshot path.").schema()), |
2370 | ("archive-name", false, &StringSchema::new("Backup archive name.").schema()), | |
2371 | ("target", false, &StringSchema::new("Target directory path.").schema()), | |
2372 | ("repository", true, &REPO_URL_SCHEMA), | |
2373 | ("keyfile", true, &StringSchema::new("Path to encryption key.").schema()), | |
2374 | ("verbose", true, &BooleanSchema::new("Verbose output.").default(false).schema()), | |
552c2259 | 2375 | ]), |
255f378a DM |
2376 | ) |
2377 | ); | |
7074a0b3 | 2378 | |
255f378a | 2379 | let mount_cmd_def = CliCommand::new(&API_METHOD_MOUNT) |
49fddd98 | 2380 | .arg_param(&["snapshot", "archive-name", "target"]) |
70235f72 CE |
2381 | .completion_cb("repository", complete_repository) |
2382 | .completion_cb("snapshot", complete_group_or_snapshot) | |
0ec9e1b0 | 2383 | .completion_cb("archive-name", complete_pxar_archive_name) |
70235f72 | 2384 | .completion_cb("target", tools::complete_file_name); |
e240d8be | 2385 | |
3cf73c4e | 2386 | |
41c039e1 | 2387 | let cmd_def = CliCommandMap::new() |
48ef3c33 DM |
2388 | .insert("backup", backup_cmd_def) |
2389 | .insert("upload-log", upload_log_cmd_def) | |
2390 | .insert("forget", forget_cmd_def) | |
2391 | .insert("garbage-collect", garbage_collect_cmd_def) | |
2392 | .insert("list", list_cmd_def) | |
2393 | .insert("login", login_cmd_def) | |
2394 | .insert("logout", logout_cmd_def) | |
2395 | .insert("prune", prune_cmd_def) | |
2396 | .insert("restore", restore_cmd_def) | |
2397 | .insert("snapshots", snapshots_cmd_def) | |
2398 | .insert("files", files_cmd_def) | |
2399 | .insert("status", status_cmd_def) | |
2400 | .insert("key", key_mgmt_cli()) | |
2401 | .insert("mount", mount_cmd_def) | |
5830c205 DM |
2402 | .insert("catalog", catalog_mgmt_cli()) |
2403 | .insert("task", task_mgmt_cli()); | |
48ef3c33 | 2404 | |
d08bc483 DM |
2405 | run_cli_command(cmd_def, Some(|future| { |
2406 | proxmox_backup::tools::runtime::main(future) | |
2407 | })); | |
ff5d3707 | 2408 | } |