codex_memory/backup/
mod.rs

1pub mod backup_manager;
2pub mod backup_verification;
3pub mod disaster_recovery;
4pub mod encryption;
5pub mod point_in_time_recovery;
6pub mod wal_archiver;
7
8pub use backup_manager::*;
9pub use backup_verification::*;
10pub use disaster_recovery::*;
11pub use encryption::*;
12pub use point_in_time_recovery::*;
13pub use wal_archiver::*;
14
15use chrono::{DateTime, Utc};
16use serde::{Deserialize, Serialize};
17use std::collections::HashMap;
18use std::path::PathBuf;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct BackupConfig {
22    /// Directory where backups are stored
23    pub backup_directory: PathBuf,
24
25    /// WAL archive directory
26    pub wal_archive_directory: PathBuf,
27
28    /// Backup retention policy in days
29    pub retention_days: u32,
30
31    /// Enable encryption for backups
32    pub enable_encryption: bool,
33
34    /// Encryption key path
35    pub encryption_key_path: Option<PathBuf>,
36
37    /// Backup schedule (cron expression)
38    pub backup_schedule: String,
39
40    /// Enable cross-region backup replication
41    pub enable_replication: bool,
42
43    /// Replication targets
44    pub replication_targets: Vec<ReplicationTarget>,
45
46    /// Recovery time objective in minutes
47    pub rto_minutes: u32,
48
49    /// Recovery point objective in minutes  
50    pub rpo_minutes: u32,
51
52    /// Enable backup verification
53    pub enable_verification: bool,
54
55    /// Verification schedule (cron expression)
56    pub verification_schedule: String,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct ReplicationTarget {
61    pub name: String,
62    pub endpoint: String,
63    pub region: String,
64    pub credentials: Option<String>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct BackupMetadata {
69    pub id: String,
70    pub backup_type: BackupType,
71    pub status: BackupStatus,
72    pub start_time: DateTime<Utc>,
73    pub end_time: Option<DateTime<Utc>>,
74    pub size_bytes: u64,
75    pub compressed_size_bytes: u64,
76    pub file_path: PathBuf,
77    pub checksum: String,
78    pub database_name: String,
79    pub wal_start_lsn: Option<String>,
80    pub wal_end_lsn: Option<String>,
81    pub encryption_enabled: bool,
82    pub replication_status: HashMap<String, ReplicationStatus>,
83    pub verification_status: Option<VerificationStatus>,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
87pub enum BackupType {
88    Full,
89    Incremental,
90    Differential,
91    WalArchive,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
95pub enum BackupStatus {
96    InProgress,
97    Completed,
98    Failed,
99    Expired,
100    Archived,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub enum ReplicationStatus {
105    Pending,
106    InProgress,
107    Completed,
108    Failed,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct VerificationStatus {
113    pub verified: bool,
114    pub verification_time: DateTime<Utc>,
115    pub integrity_check_passed: bool,
116    pub restoration_test_passed: bool,
117    pub error_message: Option<String>,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct RecoveryOptions {
122    pub target_time: Option<DateTime<Utc>>,
123    pub target_lsn: Option<String>,
124    pub target_transaction_id: Option<u64>,
125    pub target_name: Option<String>,
126    pub recovery_target_action: RecoveryTargetAction,
127    pub recovery_target_inclusive: bool,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub enum RecoveryTargetAction {
132    Pause,
133    Promote,
134    Shutdown,
135}
136
137impl Default for BackupConfig {
138    fn default() -> Self {
139        Self {
140            backup_directory: PathBuf::from("/var/lib/codex/backups"),
141            wal_archive_directory: PathBuf::from("/var/lib/codex/wal_archive"),
142            retention_days: 30,
143            enable_encryption: true,
144            encryption_key_path: Some(PathBuf::from("/etc/codex/backup.key")),
145            backup_schedule: "0 2 * * *".to_string(), // Daily at 2 AM
146            enable_replication: false,
147            replication_targets: Vec::new(),
148            rto_minutes: 60, // 1 hour RTO
149            rpo_minutes: 5,  // 5 minute RPO
150            enable_verification: true,
151            verification_schedule: "0 3 * * 0".to_string(), // Weekly on Sunday at 3 AM
152        }
153    }
154}
155
156#[derive(Debug, thiserror::Error)]
157pub enum BackupError {
158    #[error("IO error: {0}")]
159    Io(#[from] std::io::Error),
160
161    #[error("Database error: {0}")]
162    Database(#[from] sqlx::Error),
163
164    #[error("Backup failed: {message}")]
165    BackupFailed { message: String },
166
167    #[error("Recovery failed: {message}")]
168    RecoveryFailed { message: String },
169
170    #[error("Verification failed: {message}")]
171    VerificationFailed { message: String },
172
173    #[error("Encryption error: {message}")]
174    EncryptionError { message: String },
175
176    #[error("Replication error: {message}")]
177    ReplicationError { message: String },
178
179    #[error("Configuration error: {message}")]
180    ConfigurationError { message: String },
181
182    #[error("Timeout error: operation timed out after {seconds} seconds")]
183    Timeout { seconds: u64 },
184}
185
186pub type Result<T> = std::result::Result<T, BackupError>;