From 30eb68567636524ec72748f694b657a877843ce3 Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 30 Dec 2023 22:45:52 +0000 Subject: [PATCH] Add automatic database backups --- .gitignore | 3 ++- databaseHelperFunctions.js | 13 +++++++++++++ index.js | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 09b652f..43c3783 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .env config.json -*.sqlite \ No newline at end of file +*.sqlite +backups \ No newline at end of file diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index 9d86580..52caa65 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -1,4 +1,5 @@ const { Users, Games, BeatenGames } = require ('./dbObjects.js'); +const fs = require('fs'); async function checkUserRegistration(user) { @@ -183,6 +184,17 @@ async function getGames(id) { return false; } +async function backupDatabase() { + const date = new Date().toJSON().slice(0, 10); + + if (fs.existsSync('./database.sqlite')) { + // I know that this is probably not the best way to do this but for now it is fine. + fs.copyFile('./database.sqlite', String.prototype.concat('./backups/database-', date, '.sqlite'), (err) => { + console.log(err); + }); + } +} + module.exports = { checkUserRegistration, getUserRegistration, @@ -195,4 +207,5 @@ module.exports = { getLeaderboardEntries, getRecentGameEntry, getGames, + backupDatabase, }; \ No newline at end of file diff --git a/index.js b/index.js index 868d0fa..dcea5e6 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const { config } = require('dotenv'); config(); const { igdb } = require('./igdb.js'); +const { backupDatabase } = require('./databaseHelperFunctions.js'); new igdb(); // Create a new client instance @@ -55,4 +56,16 @@ client.once(Events.ClientReady, () => { }); require('sequelize'); -require('./dbObjects.js'); \ No newline at end of file +require('./dbObjects.js'); + +if (!fs.existsSync('./backups')) { + fs.mkdir('./backups', (err) => { + console.log(err); + }); +} + +setInterval(() => { + backupDatabase(); +}, 86000000); + +backupDatabase(); \ No newline at end of file