Skip to content

Commit 6edb484

Browse files
feat: add options in VmBackupDirectory
1 parent 773a830 commit 6edb484

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

@xen-orchestra/backup-archive/src/VmBackup.types.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface IVmBackupInterface {
3838
// metadataPath: string
3939
// metadata: PartialBackupMetadata
4040
rootPath: string
41+
opts: BackupCleanOptions
4142

4243
init(): Promise<void>
4344
check(): Promise<object>

@xen-orchestra/backup-archive/src/VmBackupDirectory.mts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import RemoteHandlerAbstract from '@xen-orchestra/fs'
22
import { basename, normalize } from '@xen-orchestra/fs/path'
33
import { VmFullBackupArchive } from './VmFullBackupArchive.mjs'
4-
import { IVmBackupInterface, PartialBackupMetadata } from './VmBackup.types.mjs'
4+
import { BackupCleanOptions, IVmBackupInterface, PartialBackupMetadata } from './VmBackup.types.mjs'
55

66
const FILES_TO_KEEP = ['cache.json.gz']
77

@@ -11,10 +11,22 @@ export class VmBackupDirectory implements IVmBackupInterface {
1111
files: Array<string> = new Array()
1212
orphans: Set<string> = new Set()
1313
backupArchives: Map<string, IVmBackupInterface> = new Map()
14+
opts: BackupCleanOptions
1415

15-
constructor(handler: RemoteHandlerAbstract, vmBackupPath: string) {
16+
constructor(
17+
handler: RemoteHandlerAbstract,
18+
vmBackupPath: string,
19+
opts = {
20+
fix: true,
21+
merge: false,
22+
remove: false,
23+
logInfo: console.info.bind(console),
24+
logWarn: console.warn.bind(console),
25+
}
26+
) {
1627
this.handler = handler
1728
this.rootPath = vmBackupPath
29+
this.opts = opts
1830
}
1931

2032
async init() {
@@ -25,7 +37,7 @@ export class VmBackupDirectory implements IVmBackupInterface {
2537
const backupArchive = await this.createBackupArchive(fullPath, metadata)
2638
this.backupArchives.set(fullPath, backupArchive)
2739
} catch (error) {
28-
console.warn(`Issue loading ${metadata.xva ?? metadata.vhds}`, { json: fullPath, backup: metadata })
40+
this.opts.logWarn(`Issue loading ${metadata.xva ?? metadata.vhds}`, { json: fullPath, backup: metadata })
2941
}
3042
}
3143
}
@@ -63,15 +75,15 @@ export class VmBackupDirectory implements IVmBackupInterface {
6375
fix: true,
6476
merge: true,
6577
remove: true,
66-
logWarn: console.warn,
67-
logInfo: console.log,
78+
logWarn: this.opts.logWarn,
79+
logInfo: this.opts.logInfo,
6880
})
6981
} else {
7082
//@ts-ignore
7183
backupArchive = new VmIncrementalBackupArchive(this.handler)
7284
}
7385
} catch (error) {
74-
console.warn(`Error trying to create backupArchive from ${metadataPath}`, { metadata })
86+
this.opts.logWarn(`Error trying to create backupArchive from ${metadataPath}`, { metadata })
7587
throw new Error(`Error trying to create backupArchive from ${metadataPath}`)
7688
}
7789
await backupArchive.init()

@xen-orchestra/backup-archive/src/VmIncrementalBackupArchive.mts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Disk, DiskChain } from '@xen-orchestra/disk-transform'
2-
import { IVmBackupInterface, PartialBackupMetadata } from './VmBackup.types.mjs'
2+
import { BackupCleanOptions, IVmBackupInterface, PartialBackupMetadata } from './VmBackup.types.mjs'
33
import RemoteHandlerAbstract from '@xen-orchestra/fs'
44
import { normalize } from '@xen-orchestra/fs/path'
55

@@ -11,19 +11,22 @@ export class VmIncrementalBackupArchive implements IVmBackupInterface {
1111
diskLineages: Map<string, DiskChain> = new Map() // path, disk
1212
#diskPaths: Array<string>
1313
rootPath: string
14+
opts: BackupCleanOptions
1415

1516
constructor(
1617
handler: RemoteHandlerAbstract,
1718
rootPath: string,
1819
metadataPath: string,
1920
metadata: PartialBackupMetadata,
20-
diskPaths: Array<string>
21+
diskPaths: Array<string>,
22+
opts: BackupCleanOptions
2123
) {
2224
this.handler = handler
2325
this.rootPath = normalize(rootPath)
2426
this.metadataPath = normalize(metadataPath)
2527
this.metadata = metadata
2628
this.#diskPaths = diskPaths.map(path => normalize(path))
29+
this.opts = opts
2730
}
2831

2932
async init(): Promise<void> {}

0 commit comments

Comments
 (0)