mira-app-server
Version:
Mira Server - standalone server application using mira-app-core
66 lines • 2.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseBackupService = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const mira_app_core_1 = require("mira-app-core");
const DAY_MS = 24 * 60 * 60 * 1000;
const RETENTION_MS = 30 * DAY_MS;
class DatabaseBackupService {
constructor(dataPath) {
this.dataPath = dataPath;
this.running = false;
}
async start() {
await this.runOnce();
this.timer = setInterval(() => { void this.runOnce(); }, DAY_MS);
}
stop() {
if (this.timer)
clearInterval(this.timer);
this.timer = undefined;
}
async runOnce() {
if (this.running)
return;
this.running = true;
try {
const backupRoot = path_1.default.join(this.dataPath, 'db-backups');
await promises_1.default.mkdir(backupRoot, { recursive: true });
const timestamp = new Date().toISOString().replace(/[-:TZ.]/g, '').slice(0, 14);
const targetDir = path_1.default.join(backupRoot, timestamp);
await promises_1.default.mkdir(targetDir, { recursive: true });
const libraries = await (0, mira_app_core_1.getLibraries)(this.dataPath);
for (const library of libraries) {
if (library.customFields?.enableAutoBackup ?? true) {
const source = path_1.default.join(library.path, 'library_data.db');
try {
await promises_1.default.copyFile(source, path_1.default.join(targetDir, `${library.id}.db`));
}
catch (error) {
if (error?.code !== 'ENOENT')
console.error(`DB backup failed for ${library.id}:`, error);
}
}
}
const entries = await promises_1.default.readdir(backupRoot, { withFileTypes: true });
const cutoff = Date.now() - RETENTION_MS;
await Promise.all(entries.filter(entry => entry.isDirectory()).map(async (entry) => {
const stat = await promises_1.default.stat(path_1.default.join(backupRoot, entry.name));
if (stat.mtimeMs < cutoff)
await promises_1.default.rm(path_1.default.join(backupRoot, entry.name), { recursive: true, force: true });
}));
}
catch (error) {
console.error('DB backup run failed:', error);
}
finally {
this.running = false;
}
}
}
exports.DatabaseBackupService = DatabaseBackupService;
//# sourceMappingURL=DatabaseBackupService.js.map