Add automatic database backups

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

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ 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
@ -56,3 +57,15 @@ 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();