Add automatic database backups

This commit is contained in:
baz 2023-12-30 22:45:52 +00:00
parent 76790e2621
commit 30eb685676
3 changed files with 29 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
node_modules node_modules
.env .env
config.json config.json
*.sqlite *.sqlite
backups

View File

@ -1,4 +1,5 @@
const { Users, Games, BeatenGames } = require ('./dbObjects.js'); const { Users, Games, BeatenGames } = require ('./dbObjects.js');
const fs = require('fs');
async function checkUserRegistration(user) { async function checkUserRegistration(user) {
@ -183,6 +184,17 @@ async function getGames(id) {
return false; 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 = { module.exports = {
checkUserRegistration, checkUserRegistration,
getUserRegistration, getUserRegistration,
@ -195,4 +207,5 @@ module.exports = {
getLeaderboardEntries, getLeaderboardEntries,
getRecentGameEntry, getRecentGameEntry,
getGames, getGames,
backupDatabase,
}; };

View File

@ -7,6 +7,7 @@ const { config } = require('dotenv');
config(); config();
const { igdb } = require('./igdb.js'); const { igdb } = require('./igdb.js');
const { backupDatabase } = require('./databaseHelperFunctions.js');
new igdb(); new igdb();
// Create a new client instance // Create a new client instance
@ -55,4 +56,16 @@ client.once(Events.ClientReady, () => {
}); });
require('sequelize'); require('sequelize');
require('./dbObjects.js'); require('./dbObjects.js');
if (!fs.existsSync('./backups')) {
fs.mkdir('./backups', (err) => {
console.log(err);
});
}
setInterval(() => {
backupDatabase();
}, 86000000);
backupDatabase();