Data backuping every day, and on server startup

This commit is contained in:
mrfry 2020-11-09 18:23:25 +01:00
parent 7a41281993
commit a880d67eae
4 changed files with 57 additions and 39 deletions

View file

@ -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()
}

View file

@ -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)
}
}

View file

@ -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

View file

@ -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)
}
}