2026-02-14 09:53:14 +01:00
import assert from 'node:assert' ;
import BoxError from './boxerror.js' ;
2026-02-14 15:43:24 +01:00
import database from './database.js' ;
2026-02-14 09:53:14 +01:00
import debugModule from 'debug' ;
import eventlog from './eventlog.js' ;
import hat from './hat.js' ;
import safe from 'safetydance' ;
import tasks from './tasks.js' ;
2025-07-24 18:09:33 +02:00
2026-02-14 09:53:14 +01:00
const debug = debugModule ( 'box:backups' ) ;
const BACKUP _TYPE _APP = 'app' ;
const BACKUP _STATE _NORMAL = 'normal' ;
2025-10-08 20:11:55 +02:00
2026-02-08 11:17:27 +01:00
const BACKUPS _FIELDS = [ 'id' , 'remotePath' , 'label' , 'identifier' , 'creationTime' , 'packageVersion' , 'type' , 'integrityJson' ,
'statsJson' , 'dependsOnJson' , 'state' , 'manifestJson' , 'preserveSecs' , 'encryptionVersion' , 'appConfigJson' , 'siteId' ,
'integrityCheckTaskId' , 'lastIntegrityCheckTime' , 'integrityCheckStatus' , 'integrityCheckResultJson' ] . join ( ',' ) ;
2025-07-24 18:09:33 +02:00
function postProcess ( result ) {
assert . strictEqual ( typeof result , 'object' ) ;
result . dependsOn = result . dependsOnJson ? safe . JSON . parse ( result . dependsOnJson ) : [ ] ;
delete result . dependsOnJson ;
result . manifest = result . manifestJson ? safe . JSON . parse ( result . manifestJson ) : null ;
delete result . manifestJson ;
2025-08-11 19:30:22 +05:30
result . integrity = result . integrityJson ? safe . JSON . parse ( result . integrityJson ) : null ;
delete result . integrityJson ;
2025-08-12 19:41:50 +05:30
result . stats = result . statsJson ? safe . JSON . parse ( result . statsJson ) : null ;
delete result . statsJson ;
2025-07-24 18:09:33 +02:00
result . appConfig = result . appConfigJson ? safe . JSON . parse ( result . appConfigJson ) : null ;
delete result . appConfigJson ;
2026-02-08 11:17:27 +01:00
result . integrityCheckResult = result . integrityCheckResultJson ? safe . JSON . parse ( result . integrityCheckResultJson ) : null ;
delete result . integrityCheckResultJson ;
2025-07-24 18:09:33 +02:00
return result ;
}
2025-08-13 19:45:52 +05:30
function removePrivateFields ( backup ) {
2026-02-15 12:15:40 +01:00
delete backup . integrityCheckTaskId ;
2025-08-13 19:45:52 +05:30
return backup ;
}
2026-02-15 12:15:40 +01:00
async function attachIntegrityTaskInfo ( backup ) {
backup . integrityCheckTask = backup . integrityCheckTaskId ? await tasks . get ( String ( backup . integrityCheckTaskId ) ) : null ;
}
2025-07-24 18:09:33 +02:00
async function add ( data ) {
assert ( data && typeof data === 'object' ) ;
assert . strictEqual ( typeof data . remotePath , 'string' ) ;
assert ( data . encryptionVersion === null || typeof data . encryptionVersion === 'number' ) ;
assert . strictEqual ( typeof data . packageVersion , 'string' ) ;
assert . strictEqual ( typeof data . type , 'string' ) ;
assert . strictEqual ( typeof data . identifier , 'string' ) ;
assert . strictEqual ( typeof data . state , 'string' ) ;
assert ( Array . isArray ( data . dependsOn ) ) ;
assert . strictEqual ( typeof data . manifest , 'object' ) ;
assert . strictEqual ( typeof data . preserveSecs , 'number' ) ;
assert . strictEqual ( typeof data . appConfig , 'object' ) ;
2025-09-12 09:48:37 +02:00
assert . strictEqual ( typeof data . siteId , 'string' ) ;
2025-07-24 18:09:33 +02:00
const creationTime = data . creationTime || new Date ( ) ; // allow tests to set the time
const manifestJson = JSON . stringify ( data . manifest ) ;
2026-02-14 09:53:14 +01:00
const prefixId = data . type === BACKUP _TYPE _APP ? ` ${ data . type } _ ${ data . identifier } ` : data . type ; // type and identifier are same for other types
2025-07-24 18:09:33 +02:00
const id = ` ${ prefixId } _v ${ data . packageVersion } _ ${ hat ( 32 ) } ` ; // id is used by the UI to derive dependent packages. making this a UUID will require a lot of db querying
const appConfigJson = data . appConfig ? JSON . stringify ( data . appConfig ) : null ;
2025-08-13 19:33:39 +05:30
const statsJson = data . stats ? JSON . stringify ( data . stats ) : null ;
const integrityJson = data . integrity ? JSON . stringify ( data . integrity ) : null ;
2025-07-24 18:09:33 +02:00
2025-09-12 09:48:37 +02:00
const [ error ] = await safe ( database . query ( 'INSERT INTO backups (id, remotePath, identifier, encryptionVersion, packageVersion, type, creationTime, state, dependsOnJson, manifestJson, preserveSecs, appConfigJson, siteId, statsJson, integrityJson) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ,
[ id , data . remotePath , data . identifier , data . encryptionVersion , data . packageVersion , data . type , creationTime , data . state , JSON . stringify ( data . dependsOn ) , manifestJson , data . preserveSecs , appConfigJson , data . siteId , statsJson , integrityJson ] ) ) ;
2025-07-24 18:09:33 +02:00
2025-09-29 11:55:15 +02:00
if ( error && error . sqlCode === 'ER_DUP_ENTRY' ) throw new BoxError ( BoxError . ALREADY _EXISTS , 'Backup already exists' ) ;
2025-07-24 18:09:33 +02:00
if ( error ) throw error ;
return id ;
}
2025-09-12 09:48:37 +02:00
async function getLatestInTargetByIdentifier ( identifier , siteId ) {
2025-07-25 14:03:31 +02:00
assert . strictEqual ( typeof identifier , 'string' ) ;
2025-09-12 09:48:37 +02:00
assert . strictEqual ( typeof siteId , 'string' ) ;
2025-07-25 14:03:31 +02:00
2026-02-14 09:53:14 +01:00
const results = await database . query ( ` SELECT ${ BACKUPS _FIELDS } FROM backups WHERE identifier = ? AND state = ? AND siteId = ? LIMIT 1 ` , [ identifier , BACKUP _STATE _NORMAL , siteId ] ) ;
2025-07-25 14:03:31 +02:00
if ( ! results . length ) return null ;
2026-02-15 12:15:40 +01:00
await attachIntegrityTaskInfo ( results [ 0 ] ) ;
2025-07-25 14:03:31 +02:00
return postProcess ( results [ 0 ] ) ;
}
2025-07-24 18:09:33 +02:00
async function get ( id ) {
assert . strictEqual ( typeof id , 'string' ) ;
2026-02-15 12:15:40 +01:00
const results = await database . query ( ` SELECT ${ BACKUPS _FIELDS } FROM backups WHERE id = ? ORDER BY creationTime DESC ` , [ id ] ) ;
if ( results . length === 0 ) return null ;
2025-07-24 18:09:33 +02:00
2026-02-15 12:15:40 +01:00
await attachIntegrityTaskInfo ( results [ 0 ] ) ;
return postProcess ( results [ 0 ] ) ;
2025-07-24 18:09:33 +02:00
}
function validateLabel ( label ) {
assert . strictEqual ( typeof label , 'string' ) ;
if ( label . length >= 200 ) return new BoxError ( BoxError . BAD _FIELD , 'label too long' ) ;
if ( /[^a-zA-Z0-9._() -]/ . test ( label ) ) return new BoxError ( BoxError . BAD _FIELD , 'label can only contain alphanumerals, space, dot, hyphen, brackets or underscore' ) ;
return null ;
}
// this is called by REST API
2025-07-25 11:49:13 +02:00
async function update ( backup , data ) {
assert . strictEqual ( typeof backup , 'object' ) ;
2025-07-24 18:09:33 +02:00
assert . strictEqual ( typeof data , 'object' ) ;
let error ;
if ( 'label' in data ) {
error = validateLabel ( data . label ) ;
if ( error ) throw error ;
}
const fields = [ ] , values = [ ] ;
for ( const p in data ) {
2025-10-20 13:22:51 +02:00
if ( p === 'label' || p === 'preserveSecs' || p === 'state' ) {
2025-07-24 18:09:33 +02:00
fields . push ( p + ' = ?' ) ;
values . push ( data [ p ] ) ;
2025-10-20 13:22:51 +02:00
} else if ( p === 'stats' ) {
fields . push ( ` ${ p } Json=? ` ) ;
values . push ( JSON . stringify ( data [ p ] ) ) ;
2025-07-24 18:09:33 +02:00
}
}
2025-07-25 11:49:13 +02:00
values . push ( backup . id ) ;
2025-07-24 18:09:33 +02:00
const result = await database . query ( 'UPDATE backups SET ' + fields . join ( ', ' ) + ' WHERE id = ?' , values ) ;
if ( result . affectedRows !== 1 ) throw new BoxError ( BoxError . NOT _FOUND , 'Backup not found' ) ;
if ( 'preserveSecs' in data ) {
// update the dependancies
for ( const depId of backup . dependsOn ) {
await database . query ( 'UPDATE backups SET preserveSecs=? WHERE id = ?' , [ data . preserveSecs , depId ] ) ;
}
}
}
2025-10-21 13:56:08 +02:00
async function listByTypePaged ( type , siteId , page , perPage ) {
2025-10-07 12:07:27 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
2025-10-06 14:10:29 +02:00
assert . strictEqual ( typeof siteId , 'string' ) ;
assert ( typeof page === 'number' && page > 0 ) ;
assert ( typeof perPage === 'number' && perPage > 0 ) ;
2025-10-21 13:56:08 +02:00
const results = await database . query ( ` SELECT ${ BACKUPS _FIELDS } FROM backups WHERE siteId=? AND type = ? ORDER BY creationTime DESC LIMIT ?,? ` , [ siteId , type , ( page - 1 ) * perPage , perPage ] ) ;
2025-10-06 14:10:29 +02:00
2026-02-15 12:15:40 +01:00
for ( const r of results ) {
await attachIntegrityTaskInfo ( r ) ;
postProcess ( r ) ;
}
2025-10-06 14:10:29 +02:00
return results ;
}
2026-02-15 12:15:55 +01:00
async function listByIdentifierAndStatePaged ( identifier , state , page , perPage ) {
assert . strictEqual ( typeof identifier , 'string' ) ;
assert . strictEqual ( typeof state , 'string' ) ;
assert ( typeof page === 'number' && page > 0 ) ;
assert ( typeof perPage === 'number' && perPage > 0 ) ;
const results = await database . query ( ` SELECT ${ BACKUPS _FIELDS } FROM backups WHERE identifier = ? AND state = ? ORDER BY creationTime DESC LIMIT ?,? ` , [ identifier , state , ( page - 1 ) * perPage , perPage ] ) ;
2026-02-15 12:15:40 +01:00
for ( const r of results ) {
await attachIntegrityTaskInfo ( r ) ;
postProcess ( r ) ;
}
2026-02-15 12:15:55 +01:00
return results ;
}
2025-07-24 18:09:33 +02:00
async function del ( id ) {
assert . strictEqual ( typeof id , 'string' ) ;
const result = await database . query ( 'DELETE FROM backups WHERE id=?' , [ id ] ) ;
if ( result . affectedRows !== 1 ) throw new BoxError ( BoxError . NOT _FOUND , 'Backup not found' ) ;
}
2025-08-15 16:09:58 +05:30
2026-02-08 11:17:27 +01:00
async function setIntegrityResult ( backup , status , result ) {
assert . strictEqual ( typeof backup , 'object' ) ;
assert . strictEqual ( typeof status , 'string' ) ;
assert . strictEqual ( typeof result , 'object' ) ;
const now = new Date ( ) ;
2026-03-03 18:41:57 +05:30
await database . query ( 'UPDATE backups SET lastIntegrityCheckTime = ?, integrityCheckStatus = ?, integrityCheckResultJson = ? WHERE id = ?' ,
2026-02-08 11:17:27 +01:00
[ now , status , JSON . stringify ( result ) , backup . id ] ) ;
}
async function startIntegrityCheck ( backup , auditSource ) {
2025-08-15 16:09:58 +05:30
assert . strictEqual ( typeof backup , 'object' ) ;
2026-02-08 11:17:27 +01:00
assert . strictEqual ( typeof auditSource , 'object' ) ;
const ids = [ backup . id , ... backup . dependsOn ] ;
const placeholders = ids . map ( ( ) => '?' ) . join ( ',' ) ;
2026-03-03 18:41:57 +05:30
const taskId = await tasks . add ( tasks . TASK _CHECK _BACKUP _INTEGRITY , [ backup . id ] ) ;
2026-02-08 11:17:27 +01:00
const didUpdate = await database . runInTransaction ( async ( query ) => {
2026-03-03 18:41:57 +05:30
const rows = await query ( ` SELECT id, integrityCheckTaskId FROM backups WHERE id IN ( ${ placeholders } ) FOR UPDATE ` , [ ... ids ] ) ;
for ( const b of rows ) {
if ( ! b . integrityCheckTaskId || b . integrityCheckTaskId === backup . integrityCheckTaskId ) continue ;
const t = await tasks . get ( String ( b . integrityCheckTaskId ) ) ;
if ( t ? . active ) return false ;
}
2026-02-15 23:26:03 +01:00
await query ( ` UPDATE backups SET integrityCheckTaskId = ?, lastIntegrityCheckTime = ?, integrityCheckStatus = ?, integrityCheckResultJson = ? WHERE id IN ( ${ placeholders } ) ` , [ taskId , null , null , null , ... ids ] ) ;
2026-02-08 11:17:27 +01:00
return true ;
} ) ;
if ( ! didUpdate ) throw new BoxError ( BoxError . CONFLICT , 'An integrity check is already in progress for a dependent backup' ) ;
await eventlog . add ( eventlog . ACTION _BACKUP _INTEGRITY _START , auditSource , { taskId , backupId : backup . id } ) ;
2026-02-15 21:50:01 +01:00
// background
2026-02-08 11:17:27 +01:00
tasks . startTask ( taskId , { } )
2026-02-15 23:38:05 +01:00
. then ( async ( status ) => {
debug ( ` startIntegrityCheck: task completed ` ) ;
await eventlog . add ( eventlog . ACTION _BACKUP _INTEGRITY _FINISH , auditSource , { status , taskId , backupId : backup . id } ) ;
} )
. catch ( async ( error ) => {
debug ( ` startIntegrityCheck: task error. ${ error . message } ` ) ;
await eventlog . add ( eventlog . ACTION _BACKUP _INTEGRITY _FINISH , auditSource , { errorMessage : error . message , taskId , backupId : backup . id } ) ;
2026-02-08 11:17:27 +01:00
} ) ;
2025-08-15 16:09:58 +05:30
return taskId ;
}
2025-10-07 18:42:51 +02:00
2026-02-09 21:58:40 +01:00
async function stopIntegrityCheck ( backup , auditSource ) {
assert . strictEqual ( typeof backup , 'object' ) ;
assert . strictEqual ( typeof auditSource , 'object' ) ;
2026-03-03 18:41:57 +05:30
const task = backup . integrityCheckTaskId ? await tasks . get ( String ( backup . integrityCheckTaskId ) ) : null ;
if ( ! task ? . active ) throw new BoxError ( BoxError . BAD _STATE , 'task is not active' ) ;
2026-02-09 21:58:40 +01:00
await tasks . stopTask ( backup . integrityCheckTaskId ) ;
}
2026-02-14 15:43:24 +01:00
export default {
get ,
2026-02-15 12:15:55 +01:00
listByIdentifierAndStatePaged ,
2026-02-14 15:43:24 +01:00
getLatestInTargetByIdentifier , // brutal function name
add ,
update ,
listByTypePaged ,
del ,
removePrivateFields ,
startIntegrityCheck ,
stopIntegrityCheck ,
setIntegrityResult ,
BACKUP _IDENTIFIER _BOX : 'box' ,
BACKUP _IDENTIFIER _MAIL : 'mail' ,
BACKUP _TYPE _APP ,
BACKUP _TYPE _BOX : 'box' ,
BACKUP _TYPE _MAIL : 'mail' ,
BACKUP _STATE _NORMAL ,
BACKUP _STATE _CREATING : 'creating' ,
BACKUP _STATE _ERROR : 'error' ,
} ;