UNPKG

yemot-recordings-backup

Version:

Automatic backup tool for Yemot HaMashiach call recordings to Amazon S3

238 lines (206 loc) 6.77 kB
/** * Yemot Recordings Backup Library * This module exports the core functionality for use as a library */ const BackupService = require('./backupService'); const YemotApi = require('./yemotApi'); const S3Client = require('./s3Client'); const config = require('./config'); const logger = require('./logger'); // Load systems configuration by default config.loadSystems(); /** * Run a backup for a specific system * @param {string} systemId - System ID to backup * @param {string} customPath - Optional path to backup * @returns {Promise<void>} */ async function runSystemBackup(systemId, customPath) { const system = config.systems.find(s => s.id === systemId); if (!system) { throw new Error(`System not found: ${systemId}`); } const backupService = new BackupService(system); return backupService.runBackup(customPath); } /** * Run backup for all enabled systems * @param {string} customPath - Optional path to backup * @returns {Promise<void>} */ async function runBackup(customPath) { // Filter enabled systems const enabledSystems = config.systems.filter(system => system.enabled); if (enabledSystems.length === 0) { throw new Error('No enabled systems found'); } // Run backup for each system for (const system of enabledSystems) { const backupService = new BackupService(system); await backupService.runBackup(customPath); } } /** * Add a new system to the configuration * @param {Object} system - System configuration object * @param {string} system.id - Unique system ID * @param {string} system.username - Yemot username/system number * @param {string} system.password - Yemot password * @param {boolean} system.enabled - Whether the system is enabled * @returns {boolean} Success status */ function addSystem(system) { // Validate required fields if (!system.id || !system.username || !system.password) { throw new Error('System ID, username, and password are required'); } // Check if system already exists const existingIndex = config.systems.findIndex(s => s.id === system.id); if (existingIndex >= 0) { throw new Error(`System with ID "${system.id}" already exists`); } // Add system with enabled flag (default to true if not specified) const newSystem = { ...system, enabled: system.enabled !== undefined ? system.enabled : true }; config.systems.push(newSystem); // Save to file return saveSystemsToFile(); } /** * Update an existing system * @param {string} systemId - ID of system to update * @param {Object} updates - Fields to update * @returns {boolean} Success status */ function updateSystem(systemId, updates) { // Find system const systemIndex = config.systems.findIndex(s => s.id === systemId); if (systemIndex < 0) { throw new Error(`System with ID "${systemId}" not found`); } // Update fields if (updates.username) config.systems[systemIndex].username = updates.username; if (updates.password) config.systems[systemIndex].password = updates.password; if (updates.enabled !== undefined) config.systems[systemIndex].enabled = updates.enabled; // Save to file return saveSystemsToFile(); } /** * Remove a system * @param {string} systemId - ID of system to remove * @returns {boolean} Success status */ function removeSystem(systemId) { // Find system const systemIndex = config.systems.findIndex(s => s.id === systemId); if (systemIndex < 0) { throw new Error(`System with ID "${systemId}" not found`); } // Remove system config.systems.splice(systemIndex, 1); // Save to file return saveSystemsToFile(); } /** * Get system by ID * @param {string} systemId - System ID to retrieve * @returns {Object} System configuration */ function getSystem(systemId) { // Find system const system = config.systems.find(s => s.id === systemId); if (!system) { throw new Error(`System with ID "${systemId}" not found`); } return { ...system }; } /** * Get all systems * @returns {Array} List of all systems */ function getSystems() { return [...config.systems]; } /** * Configure AWS settings * @param {Object} awsConfig - AWS configuration * @param {string} awsConfig.accessKeyId - AWS access key ID * @param {string} awsConfig.secretAccessKey - AWS secret access key * @param {string} awsConfig.region - AWS region * @param {string} awsConfig.bucket - S3 bucket name * @returns {boolean} Success status */ function configureAws(awsConfig) { // Validate required fields if (!awsConfig.accessKeyId || !awsConfig.secretAccessKey || !awsConfig.region || !awsConfig.bucket) { throw new Error('All AWS configuration fields are required'); } // Update config config.aws = { ...awsConfig }; // Save to .env file if possible try { const fs = require('fs'); const path = require('path'); const dotenv = require('dotenv'); const envPath = path.join(process.cwd(), '.env'); // Read existing .env if it exists let envConfig = {}; try { const existingEnv = fs.readFileSync(envPath, 'utf8'); envConfig = dotenv.parse(existingEnv); } catch (error) { // File doesn't exist, start with empty config } // Update values envConfig.AWS_ACCESS_KEY_ID = awsConfig.accessKeyId; envConfig.AWS_SECRET_ACCESS_KEY = awsConfig.secretAccessKey; envConfig.AWS_REGION = awsConfig.region; envConfig.AWS_S3_BUCKET = awsConfig.bucket; // Write back to .env const envContent = Object.entries(envConfig) .map(([key, value]) => `${key}=${value}`) .join('\n'); fs.writeFileSync(envPath, envContent); } catch (error) { logger.warn(`Could not save AWS config to .env file: ${error.message}`); } return true; } /** * Save systems configuration to file * @private * @returns {boolean} Success status */ function saveSystemsToFile() { try { const fs = require('fs'); const path = require('path'); const systemsPath = path.join(__dirname, 'systems.json'); fs.writeFileSync(systemsPath, JSON.stringify(config.systems, null, 2)); return true; } catch (error) { logger.error(`Failed to save systems: ${error.message}`); throw error; } } // Expose public API module.exports = { runBackup, runSystemBackup, BackupService, YemotApi, S3Client, config, logger, // System management functions addSystem, updateSystem, removeSystem, getSystem, getSystems, // AWS configuration functions configureAws };