Create database backup before migrating

This commit is contained in:
unknown 2021-10-05 13:17:09 +02:00
parent 84bd641cf2
commit 59271d3376
3 changed files with 25 additions and 4 deletions

3
.env
View file

@ -1,2 +1,3 @@
PORT=5005
NODE_ENV=development
NODE_ENV=development
VERSION=1.6.8

View file

@ -2,6 +2,7 @@ const { Sequelize } = require('sequelize');
const { join } = require('path');
const fs = require('fs');
const Umzug = require('umzug');
const backupDB = require('./utils/backupDb');
const Logger = require('../utils/Logger');
const logger = new Logger();
@ -25,9 +26,7 @@ const umzug = new Umzug({
const connectDB = async () => {
try {
if (fs.existsSync('data/db.sqlite')) {
fs.copyFileSync('data/db.sqlite', 'data/backup_db.sqlite');
}
backupDB();
await sequelize.authenticate();
logger.log('Connected to database');

21
db/utils/backupDb.js Normal file
View file

@ -0,0 +1,21 @@
const fs = require('fs');
const backupDB = () => {
if (!fs.existsSync('data/db_backups')) {
fs.mkdirSync('data/db_backups');
}
const version = process.env.VERSION;
const slug = `db-${version.replace(/\./g, '')}-backup.sqlite`;
const srcPath = 'data/db.sqlite';
const destPath = `data/db_backups/${slug}`;
if (fs.existsSync(srcPath)) {
if (!fs.existsSync(destPath)) {
fs.copyFileSync(srcPath, destPath);
}
}
};
module.exports = backupDB;