1 use std
::collections
::{HashSet, HashMap}
;
2 use std
::convert
::TryFrom
;
4 use chrono
::{TimeZone, Local}
;
5 use anyhow
::{bail, Error}
;
7 use hyper
::http
::request
::Parts
;
8 use hyper
::{header, Body, Response, StatusCode}
;
9 use serde_json
::{json, Value}
;
12 api
, ApiResponseFuture
, ApiHandler
, ApiMethod
, Router
,
13 RpcEnvironment
, RpcEnvironmentType
, Permission
};
14 use proxmox
::api
::router
::SubdirMap
;
15 use proxmox
::api
::schema
::*;
16 use proxmox
::tools
::fs
::{file_get_contents, replace_file, CreateOptions}
;
17 use proxmox
::try_block
;
18 use proxmox
::{http_err, identity, list_subdirs_api_method, sortable}
;
20 use crate::api2
::types
::*;
22 use crate::config
::datastore
;
23 use crate::server
::WorkerTask
;
25 use crate::config
::acl
::{
29 PRIV_DATASTORE_CREATE_BACKUP
,
32 fn read_backup_index(store
: &DataStore
, backup_dir
: &BackupDir
) -> Result
<Vec
<BackupContent
>, Error
> {
34 let mut path
= store
.base_path();
35 path
.push(backup_dir
.relative_path());
36 path
.push("index.json.blob");
38 let raw_data
= file_get_contents(&path
)?
;
39 let index_size
= raw_data
.len() as u64;
40 let blob
= DataBlob
::from_raw(raw_data
)?
;
42 let manifest
= BackupManifest
::try_from(blob
)?
;
44 let mut result
= Vec
::new();
45 for item
in manifest
.files() {
46 result
.push(BackupContent
{
47 filename
: item
.filename
.clone(),
48 size
: Some(item
.size
),
52 result
.push(BackupContent
{
53 filename
: "index.json.blob".to_string(),
54 size
: Some(index_size
),
60 fn group_backups(backup_list
: Vec
<BackupInfo
>) -> HashMap
<String
, Vec
<BackupInfo
>> {
62 let mut group_hash
= HashMap
::new();
64 for info
in backup_list
{
65 let group_id
= info
.backup_dir
.group().group_path().to_str().unwrap().to_owned();
66 let time_list
= group_hash
.entry(group_id
).or_insert(vec
![]);
77 schema
: DATASTORE_SCHEMA
,
83 description
: "Returns the list of backup groups.",
89 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT
, false),
92 /// List backup groups.
95 ) -> Result
<Vec
<GroupListItem
>, Error
> {
97 let datastore
= DataStore
::lookup_datastore(&store
)?
;
99 let backup_list
= BackupInfo
::list_backups(&datastore
.base_path())?
;
101 let group_hash
= group_backups(backup_list
);
103 let mut groups
= Vec
::new();
105 for (_group_id
, mut list
) in group_hash
{
107 BackupInfo
::sort_list(&mut list
, false);
110 let group
= info
.backup_dir
.group();
112 let result_item
= GroupListItem
{
113 backup_type
: group
.backup_type().to_string(),
114 backup_id
: group
.backup_id().to_string(),
115 last_backup
: info
.backup_dir
.backup_time().timestamp(),
116 backup_count
: list
.len() as u64,
117 files
: info
.files
.clone(),
119 groups
.push(result_item
);
129 schema
: DATASTORE_SCHEMA
,
132 schema
: BACKUP_TYPE_SCHEMA
,
135 schema
: BACKUP_ID_SCHEMA
,
138 schema
: BACKUP_TIME_SCHEMA
,
144 description
: "Returns the list of archive files inside a backup snapshots.",
150 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT
, false),
153 /// List snapshot files.
154 pub fn list_snapshot_files(
160 _rpcenv
: &mut dyn RpcEnvironment
,
161 ) -> Result
<Vec
<BackupContent
>, Error
> {
163 let datastore
= DataStore
::lookup_datastore(&store
)?
;
164 let snapshot
= BackupDir
::new(backup_type
, backup_id
, backup_time
);
166 let mut files
= read_backup_index(&datastore
, &snapshot
)?
;
168 let info
= BackupInfo
::new(&datastore
.base_path(), snapshot
)?
;
170 let file_set
= files
.iter().fold(HashSet
::new(), |mut acc
, item
| {
171 acc
.insert(item
.filename
.clone());
175 for file
in info
.files
{
176 if file_set
.contains(&file
) { continue; }
177 files
.push(BackupContent { filename: file, size: None }
);
187 schema
: DATASTORE_SCHEMA
,
190 schema
: BACKUP_TYPE_SCHEMA
,
193 schema
: BACKUP_ID_SCHEMA
,
196 schema
: BACKUP_TIME_SCHEMA
,
201 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_PRUNE
, false),
204 /// Delete backup snapshot.
211 _rpcenv
: &mut dyn RpcEnvironment
,
212 ) -> Result
<Value
, Error
> {
214 let snapshot
= BackupDir
::new(backup_type
, backup_id
, backup_time
);
216 let datastore
= DataStore
::lookup_datastore(&store
)?
;
218 datastore
.remove_backup_dir(&snapshot
)?
;
227 schema
: DATASTORE_SCHEMA
,
231 schema
: BACKUP_TYPE_SCHEMA
,
235 schema
: BACKUP_ID_SCHEMA
,
241 description
: "Returns the list of snapshots.",
243 type: SnapshotListItem
,
247 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT
, false),
250 /// List backup snapshots.
251 pub fn list_snapshots (
254 _rpcenv
: &mut dyn RpcEnvironment
,
255 ) -> Result
<Vec
<SnapshotListItem
>, Error
> {
257 let store
= tools
::required_string_param(¶m
, "store")?
;
258 let backup_type
= param
["backup-type"].as_str();
259 let backup_id
= param
["backup-id"].as_str();
261 let datastore
= DataStore
::lookup_datastore(store
)?
;
263 let base_path
= datastore
.base_path();
265 let backup_list
= BackupInfo
::list_backups(&base_path
)?
;
267 let mut snapshots
= vec
![];
269 for info
in backup_list
{
270 let group
= info
.backup_dir
.group();
271 if let Some(backup_type
) = backup_type
{
272 if backup_type
!= group
.backup_type() { continue; }
274 if let Some(backup_id
) = backup_id
{
275 if backup_id
!= group
.backup_id() { continue; }
278 let mut result_item
= SnapshotListItem
{
279 backup_type
: group
.backup_type().to_string(),
280 backup_id
: group
.backup_id().to_string(),
281 backup_time
: info
.backup_dir
.backup_time().timestamp(),
286 if let Ok(index
) = read_backup_index(&datastore
, &info
.backup_dir
) {
287 let mut backup_size
= 0;
288 for item
in index
.iter() {
289 if let Some(item_size
) = item
.size
{
290 backup_size
+= item_size
;
293 result_item
.size
= Some(backup_size
);
296 snapshots
.push(result_item
);
306 schema
: DATASTORE_SCHEMA
,
314 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT
, false),
317 /// Get datastore status.
321 _rpcenv
: &mut dyn RpcEnvironment
,
322 ) -> Result
<StorageStatus
, Error
> {
324 let datastore
= DataStore
::lookup_datastore(&store
)?
;
326 let base_path
= datastore
.base_path();
328 let mut stat
: libc
::statfs64
= unsafe { std::mem::zeroed() }
;
332 let res
= base_path
.with_nix_path(|cstr
| unsafe { libc::statfs64(cstr.as_ptr(), &mut stat) }
)?
;
333 nix
::errno
::Errno
::result(res
)?
;
335 let bsize
= stat
.f_bsize
as u64;
338 total
: stat
.f_blocks
*bsize
,
339 used
: (stat
.f_blocks
-stat
.f_bfree
)*bsize
,
340 avail
: stat
.f_bavail
*bsize
,
345 macro_rules
! add_common_prune_prameters
{
346 ( [ $
( $list1
:tt
)* ] ) => {
347 add_common_prune_prameters
!([$
( $list1
)* ] , [])
349 ( [ $
( $list1
:tt
)* ] , [ $
( $list2
:tt
)* ] ) => {
355 &IntegerSchema
::new("Number of daily backups to keep.")
362 &IntegerSchema
::new("Number of hourly backups to keep.")
369 &IntegerSchema
::new("Number of backups to keep.")
376 &IntegerSchema
::new("Number of monthly backups to keep.")
383 &IntegerSchema
::new("Number of weekly backups to keep.")
390 &IntegerSchema
::new("Number of yearly backups to keep.")
399 const API_METHOD_PRUNE
: ApiMethod
= ApiMethod
::new(
400 &ApiHandler
::Sync(&prune
),
402 "Prune the datastore.",
403 &add_common_prune_prameters
!([
404 ("backup-id", false, &BACKUP_ID_SCHEMA
),
405 ("backup-type", false, &BACKUP_TYPE_SCHEMA
),
406 ("dry-run", true, &BooleanSchema
::new(
407 "Just show what prune would do, but do not delete anything.")
411 ("store", false, &DATASTORE_SCHEMA
),
414 ).access(None
, &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_PRUNE
, false));
419 _rpcenv
: &mut dyn RpcEnvironment
,
420 ) -> Result
<Value
, Error
> {
422 let store
= param
["store"].as_str().unwrap();
424 let backup_type
= tools
::required_string_param(¶m
, "backup-type")?
;
425 let backup_id
= tools
::required_string_param(¶m
, "backup-id")?
;
427 let dry_run
= param
["dry-run"].as_bool().unwrap_or(false);
429 let group
= BackupGroup
::new(backup_type
, backup_id
);
431 let datastore
= DataStore
::lookup_datastore(store
)?
;
433 let prune_options
= PruneOptions
{
434 keep_last
: param
["keep-last"].as_u64(),
435 keep_hourly
: param
["keep-hourly"].as_u64(),
436 keep_daily
: param
["keep-daily"].as_u64(),
437 keep_weekly
: param
["keep-weekly"].as_u64(),
438 keep_monthly
: param
["keep-monthly"].as_u64(),
439 keep_yearly
: param
["keep-yearly"].as_u64(),
442 let worker_id
= format
!("{}_{}_{}", store
, backup_type
, backup_id
);
444 let mut prune_result
= Vec
::new();
446 let list
= group
.list_backups(&datastore
.base_path())?
;
448 let mut prune_info
= compute_prune_info(list
, &prune_options
)?
;
450 prune_info
.reverse(); // delete older snapshots first
452 let keep_all
= !prune_options
.keeps_something();
455 for (info
, mut keep
) in prune_info
{
456 if keep_all { keep = true; }
458 let backup_time
= info
.backup_dir
.backup_time();
459 let group
= info
.backup_dir
.group();
461 prune_result
.push(json
!({
462 "backup-type": group
.backup_type(),
463 "backup-id": group
.backup_id(),
464 "backup-time": backup_time
.timestamp(),
468 return Ok(json
!(prune_result
));
472 // We use a WorkerTask just to have a task log, but run synchrounously
473 let worker
= WorkerTask
::new("prune", Some(worker_id
), "root@pam", true)?
;
475 let result
= try_block
! {
477 worker
.log("No prune selection - keeping all files.");
479 worker
.log(format
!("retention options: {}", prune_options
.cli_options_string()));
480 worker
.log(format
!("Starting prune on store \"{}\" group \"{}/{}\"",
481 store
, backup_type
, backup_id
));
484 for (info
, mut keep
) in prune_info
{
485 if keep_all { keep = true; }
487 let backup_time
= info
.backup_dir
.backup_time();
488 let timestamp
= BackupDir
::backup_time_to_string(backup_time
);
489 let group
= info
.backup_dir
.group();
497 if keep { "keep" }
else { "remove" }
,
502 prune_result
.push(json
!({
503 "backup-type": group
.backup_type(),
504 "backup-id": group
.backup_id(),
505 "backup-time": backup_time
.timestamp(),
509 if !(dry_run
|| keep
) {
510 datastore
.remove_backup_dir(&info
.backup_dir
)?
;
517 worker
.log_result(&result
);
519 if let Err(err
) = result
{
520 bail
!("prune failed - {}", err
);
523 Ok(json
!(prune_result
))
530 schema
: DATASTORE_SCHEMA
,
538 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_PRUNE
, false),
541 /// Start garbage collection.
542 fn start_garbage_collection(
545 rpcenv
: &mut dyn RpcEnvironment
,
546 ) -> Result
<Value
, Error
> {
548 let datastore
= DataStore
::lookup_datastore(&store
)?
;
550 println
!("Starting garbage collection on store {}", store
);
552 let to_stdout
= if rpcenv
.env_type() == RpcEnvironmentType
::CLI { true }
else { false }
;
554 let upid_str
= WorkerTask
::new_thread(
555 "garbage_collection", Some(store
.clone()), "root@pam", to_stdout
, move |worker
|
557 worker
.log(format
!("starting garbage collection on store {}", store
));
558 datastore
.garbage_collection(worker
)
568 schema
: DATASTORE_SCHEMA
,
573 type: GarbageCollectionStatus
,
576 permission
: &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT
, false),
579 /// Garbage collection status.
580 pub fn garbage_collection_status(
583 _rpcenv
: &mut dyn RpcEnvironment
,
584 ) -> Result
<GarbageCollectionStatus
, Error
> {
586 let datastore
= DataStore
::lookup_datastore(&store
)?
;
588 let status
= datastore
.last_gc_status();
595 permission
: &Permission
::Privilege(&["datastore"], PRIV_DATASTORE_AUDIT
, false),
599 fn get_datastore_list(
602 _rpcenv
: &mut dyn RpcEnvironment
,
603 ) -> Result
<Value
, Error
> {
605 let (config
, _digest
) = datastore
::config()?
;
607 Ok(config
.convert_to_array("store", None
, &[]))
611 pub const API_METHOD_DOWNLOAD_FILE
: ApiMethod
= ApiMethod
::new(
612 &ApiHandler
::AsyncHttp(&download_file
),
614 "Download single raw file from backup snapshot.",
616 ("store", false, &DATASTORE_SCHEMA
),
617 ("backup-type", false, &BACKUP_TYPE_SCHEMA
),
618 ("backup-id", false, &BACKUP_ID_SCHEMA
),
619 ("backup-time", false, &BACKUP_TIME_SCHEMA
),
620 ("file-name", false, &BACKUP_ARCHIVE_NAME_SCHEMA
),
623 ).access(None
, &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_READ
, false));
630 _rpcenv
: Box
<dyn RpcEnvironment
>,
631 ) -> ApiResponseFuture
{
634 let store
= tools
::required_string_param(¶m
, "store")?
;
636 let datastore
= DataStore
::lookup_datastore(store
)?
;
638 let file_name
= tools
::required_string_param(¶m
, "file-name")?
.to_owned();
640 let backup_type
= tools
::required_string_param(¶m
, "backup-type")?
;
641 let backup_id
= tools
::required_string_param(¶m
, "backup-id")?
;
642 let backup_time
= tools
::required_integer_param(¶m
, "backup-time")?
;
644 println
!("Download {} from {} ({}/{}/{}/{})", file_name
, store
,
645 backup_type
, backup_id
, Local
.timestamp(backup_time
, 0), file_name
);
647 let backup_dir
= BackupDir
::new(backup_type
, backup_id
, backup_time
);
649 let mut path
= datastore
.base_path();
650 path
.push(backup_dir
.relative_path());
651 path
.push(&file_name
);
653 let file
= tokio
::fs
::File
::open(path
)
654 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("File open failed: {}", err
)))
657 let payload
= tokio_util
::codec
::FramedRead
::new(file
, tokio_util
::codec
::BytesCodec
::new())
658 .map_ok(|bytes
| hyper
::body
::Bytes
::from(bytes
.freeze()));
659 let body
= Body
::wrap_stream(payload
);
661 // fixme: set other headers ?
662 Ok(Response
::builder()
663 .status(StatusCode
::OK
)
664 .header(header
::CONTENT_TYPE
, "application/octet-stream")
671 pub const API_METHOD_UPLOAD_BACKUP_LOG
: ApiMethod
= ApiMethod
::new(
672 &ApiHandler
::AsyncHttp(&upload_backup_log
),
674 "Download single raw file from backup snapshot.",
676 ("store", false, &DATASTORE_SCHEMA
),
677 ("backup-type", false, &BACKUP_TYPE_SCHEMA
),
678 ("backup-id", false, &BACKUP_ID_SCHEMA
),
679 ("backup-time", false, &BACKUP_TIME_SCHEMA
),
682 ).access(None
, &Permission
::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_CREATE_BACKUP
, false));
684 fn upload_backup_log(
689 _rpcenv
: Box
<dyn RpcEnvironment
>,
690 ) -> ApiResponseFuture
{
693 let store
= tools
::required_string_param(¶m
, "store")?
;
695 let datastore
= DataStore
::lookup_datastore(store
)?
;
697 let file_name
= "client.log.blob";
699 let backup_type
= tools
::required_string_param(¶m
, "backup-type")?
;
700 let backup_id
= tools
::required_string_param(¶m
, "backup-id")?
;
701 let backup_time
= tools
::required_integer_param(¶m
, "backup-time")?
;
703 let backup_dir
= BackupDir
::new(backup_type
, backup_id
, backup_time
);
705 let mut path
= datastore
.base_path();
706 path
.push(backup_dir
.relative_path());
707 path
.push(&file_name
);
710 bail
!("backup already contains a log.");
713 println
!("Upload backup log to {}/{}/{}/{}/{}", store
,
714 backup_type
, backup_id
, BackupDir
::backup_time_to_string(backup_dir
.backup_time()), file_name
);
717 .map_err(Error
::from
)
718 .try_fold(Vec
::new(), |mut acc
, chunk
| {
719 acc
.extend_from_slice(&*chunk
);
720 future
::ok
::<_
, Error
>(acc
)
724 let blob
= DataBlob
::from_raw(data
)?
;
725 // always verify CRC at server side
727 let raw_data
= blob
.raw_data();
728 replace_file(&path
, raw_data
, CreateOptions
::new())?
;
730 // fixme: use correct formatter
731 Ok(crate::server
::formatter
::json_response(Ok(Value
::Null
)))
736 const DATASTORE_INFO_SUBDIRS
: SubdirMap
= &[
740 .download(&API_METHOD_DOWNLOAD_FILE
)
745 .get(&API_METHOD_LIST_SNAPSHOT_FILES
)
750 .get(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
751 .post(&API_METHOD_START_GARBAGE_COLLECTION
)
756 .get(&API_METHOD_LIST_GROUPS
)
761 .post(&API_METHOD_PRUNE
)
766 .get(&API_METHOD_LIST_SNAPSHOTS
)
767 .delete(&API_METHOD_DELETE_SNAPSHOT
)
772 .get(&API_METHOD_STATUS
)
777 .upload(&API_METHOD_UPLOAD_BACKUP_LOG
)
781 const DATASTORE_INFO_ROUTER
: Router
= Router
::new()
782 .get(&list_subdirs_api_method
!(DATASTORE_INFO_SUBDIRS
))
783 .subdirs(DATASTORE_INFO_SUBDIRS
);
786 pub const ROUTER
: Router
= Router
::new()
787 .get(&API_METHOD_GET_DATASTORE_LIST
)
788 .match_all("store", &DATASTORE_INFO_ROUTER
);