1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Backup configuration and scheduling.
use std::time::Duration;
use serde::{Deserialize, Serialize};
/// Backup configuration for a data store.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupConfig {
/// Enable backups for this store
pub enabled: bool,
/// Backup schedule (cron expression)
/// Examples: "0 * * * *" (hourly), "0 0 * * *" (daily)
pub schedule: String,
/// Maximum number of backups to retain
pub retention_count: u32,
/// Backup retention duration (delete older backups)
pub retention_days: u32,
/// Storage backend ('local', 's3', etc.)
pub storage: String,
/// Storage path or S3 bucket
pub storage_path: String,
/// Compression enabled (gzip, zstd)
pub compression: Option<String>,
/// Backup timeout
pub timeout_secs: u64,
/// Attempt to verify backup after creation
pub verify_after_backup: bool,
}
impl Default for BackupConfig {
fn default() -> Self {
Self {
enabled: true,
schedule: "0 * * * *".to_string(), // Hourly
retention_count: 24, // Keep 24 hourly backups
retention_days: 30,
storage: "local".to_string(),
storage_path: "/var/backups/fraiseql".to_string(),
compression: Some("gzip".to_string()),
timeout_secs: 600,
verify_after_backup: true,
}
}
}
impl BackupConfig {
/// Create config for PostgreSQL backups (hourly, high retention).
pub fn postgres_default() -> Self {
Self {
enabled: true,
schedule: "0 * * * *".to_string(), // Hourly
retention_count: 24,
retention_days: 30,
storage: "local".to_string(),
storage_path: "/var/backups/fraiseql/postgres".to_string(),
compression: Some("gzip".to_string()),
timeout_secs: 1800,
verify_after_backup: true,
}
}
/// Create config for Redis backups (daily).
pub fn redis_default() -> Self {
Self {
enabled: true,
schedule: "0 0 * * *".to_string(), // Daily at midnight
retention_count: 7,
retention_days: 7,
storage: "local".to_string(),
storage_path: "/var/backups/fraiseql/redis".to_string(),
compression: Some("gzip".to_string()),
timeout_secs: 600,
verify_after_backup: false,
}
}
/// Create config for ClickHouse backups (daily).
pub fn clickhouse_default() -> Self {
Self {
enabled: true,
schedule: "0 1 * * *".to_string(), // Daily at 1 AM
retention_count: 7,
retention_days: 7,
storage: "local".to_string(),
storage_path: "/var/backups/fraiseql/clickhouse".to_string(),
compression: None, // ClickHouse compression built-in
timeout_secs: 3600,
verify_after_backup: false,
}
}
/// Create config for Elasticsearch backups (daily).
pub fn elasticsearch_default() -> Self {
Self {
enabled: true,
schedule: "0 2 * * *".to_string(), // Daily at 2 AM
retention_count: 7,
retention_days: 7,
storage: "local".to_string(),
storage_path: "/var/backups/fraiseql/elasticsearch".to_string(),
compression: None, // Elasticsearch snapshot built-in compression
timeout_secs: 3600,
verify_after_backup: true,
}
}
/// Get timeout as Duration.
pub fn timeout(&self) -> Duration {
Duration::from_secs(self.timeout_secs)
}
}
/// Backup status report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupStatus {
/// Name of the data store (postgres, redis, etc.)
pub store_name: String,
/// Whether backups are enabled
pub enabled: bool,
/// Last successful backup timestamp (Unix seconds)
pub last_successful_backup: Option<i64>,
/// Size of last backup in bytes
pub last_backup_size: Option<u64>,
/// Number of available backups
pub available_backups: u32,
/// Last error message (if any)
pub last_error: Option<String>,
/// Health status: "healthy", "warning", "error"
pub status: String,
}
/// Recovery configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecoveryConfig {
/// Data store to recover (postgres, redis, etc.)
pub store_name: String,
/// Backup timestamp to restore from (Unix seconds)
pub backup_timestamp: i64,
/// Verify data after recovery
pub verify_after_recovery: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_postgres_default_config() {
let config = BackupConfig::postgres_default();
assert!(config.enabled);
assert_eq!(config.schedule, "0 * * * *");
assert_eq!(config.retention_count, 24);
}
#[test]
fn test_redis_default_config() {
let config = BackupConfig::redis_default();
assert!(config.enabled);
assert_eq!(config.schedule, "0 0 * * *");
assert_eq!(config.retention_count, 7);
}
#[test]
fn test_clickhouse_default_config() {
let config = BackupConfig::clickhouse_default();
assert!(config.enabled);
assert_eq!(config.schedule, "0 1 * * *");
}
#[test]
fn test_elasticsearch_default_config() {
let config = BackupConfig::elasticsearch_default();
assert!(config.enabled);
assert_eq!(config.schedule, "0 2 * * *");
}
#[test]
fn test_timeout_conversion() {
let config = BackupConfig::postgres_default();
let duration = config.timeout();
assert_eq!(duration.as_secs(), 1800);
}
}