From a880d67eaee0ec82eb6ef6ff43dda064ede2f4c9 Mon Sep 17 00:00:00 2001 From: mrfry Date: Mon, 9 Nov 2020 18:23:25 +0100 Subject: [PATCH] Data backuping every day, and on server startup --- src/modules/api/api.js | 10 +++++-- src/utils/actions.js | 17 ++++++++++++ src/utils/readme.md | 10 +++---- src/utils/utils.js | 59 +++++++++++++++++++----------------------- 4 files changed, 57 insertions(+), 39 deletions(-) diff --git a/src/modules/api/api.js b/src/modules/api/api.js index 076bd82..c14e58d 100644 --- a/src/modules/api/api.js +++ b/src/modules/api/api.js @@ -137,13 +137,18 @@ function GetApp() { function userSpecificMotdSeenBy(id) { if (!userSpecificMotd[id].seen) { - logger.Log(`User #${id}'s user specific motd is now seen.`, logger.GetColor('bluebg')) + logger.Log( + `User #${id}'s user specific motd is now seen.`, + logger.GetColor('bluebg') + ) userSpecificMotd[id].seen = true utils.WriteFile(JSON.stringify(userSpecificMotd), userSpecificMotdFile) } } function Load() { + actions.backupData(data) + utils.WatchFile(userSpecificMotdFile, (newData) => { logger.Log(`User Specific Motd changed: ${newData.replace(/\/n/g, '')}`) LoadUserSpecificMOTD() @@ -985,8 +990,9 @@ function GetApp() { } function DailyAction() { - ExportDailyDataCount() + actions.backupData(data) BackupDB() + ExportDailyDataCount() IncrementAvaiblePWs() } diff --git a/src/utils/actions.js b/src/utils/actions.js index 796c4c2..9e38d66 100755 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -20,6 +20,7 @@ Question Server module.exports = { ProcessIncomingRequest: ProcessIncomingRequest, LoadJSON: LoadJSON, + backupData: backupData, } const dataFile = './publicDirs/qminingPublic/data.json' @@ -196,3 +197,19 @@ function LoadJSON(dataFile) { } return data } + +function backupData(data) { + const path = './publicDirs/qminingPublic/backs/' + utils.CreatePath(path) + try { + logger.Log('Backing up data...') + utils.WriteFile( + JSON.stringify(data), + `${path}data_${utils.GetDateString(true)}.json` + ) + logger.Log('Done') + } catch (err) { + logger.Log('Error backing up data file!', logger.GetColor('redbg')) + console.error(err) + } +} diff --git a/src/utils/readme.md b/src/utils/readme.md index 185e0bf..0a02f81 100644 --- a/src/utils/readme.md +++ b/src/utils/readme.md @@ -30,6 +30,7 @@ Exportált funkciók: 1. ProcessIncomingRequest 2. LoadJSON +3. backupData ## utils.js @@ -41,11 +42,10 @@ Exportált funkciók: 2. WriteFile 3. writeFileAsync 4. AppendToFile -6. WriteBackup -7. FileExists -8. CreatePath -9. WatchFile -10. ReadDir +5. FileExists +6. CreatePath +7. WatchFile +8. ReadDir ## logger.js diff --git a/src/utils/utils.js b/src/utils/utils.js index 58886ee..8e4d1ec 100755 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -4,7 +4,6 @@ module.exports = { WriteFile: WriteFile, writeFileAsync: WriteFileAsync, AppendToFile: AppendToFile, - WriteBackup: WriteBackup, FileExists: FileExists, CreatePath: CreatePath, WatchFile: WatchFile, @@ -13,27 +12,35 @@ module.exports = { GetDateString: GetDateString, } -var fs = require('fs') +const fs = require('fs') +const logger = require('../utils/logger.js') -var logger = require('../utils/logger.js') - -const dataFile = './qminingPublic/data.json' - -function GetDateString() { +function GetDateString(noTime) { const date = new Date() - return ( - date.getFullYear() + - '-' + - ('0' + (date.getMonth() + 1)).slice(-2) + - '-' + - ('0' + date.getDate()).slice(-2) + - ' ' + - ('0' + date.getHours()).slice(-2) + - ':' + - ('0' + date.getMinutes()).slice(-2) + - ':' + - ('0' + date.getSeconds()).slice(-2) - ) + + if (noTime) { + return ( + date.getFullYear() + + '-' + + ('0' + (date.getMonth() + 1)).slice(-2) + + '-' + + ('0' + date.getDate()).slice(-2) + ) + } else { + return ( + date.getFullYear() + + '-' + + ('0' + (date.getMonth() + 1)).slice(-2) + + '-' + + ('0' + date.getDate()).slice(-2) + + ' ' + + ('0' + date.getHours()).slice(-2) + + ':' + + ('0' + date.getMinutes()).slice(-2) + + ':' + + ('0' + date.getSeconds()).slice(-2) + ) + } } function CopyFile(from, to) { @@ -137,15 +144,3 @@ function AppendToFile(data, file) { console.error(err) } } - -function WriteBackup() { - try { - WriteFileAsync( - ReadFile(dataFile), - 'public/backs/data_' + new Date().toString() - ) - } catch (err) { - logger.Log('Error backing up data json file!', logger.GetColor('redbg')) - console.error(err) - } -}