A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
feat: enhance pruning mechanism. Thanks @akleber
towfiqi663d10172c09
diff --git a/backend/__tests__/managers/handlers/PruneHandler.test.ts b/backend/__tests__/managers/handlers/PruneHandler.test.tsindex bed62a1..5a5c760 100644--- a/backend/__tests__/managers/handlers/PruneHandler.test.ts+++ b/backend/__tests__/managers/handlers/PruneHandler.test.ts@@ -73,12 +73,10 @@ describe('PruneHandler', () => {expect(result.success).toBe(true);expect(result.result).toBe('Pruned Old Snapshots Successfully.');- // Should call runResticCommand twice: once for policy, once for snapCount- expect(mockRunResticCommand).toHaveBeenCalledTimes(2);+ // All args are combined into a single runResticCommand call+ expect(mockRunResticCommand).toHaveBeenCalledTimes(1);- // First call: policy prune- expect(mockRunResticCommand).toHaveBeenNthCalledWith(- 1,+ expect(mockRunResticCommand).toHaveBeenCalledWith(expect.arrayContaining(['forget','--prune',@@ -90,17 +88,12 @@ describe('PruneHandler', () => {'4','--keep-monthly','12',+ '--keep-last',+ '50',]),expect.objectContaining({ RESTIC_PASSWORD: 'test-key' }));- // Second call: snap count prune- expect(mockRunResticCommand).toHaveBeenNthCalledWith(- 2,- expect.arrayContaining(['--keep-last', '50']),- expect.objectContaining({ RESTIC_PASSWORD: 'test-key' })- );-expect(pruneEndListener).toHaveBeenCalledWith({planId: 'plan-1',success: true,@@ -149,7 +142,7 @@ describe('PruneHandler', () => {);});- it('should successfully prune with forgetByDate policy', async () => {+ it('should treat forgetByDate as unknown policy (no policy args, keep-last 1 failsafe)', async () => {const optionsWithDate = {...baseOptions,settings: {@@ -164,10 +157,13 @@ describe('PruneHandler', () => {const result = await handler.prune('plan-1', optionsWithDate, false);expect(result.success).toBe(true);+ // forgetByDate is no longer a supported policy; only the failsafe --keep-last 1 is appliedexpect(mockRunResticCommand).toHaveBeenCalledWith(- expect.arrayContaining(['forget', '--prune', '--keep-before', '2024-01-01']),+ expect.arrayContaining(['forget', '--prune', '--keep-last', '1']),expect.any(Object));+ const call = mockRunResticCommand.mock.calls[0][0];+ expect(call).not.toContain('--keep-before');});it('should update stats when updateStats is true', async () => {@@ -328,12 +324,10 @@ describe('PruneHandler', () => {expect(result.success).toBe(true);- // Check both calls include --json- const firstCall = mockRunResticCommand.mock.calls[0][0];- const secondCall = mockRunResticCommand.mock.calls[1][0];-- expect(firstCall).toContain('--json');- expect(secondCall).toContain('--json');+ // All args are combined into a single call — verify it includes --json+ expect(mockRunResticCommand).toHaveBeenCalledTimes(1);+ const call = mockRunResticCommand.mock.calls[0][0];+ expect(call).toContain('--json');});it('should use correct repository path', async () => {@@ -366,21 +360,14 @@ describe('PruneHandler', () => {expect(mockGenerateResticRepoPath).toHaveBeenCalledWith('test-storage', '');});- it('should run policy prune before snapCount prune', async () => {- const callOrder: string[] = [];-- mockRunResticCommand.mockImplementation((args: string[]) => {- if (args.includes('--keep-last')) {- callOrder.push('snapCount');- } else {- callOrder.push('policy');- }- return Promise.resolve('{}');- });-+ it('should combine policy args and snapCount into a single prune command', async () => {await handler.prune('plan-1', baseOptions, false);- expect(callOrder).toEqual(['policy', 'snapCount']);+ // Policy args and --keep-last are all sent in one combined command+ expect(mockRunResticCommand).toHaveBeenCalledTimes(1);+ const call = mockRunResticCommand.mock.calls[0][0];+ expect(call).toContain('--keep-daily');+ expect(call).toContain('--keep-last');});it('should handle stats update failure gracefully', async () => {@@ -464,7 +451,7 @@ describe('PruneHandler', () => {expect(result.policyArgs).toContain('60d');});- it('should generate correct command for forgetByDate policy', () => {+ it('should return empty policyArgs for forgetByDate policy (no longer supported)', () => {const optionsWithDate = {...baseOptions,settings: {@@ -478,8 +465,9 @@ describe('PruneHandler', () => {const result = handler['generatePruneCommand']('plan-1', optionsWithDate);- expect(result.policyArgs).toContain('--keep-before');- expect(result.policyArgs).toContain('2023-12-31');+ // forgetByDate was removed; treated as unknown policy with no policy args+ expect(result.policyArgs).toEqual([]);+ expect(result.policyArgs).not.toContain('--keep-before');});it('should include insecure-no-password when encryption is false', () => {diff --git a/backend/src/managers/handlers/PruneHandler.ts b/backend/src/managers/handlers/PruneHandler.tsindex 23133f4..1f464a1 100644--- a/backend/src/managers/handlers/PruneHandler.ts+++ b/backend/src/managers/handlers/PruneHandler.ts@@ -10,30 +10,30 @@ export class PruneHandler {planId: string,options: Record<string, any>,updateStats: boolean = false- ): Promise<{- success: boolean;- result: string;- }> {- // const pruneResult = { policy: [], keepLast: [] };+ ): Promise<{ success: boolean; result: string }> {let pruneErrorMsg = '';+ const prune = options.settings.prune;++ // Check if pruning is disabled+ if (prune.policy === 'disable') {+ this.emitter.emit('pruneEnd', { planId, success: true, stats: null });+ return { success: true, result: 'Pruning is disabled for this plan.' };+ }// Generate restic Prune Command- const prune = options.settings.prune;const { resticArgs, policyArgs, resticEnv } = this.generatePruneCommand(planId, options);- // Perform the Prune+ // Combine ALL policy args into ONE command to ensure Restic's native "OR" logic applies safely.+ const finalCommandArgs = [...resticArgs, ...policyArgs];++ // ALWAYS keep at least 1 backup, regardless of policy or user input.+ // This prevents age-based policies from deleting the final backup if backups stop running.+ const safeKeepLast = Math.max(1, Number(prune.snapCount) || 1).toString();+ finalCommandArgs.push('--keep-last', safeKeepLast);+try {- const countFilterArgs = [...resticArgs];- // The policy prunes and the --keep-last prune should be ran separately- // The --keep-last prune should be ran after the policy prune.-- if (prune.snapCount) {- countFilterArgs.push('--keep-last', prune.snapCount.toString());- await runResticCommand([...resticArgs, ...policyArgs], resticEnv);- await runResticCommand(countFilterArgs, resticEnv);- } else {- await runResticCommand([...resticArgs, ...policyArgs], resticEnv);- }+ // Execute the combined command ONCE+ await runResticCommand(finalCommandArgs, resticEnv);let planStats;if (updateStats) {@@ -47,16 +47,12 @@ export class PruneHandler {this.emitter.emit('pruneEnd', { planId, success: true, stats: planStats });} catch (error: any) {- this.emitter.emit('pruneEnd', {- planId,- success: false,- error: error?.message || 'Unknown Error',- });pruneErrorMsg = error?.message || 'Unknown Error';+ this.emitter.emit('pruneEnd', { planId, success: false, error: pruneErrorMsg });}return {- success: pruneErrorMsg ? false : true,+ success: !pruneErrorMsg,result: pruneErrorMsg ? pruneErrorMsg : 'Pruned Old Snapshots Successfully.',};}@@ -64,6 +60,7 @@ export class PruneHandler {private generatePruneCommand(planId: string, options: Record<string, any>) {const { storagePath, storage, settings } = options;const { prune, encryption = true } = settings;+const resticArgs = ['forget', '--prune', '--tag', `plan-${planId}`];const policyArgs: string[] = [];const resticEnv = { RESTIC_PASSWORD: encryption ? configService.config.ENCRYPTION_KEY : '' };@@ -72,6 +69,7 @@ export class PruneHandler {const repoPath = generateResticRepoPath(storage.name, storagePath || '');resticArgs.push('-r', repoPath);}+switch (prune.policy) {case 'custom':if (prune.keepDailySnaps) policyArgs.push('--keep-daily', prune.keepDailySnaps.toString());@@ -82,17 +80,13 @@ export class PruneHandler {break;case 'forgetByAge':+ // This uses --keep-within. (e.g. "30d", "2m")if (prune.forgetAge) {policyArgs.push('--keep-within', prune.forgetAge);}break;-- case 'forgetByDate':- if (prune.forgetDate) {- policyArgs.push('--keep-before', prune.forgetDate);- }- break;}+if (encryption === false) {resticArgs.push('--insecure-no-password');}diff --git a/frontend/src/components/Plan/PlanPruneModal/PlanPruneModal.tsx b/frontend/src/components/Plan/PlanPruneModal/PlanPruneModal.tsxindex ca76253..79c4139 100644--- a/frontend/src/components/Plan/PlanPruneModal/PlanPruneModal.tsx+++ b/frontend/src/components/Plan/PlanPruneModal/PlanPruneModal.tsx@@ -15,6 +15,56 @@ interface PlanPruneModalProps {const PlanPruneModal = ({ planId, method, prune, snapshotsCount, taskPending, close }: PlanPruneModalProps) => {const pruneMutation = usePrunePlan();+ const getBackupMessage = () => {+ switch (prune.policy) {+ case 'keepLast':+ return (+ <>+ Your plan is set to keep the last {prune.snapCount} backups.{' '}+ {prune.snapCount < snapshotsCount+ ? `Running prune now will remove ${snapshotsCount - prune.snapCount} older snapshots, freeing up some storage space.`+ : 'There are no excess snapshots to clean up.'}+ </>+ );+ case 'forgetByAge':+ return (+ <>+ Your plan is set to remove backups older than {planIntervalAgeName(prune.forgetAge || '3m')}, while always keeping at least{' '}+ {prune.snapCount || 1} backup{(prune.snapCount || 1) > 1 ? 's' : ''}. This action will remove any backups that exceed the retention+ policy.+ </>+ );+ case 'custom':+ return (+ <>+ Your plan uses an advanced retention policy+ {prune.keepDailySnaps ? `, keeping daily backups for ${prune.keepDailySnaps} days` : ''}+ {prune.keepWeeklySnaps ? `, weekly backups for ${prune.keepWeeklySnaps} weeks` : ''}+ {prune.keepMonthlySnaps ? `, monthly backups for ${prune.keepMonthlySnaps} months` : ''}, while always keeping at least{' '}+ {prune.snapCount || 1} backup{(prune.snapCount || 1) > 1 ? 's' : ''}. This action will remove any backups that exceed the retention+ policy.+ </>+ );+ case 'disable':+ return <>Pruning is disabled for this plan. No backups will be removed.</>;+ default:+ return (+ <>+ Your plan is set to maintain {prune.snapCount} recent backups.{' '}+ {prune.snapCount < snapshotsCount+ ? `Running prune now will remove ${snapshotsCount - prune.snapCount} older snapshots, freeing up some storage space.`+ : 'There are no excess snapshots to clean up.'}+ </>+ );+ }+ };++ const canPrune = () => {+ if (prune.policy === 'disable') return false;+ if (prune.policy === 'keepLast' && prune.snapCount >= snapshotsCount) return false;+ return true;+ };+return (<ActionModaltitle={method === 'sync' ? 'Clean Up Old Revisions' : 'Clean Up Old Backups'}@@ -23,25 +73,18 @@ const PlanPruneModal = ({ planId, method, prune, snapshotsCount, taskPending, cl{method === 'sync' ? (<>Your plan is set to maintain{' '}- {prune.policy === 'forgetByAge' ? `file revisions newer than ${planIntervalAgeName(prune.forgetAge || '3m')}.` : ''}{' '}- {prune.policy === 'forgetByFileCount' ? `Only keep the last ${prune.snapCount} versions of each file.` : ''} This action will- remove any excess file revisions older than the retention policy.+ {prune.policy === 'forgetByAge' ? `file revisions newer than ${planIntervalAgeName(prune.forgetAge || '3m')}.` : ''} This action+ will remove any excess file revisions older than the retention policy.</>) : (- <>- Your plan is set to maintain {prune.snapCount} recent backups(snapshots).{' '}- {prune.snapCount < snapshotsCount- ? `Running prune now will remove ${' '}- ${snapshotsCount - prune.snapCount} older snapshots, freeing up some storage space`- : 'There are no excess snapshots to clean up.'}- </>+ getBackupMessage())}</>}closeModal={() => !pruneMutation.isPending && close()}width="500px"primaryAction={{- title: method === 'sync' ? `Yes, Remove Old Revisions` : prune.snapCount < snapshotsCount ? 'Yes, Remove Old Backups' : '',+ title: method === 'sync' ? 'Yes, Remove Old Revisions' : canPrune() ? 'Yes, Remove Old Backups' : '',type: 'default',isPending: pruneMutation.isPending,action: () =>diff --git a/frontend/src/components/Plan/PlanSettings/PlanPruneSettings.tsx b/frontend/src/components/Plan/PlanSettings/PlanPruneSettings.tsxindex 4b01147..cb394bd 100644--- a/frontend/src/components/Plan/PlanSettings/PlanPruneSettings.tsx+++ b/frontend/src/components/Plan/PlanSettings/PlanPruneSettings.tsx@@ -1,9 +1,9 @@import classes from './PlanSettings.module.scss';import Select from '../../common/form/Select/Select';-import Input from '../../common/form/Input/Input';import Tristate from '../../common/form/Tristate/Tristate';import { NewPlanSettings } from '../../../@types/plans';import NumberInput from '../../common/form/NumberInput/NumberInput';+import Toggle from '../../common/form/Toggle/Toggle';interface PlanPruneSettingsProps {plan: NewPlanSettings;@@ -11,7 +11,7 @@ interface PlanPruneSettingsProps {}const PlanPruneSettings = ({ plan, onUpdate }: PlanPruneSettingsProps) => {- const pruneSettings = plan.settings?.prune;+ const pruneSettings = plan.settings?.prune || {};return (<>@@ -19,83 +19,167 @@ const PlanPruneSettings = ({ plan, onUpdate }: PlanPruneSettingsProps) => {<label className={classes.label}>Backup Retention Policy</label><Selectoptions={[- { label: 'Remove by Age', value: 'forgetByAge' },- { label: 'Remove by Date', value: 'forgetByDate' },- { label: 'Custom Prune Policy', value: 'custom' },+ { label: 'Keep Number of Backups', value: 'keepLast' },+ { label: 'Remove Backups By Age', value: 'forgetByAge' },+ { label: 'Advanced Policy', value: 'custom' },+ { label: 'Keep All Backups (Disable Pruning)', value: 'disable' },]}- fieldValue={pruneSettings.policy}- onUpdate={(val: string) => onUpdate({ ...pruneSettings, policy: val })}+ fieldValue={pruneSettings.policy || 'keepLast'}+ onUpdate={(val: string) => {+ // Reset snapCount to a default safe value if switching contexts, or retain if preferred+ onUpdate({ ...pruneSettings, policy: val, snapCount: val === 'keepLast' ? 5 : pruneSettings.snapCount });+ }}/></div>- {pruneSettings.policy === 'forgetByAge' && (++ {/* OPTION 1: KEEP LAST N */}+ {pruneSettings.policy === 'keepLast' && (<div className={classes.field}>- <label className={classes.label}>Remove Backups Older Than</label>- <div className={classes.forgetByAgeField}>- <NumberInput- fieldValue={pruneSettings.forgetAge ? parseInt(pruneSettings.forgetAge.replace(/\D/g, ''), 10) : 3}- onUpdate={(val) =>- onUpdate({- ...pruneSettings,- forgetAge: (pruneSettings.forgetAge || '3m').replace(/\d+/g, val.toString()),- })- }- placeholder="5"- min={1}- full={true}- />- <Tristate- fieldValue={(pruneSettings.forgetAge || '3m').replace(/\d/g, '')}- options={[- { label: 'Days', value: 'd' },- { label: 'Weeks', value: 'w' },- { label: 'Months', value: 'm' },- ]}- onUpdate={(val: string) =>- onUpdate({- ...pruneSettings,- forgetAge: (pruneSettings.forgetAge || '3m').replace(/\D/g, val),- })- }- />- </div>- </div>- )}- {pruneSettings.policy === 'forgetByDate' && (- <div className={classes.field} style={{ width: '200px' }}>- <label className={classes.label}>Remove Backups Older Than</label>- <Input type="date" fieldValue={pruneSettings.forgetDate || ''} onUpdate={(val) => onUpdate({ ...pruneSettings, forgetDate: val })} />+ <NumberInput+ label="Number of Backups to Keep"+ fieldValue={pruneSettings.snapCount || 5}+ onUpdate={(val) => onUpdate({ ...pruneSettings, snapCount: val })}+ placeholder="5"+ min={1}+ hint="Only the most recent 5 backups will be kept."+ inline={false}+ /></div>)}- {pruneSettings.policy === 'custom' && (++ {/* OPTION 2: REMOVE BY AGE */}+ {pruneSettings.policy === 'forgetByAge' && (<><div className={classes.field}>- <label className={classes.label}>Custom Policy Settings</label>- <div className={classes.customPolicyOption}>- <span>Keep Daily Backups for </span>+ <label className={classes.label}>Remove Backups Older Than</label>+ <div className={classes.forgetByAgeField}><NumberInput- fieldValue={pruneSettings.keepDailySnaps || ''}- onUpdate={(val) => onUpdate({ ...pruneSettings, keepDailySnaps: val })}+ fieldValue={pruneSettings.forgetAge ? parseInt(pruneSettings.forgetAge.replace(/\D/g, ''), 10) : 3}+ onUpdate={(val) =>+ onUpdate({+ ...pruneSettings,+ forgetAge: (pruneSettings.forgetAge || '3m').replace(/\d+/g, val.toString()),+ })+ }+ placeholder="3"+ min={1}+ full={true}+ />+ <Tristate+ fieldValue={(pruneSettings.forgetAge || '3m').replace(/\d/g, '')}+ options={[+ { label: 'Days', value: 'd' },+ { label: 'Weeks', value: 'w' },+ { label: 'Months', value: 'm' },+ { label: 'Years', value: 'y' },+ ]}+ onUpdate={(val: string) =>+ onUpdate({+ ...pruneSettings,+ forgetAge: (pruneSettings.forgetAge || '3m').replace(/\D/g, val),+ })+ }/>- <span>Days</span></div>- <div className={classes.customPolicyOption}>- <span>Keep Weekly Backups for </span>+ </div>++ {/* Fail-safe keeping minimum N backups so they aren't left with 0 backups if backups fail for a month */}+ <div className={classes.field}>+ <label className={classes.label}>Minimum numbers of backups to keep</label>+ <div><NumberInput- fieldValue={pruneSettings.keepWeeklySnaps || ''}- onUpdate={(val) => onUpdate({ ...pruneSettings, keepWeeklySnaps: val })}+ fieldValue={pruneSettings.snapCount || 1}+ onUpdate={(val) => onUpdate({ ...pruneSettings, snapCount: val })}+ min={1}+ placeholder="3"+ full={true}/>- <span>Weeks</span></div>- <div className={classes.customPolicyOption}>- <span>Keep Monthly Backups for </span>+ </div>+ </>+ )}++ {/* OPTION 3: ADVANCED */}+ {pruneSettings.policy === 'custom' && (+ <div className={classes.field}>+ <label className={classes.label}>Advanced Policy Settings</label>+ <small className={classes.helperText} style={{ marginBottom: '15px', display: 'block', color: 'var(--text-muted)' }}>+ A backup is kept if it matches <strong>ANY</strong> of the checked rules below.+ </small>++ {/* Keep Daily */}+ <div className={classes.customPolicyOption}>+ <Toggle+ fieldValue={!!pruneSettings.keepDailySnaps}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepDailySnaps: val ? 7 : undefined })}+ customClasses={classes.removeRemoteToggle}+ inline={true}+ />+ <span>Keep the latest daily backup for</span>+ <NumberInput+ fieldValue={pruneSettings.keepDailySnaps || ''}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepDailySnaps: val })}+ min={1}+ />+ <span>Days</span>+ </div>++ {/* Keep Weekly */}+ <div className={classes.customPolicyOption}>+ <Toggle+ fieldValue={!!pruneSettings.keepWeeklySnaps}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepWeeklySnaps: val ? 4 : undefined })}+ customClasses={classes.removeRemoteToggle}+ inline={true}+ />+ <span>Keep the latest weekly backup for</span>+ <NumberInput+ fieldValue={pruneSettings.keepWeeklySnaps || ''}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepWeeklySnaps: val })}+ min={1}+ />+ <span>Weeks</span>+ </div>++ {/* Keep Monthly */}+ <div className={classes.customPolicyOption}>+ <Toggle+ fieldValue={!!pruneSettings.keepMonthlySnaps}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepMonthlySnaps: val ? 12 : undefined })}+ customClasses={classes.removeRemoteToggle}+ inline={true}+ />+ <span>Keep the latest monthly backup for</span>+ <NumberInput+ fieldValue={pruneSettings.keepMonthlySnaps || ''}+ onUpdate={(val) => onUpdate({ ...pruneSettings, keepMonthlySnaps: val })}+ min={1}+ />+ <span>Months</span>+ </div>+ {/* Fail-safe keeping minimum N backups so they aren't left with 0 backups if backups fail for a month */}+ <div className={classes.field} style={{ marginTop: '15px' }}>+ <label className={classes.label}>Minimum numbers of backups to keep</label>+ <div><NumberInput- fieldValue={pruneSettings.keepMonthlySnaps || ''}- onUpdate={(val) => onUpdate({ ...pruneSettings, keepMonthlySnaps: val })}+ fieldValue={pruneSettings.snapCount || 1}+ onUpdate={(val) => onUpdate({ ...pruneSettings, snapCount: val })}+ min={1}+ placeholder="3"+ full={true}/>- <span>Months</span></div></div>- </>+ </div>+ )}++ {/* OPTION 4: DISABLE */}+ {pruneSettings.policy === 'disable' && (+ <div className={classes.field}>+ <small className={classes.helperText}>+ Pruning is disabled. Old backups will never be deleted. Make sure you have enough storage!+ </small>+ </div>)}</>);diff --git a/frontend/src/components/Plan/PlanSettings/PlanSettings.module.scss b/frontend/src/components/Plan/PlanSettings/PlanSettings.module.scssindex 42a6882..7912503 100644--- a/frontend/src/components/Plan/PlanSettings/PlanSettings.module.scss+++ b/frontend/src/components/Plan/PlanSettings/PlanSettings.module.scss@@ -113,10 +113,15 @@flex-direction: row;align-items: center;margin-bottom: 10px;+ gap: 10px;& > div {width: 100px;margin: 0 10px;}+ div[class*='_toggleField'] {+ width: auto;+ margin: 0;+ }}.typeFieldActive {diff --git a/frontend/src/components/common/form/NumberInput/NumberInput.module.scss b/frontend/src/components/common/form/NumberInput/NumberInput.module.scssindex f33c869..696e53d 100644--- a/frontend/src/components/common/form/NumberInput/NumberInput.module.scss+++ b/frontend/src/components/common/form/NumberInput/NumberInput.module.scss@@ -92,6 +92,7 @@width: 100%;.input {width: 100%;+ min-width: 120px;}}&.fieldHasError {