Add automatic database backups
This commit is contained in:
		
							parent
							
								
									76790e2621
								
							
						
					
					
						commit
						23ed423033
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -2,3 +2,4 @@ node_modules | ||||
| .env | ||||
| config.json | ||||
| *.sqlite | ||||
| backups | ||||
| @ -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, | ||||
| }; | ||||
							
								
								
									
										13
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								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
 | ||||
| @ -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(); | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user