use built in database backup function
parent
434e15ba1d
commit
afcdca959b
|
|
@ -0,0 +1 @@
|
||||||
|
export declare function backupDatabase(): Promise<false | string>;
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
import sqlite from 'better-sqlite3';
|
||||||
|
import Debug from 'debug';
|
||||||
|
import { sunriseDB } from '../helpers/database.helpers.js';
|
||||||
|
const debug = Debug('sunrise:database:backupDatabase');
|
||||||
|
const backupFolder = 'data/backups';
|
||||||
|
export async function backupDatabase() {
|
||||||
|
const databasePathSplit = sunriseDB.split(/[/\\]/);
|
||||||
|
const backupDatabasePath = `${backupFolder}/${databasePathSplit.at(-1)}.${Date.now().toString()}`;
|
||||||
|
const database = sqlite(sunriseDB);
|
||||||
|
try {
|
||||||
|
const result = await database.backup(backupDatabasePath);
|
||||||
|
if (result.remainingPages === 0) {
|
||||||
|
debug('Database backup completed successfully:', backupDatabasePath);
|
||||||
|
return backupDatabasePath;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
debug('Database backup incomplete:', result.remainingPages, 'pages remaining');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
debug('Error backing up database:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
database.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
import sqlite from 'better-sqlite3'
|
||||||
|
import Debug from 'debug'
|
||||||
|
|
||||||
|
import { sunriseDB } from '../helpers/database.helpers.js'
|
||||||
|
|
||||||
|
const debug = Debug('sunrise:database:backupDatabase')
|
||||||
|
|
||||||
|
const backupFolder = 'data/backups'
|
||||||
|
|
||||||
|
export async function backupDatabase(): Promise<false | string> {
|
||||||
|
const databasePathSplit = sunriseDB.split(/[/\\]/)
|
||||||
|
|
||||||
|
const backupDatabasePath = `${backupFolder}/${databasePathSplit.at(-1)}.${Date.now().toString()}`
|
||||||
|
|
||||||
|
const database = sqlite(sunriseDB)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await database.backup(backupDatabasePath)
|
||||||
|
|
||||||
|
if (result.remainingPages === 0) {
|
||||||
|
debug('Database backup completed successfully:', backupDatabasePath)
|
||||||
|
return backupDatabasePath
|
||||||
|
} else {
|
||||||
|
debug(
|
||||||
|
'Database backup incomplete:',
|
||||||
|
result.remainingPages,
|
||||||
|
'pages remaining'
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
debug('Error backing up database:', error)
|
||||||
|
return false
|
||||||
|
} finally {
|
||||||
|
database.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { backupDatabase } from '../../helpers/database.helpers.js';
|
import { backupDatabase } from '../../database/backupDatabase.js';
|
||||||
export default async function handler(_request, response) {
|
export default async function handler(_request, response) {
|
||||||
const backupDatabasePath = await backupDatabase();
|
const backupDatabasePath = await backupDatabase();
|
||||||
if (typeof backupDatabasePath === 'string') {
|
if (typeof backupDatabasePath === 'string') {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import type { Request, Response } from 'express'
|
import type { Request, Response } from 'express'
|
||||||
|
|
||||||
import { backupDatabase } from '../../helpers/database.helpers.js'
|
import { backupDatabase } from '../../database/backupDatabase.js'
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
_request: Request,
|
_request: Request,
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,3 @@ export declare const useTestDatabases: boolean;
|
||||||
export declare const sunriseDBLive = "data/sunrise.db";
|
export declare const sunriseDBLive = "data/sunrise.db";
|
||||||
export declare const sunriseDBTesting = "data/sunrise-testing.db";
|
export declare const sunriseDBTesting = "data/sunrise-testing.db";
|
||||||
export declare const sunriseDB: string;
|
export declare const sunriseDB: string;
|
||||||
export declare function backupDatabase(): Promise<false | string>;
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import fs from 'node:fs/promises';
|
|
||||||
import Debug from 'debug';
|
import Debug from 'debug';
|
||||||
import { DEBUG_NAMESPACE } from '../debug.config.js';
|
import { DEBUG_NAMESPACE } from '../debug.config.js';
|
||||||
import { getConfigProperty } from './config.helpers.js';
|
import { getConfigProperty } from './config.helpers.js';
|
||||||
|
|
@ -11,15 +10,3 @@ if (useTestDatabases) {
|
||||||
export const sunriseDBLive = 'data/sunrise.db';
|
export const sunriseDBLive = 'data/sunrise.db';
|
||||||
export const sunriseDBTesting = 'data/sunrise-testing.db';
|
export const sunriseDBTesting = 'data/sunrise-testing.db';
|
||||||
export const sunriseDB = useTestDatabases ? sunriseDBTesting : sunriseDBLive;
|
export const sunriseDB = useTestDatabases ? sunriseDBTesting : sunriseDBLive;
|
||||||
const backupFolder = 'data/backups';
|
|
||||||
export async function backupDatabase() {
|
|
||||||
const databasePathSplit = sunriseDB.split(/[/\\]/);
|
|
||||||
const backupDatabasePath = `${backupFolder}/${databasePathSplit.at(-1)}.${Date.now().toString()}`;
|
|
||||||
try {
|
|
||||||
await fs.copyFile(sunriseDB, backupDatabasePath);
|
|
||||||
return backupDatabasePath;
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import fs from 'node:fs/promises'
|
|
||||||
|
|
||||||
import Debug from 'debug'
|
import Debug from 'debug'
|
||||||
|
|
||||||
import { DEBUG_NAMESPACE } from '../debug.config.js'
|
import { DEBUG_NAMESPACE } from '../debug.config.js'
|
||||||
|
|
||||||
import { getConfigProperty } from './config.helpers.js'
|
import { getConfigProperty } from './config.helpers.js'
|
||||||
|
|
||||||
const debug = Debug(`${DEBUG_NAMESPACE}:database.helpers:${process.pid.toString().padEnd(5)}`)
|
const debug = Debug(
|
||||||
|
`${DEBUG_NAMESPACE}:database.helpers:${process.pid.toString().padEnd(5)}`
|
||||||
|
)
|
||||||
|
|
||||||
export const useTestDatabases =
|
export const useTestDatabases =
|
||||||
getConfigProperty('application.useTestDatabases') ||
|
getConfigProperty('application.useTestDatabases') ||
|
||||||
|
|
@ -20,18 +20,3 @@ export const sunriseDBLive = 'data/sunrise.db'
|
||||||
export const sunriseDBTesting = 'data/sunrise-testing.db'
|
export const sunriseDBTesting = 'data/sunrise-testing.db'
|
||||||
|
|
||||||
export const sunriseDB = useTestDatabases ? sunriseDBTesting : sunriseDBLive
|
export const sunriseDB = useTestDatabases ? sunriseDBTesting : sunriseDBLive
|
||||||
|
|
||||||
const backupFolder = 'data/backups'
|
|
||||||
|
|
||||||
export async function backupDatabase(): Promise<false | string> {
|
|
||||||
const databasePathSplit = sunriseDB.split(/[/\\]/)
|
|
||||||
|
|
||||||
const backupDatabasePath = `${backupFolder}/${databasePathSplit.at(-1)}.${Date.now().toString()}`
|
|
||||||
|
|
||||||
try {
|
|
||||||
await fs.copyFile(sunriseDB, backupDatabasePath)
|
|
||||||
return backupDatabasePath
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue