Skip to main content

oxirs_cluster/backup/
policy.rs

1//! Backup policy definition.
2//!
3//! A `BackupPolicy` combines a schedule, retention tier, optional GFS
4//! rotation, encryption configuration and destination into a single
5//! serialisable struct that drives the backup executor.
6
7use serde::{Deserialize, Serialize};
8
9use super::{destination::DestinationConfig, gfs::GfsRotation, retention::RetentionTier};
10
11/// A complete backup policy describing when, what, and where to back up.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct BackupPolicy {
14    /// Human-readable identifier for this policy.
15    pub name: String,
16    /// When this policy runs.
17    pub schedule: CronSchedule,
18    /// How long to keep backups across hot/warm/cold tiers.
19    pub retention: RetentionTier,
20    /// Optional Grandfather-Father-Son rotation.
21    pub gfs: Option<GfsRotation>,
22    /// Encryption settings applied at write time.
23    pub encryption: EncryptionConfig,
24    /// Where to write backup artefacts.
25    pub destination: DestinationConfig,
26}
27
28/// Cron-compatible schedule expression.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct CronSchedule {
31    /// Standard 5-field cron expression (`minute hour day month weekday`).
32    pub expression: String,
33    /// Human-readable description.
34    pub description: String,
35}
36
37impl CronSchedule {
38    /// Every day at 02:00 UTC.
39    pub fn daily() -> Self {
40        CronSchedule {
41            expression: "0 2 * * *".into(),
42            description: "Every day at 02:00 UTC".into(),
43        }
44    }
45
46    /// Every Sunday at 02:00 UTC.
47    pub fn weekly() -> Self {
48        CronSchedule {
49            expression: "0 2 * * 0".into(),
50            description: "Every Sunday at 02:00 UTC".into(),
51        }
52    }
53
54    /// First day of every month at 02:00 UTC.
55    pub fn monthly() -> Self {
56        CronSchedule {
57            expression: "0 2 1 * *".into(),
58            description: "First day of month at 02:00 UTC".into(),
59        }
60    }
61}
62
63/// Encryption settings for a backup policy.
64///
65/// When `enabled` is `true`, the executor tags the backup artefact with
66/// `key_id` so the restore path knows which key to use.  Actual
67/// encryption is delegated to `oxirs_cluster::encryption`.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct EncryptionConfig {
70    /// Whether encryption is required for this policy.
71    pub enabled: bool,
72    /// Key identifier looked up in the key-management store.
73    pub key_id: Option<String>,
74}
75
76impl EncryptionConfig {
77    /// Encryption disabled.
78    pub fn none() -> Self {
79        EncryptionConfig {
80            enabled: false,
81            key_id: None,
82        }
83    }
84
85    /// Encryption enabled with the given key identifier.
86    pub fn with_key(key_id: &str) -> Self {
87        EncryptionConfig {
88            enabled: true,
89            key_id: Some(key_id.to_owned()),
90        }
91    }
92}
93
94// ---------------------------------------------------------------------------
95// Tests
96// ---------------------------------------------------------------------------
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101    use crate::backup::{
102        destination::DestinationConfig, gfs::GfsRotation, retention::RetentionTier,
103    };
104    use std::env;
105
106    fn make_policy(name: &str) -> BackupPolicy {
107        BackupPolicy {
108            name: name.into(),
109            schedule: CronSchedule::daily(),
110            retention: RetentionTier::standard(),
111            gfs: Some(GfsRotation::default()),
112            encryption: EncryptionConfig::none(),
113            destination: DestinationConfig::Filesystem {
114                path: env::temp_dir().join("oxirs_policy_test"),
115            },
116        }
117    }
118
119    #[test]
120    fn policy_builds_and_serialises() {
121        let p = make_policy("daily-test");
122        let json = serde_json::to_string(&p).unwrap();
123        let back: BackupPolicy = serde_json::from_str(&json).unwrap();
124        assert_eq!(back.name, p.name);
125        assert_eq!(back.schedule.expression, p.schedule.expression);
126    }
127
128    #[test]
129    fn cron_schedules_have_correct_expressions() {
130        assert_eq!(CronSchedule::daily().expression, "0 2 * * *");
131        assert_eq!(CronSchedule::weekly().expression, "0 2 * * 0");
132        assert_eq!(CronSchedule::monthly().expression, "0 2 1 * *");
133    }
134
135    #[test]
136    fn encryption_config_none() {
137        let cfg = EncryptionConfig::none();
138        assert!(!cfg.enabled);
139        assert!(cfg.key_id.is_none());
140    }
141
142    #[test]
143    fn encryption_config_with_key() {
144        let cfg = EncryptionConfig::with_key("key-abc");
145        assert!(cfg.enabled);
146        assert_eq!(cfg.key_id.as_deref(), Some("key-abc"));
147    }
148
149    #[test]
150    fn encryption_config_serialises() {
151        let cfg = EncryptionConfig::with_key("key-123");
152        let json = serde_json::to_string(&cfg).unwrap();
153        let back: EncryptionConfig = serde_json::from_str(&json).unwrap();
154        assert_eq!(back.key_id.as_deref(), Some("key-123"));
155        assert!(back.enabled);
156    }
157}