1 use std
::collections
::{HashSet, HashMap}
;
2 use std
::convert
::TryFrom
;
3 use std
::io
::{self, Read, Write, Seek, SeekFrom}
;
4 use std
::os
::unix
::io
::{FromRawFd, RawFd}
;
5 use std
::path
::{Path, PathBuf}
;
7 use std
::sync
::{Arc, Mutex}
;
8 use std
::task
::Context
;
10 use anyhow
::{bail, format_err, Error}
;
11 use futures
::future
::FutureExt
;
12 use futures
::stream
::{StreamExt, TryStreamExt}
;
13 use serde_json
::{json, Value}
;
14 use tokio
::sync
::mpsc
;
15 use tokio_stream
::wrappers
::ReceiverStream
;
16 use xdg
::BaseDirectories
;
18 use pathpatterns
::{MatchEntry, MatchType, PatternFlag}
;
21 time
::{strftime_local, epoch_i64}
,
22 fs
::{file_get_contents, file_get_json, replace_file, CreateOptions, image_size}
,
33 use pxar
::accessor
::{MaybeReady, ReadAt, ReadAtOperation}
;
35 use proxmox_backup
::tools
;
36 use proxmox_backup
::api2
::access
::user
::UserWithTokens
;
37 use proxmox_backup
::api2
::types
::*;
38 use proxmox_backup
::api2
::version
;
39 use proxmox_backup
::client
::*;
40 use proxmox_backup
::pxar
::catalog
::*;
41 use proxmox_backup
::backup
::{
44 rsa_encrypt_key_config
,
51 BufferedDynamicReader
,
59 ENCRYPTED_KEY_BLOB_NAME
,
68 mod proxmox_backup_client
;
69 use proxmox_backup_client
::*;
71 const ENV_VAR_PBS_FINGERPRINT
: &str = "PBS_FINGERPRINT";
72 const ENV_VAR_PBS_PASSWORD
: &str = "PBS_PASSWORD";
75 pub const REPO_URL_SCHEMA
: Schema
= StringSchema
::new("Repository URL.")
76 .format(&BACKUP_REPO_URL
)
80 pub const KEYFILE_SCHEMA
: Schema
= StringSchema
::new(
81 "Path to encryption key. All data will be encrypted using this key.")
84 pub const KEYFD_SCHEMA
: Schema
= IntegerSchema
::new(
85 "Pass an encryption key via an already opened file descriptor.")
89 const CHUNK_SIZE_SCHEMA
: Schema
= IntegerSchema
::new(
90 "Chunk size in KB. Must be a power of 2.")
96 fn get_default_repository() -> Option
<String
> {
97 std
::env
::var("PBS_REPOSITORY").ok()
100 pub fn extract_repository_from_value(
102 ) -> Result
<BackupRepository
, Error
> {
104 let repo_url
= param
["repository"]
107 .or_else(get_default_repository
)
108 .ok_or_else(|| format_err
!("unable to get (default) repository"))?
;
110 let repo
: BackupRepository
= repo_url
.parse()?
;
115 fn extract_repository_from_map(
116 param
: &HashMap
<String
, String
>,
117 ) -> Option
<BackupRepository
> {
119 param
.get("repository")
121 .or_else(get_default_repository
)
122 .and_then(|repo_url
| repo_url
.parse
::<BackupRepository
>().ok())
125 fn record_repository(repo
: &BackupRepository
) {
127 let base
= match BaseDirectories
::with_prefix("proxmox-backup") {
132 // usually $HOME/.cache/proxmox-backup/repo-list
133 let path
= match base
.place_cache_file("repo-list") {
138 let mut data
= file_get_json(&path
, None
).unwrap_or_else(|_
| json
!({}
));
140 let repo
= repo
.to_string();
142 data
[&repo
] = json
!{ data[&repo].as_i64().unwrap_or(0) + 1 }
;
144 let mut map
= serde_json
::map
::Map
::new();
147 let mut max_used
= 0;
148 let mut max_repo
= None
;
149 for (repo
, count
) in data
.as_object().unwrap() {
150 if map
.contains_key(repo
) { continue; }
151 if let Some(count
) = count
.as_i64() {
152 if count
> max_used
{
154 max_repo
= Some(repo
);
158 if let Some(repo
) = max_repo
{
159 map
.insert(repo
.to_owned(), json
!(max_used
));
163 if map
.len() > 10 { // store max. 10 repos
168 let new_data
= json
!(map
);
170 let _
= replace_file(path
, new_data
.to_string().as_bytes(), CreateOptions
::new());
173 pub fn complete_repository(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
175 let mut result
= vec
![];
177 let base
= match BaseDirectories
::with_prefix("proxmox-backup") {
182 // usually $HOME/.cache/proxmox-backup/repo-list
183 let path
= match base
.place_cache_file("repo-list") {
188 let data
= file_get_json(&path
, None
).unwrap_or_else(|_
| json
!({}
));
190 if let Some(map
) = data
.as_object() {
191 for (repo
, _count
) in map
{
192 result
.push(repo
.to_owned());
199 fn connect(repo
: &BackupRepository
) -> Result
<HttpClient
, Error
> {
200 connect_do(repo
.host(), repo
.port(), repo
.auth_id())
201 .map_err(|err
| format_err
!("error building client for repository {} - {}", repo
, err
))
204 fn connect_do(server
: &str, port
: u16, auth_id
: &Authid
) -> Result
<HttpClient
, Error
> {
205 let fingerprint
= std
::env
::var(ENV_VAR_PBS_FINGERPRINT
).ok();
207 use std
::env
::VarError
::*;
208 let password
= match std
::env
::var(ENV_VAR_PBS_PASSWORD
) {
210 Err(NotUnicode(_
)) => bail
!(format
!("{} contains bad characters", ENV_VAR_PBS_PASSWORD
)),
211 Err(NotPresent
) => None
,
214 let options
= HttpClientOptions
::new()
215 .prefix(Some("proxmox-backup".to_string()))
218 .fingerprint(fingerprint
)
219 .fingerprint_cache(true)
222 HttpClient
::new(server
, port
, auth_id
, options
)
225 async
fn view_task_result(
229 ) -> Result
<(), Error
> {
230 let data
= &result
["data"];
231 if output_format
== "text" {
232 if let Some(upid
) = data
.as_str() {
233 display_task_log(client
, upid
, true).await?
;
236 format_and_print_result(&data
, &output_format
);
242 async
fn api_datastore_list_snapshots(
245 group
: Option
<BackupGroup
>,
246 ) -> Result
<Value
, Error
> {
248 let path
= format
!("api2/json/admin/datastore/{}/snapshots", store
);
250 let mut args
= json
!({}
);
251 if let Some(group
) = group
{
252 args
["backup-type"] = group
.backup_type().into();
253 args
["backup-id"] = group
.backup_id().into();
256 let mut result
= client
.get(&path
, Some(args
)).await?
;
258 Ok(result
["data"].take())
261 pub async
fn api_datastore_latest_snapshot(
265 ) -> Result
<(String
, String
, i64), Error
> {
267 let list
= api_datastore_list_snapshots(client
, store
, Some(group
.clone())).await?
;
268 let mut list
: Vec
<SnapshotListItem
> = serde_json
::from_value(list
)?
;
271 bail
!("backup group {:?} does not contain any snapshots.", group
.group_path());
274 list
.sort_unstable_by(|a
, b
| b
.backup_time
.cmp(&a
.backup_time
));
276 let backup_time
= list
[0].backup_time
;
278 Ok((group
.backup_type().to_owned(), group
.backup_id().to_owned(), backup_time
))
281 async
fn backup_directory
<P
: AsRef
<Path
>>(
282 client
: &BackupWriter
,
283 previous_manifest
: Option
<Arc
<BackupManifest
>>,
286 chunk_size
: Option
<usize>,
287 device_set
: Option
<HashSet
<u64>>,
289 skip_lost_and_found
: bool
,
290 catalog
: Arc
<Mutex
<CatalogWriter
<crate::tools
::StdChannelWriter
>>>,
291 exclude_pattern
: Vec
<MatchEntry
>,
295 ) -> Result
<BackupStats
, Error
> {
297 let pxar_stream
= PxarBackupStream
::open(
306 let mut chunk_stream
= ChunkStream
::new(pxar_stream
, chunk_size
);
308 let (tx
, rx
) = mpsc
::channel(10); // allow to buffer 10 chunks
310 let stream
= ReceiverStream
::new(rx
)
311 .map_err(Error
::from
);
313 // spawn chunker inside a separate task so that it can run parallel
314 tokio
::spawn(async
move {
315 while let Some(v
) = chunk_stream
.next().await
{
316 let _
= tx
.send(v
).await
;
321 .upload_stream(previous_manifest
, archive_name
, stream
, "dynamic", None
, compress
, encrypt
)
327 async
fn backup_image
<P
: AsRef
<Path
>>(
328 client
: &BackupWriter
,
329 previous_manifest
: Option
<Arc
<BackupManifest
>>,
333 chunk_size
: Option
<usize>,
337 ) -> Result
<BackupStats
, Error
> {
339 let path
= image_path
.as_ref().to_owned();
341 let file
= tokio
::fs
::File
::open(path
).await?
;
343 let stream
= tokio_util
::codec
::FramedRead
::new(file
, tokio_util
::codec
::BytesCodec
::new())
344 .map_err(Error
::from
);
346 let stream
= FixedChunkStream
::new(stream
, chunk_size
.unwrap_or(4*1024*1024));
349 .upload_stream(previous_manifest
, archive_name
, stream
, "fixed", Some(image_size
), compress
, encrypt
)
359 schema
: REPO_URL_SCHEMA
,
363 schema
: OUTPUT_FORMAT
,
369 /// List backup groups.
370 async
fn list_backup_groups(param
: Value
) -> Result
<Value
, Error
> {
372 let output_format
= get_output_format(¶m
);
374 let repo
= extract_repository_from_value(¶m
)?
;
376 let client
= connect(&repo
)?
;
378 let path
= format
!("api2/json/admin/datastore/{}/groups", repo
.store());
380 let mut result
= client
.get(&path
, None
).await?
;
382 record_repository(&repo
);
384 let render_group_path
= |_v
: &Value
, record
: &Value
| -> Result
<String
, Error
> {
385 let item
: GroupListItem
= serde_json
::from_value(record
.to_owned())?
;
386 let group
= BackupGroup
::new(item
.backup_type
, item
.backup_id
);
387 Ok(group
.group_path().to_str().unwrap().to_owned())
390 let render_last_backup
= |_v
: &Value
, record
: &Value
| -> Result
<String
, Error
> {
391 let item
: GroupListItem
= serde_json
::from_value(record
.to_owned())?
;
392 let snapshot
= BackupDir
::new(item
.backup_type
, item
.backup_id
, item
.last_backup
)?
;
393 Ok(snapshot
.relative_path().to_str().unwrap().to_owned())
396 let render_files
= |_v
: &Value
, record
: &Value
| -> Result
<String
, Error
> {
397 let item
: GroupListItem
= serde_json
::from_value(record
.to_owned())?
;
398 Ok(tools
::format
::render_backup_file_list(&item
.files
))
401 let options
= default_table_format_options()
402 .sortby("backup-type", false)
403 .sortby("backup-id", false)
404 .column(ColumnConfig
::new("backup-id").renderer(render_group_path
).header("group"))
406 ColumnConfig
::new("last-backup")
407 .renderer(render_last_backup
)
408 .header("last snapshot")
411 .column(ColumnConfig
::new("backup-count"))
412 .column(ColumnConfig
::new("files").renderer(render_files
));
414 let mut data
: Value
= result
["data"].take();
416 let return_type
= &proxmox_backup
::api2
::admin
::datastore
::API_METHOD_LIST_GROUPS
.returns
;
418 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
427 schema
: REPO_URL_SCHEMA
,
432 description
: "Backup group.",
440 /// Change owner of a backup group
441 async
fn change_backup_owner(group
: String
, mut param
: Value
) -> Result
<(), Error
> {
443 let repo
= extract_repository_from_value(¶m
)?
;
445 let mut client
= connect(&repo
)?
;
447 param
.as_object_mut().unwrap().remove("repository");
449 let group
: BackupGroup
= group
.parse()?
;
451 param
["backup-type"] = group
.backup_type().into();
452 param
["backup-id"] = group
.backup_id().into();
454 let path
= format
!("api2/json/admin/datastore/{}/change-owner", repo
.store());
455 client
.post(&path
, Some(param
)).await?
;
457 record_repository(&repo
);
466 schema
: REPO_URL_SCHEMA
,
472 /// Try to login. If successful, store ticket.
473 async
fn api_login(param
: Value
) -> Result
<Value
, Error
> {
475 let repo
= extract_repository_from_value(¶m
)?
;
477 let client
= connect(&repo
)?
;
478 client
.login().await?
;
480 record_repository(&repo
);
489 schema
: REPO_URL_SCHEMA
,
495 /// Logout (delete stored ticket).
496 fn api_logout(param
: Value
) -> Result
<Value
, Error
> {
498 let repo
= extract_repository_from_value(¶m
)?
;
500 delete_ticket_info("proxmox-backup", repo
.host(), repo
.user())?
;
509 schema
: REPO_URL_SCHEMA
,
513 schema
: OUTPUT_FORMAT
,
519 /// Show client and optional server version
520 async
fn api_version(param
: Value
) -> Result
<(), Error
> {
522 let output_format
= get_output_format(¶m
);
524 let mut version_info
= json
!({
526 "version": version
::PROXMOX_PKG_VERSION
,
527 "release": version
::PROXMOX_PKG_RELEASE
,
528 "repoid": version
::PROXMOX_PKG_REPOID
,
532 let repo
= extract_repository_from_value(¶m
);
533 if let Ok(repo
) = repo
{
534 let client
= connect(&repo
)?
;
536 match client
.get("api2/json/version", None
).await
{
537 Ok(mut result
) => version_info
["server"] = result
["data"].take(),
538 Err(e
) => eprintln
!("could not connect to server - {}", e
),
541 if output_format
== "text" {
542 println
!("client version: {}.{}", version
::PROXMOX_PKG_VERSION
, version
::PROXMOX_PKG_RELEASE
);
543 if let Some(server
) = version_info
["server"].as_object() {
544 let server_version
= server
["version"].as_str().unwrap();
545 let server_release
= server
["release"].as_str().unwrap();
546 println
!("server version: {}.{}", server_version
, server_release
);
549 format_and_print_result(&version_info
, &output_format
);
559 schema
: REPO_URL_SCHEMA
,
563 schema
: OUTPUT_FORMAT
,
569 /// Start garbage collection for a specific repository.
570 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
572 let repo
= extract_repository_from_value(¶m
)?
;
574 let output_format
= get_output_format(¶m
);
576 let mut client
= connect(&repo
)?
;
578 let path
= format
!("api2/json/admin/datastore/{}/gc", repo
.store());
580 let result
= client
.post(&path
, None
).await?
;
582 record_repository(&repo
);
584 view_task_result(client
, result
, &output_format
).await?
;
589 fn spawn_catalog_upload(
590 client
: Arc
<BackupWriter
>,
594 Arc
<Mutex
<CatalogWriter
<crate::tools
::StdChannelWriter
>>>,
595 tokio
::sync
::oneshot
::Receiver
<Result
<BackupStats
, Error
>>
598 let (catalog_tx
, catalog_rx
) = std
::sync
::mpsc
::sync_channel(10); // allow to buffer 10 writes
599 let catalog_stream
= crate::tools
::StdChannelStream(catalog_rx
);
600 let catalog_chunk_size
= 512*1024;
601 let catalog_chunk_stream
= ChunkStream
::new(catalog_stream
, Some(catalog_chunk_size
));
603 let catalog
= Arc
::new(Mutex
::new(CatalogWriter
::new(crate::tools
::StdChannelWriter
::new(catalog_tx
))?
));
605 let (catalog_result_tx
, catalog_result_rx
) = tokio
::sync
::oneshot
::channel();
607 tokio
::spawn(async
move {
608 let catalog_upload_result
= client
609 .upload_stream(None
, CATALOG_NAME
, catalog_chunk_stream
, "dynamic", None
, true, encrypt
)
612 if let Err(ref err
) = catalog_upload_result
{
613 eprintln
!("catalog upload error - {}", err
);
617 let _
= catalog_result_tx
.send(catalog_upload_result
);
620 Ok((catalog
, catalog_result_rx
))
623 fn keyfile_parameters(param
: &Value
) -> Result
<(Option
<Vec
<u8>>, CryptMode
), Error
> {
624 let keyfile
= match param
.get("keyfile") {
625 Some(Value
::String(keyfile
)) => Some(keyfile
),
626 Some(_
) => bail
!("bad --keyfile parameter type"),
630 let key_fd
= match param
.get("keyfd") {
631 Some(Value
::Number(key_fd
)) => Some(
632 RawFd
::try_from(key_fd
634 .ok_or_else(|| format_err
!("bad key fd: {:?}", key_fd
))?
636 .map_err(|err
| format_err
!("bad key fd: {:?}: {}", key_fd
, err
))?
638 Some(_
) => bail
!("bad --keyfd parameter type"),
642 let crypt_mode
: Option
<CryptMode
> = match param
.get("crypt-mode") {
643 Some(mode
) => Some(serde_json
::from_value(mode
.clone())?
),
647 let keydata
= match (keyfile
, key_fd
) {
648 (None
, None
) => None
,
649 (Some(_
), Some(_
)) => bail
!("--keyfile and --keyfd are mutually exclusive"),
650 (Some(keyfile
), None
) => {
651 eprintln
!("Using encryption key file: {}", keyfile
);
652 Some(file_get_contents(keyfile
)?
)
654 (None
, Some(fd
)) => {
655 let input
= unsafe { std::fs::File::from_raw_fd(fd) }
;
656 let mut data
= Vec
::new();
657 let _len
: usize = { input }
.read_to_end(&mut data
)
659 format_err
!("error reading encryption key from fd {}: {}", fd
, err
)
661 eprintln
!("Using encryption key from file descriptor");
666 Ok(match (keydata
, crypt_mode
) {
668 (None
, None
) => match key
::read_optional_default_encryption_key()?
{
670 eprintln
!("Encrypting with default encryption key!");
671 (Some(key
), CryptMode
::Encrypt
)
673 None
=> (None
, CryptMode
::None
),
676 // just --crypt-mode=none
677 (None
, Some(CryptMode
::None
)) => (None
, CryptMode
::None
),
679 // just --crypt-mode other than none
680 (None
, Some(crypt_mode
)) => match key
::read_optional_default_encryption_key()?
{
681 None
=> bail
!("--crypt-mode without --keyfile and no default key file available"),
683 eprintln
!("Encrypting with default encryption key!");
684 (Some(key
), crypt_mode
)
689 (Some(key
), None
) => (Some(key
), CryptMode
::Encrypt
),
691 // --keyfile and --crypt-mode=none
692 (Some(_
), Some(CryptMode
::None
)) => {
693 bail
!("--keyfile/--keyfd and --crypt-mode=none are mutually exclusive");
696 // --keyfile and --crypt-mode other than none
697 (Some(key
), Some(crypt_mode
)) => (Some(key
), crypt_mode
),
706 description
: "List of backup source specifications ([<label.ext>:<path>] ...)",
708 schema
: BACKUP_SOURCE_SCHEMA
,
712 schema
: REPO_URL_SCHEMA
,
716 description
: "Include mountpoints with same st_dev number (see ``man fstat``) as specified files.",
720 description
: "Path to file.",
723 "all-file-systems": {
725 description
: "Include all mounted subdirectories.",
729 schema
: KEYFILE_SCHEMA
,
733 schema
: KEYFD_SCHEMA
,
740 "skip-lost-and-found": {
742 description
: "Skip lost+found directory.",
746 schema
: BACKUP_TYPE_SCHEMA
,
750 schema
: BACKUP_ID_SCHEMA
,
754 schema
: BACKUP_TIME_SCHEMA
,
758 schema
: CHUNK_SIZE_SCHEMA
,
763 description
: "List of paths or patterns for matching files to exclude.",
767 description
: "Path or match pattern.",
772 description
: "Max number of entries to hold in memory.",
774 default: proxmox_backup
::pxar
::ENCODER_MAX_ENTRIES
as isize,
778 description
: "Verbose output.",
784 /// Create (host) backup.
785 async
fn create_backup(
788 _rpcenv
: &mut dyn RpcEnvironment
,
789 ) -> Result
<Value
, Error
> {
791 let repo
= extract_repository_from_value(¶m
)?
;
793 let backupspec_list
= tools
::required_array_param(¶m
, "backupspec")?
;
795 let all_file_systems
= param
["all-file-systems"].as_bool().unwrap_or(false);
797 let skip_lost_and_found
= param
["skip-lost-and-found"].as_bool().unwrap_or(false);
799 let verbose
= param
["verbose"].as_bool().unwrap_or(false);
801 let backup_time_opt
= param
["backup-time"].as_i64();
803 let chunk_size_opt
= param
["chunk-size"].as_u64().map(|v
| (v
*1024) as usize);
805 if let Some(size
) = chunk_size_opt
{
806 verify_chunk_size(size
)?
;
809 let (keydata
, crypt_mode
) = keyfile_parameters(¶m
)?
;
811 let backup_id
= param
["backup-id"].as_str().unwrap_or(&proxmox
::tools
::nodename());
813 let backup_type
= param
["backup-type"].as_str().unwrap_or("host");
815 let include_dev
= param
["include-dev"].as_array();
817 let entries_max
= param
["entries-max"].as_u64()
818 .unwrap_or(proxmox_backup
::pxar
::ENCODER_MAX_ENTRIES
as u64);
820 let empty
= Vec
::new();
821 let exclude_args
= param
["exclude"].as_array().unwrap_or(&empty
);
823 let mut pattern_list
= Vec
::with_capacity(exclude_args
.len());
824 for entry
in exclude_args
{
825 let entry
= entry
.as_str().ok_or_else(|| format_err
!("Invalid pattern string slice"))?
;
827 MatchEntry
::parse_pattern(entry
, PatternFlag
::PATH_NAME
, MatchType
::Exclude
)
828 .map_err(|err
| format_err
!("invalid exclude pattern entry: {}", err
))?
832 let mut devices
= if all_file_systems { None }
else { Some(HashSet::new()) }
;
834 if let Some(include_dev
) = include_dev
{
835 if all_file_systems
{
836 bail
!("option 'all-file-systems' conflicts with option 'include-dev'");
839 let mut set
= HashSet
::new();
840 for path
in include_dev
{
841 let path
= path
.as_str().unwrap();
842 let stat
= nix
::sys
::stat
::stat(path
)
843 .map_err(|err
| format_err
!("fstat {:?} failed - {}", path
, err
))?
;
844 set
.insert(stat
.st_dev
);
849 let mut upload_list
= vec
![];
850 let mut target_set
= HashSet
::new();
852 for backupspec
in backupspec_list
{
853 let spec
= parse_backup_specification(backupspec
.as_str().unwrap())?
;
854 let filename
= &spec
.config_string
;
855 let target
= &spec
.archive_name
;
857 if target_set
.contains(target
) {
858 bail
!("got target twice: '{}'", target
);
860 target_set
.insert(target
.to_string());
862 use std
::os
::unix
::fs
::FileTypeExt
;
864 let metadata
= std
::fs
::metadata(filename
)
865 .map_err(|err
| format_err
!("unable to access '{}' - {}", filename
, err
))?
;
866 let file_type
= metadata
.file_type();
868 match spec
.spec_type
{
869 BackupSpecificationType
::PXAR
=> {
870 if !file_type
.is_dir() {
871 bail
!("got unexpected file type (expected directory)");
873 upload_list
.push((BackupSpecificationType
::PXAR
, filename
.to_owned(), format
!("{}.didx", target
), 0));
875 BackupSpecificationType
::IMAGE
=> {
876 if !(file_type
.is_file() || file_type
.is_block_device()) {
877 bail
!("got unexpected file type (expected file or block device)");
880 let size
= image_size(&PathBuf
::from(filename
))?
;
882 if size
== 0 { bail!("got zero-sized file '{}'
", filename); }
884 upload_list.push((BackupSpecificationType::IMAGE, filename.to_owned(), format!("{}
.fidx
", target), size));
886 BackupSpecificationType::CONFIG => {
887 if !file_type.is_file() {
888 bail!("got unexpected file
type (expected regular file
)");
890 upload_list.push((BackupSpecificationType::CONFIG, filename.to_owned(), format!("{}
.blob
", target), metadata.len()));
892 BackupSpecificationType::LOGFILE => {
893 if !file_type.is_file() {
894 bail!("got unexpected file
type (expected regular file
)");
896 upload_list.push((BackupSpecificationType::LOGFILE, filename.to_owned(), format!("{}
.blob
", target), metadata.len()));
901 let backup_time = backup_time_opt.unwrap_or_else(epoch_i64);
903 let client = connect(&repo)?;
904 record_repository(&repo);
906 println!("Starting backup
: {}
/{}
/{}
", backup_type, backup_id, BackupDir::backup_time_to_string(backup_time)?);
908 println!("Client name
: {}
", proxmox::tools::nodename());
910 let start_time = std::time::Instant::now();
912 println!("Starting backup protocol
: {}
", strftime_local("%c
", epoch_i64())?);
914 let (crypt_config, rsa_encrypted_key) = match keydata {
915 None => (None, None),
917 let (key, created, fingerprint) = decrypt_key(&key, &key::get_encryption_key_password)?;
918 println!("Encryption key fingerprint
: {}
", fingerprint);
920 let crypt_config = CryptConfig::new(key)?;
922 match key::find_master_pubkey()? {
923 Some(ref path) if path.exists() => {
924 let pem_data = file_get_contents(path)?;
925 let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
926 let key_config = KeyConfig {
929 modified: proxmox::tools::time::epoch_i64(),
931 fingerprint: Some(fingerprint),
933 let enc_key = rsa_encrypt_key_config(rsa, &key_config)?;
934 println!("Master key '{:?}'
", path);
936 (Some(Arc::new(crypt_config)), Some(enc_key))
938 _ => (Some(Arc::new(crypt_config)), None),
943 let client = BackupWriter::start(
945 crypt_config.clone(),
954 let download_previous_manifest = match client.previous_backup_time().await {
955 Ok(Some(backup_time)) => {
957 "Downloading previous
manifest ({}
)",
958 strftime_local("%c
", backup_time)?
963 println!("No previous manifest available
.");
967 // Fallback for outdated server, TODO remove/bubble up with 2.0
972 let previous_manifest = if download_previous_manifest {
973 match client.download_previous_manifest().await {
974 Ok(previous_manifest) => {
975 match previous_manifest.check_fingerprint(crypt_config.as_ref().map(Arc::as_ref)) {
976 Ok(()) => Some(Arc::new(previous_manifest)),
978 println!("Couldn't re
-use previous manifest
- {}
", err);
984 println!("Couldn't download previous manifest
- {}
", err);
992 let snapshot = BackupDir::new(backup_type, backup_id, backup_time)?;
993 let mut manifest = BackupManifest::new(snapshot);
995 let mut catalog = None;
996 let mut catalog_result_tx = None;
998 for (backup_type, filename, target, size) in upload_list {
1000 BackupSpecificationType::CONFIG => {
1001 println!("Upload config file '{}' to '{}'
as {}
", filename, repo, target);
1003 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
1005 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1007 BackupSpecificationType::LOGFILE => { // fixme: remove - not needed anymore ?
1008 println!("Upload log file '{}' to '{}'
as {}
", filename, repo, target);
1010 .upload_blob_from_file(&filename, &target, true, crypt_mode == CryptMode::Encrypt)
1012 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1014 BackupSpecificationType::PXAR => {
1015 // start catalog upload on first use
1016 if catalog.is_none() {
1017 let (cat, res) = spawn_catalog_upload(client.clone(), crypt_mode == CryptMode::Encrypt)?;
1018 catalog = Some(cat);
1019 catalog_result_tx = Some(res);
1021 let catalog = catalog.as_ref().unwrap();
1023 println!("Upload directory '{}' to '{}'
as {}
", filename, repo, target);
1024 catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?;
1025 let stats = backup_directory(
1027 previous_manifest.clone(),
1033 skip_lost_and_found,
1035 pattern_list.clone(),
1036 entries_max as usize,
1038 crypt_mode == CryptMode::Encrypt,
1040 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1041 catalog.lock().unwrap().end_directory()?;
1043 BackupSpecificationType::IMAGE => {
1044 println!("Upload image '{}' to '{:?}'
as {}
", filename, repo, target);
1045 let stats = backup_image(
1047 previous_manifest.clone(),
1053 crypt_mode == CryptMode::Encrypt,
1056 manifest.add_file(target, stats.size, stats.csum, crypt_mode)?;
1061 // finalize and upload catalog
1062 if let Some(catalog) = catalog {
1063 let mutex = Arc::try_unwrap(catalog)
1064 .map_err(|_| format_err!("unable to get
catalog (still used
)"))?;
1065 let mut catalog = mutex.into_inner().unwrap();
1069 drop(catalog); // close upload stream
1071 if let Some(catalog_result_rx) = catalog_result_tx {
1072 let stats = catalog_result_rx.await??;
1073 manifest.add_file(CATALOG_NAME.to_owned(), stats.size, stats.csum, crypt_mode)?;
1077 if let Some(rsa_encrypted_key) = rsa_encrypted_key {
1078 let target = ENCRYPTED_KEY_BLOB_NAME;
1079 println!("Upload RSA encoded key to '{:?}'
as {}
", repo, target);
1081 .upload_blob_from_data(rsa_encrypted_key, target, false, false)
1083 manifest.add_file(target.to_string(), stats.size, stats.csum, crypt_mode)?;
1086 // create manifest (index.json)
1087 // manifests are never encrypted, but include a signature
1088 let manifest = manifest.to_string(crypt_config.as_ref().map(Arc::as_ref))
1089 .map_err(|err| format_err!("unable to format manifest
- {}
", err))?;
1092 if verbose { println!("Upload index.json to '{}'", repo
) };
1094 .upload_blob_from_data(manifest
.into_bytes(), MANIFEST_BLOB_NAME
, true, false)
1097 client
.finish().await?
;
1099 let end_time
= std
::time
::Instant
::now();
1100 let elapsed
= end_time
.duration_since(start_time
);
1101 println
!("Duration: {:.2}s", elapsed
.as_secs_f64());
1103 println
!("End Time: {}", strftime_local("%c", epoch_i64())?
);
1108 fn complete_backup_source(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1110 let mut result
= vec
![];
1112 let data
: Vec
<&str> = arg
.splitn(2, '
:'
).collect();
1114 if data
.len() != 2 {
1115 result
.push(String
::from("root.pxar:/"));
1116 result
.push(String
::from("etc.pxar:/etc"));
1120 let files
= tools
::complete_file_name(data
[1], param
);
1123 result
.push(format
!("{}:{}", data
[0], file
));
1129 async
fn dump_image
<W
: Write
>(
1130 client
: Arc
<BackupReader
>,
1131 crypt_config
: Option
<Arc
<CryptConfig
>>,
1132 crypt_mode
: CryptMode
,
1133 index
: FixedIndexReader
,
1136 ) -> Result
<(), Error
> {
1138 let most_used
= index
.find_most_used_chunks(8);
1140 let chunk_reader
= RemoteChunkReader
::new(client
.clone(), crypt_config
, crypt_mode
, most_used
);
1142 // Note: we avoid using BufferedFixedReader, because that add an additional buffer/copy
1143 // and thus slows down reading. Instead, directly use RemoteChunkReader
1146 let start_time
= std
::time
::Instant
::now();
1148 for pos
in 0..index
.index_count() {
1149 let digest
= index
.index_digest(pos
).unwrap();
1150 let raw_data
= chunk_reader
.read_chunk(&digest
).await?
;
1151 writer
.write_all(&raw_data
)?
;
1152 bytes
+= raw_data
.len();
1154 let next_per
= ((pos
+1)*100)/index
.index_count();
1155 if per
!= next_per
{
1156 eprintln
!("progress {}% (read {} bytes, duration {} sec)",
1157 next_per
, bytes
, start_time
.elapsed().as_secs());
1163 let end_time
= std
::time
::Instant
::now();
1164 let elapsed
= end_time
.duration_since(start_time
);
1165 eprintln
!("restore image complete (bytes={}, duration={:.2}s, speed={:.2}MB/s)",
1167 elapsed
.as_secs_f64(),
1168 bytes
as f64/(1024.0*1024.0*elapsed
.as_secs_f64())
1175 fn parse_archive_type(name
: &str) -> (String
, ArchiveType
) {
1176 if name
.ends_with(".didx") || name
.ends_with(".fidx") || name
.ends_with(".blob") {
1177 (name
.into(), archive_type(name
).unwrap())
1178 } else if name
.ends_with(".pxar") {
1179 (format
!("{}.didx", name
), ArchiveType
::DynamicIndex
)
1180 } else if name
.ends_with(".img") {
1181 (format
!("{}.fidx", name
), ArchiveType
::FixedIndex
)
1183 (format
!("{}.blob", name
), ArchiveType
::Blob
)
1191 schema
: REPO_URL_SCHEMA
,
1196 description
: "Group/Snapshot path.",
1199 description
: "Backup archive name.",
1204 description
: r
###"Target directory path. Use '-' to write to standard output.
1206 We do not extraxt '.pxar' archives when writing to standard output.
1210 "allow-existing-dirs": {
1212 description
: "Do not fail if directories already exists.",
1216 schema
: KEYFILE_SCHEMA
,
1220 schema
: KEYFD_SCHEMA
,
1230 /// Restore backup repository.
1231 async
fn restore(param
: Value
) -> Result
<Value
, Error
> {
1232 let repo
= extract_repository_from_value(¶m
)?
;
1234 let verbose
= param
["verbose"].as_bool().unwrap_or(false);
1236 let allow_existing_dirs
= param
["allow-existing-dirs"].as_bool().unwrap_or(false);
1238 let archive_name
= tools
::required_string_param(¶m
, "archive-name")?
;
1240 let client
= connect(&repo
)?
;
1242 record_repository(&repo
);
1244 let path
= tools
::required_string_param(¶m
, "snapshot")?
;
1246 let (backup_type
, backup_id
, backup_time
) = if path
.matches('
/'
).count() == 1 {
1247 let group
: BackupGroup
= path
.parse()?
;
1248 api_datastore_latest_snapshot(&client
, repo
.store(), group
).await?
1250 let snapshot
: BackupDir
= path
.parse()?
;
1251 (snapshot
.group().backup_type().to_owned(), snapshot
.group().backup_id().to_owned(), snapshot
.backup_time())
1254 let target
= tools
::required_string_param(¶m
, "target")?
;
1255 let target
= if target
== "-" { None }
else { Some(target) }
;
1257 let (keydata
, _crypt_mode
) = keyfile_parameters(¶m
)?
;
1259 let crypt_config
= match keydata
{
1262 let (key
, _
, fingerprint
) = decrypt_key(&key
, &key
::get_encryption_key_password
)?
;
1263 eprintln
!("Encryption key fingerprint: '{}'", fingerprint
);
1264 Some(Arc
::new(CryptConfig
::new(key
)?
))
1268 let client
= BackupReader
::start(
1270 crypt_config
.clone(),
1278 let (archive_name
, archive_type
) = parse_archive_type(archive_name
);
1280 let (manifest
, backup_index_data
) = client
.download_manifest().await?
;
1282 if archive_name
== ENCRYPTED_KEY_BLOB_NAME
&& crypt_config
.is_none() {
1283 eprintln
!("Restoring encrypted key blob without original key - skipping manifest fingerprint check!")
1285 manifest
.check_fingerprint(crypt_config
.as_ref().map(Arc
::as_ref
))?
;
1288 if archive_name
== MANIFEST_BLOB_NAME
{
1289 if let Some(target
) = target
{
1290 replace_file(target
, &backup_index_data
, CreateOptions
::new())?
;
1292 let stdout
= std
::io
::stdout();
1293 let mut writer
= stdout
.lock();
1294 writer
.write_all(&backup_index_data
)
1295 .map_err(|err
| format_err
!("unable to pipe data - {}", err
))?
;
1298 return Ok(Value
::Null
);
1301 let file_info
= manifest
.lookup_file_info(&archive_name
)?
;
1303 if archive_type
== ArchiveType
::Blob
{
1305 let mut reader
= client
.download_blob(&manifest
, &archive_name
).await?
;
1307 if let Some(target
) = target
{
1308 let mut writer
= std
::fs
::OpenOptions
::new()
1313 .map_err(|err
| format_err
!("unable to create target file {:?} - {}", target
, err
))?
;
1314 std
::io
::copy(&mut reader
, &mut writer
)?
;
1316 let stdout
= std
::io
::stdout();
1317 let mut writer
= stdout
.lock();
1318 std
::io
::copy(&mut reader
, &mut writer
)
1319 .map_err(|err
| format_err
!("unable to pipe data - {}", err
))?
;
1322 } else if archive_type
== ArchiveType
::DynamicIndex
{
1324 let index
= client
.download_dynamic_index(&manifest
, &archive_name
).await?
;
1326 let most_used
= index
.find_most_used_chunks(8);
1328 let chunk_reader
= RemoteChunkReader
::new(client
.clone(), crypt_config
, file_info
.chunk_crypt_mode(), most_used
);
1330 let mut reader
= BufferedDynamicReader
::new(index
, chunk_reader
);
1332 if let Some(target
) = target
{
1333 proxmox_backup
::pxar
::extract_archive(
1334 pxar
::decoder
::Decoder
::from_std(reader
)?
,
1338 proxmox_backup
::pxar
::Flags
::DEFAULT
,
1339 allow_existing_dirs
,
1342 println
!("{:?}", path
);
1347 .map_err(|err
| format_err
!("error extracting archive - {}", err
))?
;
1349 let mut writer
= std
::fs
::OpenOptions
::new()
1351 .open("/dev/stdout")
1352 .map_err(|err
| format_err
!("unable to open /dev/stdout - {}", err
))?
;
1354 std
::io
::copy(&mut reader
, &mut writer
)
1355 .map_err(|err
| format_err
!("unable to pipe data - {}", err
))?
;
1357 } else if archive_type
== ArchiveType
::FixedIndex
{
1359 let index
= client
.download_fixed_index(&manifest
, &archive_name
).await?
;
1361 let mut writer
= if let Some(target
) = target
{
1362 std
::fs
::OpenOptions
::new()
1367 .map_err(|err
| format_err
!("unable to create target file {:?} - {}", target
, err
))?
1369 std
::fs
::OpenOptions
::new()
1371 .open("/dev/stdout")
1372 .map_err(|err
| format_err
!("unable to open /dev/stdout - {}", err
))?
1375 dump_image(client
.clone(), crypt_config
.clone(), file_info
.chunk_crypt_mode(), index
, &mut writer
, verbose
).await?
;
1381 const API_METHOD_PRUNE
: ApiMethod
= ApiMethod
::new(
1382 &ApiHandler
::Async(&prune
),
1384 "Prune a backup repository.",
1385 &proxmox_backup
::add_common_prune_prameters
!([
1386 ("dry-run", true, &BooleanSchema
::new(
1387 "Just show what prune would do, but do not delete anything.")
1389 ("group", false, &StringSchema
::new("Backup group.").schema()),
1391 ("output-format", true, &OUTPUT_FORMAT
),
1395 &BooleanSchema
::new("Minimal output - only show removals.")
1398 ("repository", true, &REPO_URL_SCHEMA
),
1406 _rpcenv
: &'a
mut dyn RpcEnvironment
,
1407 ) -> proxmox
::api
::ApiFuture
<'a
> {
1409 prune_async(param
).await
1413 async
fn prune_async(mut param
: Value
) -> Result
<Value
, Error
> {
1414 let repo
= extract_repository_from_value(¶m
)?
;
1416 let mut client
= connect(&repo
)?
;
1418 let path
= format
!("api2/json/admin/datastore/{}/prune", repo
.store());
1420 let group
= tools
::required_string_param(¶m
, "group")?
;
1421 let group
: BackupGroup
= group
.parse()?
;
1423 let output_format
= get_output_format(¶m
);
1425 let quiet
= param
["quiet"].as_bool().unwrap_or(false);
1427 param
.as_object_mut().unwrap().remove("repository");
1428 param
.as_object_mut().unwrap().remove("group");
1429 param
.as_object_mut().unwrap().remove("output-format");
1430 param
.as_object_mut().unwrap().remove("quiet");
1432 param
["backup-type"] = group
.backup_type().into();
1433 param
["backup-id"] = group
.backup_id().into();
1435 let mut result
= client
.post(&path
, Some(param
)).await?
;
1437 record_repository(&repo
);
1439 let render_snapshot_path
= |_v
: &Value
, record
: &Value
| -> Result
<String
, Error
> {
1440 let item
: PruneListItem
= serde_json
::from_value(record
.to_owned())?
;
1441 let snapshot
= BackupDir
::new(item
.backup_type
, item
.backup_id
, item
.backup_time
)?
;
1442 Ok(snapshot
.relative_path().to_str().unwrap().to_owned())
1445 let render_prune_action
= |v
: &Value
, _record
: &Value
| -> Result
<String
, Error
> {
1446 Ok(match v
.as_bool() {
1447 Some(true) => "keep",
1448 Some(false) => "remove",
1453 let options
= default_table_format_options()
1454 .sortby("backup-type", false)
1455 .sortby("backup-id", false)
1456 .sortby("backup-time", false)
1457 .column(ColumnConfig
::new("backup-id").renderer(render_snapshot_path
).header("snapshot"))
1458 .column(ColumnConfig
::new("backup-time").renderer(tools
::format
::render_epoch
).header("date"))
1459 .column(ColumnConfig
::new("keep").renderer(render_prune_action
).header("action"))
1462 let return_type
= &proxmox_backup
::api2
::admin
::datastore
::API_METHOD_PRUNE
.returns
;
1464 let mut data
= result
["data"].take();
1467 let list
: Vec
<Value
> = data
.as_array().unwrap().iter().filter(|item
| {
1468 item
["keep"].as_bool() == Some(false)
1469 }).map(|v
| v
.clone()).collect();
1473 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
1482 schema
: REPO_URL_SCHEMA
,
1486 schema
: OUTPUT_FORMAT
,
1492 type: StorageStatus
,
1495 /// Get repository status.
1496 async
fn status(param
: Value
) -> Result
<Value
, Error
> {
1498 let repo
= extract_repository_from_value(¶m
)?
;
1500 let output_format
= get_output_format(¶m
);
1502 let client
= connect(&repo
)?
;
1504 let path
= format
!("api2/json/admin/datastore/{}/status", repo
.store());
1506 let mut result
= client
.get(&path
, None
).await?
;
1507 let mut data
= result
["data"].take();
1509 record_repository(&repo
);
1511 let render_total_percentage
= |v
: &Value
, record
: &Value
| -> Result
<String
, Error
> {
1512 let v
= v
.as_u64().unwrap();
1513 let total
= record
["total"].as_u64().unwrap();
1514 let roundup
= total
/200;
1515 let per
= ((v
+roundup
)*100)/total
;
1516 let info
= format
!(" ({} %)", per
);
1517 Ok(format
!("{} {:>8}", v
, info
))
1520 let options
= default_table_format_options()
1522 .column(ColumnConfig
::new("total").renderer(render_total_percentage
))
1523 .column(ColumnConfig
::new("used").renderer(render_total_percentage
))
1524 .column(ColumnConfig
::new("avail").renderer(render_total_percentage
));
1526 let return_type
= &API_METHOD_STATUS
.returns
;
1528 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
1533 // like get, but simply ignore errors and return Null instead
1534 async
fn try_get(repo
: &BackupRepository
, url
: &str) -> Value
{
1536 let fingerprint
= std
::env
::var(ENV_VAR_PBS_FINGERPRINT
).ok();
1537 let password
= std
::env
::var(ENV_VAR_PBS_PASSWORD
).ok();
1539 let options
= HttpClientOptions
::new()
1540 .prefix(Some("proxmox-backup".to_string()))
1543 .fingerprint(fingerprint
)
1544 .fingerprint_cache(true)
1545 .ticket_cache(true);
1547 let client
= match HttpClient
::new(repo
.host(), repo
.port(), repo
.auth_id(), options
) {
1549 _
=> return Value
::Null
,
1552 let mut resp
= match client
.get(url
, None
).await
{
1554 _
=> return Value
::Null
,
1557 if let Some(map
) = resp
.as_object_mut() {
1558 if let Some(data
) = map
.remove("data") {
1565 fn complete_backup_group(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1566 proxmox_backup
::tools
::runtime
::main(async { complete_backup_group_do(param).await }
)
1569 async
fn complete_backup_group_do(param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1571 let mut result
= vec
![];
1573 let repo
= match extract_repository_from_map(param
) {
1578 let path
= format
!("api2/json/admin/datastore/{}/groups", repo
.store());
1580 let data
= try_get(&repo
, &path
).await
;
1582 if let Some(list
) = data
.as_array() {
1584 if let (Some(backup_id
), Some(backup_type
)) =
1585 (item
["backup-id"].as_str(), item
["backup-type"].as_str())
1587 result
.push(format
!("{}/{}", backup_type
, backup_id
));
1595 pub fn complete_group_or_snapshot(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1596 proxmox_backup
::tools
::runtime
::main(async { complete_group_or_snapshot_do(arg, param).await }
)
1599 async
fn complete_group_or_snapshot_do(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1601 if arg
.matches('
/'
).count() < 2 {
1602 let groups
= complete_backup_group_do(param
).await
;
1603 let mut result
= vec
![];
1604 for group
in groups
{
1605 result
.push(group
.to_string());
1606 result
.push(format
!("{}/", group
));
1611 complete_backup_snapshot_do(param
).await
1614 fn complete_backup_snapshot(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1615 proxmox_backup
::tools
::runtime
::main(async { complete_backup_snapshot_do(param).await }
)
1618 async
fn complete_backup_snapshot_do(param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1620 let mut result
= vec
![];
1622 let repo
= match extract_repository_from_map(param
) {
1627 let path
= format
!("api2/json/admin/datastore/{}/snapshots", repo
.store());
1629 let data
= try_get(&repo
, &path
).await
;
1631 if let Some(list
) = data
.as_array() {
1633 if let (Some(backup_id
), Some(backup_type
), Some(backup_time
)) =
1634 (item
["backup-id"].as_str(), item
["backup-type"].as_str(), item
["backup-time"].as_i64())
1636 if let Ok(snapshot
) = BackupDir
::new(backup_type
, backup_id
, backup_time
) {
1637 result
.push(snapshot
.relative_path().to_str().unwrap().to_owned());
1646 fn complete_server_file_name(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1647 proxmox_backup
::tools
::runtime
::main(async { complete_server_file_name_do(param).await }
)
1650 async
fn complete_server_file_name_do(param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1652 let mut result
= vec
![];
1654 let repo
= match extract_repository_from_map(param
) {
1659 let snapshot
: BackupDir
= match param
.get("snapshot") {
1661 match path
.parse() {
1669 let query
= tools
::json_object_to_query(json
!({
1670 "backup-type": snapshot
.group().backup_type(),
1671 "backup-id": snapshot
.group().backup_id(),
1672 "backup-time": snapshot
.backup_time(),
1675 let path
= format
!("api2/json/admin/datastore/{}/files?{}", repo
.store(), query
);
1677 let data
= try_get(&repo
, &path
).await
;
1679 if let Some(list
) = data
.as_array() {
1681 if let Some(filename
) = item
["filename"].as_str() {
1682 result
.push(filename
.to_owned());
1690 fn complete_archive_name(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1691 complete_server_file_name(arg
, param
)
1693 .map(|v
| tools
::format
::strip_server_file_extension(&v
))
1697 pub fn complete_pxar_archive_name(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1698 complete_server_file_name(arg
, param
)
1700 .filter_map(|name
| {
1701 if name
.ends_with(".pxar.didx") {
1702 Some(tools
::format
::strip_server_file_extension(name
))
1710 pub fn complete_img_archive_name(arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1711 complete_server_file_name(arg
, param
)
1713 .filter_map(|name
| {
1714 if name
.ends_with(".img.fidx") {
1715 Some(tools
::format
::strip_server_file_extension(name
))
1723 fn complete_chunk_size(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1725 let mut result
= vec
![];
1729 result
.push(size
.to_string());
1731 if size
> 4096 { break; }
1737 fn complete_auth_id(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1738 proxmox_backup
::tools
::runtime
::main(async { complete_auth_id_do(param).await }
)
1741 async
fn complete_auth_id_do(param
: &HashMap
<String
, String
>) -> Vec
<String
> {
1743 let mut result
= vec
![];
1745 let repo
= match extract_repository_from_map(param
) {
1750 let data
= try_get(&repo
, "api2/json/access/users?include_tokens=true").await
;
1752 if let Ok(parsed
) = serde_json
::from_value
::<Vec
<UserWithTokens
>>(data
) {
1753 for user
in parsed
{
1754 result
.push(user
.userid
.to_string());
1755 for token
in user
.tokens
{
1756 result
.push(token
.tokenid
.to_string());
1764 use proxmox_backup
::client
::RemoteChunkReader
;
1765 /// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better
1768 /// Ideally BufferedDynamicReader gets replaced so the LruCache maps to `BroadcastFuture<Chunk>`,
1769 /// so that we can properly access it from multiple threads simultaneously while not issuing
1770 /// duplicate simultaneous reads over http.
1771 pub struct BufferedDynamicReadAt
{
1772 inner
: Mutex
<BufferedDynamicReader
<RemoteChunkReader
>>,
1775 impl BufferedDynamicReadAt
{
1776 fn new(inner
: BufferedDynamicReader
<RemoteChunkReader
>) -> Self {
1778 inner
: Mutex
::new(inner
),
1783 impl ReadAt
for BufferedDynamicReadAt
{
1784 fn start_read_at
<'a
>(
1785 self: Pin
<&'a
Self>,
1789 ) -> MaybeReady
<io
::Result
<usize>, ReadAtOperation
<'a
>> {
1790 MaybeReady
::Ready(tokio
::task
::block_in_place(move || {
1791 let mut reader
= self.inner
.lock().unwrap();
1792 reader
.seek(SeekFrom
::Start(offset
))?
;
1793 Ok(reader
.read(buf
)?
)
1797 fn poll_complete
<'a
>(
1798 self: Pin
<&'a
Self>,
1799 _op
: ReadAtOperation
<'a
>,
1800 ) -> MaybeReady
<io
::Result
<usize>, ReadAtOperation
<'a
>> {
1801 panic
!("LocalDynamicReadAt::start_read_at returned Pending");
1807 let backup_cmd_def
= CliCommand
::new(&API_METHOD_CREATE_BACKUP
)
1808 .arg_param(&["backupspec"])
1809 .completion_cb("repository", complete_repository
)
1810 .completion_cb("backupspec", complete_backup_source
)
1811 .completion_cb("keyfile", tools
::complete_file_name
)
1812 .completion_cb("chunk-size", complete_chunk_size
);
1814 let benchmark_cmd_def
= CliCommand
::new(&API_METHOD_BENCHMARK
)
1815 .completion_cb("repository", complete_repository
)
1816 .completion_cb("keyfile", tools
::complete_file_name
);
1818 let list_cmd_def
= CliCommand
::new(&API_METHOD_LIST_BACKUP_GROUPS
)
1819 .completion_cb("repository", complete_repository
);
1821 let garbage_collect_cmd_def
= CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
1822 .completion_cb("repository", complete_repository
);
1824 let restore_cmd_def
= CliCommand
::new(&API_METHOD_RESTORE
)
1825 .arg_param(&["snapshot", "archive-name", "target"])
1826 .completion_cb("repository", complete_repository
)
1827 .completion_cb("snapshot", complete_group_or_snapshot
)
1828 .completion_cb("archive-name", complete_archive_name
)
1829 .completion_cb("target", tools
::complete_file_name
);
1831 let prune_cmd_def
= CliCommand
::new(&API_METHOD_PRUNE
)
1832 .arg_param(&["group"])
1833 .completion_cb("group", complete_backup_group
)
1834 .completion_cb("repository", complete_repository
);
1836 let status_cmd_def
= CliCommand
::new(&API_METHOD_STATUS
)
1837 .completion_cb("repository", complete_repository
);
1839 let login_cmd_def
= CliCommand
::new(&API_METHOD_API_LOGIN
)
1840 .completion_cb("repository", complete_repository
);
1842 let logout_cmd_def
= CliCommand
::new(&API_METHOD_API_LOGOUT
)
1843 .completion_cb("repository", complete_repository
);
1845 let version_cmd_def
= CliCommand
::new(&API_METHOD_API_VERSION
)
1846 .completion_cb("repository", complete_repository
);
1848 let change_owner_cmd_def
= CliCommand
::new(&API_METHOD_CHANGE_BACKUP_OWNER
)
1849 .arg_param(&["group", "new-owner"])
1850 .completion_cb("group", complete_backup_group
)
1851 .completion_cb("new-owner", complete_auth_id
)
1852 .completion_cb("repository", complete_repository
);
1854 let cmd_def
= CliCommandMap
::new()
1855 .insert("backup", backup_cmd_def
)
1856 .insert("garbage-collect", garbage_collect_cmd_def
)
1857 .insert("list", list_cmd_def
)
1858 .insert("login", login_cmd_def
)
1859 .insert("logout", logout_cmd_def
)
1860 .insert("prune", prune_cmd_def
)
1861 .insert("restore", restore_cmd_def
)
1862 .insert("snapshot", snapshot_mgtm_cli())
1863 .insert("status", status_cmd_def
)
1864 .insert("key", key
::cli())
1865 .insert("mount", mount_cmd_def())
1866 .insert("map", map_cmd_def())
1867 .insert("unmap", unmap_cmd_def())
1868 .insert("catalog", catalog_mgmt_cli())
1869 .insert("task", task_mgmt_cli())
1870 .insert("version", version_cmd_def
)
1871 .insert("benchmark", benchmark_cmd_def
)
1872 .insert("change-owner", change_owner_cmd_def
)
1874 .alias(&["files"], &["snapshot", "files"])
1875 .alias(&["forget"], &["snapshot", "forget"])
1876 .alias(&["upload-log"], &["snapshot", "upload-log"])
1877 .alias(&["snapshots"], &["snapshot", "list"])
1880 let rpcenv
= CliEnvironment
::new();
1881 run_cli_command(cmd_def
, rpcenv
, Some(|future
| {
1882 proxmox_backup
::tools
::runtime
::main(future
)