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