CEO updated

This commit is contained in:
andrei-mihnea-cerbu
2024-04-20 03:18:44 +03:00
parent 869305a491
commit 57c2c7ec55
81 changed files with 3517 additions and 1035 deletions
+43 -18
View File
@@ -1,4 +1,5 @@
const fs = require('fs').promises;
const fsPromises = require('fs').promises;
const fs = require('fs');
const path = require('path');
const ip = require('ip');
const {encryptFileInPlace, decryptFileInPlace} = require('../src/main/aes_encrypt');
@@ -19,7 +20,7 @@ async function fetchBackupSchemes(serverIp) {
// Write the fetched backup schemes to the specified file
try {
await lockFile(destPath);
await fs.writeFile(destPath, JSON.stringify(backupSchemes, null, 2)); // Use null, 2 for pretty formatting
await fsPromises.writeFile(destPath, JSON.stringify(backupSchemes, null, 2)); // Use null, 2 for pretty formatting
await unlockFile(destPath);
console.log('Backup schemes saved successfully to', destPath);
} catch (error) {
@@ -29,14 +30,17 @@ async function fetchBackupSchemes(serverIp) {
async function fetchUsersAndDepartments(serverIp) {
try {
console.log('se asteapta depart');
// Fetch departments
const depResponse = await fetch(`http://${serverIp}/users/departments`, {
method: 'GET',
headers: {'x-api-key': 'uc_api'}
});
const departmentsJson = await depResponse.json();
const departments = departmentsJson['data'];
console.log('se asteapta users')
// Fetch users
const userResponse = await fetch(`http://${serverIp}/users`, {
method: 'GET',
@@ -56,8 +60,11 @@ async function fetchUsersAndDepartments(serverIp) {
// Write combined data to a file
const filePath = path.join(__dirname, '..', 'usersInSystem.json');
if(!fs.existsSync(filePath)){
await fsPromises.writeFile(filePath, '');
}
await lockFile(filePath);
await fs.writeFile(filePath, JSON.stringify(combinedData, null, 2), 'utf8');
await fsPromises.writeFile(filePath, JSON.stringify(combinedData, null, 2), 'utf8');
await encryptFileInPlace(filePath);
await unlockFile(filePath);
@@ -72,7 +79,7 @@ async function decryptAndGetServerIP(filePath) {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const fileContent = await fs.readFile(filePath, 'utf8');
const fileContent = await fsPromises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
const serverIP = jsonData.ip;
@@ -87,7 +94,7 @@ async function decryptAndGetServerIP(filePath) {
async function getDirectoryStructure(dirPath) {
const baseName = path.basename(dirPath);
const entries = await fs.readdir(dirPath, {withFileTypes: true});
const entries = await fsPromises.readdir(dirPath, {withFileTypes: true});
const result = {};
result[baseName] = {files: []};
let totalSize = 0;
@@ -101,7 +108,7 @@ async function getDirectoryStructure(dirPath) {
totalSize += subSize;
} else {
// Add file name to the 'files' array and calculate total size
const stats = await fs.stat(entryPath);
const stats = await fsPromises.stat(entryPath);
result[baseName].files.push(entry.name);
totalSize += stats.size;
}
@@ -114,7 +121,7 @@ async function createBackupScheme(serverIp, structure, size) {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const userContent = await fs.readFile(filePath, 'utf8');
const userContent = await fsPromises.readFile(filePath, 'utf8');
const userJson = JSON.parse(userContent);
await encryptFileInPlace(filePath)
@@ -151,7 +158,7 @@ async function createBackupScheme(serverIp, structure, size) {
async function watchDirectoryChanges(serverIp) {
async function readBackupConfig(filePath) {
await lockFile(filePath);
const fileContent = await fs.readFile(filePath, 'utf8');
const fileContent = await fsPromises.readFile(filePath, 'utf8');
await unlockFile(filePath);
return JSON.parse(fileContent);
}
@@ -163,11 +170,15 @@ async function watchDirectoryChanges(serverIp) {
size: size
};
await lockFile(filePath);
await fs.writeFile(path.join(__dirname, '..', 'dirBackup.json'), JSON.stringify(data, null, 2));
await fsPromises.writeFile(path.join(__dirname, '..', 'dirBackup.json'), JSON.stringify(data, null, 2));
await unlockFile(filePath);
}
const configPath = path.join(__dirname, '..', 'dirBackup.json');
if(!fs.existsSync(configPath)){
console.log('backupDir file doesn\'t exists');
return;
}
const backupConfig = await readBackupConfig(configPath);
const storedStructure = backupConfig.structure;
@@ -178,32 +189,46 @@ async function watchDirectoryChanges(serverIp) {
await createBackupScheme(serverIp, currentStructure, currentSize);
await updateBackupConfig(backupConfig.path, currentStructure, currentSize);
console.log('FetcherProcess: start backup.');
process.send({type: 'startBackup'});
} else {
console.log("No changes detected in the directory structure.");
}
}
process.on('SIGINT', () => {
console.log('Process terminated');
process.exit(0);
});
let serverIp = null
decryptAndGetServerIP(path.join(__dirname, '..', 'ipConfig.json')).then(ip => {
serverIp = ip;
});
const timeoutInterval = 6 * 60 * 1000 //minutes * seconds * miliseconds
setInterval(() => {
fetchUsersAndDepartments(serverIp);
}, 5000);
}, timeoutInterval);
setInterval(() => {
watchDirectoryChanges(serverIp);
}, 5000);
}, timeoutInterval);
setInterval(() => {
fetchBackupSchemes(serverIp);
}, 5000);
}, timeoutInterval);
console.log("Service running. Press CTRL+C to stop.");
console.log("fetcher.js process started.");
process.on('SIGINT', async () => {
console.log('Shutdown signal received in \'fetcher.js\'. Cleaning up...');
try {
console.log('Performing cleanup tasks...');
console.log('Cleanup completed successfully.');
} catch (error) {
console.error('An error occurred during cleanup:', error);
} finally {
console.log('Process terminated');
process.exit(0);
}
});