From 84dccd800ff51fd9f09c3246358fbdebabfeed9a Mon Sep 17 00:00:00 2001 From: Florent BEAUCHAMP Date: Thu, 14 Oct 2021 14:44:40 +0200 Subject: [PATCH] feat(backups): clean up other schedules snapshots (#5949) Fixes xoa-support#4129 --- @xen-orchestra/backups/_VmBackup.js | 28 ++++++++++++++++++++-------- CHANGELOG.unreleased.md | 4 ++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/@xen-orchestra/backups/_VmBackup.js b/@xen-orchestra/backups/_VmBackup.js index 5b7a1eeb4..930357044 100644 --- a/@xen-orchestra/backups/_VmBackup.js +++ b/@xen-orchestra/backups/_VmBackup.js @@ -1,5 +1,6 @@ const assert = require('assert') const findLast = require('lodash/findLast.js') +const groupBy = require('lodash/groupBy.js') const ignoreErrors = require('promise-toolbox/ignoreErrors.js') const keyBy = require('lodash/keyBy.js') const mapValues = require('lodash/mapValues.js') @@ -284,17 +285,28 @@ exports.VmBackup = class VmBackup { } async _removeUnusedSnapshots() { - // TODO: handle all schedules (no longer existing schedules default to 0 retention) - - const { scheduleId } = this - const scheduleSnapshots = this._jobSnapshots.filter(_ => _.other_config['xo:backup:schedule'] === scheduleId) - + const jobSettings = this.job.settings const baseVmRef = this._baseVm?.$ref + const { config } = this + const baseSettings = { + ...config.defaultSettings, + ...config.metadata.defaultSettings, + ...jobSettings[''], + } + + const snapshotsPerSchedule = groupBy(this._jobSnapshots, _ => _.other_config['xo:backup:schedule']) const xapi = this._xapi - await asyncMap(getOldEntries(this._settings.snapshotRetention, scheduleSnapshots), ({ $ref }) => { - if ($ref !== baseVmRef) { - return xapi.VM_destroy($ref) + await asyncMap(Object.entries(snapshotsPerSchedule), ([scheduleId, snapshots]) => { + const settings = { + ...baseSettings, + ...jobSettings[scheduleId], + ...jobSettings[this.vm.uuid], } + return asyncMap(getOldEntries(settings.snapshotRetention, snapshots), ({ $ref }) => { + if ($ref !== baseVmRef) { + return xapi.VM_destroy($ref) + } + }) }) } diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 3ccbe8c3c..b9dc709fb 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -13,6 +13,8 @@ > Users must be able to say: “I had this issue, happy to know it's fixed” +- [Backups] Delete unused snapshots related to other schedules (even no longer existing) (PR [#5949](https://github.com/vatesfr/xen-orchestra/pull/5949)) + ### Packages to release > Packages will be released in the order they are here, therefore, they should @@ -30,5 +32,7 @@ > > In case of conflict, the highest (lowest in previous list) `$version` wins. +- @xen-orchestra/backup minor - @xen-orchestra/proxy minor +- xo-server patch - xo-web minor