]>
Commit | Line | Data |
---|---|---|
254b1f22 SR |
1 | //! See the different modules for documentation on their usage. |
2 | //! | |
3 | //! The [backup](backup/index.html) module contains some detailed information | |
4 | //! on the inner workings of the backup server regarding data storage. | |
5 | ||
450105b0 WB |
6 | use std::path::PathBuf; |
7 | ||
fa49d0fd DM |
8 | use once_cell::sync::OnceCell; |
9 | use anyhow::{format_err, Error}; | |
10 | ||
09340f28 DM |
11 | use proxmox::tools::fs::CreateOptions; |
12 | ||
c68fa58a | 13 | use pbs_api_types::{RRDMode, RRDTimeFrame}; |
450105b0 WB |
14 | use pbs_buildcfg::configdir; |
15 | use pbs_tools::cert::CertInfo; | |
eb37d4ec | 16 | use proxmox_rrd::{rrd::CF, RRDCache}; |
450105b0 | 17 | |
8f973f81 | 18 | #[macro_use] |
51b499db DM |
19 | pub mod tools; |
20 | ||
7e21da6e | 21 | #[macro_use] |
882594c5 | 22 | pub mod server; |
16b48b81 | 23 | |
986bef16 | 24 | #[macro_use] |
cbdd8c54 | 25 | pub mod backup; |
35cf5daa | 26 | |
a8f268af | 27 | pub mod config; |
678d72df | 28 | |
576e3bf2 | 29 | pub mod api2; |
e8edbbd4 | 30 | |
6c30068e | 31 | pub mod auth_helpers; |
7d817b03 DM |
32 | |
33 | pub mod auth; | |
6359dc89 | 34 | |
8c15560b | 35 | pub mod tape; |
f2f526b6 WB |
36 | |
37 | pub mod acme; | |
01a08021 WB |
38 | |
39 | pub mod client_helpers; | |
450105b0 WB |
40 | |
41 | /// Get the server's certificate info (from `proxy.pem`). | |
42 | pub fn cert_info() -> Result<CertInfo, anyhow::Error> { | |
43 | CertInfo::from_path(PathBuf::from(configdir!("/proxy.pem"))) | |
44 | } | |
09340f28 | 45 | |
fa49d0fd DM |
46 | pub static RRD_CACHE: OnceCell<RRDCache> = OnceCell::new(); |
47 | ||
48 | /// Get the RRD cache instance | |
49 | pub fn get_rrd_cache() -> Result<&'static RRDCache, Error> { | |
50 | RRD_CACHE.get().ok_or_else(|| format_err!("RRD cache not initialized!")) | |
51 | } | |
52 | ||
53 | /// Initialize the RRD cache instance | |
54 | /// | |
55 | /// Note: Only a single process must do this (proxmox-backup-proxy) | |
56 | pub fn initialize_rrd_cache() -> Result<&'static RRDCache, Error> { | |
57 | ||
58 | let backup_user = pbs_config::backup_user()?; | |
59 | ||
60 | let file_options = CreateOptions::new() | |
61 | .owner(backup_user.uid) | |
62 | .group(backup_user.gid); | |
63 | ||
64 | let dir_options = CreateOptions::new() | |
65 | .owner(backup_user.uid) | |
66 | .group(backup_user.gid); | |
67 | ||
68 | let apply_interval = 30.0*60.0; // 30 minutes | |
69 | ||
70 | let cache = RRDCache::new( | |
71 | "/var/lib/proxmox-backup/rrdb", | |
72 | Some(file_options), | |
73 | Some(dir_options), | |
74 | apply_interval, | |
75 | )?; | |
76 | ||
77 | RRD_CACHE.set(cache) | |
78 | .map_err(|_| format_err!("RRD cache already initialized!"))?; | |
79 | ||
80 | Ok(RRD_CACHE.get().unwrap()) | |
09340f28 | 81 | } |
eb37d4ec DM |
82 | |
83 | /// Extracts data for the specified time frame from from RRD cache | |
84 | pub fn extract_rrd_data( | |
85 | basedir: &str, | |
86 | name: &str, | |
c68fa58a | 87 | timeframe: RRDTimeFrame, |
eb37d4ec DM |
88 | mode: RRDMode, |
89 | ) -> Result<Option<(u64, u64, Vec<Option<f64>>)>, Error> { | |
90 | ||
91 | let end = proxmox_time::epoch_f64() as u64; | |
92 | ||
93 | let (start, resolution) = match timeframe { | |
c68fa58a DM |
94 | RRDTimeFrame::Hour => (end - 3600, 60), |
95 | RRDTimeFrame::Day => (end - 3600*24, 60), | |
96 | RRDTimeFrame::Week => (end - 3600*24*7, 30*60), | |
97 | RRDTimeFrame::Month => (end - 3600*24*30, 30*60), | |
98 | RRDTimeFrame::Year => (end - 3600*24*365, 6*60*60), | |
99 | RRDTimeFrame::Decade => (end - 10*3600*24*366, 7*86400), | |
eb37d4ec DM |
100 | }; |
101 | ||
102 | let cf = match mode { | |
103 | RRDMode::Max => CF::Maximum, | |
104 | RRDMode::Average => CF::Average, | |
105 | }; | |
106 | ||
107 | let rrd_cache = get_rrd_cache()?; | |
108 | ||
109 | rrd_cache.extract_cached_data(basedir, name, cf, resolution, Some(start), Some(end)) | |
110 | } |