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