]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-client.rs
src/bin/proxmox-backup-client.rs: add restore flag --allow-existing-dirs
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
CommitLineData
826f309b 1//#[macro_use]
fe0e04c6 2extern crate proxmox_backup;
ff5d3707 3
4use failure::*;
728797d0 5//use std::os::unix::io::AsRawFd;
fa5d6977 6use chrono::{Local, Utc, TimeZone};
e9c9409a 7use std::path::{Path, PathBuf};
2eeaacb9 8use std::collections::{HashSet, HashMap};
bf125261 9use std::io::Write;
ff5d3707 10
fe0e04c6 11use proxmox_backup::tools;
4de0e142 12use proxmox_backup::cli::*;
bbf9e7e9 13use proxmox_backup::api2::types::*;
ef2f2efb 14use proxmox_backup::api_schema::*;
dc9a007b 15use proxmox_backup::api_schema::router::*;
151c6ce2 16use proxmox_backup::client::*;
247cdbce 17use proxmox_backup::backup::*;
86eda3eb
DM
18use proxmox_backup::pxar;
19
fe0e04c6
DM
20//use proxmox_backup::backup::image_index::*;
21//use proxmox_backup::config::datastore;
8968258b 22//use proxmox_backup::pxar::encoder::*;
728797d0 23//use proxmox_backup::backup::datastore::*;
23bb8780 24
f5f13ebc 25use serde_json::{json, Value};
1c0472e8 26//use hyper::Body;
33d64b81 27use std::sync::Arc;
ae0be2dd 28use regex::Regex;
d0a03d40 29use xdg::BaseDirectories;
ae0be2dd
DM
30
31use lazy_static::lazy_static;
5a2df000 32use futures::*;
c4ff3dce 33use tokio::sync::mpsc;
ae0be2dd
DM
34
35lazy_static! {
79679c2d 36 static ref BACKUPSPEC_REGEX: Regex = Regex::new(r"^([a-zA-Z0-9_-]+\.(?:pxar|img|conf|log)):(.+)$").unwrap();
f2401311
DM
37
38 static ref REPO_URL_SCHEMA: Arc<Schema> = Arc::new(
39 StringSchema::new("Repository URL.")
40 .format(BACKUP_REPO_URL.clone())
41 .max_length(256)
42 .into()
43 );
ae0be2dd 44}
33d64b81 45
d0a03d40 46
2665cef7
DM
47fn get_default_repository() -> Option<String> {
48 std::env::var("PBS_REPOSITORY").ok()
49}
50
51fn extract_repository_from_value(
52 param: &Value,
53) -> Result<BackupRepository, Error> {
54
55 let repo_url = param["repository"]
56 .as_str()
57 .map(String::from)
58 .or_else(get_default_repository)
59 .ok_or_else(|| format_err!("unable to get (default) repository"))?;
60
61 let repo: BackupRepository = repo_url.parse()?;
62
63 Ok(repo)
64}
65
66fn extract_repository_from_map(
67 param: &HashMap<String, String>,
68) -> Option<BackupRepository> {
69
70 param.get("repository")
71 .map(String::from)
72 .or_else(get_default_repository)
73 .and_then(|repo_url| repo_url.parse::<BackupRepository>().ok())
74}
75
d0a03d40
DM
76fn record_repository(repo: &BackupRepository) {
77
78 let base = match BaseDirectories::with_prefix("proxmox-backup") {
79 Ok(v) => v,
80 _ => return,
81 };
82
83 // usually $HOME/.cache/proxmox-backup/repo-list
84 let path = match base.place_cache_file("repo-list") {
85 Ok(v) => v,
86 _ => return,
87 };
88
49cf9f3d 89 let mut data = tools::file_get_json(&path, None).unwrap_or(json!({}));
d0a03d40
DM
90
91 let repo = repo.to_string();
92
93 data[&repo] = json!{ data[&repo].as_i64().unwrap_or(0) + 1 };
94
95 let mut map = serde_json::map::Map::new();
96
97 loop {
98 let mut max_used = 0;
99 let mut max_repo = None;
100 for (repo, count) in data.as_object().unwrap() {
101 if map.contains_key(repo) { continue; }
102 if let Some(count) = count.as_i64() {
103 if count > max_used {
104 max_used = count;
105 max_repo = Some(repo);
106 }
107 }
108 }
109 if let Some(repo) = max_repo {
110 map.insert(repo.to_owned(), json!(max_used));
111 } else {
112 break;
113 }
114 if map.len() > 10 { // store max. 10 repos
115 break;
116 }
117 }
118
119 let new_data = json!(map);
120
121 let _ = tools::file_set_contents(path, new_data.to_string().as_bytes(), None);
122}
123
49811347 124fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
d0a03d40
DM
125
126 let mut result = vec![];
127
128 let base = match BaseDirectories::with_prefix("proxmox-backup") {
129 Ok(v) => v,
130 _ => return result,
131 };
132
133 // usually $HOME/.cache/proxmox-backup/repo-list
134 let path = match base.place_cache_file("repo-list") {
135 Ok(v) => v,
136 _ => return result,
137 };
138
49cf9f3d 139 let data = tools::file_get_json(&path, None).unwrap_or(json!({}));
d0a03d40
DM
140
141 if let Some(map) = data.as_object() {
49811347 142 for (repo, _count) in map {
d0a03d40
DM
143 result.push(repo.to_owned());
144 }
145 }
146
147 result
148}
149
17d6979a 150fn backup_directory<P: AsRef<Path>>(
c4ff3dce 151 client: &BackupClient,
17d6979a 152 dir_path: P,
247cdbce 153 archive_name: &str,
36898ffc 154 chunk_size: Option<usize>,
2eeaacb9 155 device_set: Option<HashSet<u64>>,
219ef0e6 156 verbose: bool,
5b72c9b4 157 skip_lost_and_found: bool,
f98ac774 158 crypt_config: Option<Arc<CryptConfig>>,
247cdbce 159) -> Result<(), Error> {
33d64b81 160
5b72c9b4 161 let pxar_stream = PxarBackupStream::open(dir_path.as_ref(), device_set, verbose, skip_lost_and_found)?;
36898ffc 162 let chunk_stream = ChunkStream::new(pxar_stream, chunk_size);
ff3d3100 163
c4ff3dce 164 let (tx, rx) = mpsc::channel(10); // allow to buffer 10 chunks
5e7a09be 165
c4ff3dce
DM
166 let stream = rx
167 .map_err(Error::from)
168 .and_then(|x| x); // flatten
17d6979a 169
c4ff3dce
DM
170 // spawn chunker inside a separate task so that it can run parallel
171 tokio::spawn(
172 tx.send_all(chunk_stream.then(|r| Ok(r)))
1c0472e8 173 .map_err(|_| {}).map(|_| ())
c4ff3dce 174 );
17d6979a 175
f98ac774 176 client.upload_stream(archive_name, stream, "dynamic", None, crypt_config).wait()?;
bcd879cf
DM
177
178 Ok(())
179}
180
6af905c1
DM
181fn backup_image<P: AsRef<Path>>(
182 client: &BackupClient,
183 image_path: P,
184 archive_name: &str,
185 image_size: u64,
36898ffc 186 chunk_size: Option<usize>,
1c0472e8 187 _verbose: bool,
f98ac774 188 crypt_config: Option<Arc<CryptConfig>>,
6af905c1
DM
189) -> Result<(), Error> {
190
6af905c1
DM
191 let path = image_path.as_ref().to_owned();
192
193 let file = tokio::fs::File::open(path).wait()?;
194
195 let stream = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new())
196 .map_err(Error::from);
197
36898ffc 198 let stream = FixedChunkStream::new(stream, chunk_size.unwrap_or(4*1024*1024));
6af905c1 199
f98ac774 200 client.upload_stream(archive_name, stream, "fixed", Some(image_size), crypt_config).wait()?;
6af905c1
DM
201
202 Ok(())
203}
204
6899dbfb 205fn strip_server_file_expenstions(list: Vec<String>) -> Vec<String> {
8e39232a
DM
206
207 let mut result = vec![];
208
209 for file in list.into_iter() {
210 if file.ends_with(".didx") {
211 result.push(file[..file.len()-5].to_owned());
212 } else if file.ends_with(".fidx") {
213 result.push(file[..file.len()-5].to_owned());
6899dbfb
DM
214 } else if file.ends_with(".blob") {
215 result.push(file[..file.len()-5].to_owned());
8e39232a
DM
216 } else {
217 result.push(file); // should not happen
218 }
219 }
220
221 result
222}
223
812c6f87
DM
224fn list_backup_groups(
225 param: Value,
226 _info: &ApiMethod,
dd5495d6 227 _rpcenv: &mut dyn RpcEnvironment,
812c6f87
DM
228) -> Result<Value, Error> {
229
2665cef7 230 let repo = extract_repository_from_value(&param)?;
812c6f87 231
45cdce06 232 let client = HttpClient::new(repo.host(), repo.user())?;
812c6f87 233
d0a03d40 234 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
812c6f87 235
9e391bb7 236 let mut result = client.get(&path, None).wait()?;
812c6f87 237
d0a03d40
DM
238 record_repository(&repo);
239
812c6f87 240 // fixme: implement and use output formatter instead ..
80822b95
DM
241 let list = result["data"].as_array_mut().unwrap();
242
243 list.sort_unstable_by(|a, b| {
244 let a_id = a["backup-id"].as_str().unwrap();
245 let a_backup_type = a["backup-type"].as_str().unwrap();
246 let b_id = b["backup-id"].as_str().unwrap();
247 let b_backup_type = b["backup-type"].as_str().unwrap();
248
249 let type_order = a_backup_type.cmp(b_backup_type);
250 if type_order == std::cmp::Ordering::Equal {
251 a_id.cmp(b_id)
252 } else {
253 type_order
254 }
255 });
812c6f87 256
34a816cc
DM
257 let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
258
259 let mut result = vec![];
260
812c6f87
DM
261 for item in list {
262
ad20d198
DM
263 let id = item["backup-id"].as_str().unwrap();
264 let btype = item["backup-type"].as_str().unwrap();
265 let epoch = item["last-backup"].as_i64().unwrap();
fa5d6977 266 let last_backup = Utc.timestamp(epoch, 0);
ad20d198 267 let backup_count = item["backup-count"].as_u64().unwrap();
812c6f87 268
1e9a94e5 269 let group = BackupGroup::new(btype, id);
812c6f87
DM
270
271 let path = group.group_path().to_str().unwrap().to_owned();
ad20d198 272
8e39232a 273 let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
6899dbfb 274 let files = strip_server_file_expenstions(files);
ad20d198 275
34a816cc 276 if output_format == "text" {
fa5d6977
DM
277 println!(
278 "{:20} | {} | {:5} | {}",
279 path,
280 BackupDir::backup_time_to_string(last_backup),
281 backup_count,
282 tools::join(&files, ' '),
283 );
34a816cc
DM
284 } else {
285 result.push(json!({
286 "backup-type": btype,
287 "backup-id": id,
288 "last-backup": epoch,
289 "backup-count": backup_count,
290 "files": files,
291 }));
292 }
812c6f87
DM
293 }
294
9aa3f682 295 if output_format != "text" { format_and_print_result(&result.into(), &output_format); }
34a816cc 296
812c6f87
DM
297 Ok(Value::Null)
298}
299
184f17af
DM
300fn list_snapshots(
301 param: Value,
302 _info: &ApiMethod,
dd5495d6 303 _rpcenv: &mut dyn RpcEnvironment,
184f17af
DM
304) -> Result<Value, Error> {
305
2665cef7 306 let repo = extract_repository_from_value(&param)?;
184f17af 307
34a816cc
DM
308 let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
309
45cdce06 310 let client = HttpClient::new(repo.host(), repo.user())?;
184f17af 311
9e391bb7 312 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
184f17af 313
15c847f1
DM
314 let mut args = json!({});
315 if let Some(path) = param["group"].as_str() {
316 let group = BackupGroup::parse(path)?;
317 args["backup-type"] = group.backup_type().into();
318 args["backup-id"] = group.backup_id().into();
319 }
320
321 let result = client.get(&path, Some(args)).wait()?;
184f17af 322
d0a03d40
DM
323 record_repository(&repo);
324
184f17af
DM
325 let list = result["data"].as_array().unwrap();
326
34a816cc
DM
327 let mut result = vec![];
328
184f17af
DM
329 for item in list {
330
331 let id = item["backup-id"].as_str().unwrap();
332 let btype = item["backup-type"].as_str().unwrap();
333 let epoch = item["backup-time"].as_i64().unwrap();
184f17af 334
391d3107 335 let snapshot = BackupDir::new(btype, id, epoch);
184f17af
DM
336
337 let path = snapshot.relative_path().to_str().unwrap().to_owned();
338
8e39232a 339 let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
6899dbfb 340 let files = strip_server_file_expenstions(files);
184f17af 341
34a816cc 342 if output_format == "text" {
fa5d6977 343 println!("{} | {}", path, tools::join(&files, ' '));
34a816cc
DM
344 } else {
345 result.push(json!({
346 "backup-type": btype,
347 "backup-id": id,
348 "backup-time": epoch,
349 "files": files,
350 }));
351 }
184f17af
DM
352 }
353
f6ede796 354 if output_format != "text" { format_and_print_result(&result.into(), &output_format); }
34a816cc 355
184f17af
DM
356 Ok(Value::Null)
357}
358
6f62c924
DM
359fn forget_snapshots(
360 param: Value,
361 _info: &ApiMethod,
dd5495d6 362 _rpcenv: &mut dyn RpcEnvironment,
6f62c924
DM
363) -> Result<Value, Error> {
364
2665cef7 365 let repo = extract_repository_from_value(&param)?;
6f62c924
DM
366
367 let path = tools::required_string_param(&param, "snapshot")?;
368 let snapshot = BackupDir::parse(path)?;
369
45cdce06 370 let mut client = HttpClient::new(repo.host(), repo.user())?;
6f62c924 371
9e391bb7 372 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
6f62c924 373
9e391bb7
DM
374 let result = client.delete(&path, Some(json!({
375 "backup-type": snapshot.group().backup_type(),
376 "backup-id": snapshot.group().backup_id(),
377 "backup-time": snapshot.backup_time().timestamp(),
378 }))).wait()?;
6f62c924 379
d0a03d40
DM
380 record_repository(&repo);
381
6f62c924
DM
382 Ok(result)
383}
384
8cc0d6af
DM
385fn start_garbage_collection(
386 param: Value,
387 _info: &ApiMethod,
dd5495d6 388 _rpcenv: &mut dyn RpcEnvironment,
8cc0d6af
DM
389) -> Result<Value, Error> {
390
2665cef7 391 let repo = extract_repository_from_value(&param)?;
8cc0d6af 392
45cdce06 393 let mut client = HttpClient::new(repo.host(), repo.user())?;
8cc0d6af 394
d0a03d40 395 let path = format!("api2/json/admin/datastore/{}/gc", repo.store());
8cc0d6af 396
5a2df000 397 let result = client.post(&path, None).wait()?;
8cc0d6af 398
d0a03d40
DM
399 record_repository(&repo);
400
8cc0d6af
DM
401 Ok(result)
402}
33d64b81 403
ae0be2dd
DM
404fn parse_backupspec(value: &str) -> Result<(&str, &str), Error> {
405
406 if let Some(caps) = BACKUPSPEC_REGEX.captures(value) {
407 return Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()));
408 }
409 bail!("unable to parse directory specification '{}'", value);
410}
411
6049b71f
DM
412fn create_backup(
413 param: Value,
414 _info: &ApiMethod,
dd5495d6 415 _rpcenv: &mut dyn RpcEnvironment,
6049b71f 416) -> Result<Value, Error> {
ff5d3707 417
2665cef7 418 let repo = extract_repository_from_value(&param)?;
ae0be2dd
DM
419
420 let backupspec_list = tools::required_array_param(&param, "backupspec")?;
a914a774 421
eed6db39
DM
422 let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
423
5b72c9b4
DM
424 let skip_lost_and_found = param["skip-lost-and-found"].as_bool().unwrap_or(false);
425
219ef0e6
DM
426 let verbose = param["verbose"].as_bool().unwrap_or(false);
427
ca5d0b61
DM
428 let backup_time_opt = param["backup-time"].as_i64();
429
36898ffc 430 let chunk_size_opt = param["chunk-size"].as_u64().map(|v| (v*1024) as usize);
2d9d143a 431
247cdbce
DM
432 if let Some(size) = chunk_size_opt {
433 verify_chunk_size(size)?;
2d9d143a
DM
434 }
435
6d0983db
DM
436 let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p));
437
bbf9e7e9 438 let backup_id = param["backup-id"].as_str().unwrap_or(&tools::nodename());
fba30411 439
bbf9e7e9 440 let backup_type = param["backup-type"].as_str().unwrap_or("host");
ca5d0b61 441
2eeaacb9
DM
442 let include_dev = param["include-dev"].as_array();
443
444 let mut devices = if all_file_systems { None } else { Some(HashSet::new()) };
445
446 if let Some(include_dev) = include_dev {
447 if all_file_systems {
448 bail!("option 'all-file-systems' conflicts with option 'include-dev'");
449 }
450
451 let mut set = HashSet::new();
452 for path in include_dev {
453 let path = path.as_str().unwrap();
454 let stat = nix::sys::stat::stat(path)
455 .map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?;
456 set.insert(stat.st_dev);
457 }
458 devices = Some(set);
459 }
460
ae0be2dd 461 let mut upload_list = vec![];
a914a774 462
79679c2d 463 enum BackupType { PXAR, IMAGE, CONFIG, LOGFILE };
6af905c1 464
ae0be2dd
DM
465 for backupspec in backupspec_list {
466 let (target, filename) = parse_backupspec(backupspec.as_str().unwrap())?;
bcd879cf 467
eb1804c5
DM
468 use std::os::unix::fs::FileTypeExt;
469
470 let metadata = match std::fs::metadata(filename) {
471 Ok(m) => m,
ae0be2dd
DM
472 Err(err) => bail!("unable to access '{}' - {}", filename, err),
473 };
eb1804c5 474 let file_type = metadata.file_type();
23bb8780 475
ec8a9bb9 476 let extension = Path::new(target).extension().map(|s| s.to_str().unwrap()).unwrap();
bcd879cf 477
ec8a9bb9
DM
478 match extension {
479 "pxar" => {
480 if !file_type.is_dir() {
481 bail!("got unexpected file type (expected directory)");
482 }
483 upload_list.push((BackupType::PXAR, filename.to_owned(), target.to_owned(), 0));
484 }
485 "img" => {
eb1804c5 486
ec8a9bb9
DM
487 if !(file_type.is_file() || file_type.is_block_device()) {
488 bail!("got unexpected file type (expected file or block device)");
489 }
eb1804c5 490
ec8a9bb9 491 let size = tools::image_size(&PathBuf::from(filename))?;
23bb8780 492
ec8a9bb9 493 if size == 0 { bail!("got zero-sized file '{}'", filename); }
ae0be2dd 494
ec8a9bb9
DM
495 upload_list.push((BackupType::IMAGE, filename.to_owned(), target.to_owned(), size));
496 }
497 "conf" => {
498 if !file_type.is_file() {
499 bail!("got unexpected file type (expected regular file)");
500 }
501 upload_list.push((BackupType::CONFIG, filename.to_owned(), target.to_owned(), metadata.len()));
502 }
79679c2d
DM
503 "log" => {
504 if !file_type.is_file() {
505 bail!("got unexpected file type (expected regular file)");
506 }
507 upload_list.push((BackupType::LOGFILE, filename.to_owned(), target.to_owned(), metadata.len()));
508 }
ec8a9bb9
DM
509 _ => {
510 bail!("got unknown archive extension '{}'", extension);
511 }
ae0be2dd
DM
512 }
513 }
514
ca5d0b61 515 let backup_time = Utc.timestamp(backup_time_opt.unwrap_or(Utc::now().timestamp()), 0);
ae0be2dd 516
c4ff3dce 517 let client = HttpClient::new(repo.host(), repo.user())?;
d0a03d40
DM
518 record_repository(&repo);
519
ca5d0b61
DM
520 println!("Starting backup: {}/{}/{}", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time));
521
cdebd467 522 println!("Client name: {}", tools::nodename());
ca5d0b61
DM
523
524 let start_time = Local::now();
525
7a6cfbd9 526 println!("Starting protocol: {}", start_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
51144821 527
bb823140
DM
528 let (crypt_config, rsa_encrypted_key) = match keyfile {
529 None => (None, None),
6d0983db 530 Some(path) => {
bb823140
DM
531 let (key, created) = load_and_decrtypt_key(&path, get_encryption_key_password)?;
532
533 let crypt_config = CryptConfig::new(key)?;
534
535 let path = master_pubkey_path()?;
536 if path.exists() {
537 let pem_data = proxmox_backup::tools::file_get_contents(&path)?;
538 let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
539 let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?;
540 (Some(Arc::new(crypt_config)), Some(enc_key))
541 } else {
542 (Some(Arc::new(crypt_config)), None)
543 }
6d0983db
DM
544 }
545 };
f98ac774 546
ca5d0b61 547 let client = client.start_backup(repo.store(), backup_type, &backup_id, backup_time, verbose).wait()?;
c4ff3dce 548
6af905c1
DM
549 for (backup_type, filename, target, size) in upload_list {
550 match backup_type {
ec8a9bb9
DM
551 BackupType::CONFIG => {
552 println!("Upload config file '{}' to '{:?}' as {}", filename, repo, target);
9f46c7de 553 client.upload_blob_from_file(&filename, &target, crypt_config.clone(), true).wait()?;
ec8a9bb9 554 }
ca5d0b61 555 BackupType::LOGFILE => { // fixme: remove - not needed anymore ?
79679c2d
DM
556 println!("Upload log file '{}' to '{:?}' as {}", filename, repo, target);
557 client.upload_blob_from_file(&filename, &target, crypt_config.clone(), true).wait()?;
558 }
6af905c1
DM
559 BackupType::PXAR => {
560 println!("Upload directory '{}' to '{:?}' as {}", filename, repo, target);
f98ac774
DM
561 backup_directory(
562 &client,
563 &filename,
564 &target,
565 chunk_size_opt,
2eeaacb9 566 devices.clone(),
f98ac774 567 verbose,
5b72c9b4 568 skip_lost_and_found,
f98ac774
DM
569 crypt_config.clone(),
570 )?;
6af905c1
DM
571 }
572 BackupType::IMAGE => {
573 println!("Upload image '{}' to '{:?}' as {}", filename, repo, target);
f98ac774
DM
574 backup_image(
575 &client,
576 &filename,
577 &target,
578 size,
579 chunk_size_opt,
580 verbose,
581 crypt_config.clone(),
582 )?;
6af905c1
DM
583 }
584 }
4818c8b6
DM
585 }
586
bb823140
DM
587 if let Some(rsa_encrypted_key) = rsa_encrypted_key {
588 let target = "rsa-encrypted.key";
589 println!("Upload RSA encoded key to '{:?}' as {}", repo, target);
590 client.upload_blob_from_data(rsa_encrypted_key, target, None, false).wait()?;
591
592 // openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t
593 /*
594 let mut buffer2 = vec![0u8; rsa.size() as usize];
595 let pem_data = proxmox_backup::tools::file_get_contents("master-private.pem")?;
596 let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?;
597 let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?;
598 println!("TEST {} {:?}", len, buffer2);
599 */
9f46c7de
DM
600 }
601
c4ff3dce
DM
602 client.finish().wait()?;
603
7a6cfbd9 604 let end_time = Local::now();
ca5d0b61 605 let elapsed = end_time.signed_duration_since(start_time);
3ec3ec3f
DM
606 println!("Duration: {}", elapsed);
607
7a6cfbd9 608 println!("End Time: {}", end_time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false));
3d5c11e5 609
ff5d3707 610 Ok(Value::Null)
f98ea63d
DM
611}
612
d0a03d40 613fn complete_backup_source(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
f98ea63d
DM
614
615 let mut result = vec![];
616
617 let data: Vec<&str> = arg.splitn(2, ':').collect();
618
bff11030 619 if data.len() != 2 {
8968258b
DM
620 result.push(String::from("root.pxar:/"));
621 result.push(String::from("etc.pxar:/etc"));
bff11030
DM
622 return result;
623 }
f98ea63d 624
496a6784 625 let files = tools::complete_file_name(data[1], param);
f98ea63d
DM
626
627 for file in files {
628 result.push(format!("{}:{}", data[0], file));
629 }
630
631 result
ff5d3707 632}
633
9f912493
DM
634fn restore(
635 param: Value,
636 _info: &ApiMethod,
dd5495d6 637 _rpcenv: &mut dyn RpcEnvironment,
9f912493
DM
638) -> Result<Value, Error> {
639
2665cef7 640 let repo = extract_repository_from_value(&param)?;
9f912493 641
86eda3eb
DM
642 let verbose = param["verbose"].as_bool().unwrap_or(false);
643
46d5aa0a
DM
644 let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
645
d5c34d98
DM
646 let archive_name = tools::required_string_param(&param, "archive-name")?;
647
86eda3eb 648 let client = HttpClient::new(repo.host(), repo.user())?;
d0a03d40 649
d0a03d40 650 record_repository(&repo);
d5c34d98 651
9f912493 652 let path = tools::required_string_param(&param, "snapshot")?;
9f912493 653
86eda3eb 654 let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 {
d5c34d98 655 let group = BackupGroup::parse(path)?;
9f912493 656
9e391bb7
DM
657 let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
658 let result = client.get(&path, Some(json!({
d5c34d98
DM
659 "backup-type": group.backup_type(),
660 "backup-id": group.backup_id(),
9e391bb7 661 }))).wait()?;
9f912493 662
d5c34d98
DM
663 let list = result["data"].as_array().unwrap();
664 if list.len() == 0 {
665 bail!("backup group '{}' does not contain any snapshots:", path);
666 }
9f912493 667
86eda3eb 668 let epoch = list[0]["backup-time"].as_i64().unwrap();
fa5d6977 669 let backup_time = Utc.timestamp(epoch, 0);
86eda3eb 670 (group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time)
d5c34d98
DM
671 } else {
672 let snapshot = BackupDir::parse(path)?;
86eda3eb
DM
673 (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
674 };
9f912493 675
d5c34d98 676 let target = tools::required_string_param(&param, "target")?;
bf125261 677 let target = if target == "-" { None } else { Some(target) };
2ae7d196 678
86eda3eb 679 let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p));
2ae7d196 680
86eda3eb
DM
681 let crypt_config = match keyfile {
682 None => None,
683 Some(path) => {
684 let (key, _) = load_and_decrtypt_key(&path, get_encryption_key_password)?;
685 Some(Arc::new(CryptConfig::new(key)?))
686 }
687 };
d5c34d98 688
afb4cd28
DM
689 let server_archive_name = if archive_name.ends_with(".pxar") {
690 format!("{}.didx", archive_name)
691 } else if archive_name.ends_with(".img") {
692 format!("{}.fidx", archive_name)
693 } else {
f8100e96 694 format!("{}.blob", archive_name)
afb4cd28 695 };
9f912493 696
86eda3eb
DM
697 let client = client.start_backup_reader(repo.store(), &backup_type, &backup_id, backup_time, true).wait()?;
698
699 use std::os::unix::fs::OpenOptionsExt;
700
701 let tmpfile = std::fs::OpenOptions::new()
702 .write(true)
703 .read(true)
704 .custom_flags(libc::O_TMPFILE)
705 .open("/tmp")?;
706
f8100e96
DM
707 if server_archive_name.ends_with(".blob") {
708
709 let writer = Vec::with_capacity(1024*1024);
710 let blob_data = client.download(&server_archive_name, writer).wait()?;
711 let blob = DataBlob::from_raw(blob_data)?;
712 blob.verify_crc()?;
713
714 let raw_data = match crypt_config {
715 Some(ref crypt_config) => blob.decode(Some(crypt_config))?,
716 None => blob.decode(None)?,
717 };
718
bf125261
DM
719 if let Some(target) = target {
720 crate::tools::file_set_contents(target, &raw_data, None)?;
721 } else {
722 let stdout = std::io::stdout();
723 let mut writer = stdout.lock();
724 writer.write_all(&raw_data)
725 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
726 }
f8100e96
DM
727
728 } else if server_archive_name.ends_with(".didx") {
afb4cd28 729 let tmpfile = client.download(&server_archive_name, tmpfile).wait()?;
86eda3eb 730
afb4cd28
DM
731 let index = DynamicIndexReader::new(tmpfile)
732 .map_err(|err| format_err!("unable to read dynamic index '{}' - {}", archive_name, err))?;
86eda3eb 733
f4bf7dfc
DM
734 let most_used = index.find_most_used_chunks(8);
735
736 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
737
afb4cd28 738 let mut reader = BufferedDynamicReader::new(index, chunk_reader);
86eda3eb 739
bf125261 740 if let Some(target) = target {
86eda3eb 741
46d5aa0a
DM
742 if allow_existing_dirs {
743 unimplemented!();
744 }
745
bf125261
DM
746 let feature_flags = pxar::CA_FORMAT_DEFAULT;
747 let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags, |path| {
748 if verbose {
749 println!("{:?}", path);
750 }
751 Ok(())
752 });
753
fa7e957c 754 decoder.restore(Path::new(target), &Vec::new())?;
bf125261
DM
755 } else {
756 let stdout = std::io::stdout();
757 let mut writer = stdout.lock();
afb4cd28 758
bf125261
DM
759 std::io::copy(&mut reader, &mut writer)
760 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
761 }
afb4cd28
DM
762 } else if server_archive_name.ends_with(".fidx") {
763 let tmpfile = client.download(&server_archive_name, tmpfile).wait()?;
764
765 let index = FixedIndexReader::new(tmpfile)
766 .map_err(|err| format_err!("unable to read fixed index '{}' - {}", archive_name, err))?;
7dcbe051 767
f4bf7dfc
DM
768 let most_used = index.find_most_used_chunks(8);
769
770 let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
771
afb4cd28
DM
772 let mut reader = BufferedFixedReader::new(index, chunk_reader);
773
bf125261
DM
774 if let Some(target) = target {
775 let mut writer = std::fs::OpenOptions::new()
776 .write(true)
777 .create(true)
778 .create_new(true)
779 .open(target)
780 .map_err(|err| format_err!("unable to create target file {:?} - {}", target, err))?;
781
782 std::io::copy(&mut reader, &mut writer)
783 .map_err(|err| format_err!("unable to store data - {}", err))?;
784 } else {
785 let stdout = std::io::stdout();
786 let mut writer = stdout.lock();
afb4cd28 787
bf125261
DM
788 std::io::copy(&mut reader, &mut writer)
789 .map_err(|err| format_err!("unable to pipe data - {}", err))?;
790 }
45db6f89 791 } else {
f8100e96 792 bail!("unknown archive file extension (expected .pxar of .img)");
3031e44c 793 }
fef44d4f
DM
794
795 Ok(Value::Null)
45db6f89
DM
796}
797
ec34f7eb
DM
798fn upload_log(
799 param: Value,
800 _info: &ApiMethod,
801 _rpcenv: &mut dyn RpcEnvironment,
802) -> Result<Value, Error> {
803
804 let logfile = tools::required_string_param(&param, "logfile")?;
805 let repo = extract_repository_from_value(&param)?;
806
807 let snapshot = tools::required_string_param(&param, "snapshot")?;
808 let snapshot = BackupDir::parse(snapshot)?;
809
810 let mut client = HttpClient::new(repo.host(), repo.user())?;
811
812 let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p));
813
814 let crypt_config = match keyfile {
815 None => None,
816 Some(path) => {
817 let (key, _created) = load_and_decrtypt_key(&path, get_encryption_key_password)?;
818 let crypt_config = CryptConfig::new(key)?;
819 Some(crypt_config)
820 }
821 };
822
823 let data = crate::tools::file_get_contents(logfile)?;
824
825 let blob = if let Some(ref crypt_config) = crypt_config {
826 DataBlob::encode(&data, Some(crypt_config), true)?
827 } else {
828 DataBlob::encode(&data, None, true)?
829 };
830
831 let raw_data = blob.into_inner();
832
833 let path = format!("api2/json/admin/datastore/{}/upload-backup-log", repo.store());
834
835 let args = json!({
836 "backup-type": snapshot.group().backup_type(),
837 "backup-id": snapshot.group().backup_id(),
838 "backup-time": snapshot.backup_time().timestamp(),
839 });
840
841 let body = hyper::Body::from(raw_data);
842
843 let result = client.upload("application/octet-stream", body, &path, Some(args)).wait()?;
844
845 Ok(result)
846}
847
83b7db02 848fn prune(
ea7a7ef2 849 mut param: Value,
83b7db02 850 _info: &ApiMethod,
dd5495d6 851 _rpcenv: &mut dyn RpcEnvironment,
83b7db02
DM
852) -> Result<Value, Error> {
853
2665cef7 854 let repo = extract_repository_from_value(&param)?;
83b7db02 855
45cdce06 856 let mut client = HttpClient::new(repo.host(), repo.user())?;
83b7db02 857
d0a03d40 858 let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
83b7db02 859
9fdc3ef4
DM
860 let group = tools::required_string_param(&param, "group")?;
861 let group = BackupGroup::parse(group)?;
862
ea7a7ef2
DM
863 param.as_object_mut().unwrap().remove("repository");
864 param.as_object_mut().unwrap().remove("group");
865
866 param["backup-type"] = group.backup_type().into();
867 param["backup-id"] = group.backup_id().into();
83b7db02 868
ea7a7ef2 869 let result = client.post(&path, Some(param)).wait()?;
83b7db02 870
d0a03d40
DM
871 record_repository(&repo);
872
83b7db02
DM
873 Ok(result)
874}
875
34a816cc
DM
876fn status(
877 param: Value,
878 _info: &ApiMethod,
879 _rpcenv: &mut dyn RpcEnvironment,
880) -> Result<Value, Error> {
881
882 let repo = extract_repository_from_value(&param)?;
883
884 let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
885
886 let client = HttpClient::new(repo.host(), repo.user())?;
887
888 let path = format!("api2/json/admin/datastore/{}/status", repo.store());
889
890 let result = client.get(&path, None).wait()?;
891 let data = &result["data"];
892
893 record_repository(&repo);
894
895 if output_format == "text" {
896 let total = data["total"].as_u64().unwrap();
897 let used = data["used"].as_u64().unwrap();
898 let avail = data["avail"].as_u64().unwrap();
899 let roundup = total/200;
900
901 println!(
902 "total: {} used: {} ({} %) available: {}",
903 total,
904 used,
905 ((used+roundup)*100)/total,
906 avail,
907 );
908 } else {
f6ede796 909 format_and_print_result(data, &output_format);
34a816cc
DM
910 }
911
912 Ok(Value::Null)
913}
914
5a2df000 915// like get, but simply ignore errors and return Null instead
b2388518 916fn try_get(repo: &BackupRepository, url: &str) -> Value {
024f11bb 917
45cdce06
DM
918 let client = match HttpClient::new(repo.host(), repo.user()) {
919 Ok(v) => v,
920 _ => return Value::Null,
921 };
b2388518 922
9e391bb7 923 let mut resp = match client.get(url, None).wait() {
b2388518
DM
924 Ok(v) => v,
925 _ => return Value::Null,
926 };
927
928 if let Some(map) = resp.as_object_mut() {
929 if let Some(data) = map.remove("data") {
930 return data;
931 }
932 }
933 Value::Null
934}
935
b2388518 936fn complete_backup_group(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
024f11bb 937
b2388518
DM
938 let mut result = vec![];
939
2665cef7 940 let repo = match extract_repository_from_map(param) {
b2388518 941 Some(v) => v,
024f11bb
DM
942 _ => return result,
943 };
944
b2388518
DM
945 let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
946
947 let data = try_get(&repo, &path);
948
949 if let Some(list) = data.as_array() {
024f11bb 950 for item in list {
98f0b972
DM
951 if let (Some(backup_id), Some(backup_type)) =
952 (item["backup-id"].as_str(), item["backup-type"].as_str())
953 {
954 result.push(format!("{}/{}", backup_type, backup_id));
024f11bb
DM
955 }
956 }
957 }
958
959 result
960}
961
b2388518
DM
962fn complete_group_or_snapshot(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
963
964 let mut result = vec![];
965
2665cef7 966 let repo = match extract_repository_from_map(param) {
b2388518
DM
967 Some(v) => v,
968 _ => return result,
969 };
970
971 if arg.matches('/').count() < 2 {
972 let groups = complete_backup_group(arg, param);
973 for group in groups {
974 result.push(group.to_string());
975 result.push(format!("{}/", group));
976 }
977 return result;
978 }
979
980 let mut parts = arg.split('/');
981 let query = tools::json_object_to_query(json!({
982 "backup-type": parts.next().unwrap(),
983 "backup-id": parts.next().unwrap(),
984 })).unwrap();
985
986 let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
987
988 let data = try_get(&repo, &path);
989
990 if let Some(list) = data.as_array() {
991 for item in list {
992 if let (Some(backup_id), Some(backup_type), Some(backup_time)) =
993 (item["backup-id"].as_str(), item["backup-type"].as_str(), item["backup-time"].as_i64())
994 {
995 let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
996 result.push(snapshot.relative_path().to_str().unwrap().to_owned());
997 }
998 }
999 }
1000
1001 result
1002}
1003
45db6f89 1004fn complete_server_file_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
08dc340a
DM
1005
1006 let mut result = vec![];
1007
2665cef7 1008 let repo = match extract_repository_from_map(param) {
08dc340a
DM
1009 Some(v) => v,
1010 _ => return result,
1011 };
1012
1013 let snapshot = match param.get("snapshot") {
1014 Some(path) => {
1015 match BackupDir::parse(path) {
1016 Ok(v) => v,
1017 _ => return result,
1018 }
1019 }
1020 _ => return result,
1021 };
1022
1023 let query = tools::json_object_to_query(json!({
1024 "backup-type": snapshot.group().backup_type(),
1025 "backup-id": snapshot.group().backup_id(),
1026 "backup-time": snapshot.backup_time().timestamp(),
1027 })).unwrap();
1028
1029 let path = format!("api2/json/admin/datastore/{}/files?{}", repo.store(), query);
1030
1031 let data = try_get(&repo, &path);
1032
1033 if let Some(list) = data.as_array() {
1034 for item in list {
1035 if let Some(filename) = item.as_str() {
1036 result.push(filename.to_owned());
1037 }
1038 }
1039 }
1040
45db6f89
DM
1041 result
1042}
1043
1044fn complete_archive_name(arg: &str, param: &HashMap<String, String>) -> Vec<String> {
1045
1046 let result = complete_server_file_name(arg, param);
1047
6899dbfb 1048 strip_server_file_expenstions(result)
08dc340a
DM
1049}
1050
49811347
DM
1051fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
1052
1053 let mut result = vec![];
1054
1055 let mut size = 64;
1056 loop {
1057 result.push(size.to_string());
1058 size = size * 2;
1059 if size > 4096 { break; }
1060 }
1061
1062 result
1063}
1064
826f309b 1065fn get_encryption_key_password() -> Result<Vec<u8>, Error> {
ff5d3707 1066
f2401311
DM
1067 // fixme: implement other input methods
1068
1069 use std::env::VarError::*;
1070 match std::env::var("PBS_ENCRYPTION_PASSWORD") {
826f309b 1071 Ok(p) => return Ok(p.as_bytes().to_vec()),
f2401311
DM
1072 Err(NotUnicode(_)) => bail!("PBS_ENCRYPTION_PASSWORD contains bad characters"),
1073 Err(NotPresent) => {
1074 // Try another method
1075 }
1076 }
1077
1078 // If we're on a TTY, query the user for a password
1079 if crate::tools::tty::stdin_isatty() {
826f309b 1080 return Ok(crate::tools::tty::read_password("Encryption Key Password: ")?);
f2401311
DM
1081 }
1082
1083 bail!("no password input mechanism available");
1084}
1085
ac716234
DM
1086fn key_create(
1087 param: Value,
1088 _info: &ApiMethod,
1089 _rpcenv: &mut dyn RpcEnvironment,
1090) -> Result<Value, Error> {
1091
9b06db45
DM
1092 let path = tools::required_string_param(&param, "path")?;
1093 let path = PathBuf::from(path);
ac716234 1094
181f097a 1095 let kdf = param["kdf"].as_str().unwrap_or("scrypt");
ac716234
DM
1096
1097 let key = proxmox::sys::linux::random_data(32)?;
1098
181f097a
DM
1099 if kdf == "scrypt" {
1100 // always read passphrase from tty
1101 if !crate::tools::tty::stdin_isatty() {
1102 bail!("unable to read passphrase - no tty");
1103 }
ac716234 1104
181f097a
DM
1105 let password = crate::tools::tty::read_password("Encryption Key Password: ")?;
1106
ab44acff 1107 let key_config = encrypt_key_with_passphrase(&key, &password)?;
37c5a175 1108
ab44acff 1109 store_key_config(&path, false, key_config)?;
181f097a
DM
1110
1111 Ok(Value::Null)
1112 } else if kdf == "none" {
1113 let created = Local.timestamp(Local::now().timestamp(), 0);
1114
1115 store_key_config(&path, false, KeyConfig {
1116 kdf: None,
1117 created,
ab44acff 1118 modified: created,
181f097a
DM
1119 data: key,
1120 })?;
1121
1122 Ok(Value::Null)
1123 } else {
1124 unreachable!();
1125 }
ac716234
DM
1126}
1127
9f46c7de
DM
1128fn master_pubkey_path() -> Result<PathBuf, Error> {
1129 let base = BaseDirectories::with_prefix("proxmox-backup")?;
1130
1131 // usually $HOME/.config/proxmox-backup/master-public.pem
1132 let path = base.place_config_file("master-public.pem")?;
1133
1134 Ok(path)
1135}
1136
3ea8bfc9
DM
1137fn key_import_master_pubkey(
1138 param: Value,
1139 _info: &ApiMethod,
1140 _rpcenv: &mut dyn RpcEnvironment,
1141) -> Result<Value, Error> {
1142
1143 let path = tools::required_string_param(&param, "path")?;
1144 let path = PathBuf::from(path);
1145
1146 let pem_data = proxmox_backup::tools::file_get_contents(&path)?;
1147
1148 if let Err(err) = openssl::pkey::PKey::public_key_from_pem(&pem_data) {
1149 bail!("Unable to decode PEM data - {}", err);
1150 }
1151
9f46c7de 1152 let target_path = master_pubkey_path()?;
3ea8bfc9
DM
1153
1154 proxmox_backup::tools::file_set_contents(&target_path, &pem_data, None)?;
1155
1156 println!("Imported public master key to {:?}", target_path);
1157
1158 Ok(Value::Null)
1159}
1160
37c5a175
DM
1161fn key_create_master_key(
1162 _param: Value,
1163 _info: &ApiMethod,
1164 _rpcenv: &mut dyn RpcEnvironment,
1165) -> Result<Value, Error> {
1166
1167 // we need a TTY to query the new password
1168 if !crate::tools::tty::stdin_isatty() {
1169 bail!("unable to create master key - no tty");
1170 }
1171
1172 let rsa = openssl::rsa::Rsa::generate(4096)?;
1173 let pkey = openssl::pkey::PKey::from_rsa(rsa)?;
1174
1175 let new_pw = String::from_utf8(crate::tools::tty::read_password("Master Key Password: ")?)?;
1176 let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?;
1177
1178 if new_pw != verify_pw {
1179 bail!("Password verification fail!");
1180 }
1181
1182 if new_pw.len() < 5 {
1183 bail!("Password is too short!");
1184 }
1185
1186 let pub_key: Vec<u8> = pkey.public_key_to_pem()?;
1187 let filename_pub = "master-public.pem";
1188 println!("Writing public master key to {}", filename_pub);
1189 proxmox_backup::tools::file_set_contents(filename_pub, pub_key.as_slice(), None)?;
1190
1191 let cipher = openssl::symm::Cipher::aes_256_cbc();
1192 let priv_key: Vec<u8> = pkey.private_key_to_pem_pkcs8_passphrase(cipher, new_pw.as_bytes())?;
1193
1194 let filename_priv = "master-private.pem";
1195 println!("Writing private master key to {}", filename_priv);
1196 proxmox_backup::tools::file_set_contents(filename_priv, priv_key.as_slice(), None)?;
1197
1198 Ok(Value::Null)
1199}
ac716234
DM
1200
1201fn key_change_passphrase(
1202 param: Value,
1203 _info: &ApiMethod,
1204 _rpcenv: &mut dyn RpcEnvironment,
1205) -> Result<Value, Error> {
1206
9b06db45
DM
1207 let path = tools::required_string_param(&param, "path")?;
1208 let path = PathBuf::from(path);
ac716234 1209
181f097a
DM
1210 let kdf = param["kdf"].as_str().unwrap_or("scrypt");
1211
ac716234
DM
1212 // we need a TTY to query the new password
1213 if !crate::tools::tty::stdin_isatty() {
1214 bail!("unable to change passphrase - no tty");
1215 }
1216
ab44acff 1217 let (key, created) = load_and_decrtypt_key(&path, get_encryption_key_password)?;
ac716234 1218
181f097a 1219 if kdf == "scrypt" {
ac716234 1220
181f097a
DM
1221 let new_pw = String::from_utf8(crate::tools::tty::read_password("New Password: ")?)?;
1222 let verify_pw = String::from_utf8(crate::tools::tty::read_password("Verify Password: ")?)?;
ac716234 1223
181f097a
DM
1224 if new_pw != verify_pw {
1225 bail!("Password verification fail!");
1226 }
1227
1228 if new_pw.len() < 5 {
1229 bail!("Password is too short!");
1230 }
ac716234 1231
ab44acff
DM
1232 let mut new_key_config = encrypt_key_with_passphrase(&key, new_pw.as_bytes())?;
1233 new_key_config.created = created; // keep original value
1234
1235 store_key_config(&path, true, new_key_config)?;
ac716234 1236
181f097a
DM
1237 Ok(Value::Null)
1238 } else if kdf == "none" {
ab44acff 1239 let modified = Local.timestamp(Local::now().timestamp(), 0);
181f097a
DM
1240
1241 store_key_config(&path, true, KeyConfig {
1242 kdf: None,
ab44acff
DM
1243 created, // keep original value
1244 modified,
6d0983db 1245 data: key.to_vec(),
181f097a
DM
1246 })?;
1247
1248 Ok(Value::Null)
1249 } else {
1250 unreachable!();
1251 }
f2401311
DM
1252}
1253
1254fn key_mgmt_cli() -> CliCommandMap {
1255
181f097a
DM
1256 let kdf_schema: Arc<Schema> = Arc::new(
1257 StringSchema::new("Key derivation function. Choose 'none' to store the key unecrypted.")
1258 .format(Arc::new(ApiStringFormat::Enum(&["scrypt", "none"])))
1259 .default("scrypt")
1260 .into()
1261 );
1262
f2401311
DM
1263 let key_create_cmd_def = CliCommand::new(
1264 ApiMethod::new(
1265 key_create,
1266 ObjectSchema::new("Create a new encryption key.")
9b06db45 1267 .required("path", StringSchema::new("File system path."))
181f097a 1268 .optional("kdf", kdf_schema.clone())
f2401311 1269 ))
9b06db45
DM
1270 .arg_param(vec!["path"])
1271 .completion_cb("path", tools::complete_file_name);
f2401311 1272
ac716234
DM
1273 let key_change_passphrase_cmd_def = CliCommand::new(
1274 ApiMethod::new(
1275 key_change_passphrase,
1276 ObjectSchema::new("Change the passphrase required to decrypt the key.")
9b06db45 1277 .required("path", StringSchema::new("File system path."))
181f097a 1278 .optional("kdf", kdf_schema.clone())
9b06db45
DM
1279 ))
1280 .arg_param(vec!["path"])
1281 .completion_cb("path", tools::complete_file_name);
ac716234 1282
37c5a175
DM
1283 let key_create_master_key_cmd_def = CliCommand::new(
1284 ApiMethod::new(
1285 key_create_master_key,
1286 ObjectSchema::new("Create a new 4096 bit RSA master pub/priv key pair.")
1287 ));
1288
3ea8bfc9
DM
1289 let key_import_master_pubkey_cmd_def = CliCommand::new(
1290 ApiMethod::new(
1291 key_import_master_pubkey,
1292 ObjectSchema::new("Import a new RSA public key and use it as master key. The key is expected to be in '.pem' format.")
1293 .required("path", StringSchema::new("File system path."))
1294 ))
1295 .arg_param(vec!["path"])
1296 .completion_cb("path", tools::complete_file_name);
1297
f2401311 1298 let cmd_def = CliCommandMap::new()
ac716234 1299 .insert("create".to_owned(), key_create_cmd_def.into())
37c5a175 1300 .insert("create-master-key".to_owned(), key_create_master_key_cmd_def.into())
3ea8bfc9 1301 .insert("import-master-pubkey".to_owned(), key_import_master_pubkey_cmd_def.into())
ac716234 1302 .insert("change-passphrase".to_owned(), key_change_passphrase_cmd_def.into());
f2401311
DM
1303
1304 cmd_def
1305}
1306
f2401311 1307fn main() {
33d64b81 1308
25f1650b
DM
1309 let backup_source_schema: Arc<Schema> = Arc::new(
1310 StringSchema::new("Backup source specification ([<label>:<path>]).")
1311 .format(Arc::new(ApiStringFormat::Pattern(&BACKUPSPEC_REGEX)))
1312 .into()
1313 );
1314
597a9203 1315 let backup_cmd_def = CliCommand::new(
ff5d3707 1316 ApiMethod::new(
bcd879cf 1317 create_backup,
597a9203 1318 ObjectSchema::new("Create (host) backup.")
ae0be2dd
DM
1319 .required(
1320 "backupspec",
1321 ArraySchema::new(
74cdb521 1322 "List of backup source specifications ([<label.ext>:<path>] ...)",
25f1650b 1323 backup_source_schema,
ae0be2dd
DM
1324 ).min_length(1)
1325 )
2665cef7 1326 .optional("repository", REPO_URL_SCHEMA.clone())
2eeaacb9
DM
1327 .optional(
1328 "include-dev",
1329 ArraySchema::new(
1330 "Include mountpoints with same st_dev number (see ``man fstat``) as specified files.",
1331 StringSchema::new("Path to file.").into()
1332 )
1333 )
6d0983db
DM
1334 .optional(
1335 "keyfile",
1336 StringSchema::new("Path to encryption key. All data will be encrypted using this key."))
219ef0e6
DM
1337 .optional(
1338 "verbose",
1339 BooleanSchema::new("Verbose output.").default(false))
5b72c9b4
DM
1340 .optional(
1341 "skip-lost-and-found",
1342 BooleanSchema::new("Skip lost+found directory").default(false))
fba30411 1343 .optional(
bbf9e7e9
DM
1344 "backup-type",
1345 BACKUP_TYPE_SCHEMA.clone()
1346 )
1347 .optional(
1348 "backup-id",
1349 BACKUP_ID_SCHEMA.clone()
1350 )
ca5d0b61
DM
1351 .optional(
1352 "backup-time",
bbf9e7e9 1353 BACKUP_TIME_SCHEMA.clone()
ca5d0b61 1354 )
2d9d143a
DM
1355 .optional(
1356 "chunk-size",
1357 IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
1358 .minimum(64)
1359 .maximum(4096)
1360 .default(4096)
1361 )
ff5d3707 1362 ))
2665cef7 1363 .arg_param(vec!["backupspec"])
d0a03d40 1364 .completion_cb("repository", complete_repository)
49811347 1365 .completion_cb("backupspec", complete_backup_source)
6d0983db 1366 .completion_cb("keyfile", tools::complete_file_name)
49811347 1367 .completion_cb("chunk-size", complete_chunk_size);
f8838fe9 1368
ec34f7eb
DM
1369 let upload_log_cmd_def = CliCommand::new(
1370 ApiMethod::new(
1371 upload_log,
1372 ObjectSchema::new("Upload backup log file.")
1373 .required("snapshot", StringSchema::new("Snapshot path."))
1374 .required("logfile", StringSchema::new("The path to the log file you want to upload."))
1375 .optional("repository", REPO_URL_SCHEMA.clone())
1376 .optional(
1377 "keyfile",
1378 StringSchema::new("Path to encryption key. All data will be encrypted using this key."))
1379 ))
1380 .arg_param(vec!["snapshot", "logfile"])
1381 .completion_cb("snapshot", complete_group_or_snapshot)
1382 .completion_cb("logfile", tools::complete_file_name)
1383 .completion_cb("keyfile", tools::complete_file_name)
1384 .completion_cb("repository", complete_repository);
1385
41c039e1
DM
1386 let list_cmd_def = CliCommand::new(
1387 ApiMethod::new(
812c6f87
DM
1388 list_backup_groups,
1389 ObjectSchema::new("List backup groups.")
2665cef7 1390 .optional("repository", REPO_URL_SCHEMA.clone())
34a816cc 1391 .optional("output-format", OUTPUT_FORMAT.clone())
41c039e1 1392 ))
d0a03d40 1393 .completion_cb("repository", complete_repository);
41c039e1 1394
184f17af
DM
1395 let snapshots_cmd_def = CliCommand::new(
1396 ApiMethod::new(
1397 list_snapshots,
1398 ObjectSchema::new("List backup snapshots.")
15c847f1 1399 .optional("group", StringSchema::new("Backup group."))
2665cef7 1400 .optional("repository", REPO_URL_SCHEMA.clone())
34a816cc 1401 .optional("output-format", OUTPUT_FORMAT.clone())
184f17af 1402 ))
2665cef7 1403 .arg_param(vec!["group"])
024f11bb 1404 .completion_cb("group", complete_backup_group)
d0a03d40 1405 .completion_cb("repository", complete_repository);
184f17af 1406
6f62c924
DM
1407 let forget_cmd_def = CliCommand::new(
1408 ApiMethod::new(
1409 forget_snapshots,
1410 ObjectSchema::new("Forget (remove) backup snapshots.")
6f62c924 1411 .required("snapshot", StringSchema::new("Snapshot path."))
2665cef7 1412 .optional("repository", REPO_URL_SCHEMA.clone())
6f62c924 1413 ))
2665cef7 1414 .arg_param(vec!["snapshot"])
b2388518
DM
1415 .completion_cb("repository", complete_repository)
1416 .completion_cb("snapshot", complete_group_or_snapshot);
6f62c924 1417
8cc0d6af
DM
1418 let garbage_collect_cmd_def = CliCommand::new(
1419 ApiMethod::new(
1420 start_garbage_collection,
1421 ObjectSchema::new("Start garbage collection for a specific repository.")
2665cef7 1422 .optional("repository", REPO_URL_SCHEMA.clone())
8cc0d6af 1423 ))
d0a03d40 1424 .completion_cb("repository", complete_repository);
8cc0d6af 1425
9f912493
DM
1426 let restore_cmd_def = CliCommand::new(
1427 ApiMethod::new(
1428 restore,
1429 ObjectSchema::new("Restore backup repository.")
d5c34d98
DM
1430 .required("snapshot", StringSchema::new("Group/Snapshot path."))
1431 .required("archive-name", StringSchema::new("Backup archive name."))
bf125261
DM
1432 .required("target", StringSchema::new(r###"Target directory path. Use '-' to write to stdandard output.
1433
1434We do not extraxt '.pxar' archives when writing to stdandard output.
1435
1436"###
1437 ))
46d5aa0a
DM
1438 .optional(
1439 "allow-existing-dirs",
1440 BooleanSchema::new("Do not fail if directories already exists.").default(false))
2665cef7 1441 .optional("repository", REPO_URL_SCHEMA.clone())
86eda3eb
DM
1442 .optional("keyfile", StringSchema::new("Path to encryption key."))
1443 .optional(
1444 "verbose",
1445 BooleanSchema::new("Verbose output.").default(false)
1446 )
9f912493 1447 ))
2665cef7 1448 .arg_param(vec!["snapshot", "archive-name", "target"])
b2388518 1449 .completion_cb("repository", complete_repository)
08dc340a
DM
1450 .completion_cb("snapshot", complete_group_or_snapshot)
1451 .completion_cb("archive-name", complete_archive_name)
1452 .completion_cb("target", tools::complete_file_name);
9f912493 1453
83b7db02
DM
1454 let prune_cmd_def = CliCommand::new(
1455 ApiMethod::new(
1456 prune,
1457 proxmox_backup::api2::admin::datastore::add_common_prune_prameters(
1458 ObjectSchema::new("Prune backup repository.")
9fdc3ef4 1459 .required("group", StringSchema::new("Backup group."))
2665cef7 1460 .optional("repository", REPO_URL_SCHEMA.clone())
83b7db02
DM
1461 )
1462 ))
9fdc3ef4
DM
1463 .arg_param(vec!["group"])
1464 .completion_cb("group", complete_backup_group)
d0a03d40 1465 .completion_cb("repository", complete_repository);
9f912493 1466
34a816cc
DM
1467 let status_cmd_def = CliCommand::new(
1468 ApiMethod::new(
1469 status,
1470 ObjectSchema::new("Get repository status.")
1471 .optional("repository", REPO_URL_SCHEMA.clone())
1472 .optional("output-format", OUTPUT_FORMAT.clone())
1473 ))
1474 .completion_cb("repository", complete_repository);
1475
41c039e1 1476 let cmd_def = CliCommandMap::new()
597a9203 1477 .insert("backup".to_owned(), backup_cmd_def.into())
ec34f7eb 1478 .insert("upload-log".to_owned(), upload_log_cmd_def.into())
6f62c924 1479 .insert("forget".to_owned(), forget_cmd_def.into())
8cc0d6af 1480 .insert("garbage-collect".to_owned(), garbage_collect_cmd_def.into())
83b7db02 1481 .insert("list".to_owned(), list_cmd_def.into())
184f17af 1482 .insert("prune".to_owned(), prune_cmd_def.into())
9f912493 1483 .insert("restore".to_owned(), restore_cmd_def.into())
f2401311 1484 .insert("snapshots".to_owned(), snapshots_cmd_def.into())
34a816cc 1485 .insert("status".to_owned(), status_cmd_def.into())
f2401311 1486 .insert("key".to_owned(), key_mgmt_cli().into());
a914a774 1487
5a2df000
DM
1488 hyper::rt::run(futures::future::lazy(move || {
1489 run_cli_command(cmd_def.into());
1490 Ok(())
1491 }));
496a6784 1492
ff5d3707 1493}