Skip to content

Commit 773a830

Browse files
feat: add logInfo and logWarn in VmFullBackup
1 parent f7db79f commit 773a830

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export interface PartialBackupMetadata {
1616
timestamp: number
1717
}
1818

19+
export interface BackupCleanOptions {
20+
fix: boolean
21+
merge: boolean
22+
remove: boolean
23+
logInfo: (message: any, opts?: object) => void
24+
logWarn: (message: any, opts?: object) => void
25+
}
26+
1927
export interface IBackupLineage {
2028
init(): Promise<void>
2129
check(): Promise<void>

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ export class VmBackupDirectory implements IVmBackupInterface {
5959
let backupArchive: IVmBackupInterface
6060
try {
6161
if (metadata.mode == 'full') {
62-
backupArchive = new VmFullBackupArchive(this.handler, this.rootPath, metadataPath, metadata, metadata.xva!)
62+
backupArchive = new VmFullBackupArchive(this.handler, this.rootPath, metadataPath, metadata, metadata.xva!, {
63+
fix: true,
64+
merge: true,
65+
remove: true,
66+
logWarn: console.warn,
67+
logInfo: console.log,
68+
})
6369
} else {
6470
//@ts-ignore
6571
backupArchive = new VmIncrementalBackupArchive(this.handler)

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

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

77
const COMPRESSED_MAGIC_NUMBERS: Buffer[] = [
@@ -72,19 +72,22 @@ export class VmFullBackupArchive implements IVmBackupInterface {
7272
isValid?: boolean
7373
metadataPath: string
7474
metadata: PartialBackupMetadata
75+
opts: BackupCleanOptions
7576

7677
constructor(
7778
handler: RemoteHandlerAbstract,
7879
rootPath: string,
7980
metadataPath: string,
8081
metadata: PartialBackupMetadata,
81-
xvaPath: string
82+
xvaPath: string,
83+
opts: BackupCleanOptions
8284
) {
8385
this.handler = handler
8486
this.rootPath = normalize(rootPath)
8587
this.metadataPath = normalize(metadataPath)
8688
this.metadata = metadata
8789
this.xvaPath = normalize(xvaPath)
90+
this.opts = opts
8891
}
8992

9093
async init() {
@@ -97,13 +100,13 @@ export class VmFullBackupArchive implements IVmBackupInterface {
97100
this.isValid = await isValidXva(this.handler, this.xvaPath)
98101
}
99102
} catch (error) {
100-
console.log(error)
103+
this.opts.logWarn(error)
101104
this.isValid = false
102105
}
103106
// TODO: check isValid
104107
// isValid is always false in test because XVA test is too small
105108
if (this.isValid) {
106-
console.warn('XVA might be broken', { path: this.xvaPath })
109+
this.opts.logWarn('XVA might be broken', { path: this.xvaPath })
107110
}
108111
return { xvaValid: this.isValid }
109112
}
@@ -114,14 +117,14 @@ export class VmFullBackupArchive implements IVmBackupInterface {
114117
* @param opts { remove: boolean }
115118
* @returns
116119
*/
117-
async clean({ remove = false }) {
120+
async clean({ remove = this.opts.remove ?? false }) {
118121
let filesToRemove: Array<string> = []
119122
if (remove) {
120123
for (const file of filesToRemove) {
121124
try {
122125
await this.handler.unlink(file)
123126
} catch (error) {
124-
console.warn(`Issue removing ${file}`)
127+
this.opts.logWarn(`Issue removing ${file}`)
125128
}
126129
}
127130
}

0 commit comments

Comments
 (0)