Repository navigation
Expand file tree
/
Copy pathBackupManager.cpp
More file actions
537 lines (447 loc) · 19.3 KB
/
Copy pathBackupManager.cpp
File metadata and controls
537 lines (447 loc) · 19.3 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "BackupManager.hpp"
#include "Application.hpp"
#include "DrawableModule.hpp"
#include "MultiLogger.hpp"
#include "webconfig.hpp"
#include "HardwareConfig.hpp"
#include <time.h>
#include <sys/stat.h>
#include <algorithm>
// PSRAM Allocator for ArduinoJson (same pattern as ThemeParkModule and WeatherModule)
struct SpiRamAllocator : ArduinoJson::Allocator {
void* allocate(size_t size) override { return ps_malloc(size); }
void deallocate(void* pointer) override { free(pointer); }
void* reallocate(void* ptr, size_t new_size) override { return ps_realloc(ptr, new_size); }
};
// External references
extern DeviceConfig* deviceConfig;
extern HardwareConfig* hardwareConfig;
extern GeneralTimeConverter* timeConverter;
#define BACKUP_DIR "/backups"
#define LAST_BACKUP_TIME_FILE "/last_backup_time.txt"
BackupManager::BackupManager(Application* app)
: _app(app), _lastBackupTime(0) {
}
BackupManager::~BackupManager() {
}
void BackupManager::begin() {
Log.println("[BackupManager] Initializing...");
ensureBackupDirectory();
loadLastBackupTime();
Log.println("[BackupManager] Ready");
}
void BackupManager::ensureBackupDirectory() {
if (!LittleFS.exists(BACKUP_DIR)) {
Log.printf("[BackupManager] Creating backup directory: %s\n", BACKUP_DIR);
LittleFS.mkdir(BACKUP_DIR);
}
}
PsramString BackupManager::generateBackupFilename(bool manualBackup) {
char filename[64];
time_t now_utc = time(nullptr);
time_t now_local = timeConverter ? timeConverter->toLocal(now_utc) : now_utc;
struct tm timeinfo;
gmtime_r(&now_local, &timeinfo);
const char* prefix = manualBackup ? "manual_" : "";
snprintf(filename, sizeof(filename), "%sbackup_%04d-%02d-%02d_%02d-%02d-%02d.json",
prefix,
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec);
return filename;
}
PsramString BackupManager::getBackupPath(const PsramString& filename) {
PsramString path = BACKUP_DIR;
path += "/";
path += filename;
return path;
}
bool BackupManager::createBackup(bool manualBackup) {
Log.println("[BackupManager] Creating backup...");
// Generate filename
PsramString filename = generateBackupFilename(manualBackup);
PsramString fullPath = getBackupPath(filename);
Log.printf("[BackupManager] Backup file: %s\n", fullPath.c_str());
// Create JSON document with PSRAM allocator (same pattern as ThemeParkModule and WeatherModule)
SpiRamAllocator allocator;
JsonDocument doc(&allocator);
// Add metadata
doc["version"] = "1.0";
doc["timestamp"] = time(nullptr);
doc["manual"] = manualBackup;
// Collect all data
collectConfiguration(doc);
collectModuleData(doc);
collectCertificates(doc);
collectJsonFiles(doc);
// Write to file
File file = LittleFS.open(fullPath.c_str(), "w");
if (!file) {
Log.printf("[BackupManager] ERROR: Could not create backup file: %s\n", fullPath.c_str());
return false;
}
size_t bytesWritten = serializeJson(doc, file);
file.close();
Log.printf("[BackupManager] Backup created successfully: %s (%u bytes)\n",
filename.c_str(), bytesWritten);
// Update last backup time
_lastBackupTime = time(nullptr);
saveLastBackupTime();
// Rotate old backups
rotateBackups();
return true;
}
void BackupManager::collectConfiguration(JsonDocument& doc) {
Log.println("[BackupManager] Collecting configuration...");
JsonObject config = doc["configuration"].to<JsonObject>();
// Read config.json directly - this automatically includes ALL fields without manual listing
// Any new config fields are automatically backed up without needing to modify this code
if (LittleFS.exists("/config.json")) {
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
SpiRamAllocator allocator;
JsonDocument configDoc(&allocator);
DeserializationError error = deserializeJson(configDoc, configFile);
configFile.close();
if (!error) {
config["device"] = configDoc.as<JsonVariant>();
Log.println("[BackupManager] Device config collected from config.json");
} else {
Log.printf("[BackupManager] Error parsing config.json: %s\n", error.c_str());
}
}
}
// Read hardware.json directly - same approach for hardware config
if (LittleFS.exists("/hardware.json")) {
File hwFile = LittleFS.open("/hardware.json", "r");
if (hwFile) {
SpiRamAllocator allocator;
JsonDocument hwDoc(&allocator);
DeserializationError error = deserializeJson(hwDoc, hwFile);
hwFile.close();
if (!error) {
config["hardware"] = hwDoc.as<JsonVariant>();
Log.println("[BackupManager] Hardware config collected from hardware.json");
} else {
Log.printf("[BackupManager] Error parsing hardware.json: %s\n", error.c_str());
}
}
}
}
void BackupManager::collectModuleData(JsonDocument& doc) {
// Module data is now backed up directly via their JSON files in collectJsonFiles()
// No need for individual module backup() methods anymore
Log.println("[BackupManager] Module data will be backed up via JSON files");
}
void BackupManager::collectCertificates(JsonDocument& doc) {
Log.println("[BackupManager] Collecting certificates...");
JsonObject certs = doc["certificates"].to<JsonObject>();
// Helper lambda to scan directory for certificates
auto scanDirForCerts = [&](const char* dirPath) {
File dir = LittleFS.open(dirPath, "r");
if (!dir || !dir.isDirectory()) {
Log.printf("[BackupManager] Could not open directory: %s\n", dirPath);
return;
}
File file = dir.openNextFile();
while (file) {
if (!file.isDirectory()) {
PsramString filename = file.name();
if (endsWith(filename, ".pem") || endsWith(filename, ".crt") || endsWith(filename, ".cer")) {
// Extract relative path (remove leading slash if present)
PsramString relativePath = filename;
if (startsWith(relativePath, "/")) {
relativePath = substring(relativePath, 1);
}
Log.printf("[BackupManager] Backing up certificate: %s\n", relativePath.c_str());
// Read certificate content
size_t fileSize = file.size();
char* buffer = (char*)ps_malloc(fileSize + 1);
if (buffer) {
file.seek(0);
size_t bytesRead = file.readBytes(buffer, fileSize);
buffer[bytesRead] = '\0';
certs[relativePath.c_str()] = buffer;
free(buffer);
} else {
Log.printf("[BackupManager] ERROR: Memory allocation failed for %s\n", relativePath.c_str());
}
}
}
file.close();
file = dir.openNextFile();
}
dir.close();
};
// Scan root directory for certificates
scanDirForCerts("/");
// Scan /certs directory for certificates (if it exists)
if (LittleFS.exists("/certs")) {
scanDirForCerts("/certs");
}
}
void BackupManager::collectJsonFiles(JsonDocument& doc) {
Log.println("[BackupManager] Collecting JSON files...");
JsonObject jsonFiles = doc["json_files"].to<JsonObject>();
// Automatically discover and backup all JSON files in LittleFS root directory
// Exclude files that are handled separately or should not be backed up
File root = LittleFS.open("/", "r");
if (!root || !root.isDirectory()) {
Log.println("[BackupManager] Could not open root directory");
return;
}
File file = root.openNextFile();
while (file) {
if (!file.isDirectory()) {
PsramString filename = file.name();
// Extract just filename without path
int lastSlash = lastIndexOf(filename, '/');
if (lastSlash >= 0) {
filename = substring(filename, lastSlash + 1);
}
// Skip files that should not be backed up:
// - config.json and hardware.json (handled separately)
// - backup files themselves
// - last_backup_time.txt (internal backup manager state)
// - any non-JSON files
if (endsWith(filename, ".json") &&
filename != "config.json" &&
filename != "hardware.json" &&
!startsWith(filename, "backup_") &&
!startsWith(filename, "manual_backup_") &&
!startsWith(filename, "uploaded_backup_")) {
Log.printf("[BackupManager] Backing up JSON file: %s\n", filename.c_str());
// Re-open the file for reading (the iterator file is for listing only)
PsramString fullPath = "/";
fullPath += filename;
File jsonFile = LittleFS.open(fullPath.c_str(), "r");
if (jsonFile) {
SpiRamAllocator allocator;
JsonDocument fileDoc(&allocator);
DeserializationError error = deserializeJson(fileDoc, jsonFile);
jsonFile.close();
if (!error) {
// Copy the entire JSON structure (object, array, or primitive)
// This preserves the original structure of the file
jsonFiles[filename.c_str()] = fileDoc.as<JsonVariant>();
} else {
Log.printf("[BackupManager] Error parsing JSON file %s: %s\n",
filename.c_str(), error.c_str());
}
}
}
}
file.close();
file = root.openNextFile();
}
root.close();
Log.printf("[BackupManager] Finished collecting JSON files\n");
}
bool BackupManager::restoreFromBackup(const PsramString& filename) {
Log.printf("[BackupManager] Restoring from backup: %s\n", filename.c_str());
PsramString fullPath = getBackupPath(filename);
if (!LittleFS.exists(fullPath.c_str())) {
Log.printf("[BackupManager] ERROR: Backup file not found: %s\n", fullPath.c_str());
return false;
}
File file = LittleFS.open(fullPath.c_str(), "r");
if (!file) {
Log.printf("[BackupManager] ERROR: Could not open backup file: %s\n", fullPath.c_str());
return false;
}
// Parse backup file with PSRAM allocator (same pattern as ThemeParkModule and WeatherModule)
SpiRamAllocator allocator;
JsonDocument doc(&allocator);
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error) {
Log.printf("[BackupManager] ERROR: Could not parse backup file: %s\n", error.c_str());
return false;
}
Log.println("[BackupManager] Backup file parsed successfully");
// Restore configuration
if (doc["configuration"].is<JsonObject>()) {
JsonObject config = doc["configuration"];
// Restore device config
if (config["device"].is<JsonObject>() && deviceConfig) {
JsonObject deviceConf = config["device"];
// We'll write this to config.json
File configFile = LittleFS.open("/config.json", "w");
if (configFile) {
serializeJson(deviceConf, configFile);
configFile.close();
Log.println("[BackupManager] Device config restored");
}
}
// Restore hardware config
if (config["hardware"].is<JsonObject>() && hardwareConfig) {
JsonObject hwConf = config["hardware"];
File hwFile = LittleFS.open("/hardware.json", "w");
if (hwFile) {
serializeJson(hwConf, hwFile);
hwFile.close();
Log.println("[BackupManager] Hardware config restored");
}
}
}
// Restore certificates
if (doc["certificates"].is<JsonObject>()) {
JsonObject certs = doc["certificates"];
for (JsonPair kv : certs) {
PsramString relativePath = kv.key().c_str();
// Security: Validate path to prevent directory traversal
if (indexOf(relativePath, "..") >= 0 || startsWith(relativePath, "/")) {
Log.printf("[BackupManager] WARNING: Skipping invalid certificate path: %s\n", relativePath.c_str());
continue;
}
PsramString certPath = "/";
certPath += relativePath;
// Ensure parent directory exists
int lastSlash = lastIndexOf(certPath, '/');
if (lastSlash > 0) {
PsramString parentDir = substring(certPath, 0, lastSlash);
if (!LittleFS.exists(parentDir.c_str())) {
LittleFS.mkdir(parentDir.c_str());
Log.printf("[BackupManager] Created directory: %s\n", parentDir.c_str());
}
}
File certFile = LittleFS.open(certPath.c_str(), "w");
if (certFile) {
certFile.print(kv.value().as<const char*>());
certFile.close();
Log.printf("[BackupManager] Certificate restored: %s\n", certPath.c_str());
}
}
}
// Restore JSON files
if (doc["json_files"].is<JsonObject>()) {
JsonObject jsonFiles = doc["json_files"];
for (JsonPair kv : jsonFiles) {
PsramString relativePath = kv.key().c_str();
// Security: Validate path to prevent directory traversal
if (indexOf(relativePath, "..") >= 0 || startsWith(relativePath, "/")) {
Log.printf("[BackupManager] WARNING: Skipping invalid JSON file path: %s\n", relativePath.c_str());
continue;
}
PsramString filepath = "/";
filepath += relativePath;
// Ensure parent directory exists
int lastSlash = lastIndexOf(filepath, '/');
if (lastSlash > 0) {
PsramString parentDir = substring(filepath, 0, lastSlash);
if (!LittleFS.exists(parentDir.c_str())) {
LittleFS.mkdir(parentDir.c_str());
Log.printf("[BackupManager] Created directory: %s\n", parentDir.c_str());
}
}
File jsonFile = LittleFS.open(filepath.c_str(), "w");
if (jsonFile) {
serializeJson(kv.value(), jsonFile);
jsonFile.close();
Log.printf("[BackupManager] JSON file restored: %s\n", filepath.c_str());
}
}
}
// Module data is restored via JSON files (no individual module restore needed)
Log.println("[BackupManager] Restore completed successfully. Device needs restart.");
return true;
}
PsramVector<BackupInfo> BackupManager::listBackups() {
PsramVector<BackupInfo> backups;
File dir = LittleFS.open(BACKUP_DIR, "r");
if (!dir || !dir.isDirectory()) {
Log.println("[BackupManager] Backup directory not found");
return backups;
}
File file = dir.openNextFile();
while (file) {
if (!file.isDirectory()) {
PsramString filename = file.name();
// Extract just filename without path
int lastSlash = lastIndexOf(filename, '/');
if (lastSlash >= 0) {
filename = substring(filename, lastSlash + 1);
}
if (endsWith(filename, ".json")) {
size_t fileSize = file.size();
// Try to extract timestamp from file content
time_t timestamp_utc = file.getLastWrite();
time_t timestamp_local = timeConverter ? timeConverter->toLocal(timestamp_utc) : timestamp_utc;
char timeStr[32];
struct tm timeinfo;
gmtime_r(×tamp_local, &timeinfo);
snprintf(timeStr, sizeof(timeStr), "%04d-%02d-%02d %02d:%02d:%02d",
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec);
backups.push_back(BackupInfo(filename, timeStr, fileSize));
}
}
file.close();
file = dir.openNextFile();
}
dir.close();
// Sort backups by filename (which includes timestamp)
std::sort(backups.begin(), backups.end(),
[](const BackupInfo& a, const BackupInfo& b) {
return a.filename > b.filename; // Descending order (newest first)
});
return backups;
}
void BackupManager::rotateBackups(int keepCount) {
Log.printf("[BackupManager] Rotating backups (keeping %d most recent)...\n", keepCount);
PsramVector<BackupInfo> backups = listBackups();
// Delete old backups beyond keepCount
for (size_t i = keepCount; i < backups.size(); i++) {
PsramString fullPath = getBackupPath(backups[i].filename);
Log.printf("[BackupManager] Deleting old backup: %s\n", backups[i].filename.c_str());
LittleFS.remove(fullPath.c_str());
}
}
bool BackupManager::shouldCreateAutomaticBackup() {
// Check if 24 hours have passed since last backup
time_t now = time(nullptr);
const time_t ONE_DAY = 24 * 60 * 60;
if (_lastBackupTime == 0 || (now - _lastBackupTime) >= ONE_DAY) {
return true;
}
return false;
}
void BackupManager::periodicCheck() {
if (shouldCreateAutomaticBackup()) {
Log.println("[BackupManager] Time for automatic backup");
createBackup(false); // automatic backup
}
}
void BackupManager::loadLastBackupTime() {
if (LittleFS.exists(LAST_BACKUP_TIME_FILE)) {
File file = LittleFS.open(LAST_BACKUP_TIME_FILE, "r");
if (file) {
String timeStr = file.readString();
_lastBackupTime = timeStr.toInt();
file.close();
Log.printf("[BackupManager] Last backup time loaded: %ld\n", _lastBackupTime);
}
}
}
void BackupManager::saveLastBackupTime() {
File file = LittleFS.open(LAST_BACKUP_TIME_FILE, "w");
if (file) {
file.print(_lastBackupTime);
file.close();
}
}
PsramString BackupManager::readFileAsBase64(const PsramString& path) {
// Not currently used but kept for potential binary file support
return "";
}
bool BackupManager::writeBase64ToFile(const PsramString& path, const PsramString& base64Content) {
// Not currently used but kept for potential binary file support
return false;
}