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

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules
.env
config.json
*.sqlite
backups

View File

@ -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,
};

View File

@ -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
@ -56,3 +57,15 @@ client.once(Events.ClientReady, () => {
require('sequelize');
require('./dbObjects.js');
if (!fs.existsSync('./backups')) {
fs.mkdir('./backups', (err) => {
console.log(err);
});
}
setInterval(() => {
backupDatabase();
}, 86000000);
backupDatabase();