Skip to content

Commit

Permalink
Allow setting custom interval for keeping backups
Browse files Browse the repository at this point in the history
  • Loading branch information
tkleinke committed Mar 5, 2025
1 parent d22c9fe commit ec8194e
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 5 deletions.
4 changes: 3 additions & 1 deletion desktop/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ global.setConfigDefaults = config => {
if (config.highlightCustomElements === undefined) config.highlightCustomElements = true;
setLanguages(config);
if (os.type() === 'Linux') config.isAutoUpdateActive = false;
if (!config.keepBackups) config.keepBackups = { daily: 0, weekly: 0, monthly: 0 };
if (!config.keepBackups) {
config.keepBackups = { custom: 0, customInterval: 0, daily: 0, weekly: 0, monthly: 0 };
}

return config;
};
Expand Down
14 changes: 14 additions & 0 deletions desktop/src/app/components/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ <h5 i18n="@@settings.backups">Automatische Backups</h5>
<label class="control-label col-form-label"
i18n="@@settings.backups.keep">Backup-Dateien aufbewahren</label>
<div class="row">
<div class="col-md-4 form-floating">
<input id="daily-backups-input" type="number" min="0" [ngModel]="settings.keepBackups.custom"
(ngModelChange)="settings.keepBackups.custom = $event"
class="form-control">
<label for="daily-backups-input"
i18n="@@settings.backups.keep.custom">Stunden-Backups</label>
</div>
<div class="col-md-4 form-floating">
<input id="daily-backups-input" type="number" min="0" [ngModel]="settings.keepBackups.customInterval"
(ngModelChange)="settings.keepBackups.customInterval = $event"
class="form-control">
<label for="daily-backups-input"
i18n="@@settings.backups.keep.customInterval">Alle ... Stunden</label>
</div>
<div class="col-md-4 form-floating">
<input id="daily-backups-input" type="number" min="0" [ngModel]="settings.keepBackups.daily"
(ngModelChange)="settings.keepBackups.daily = $event"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSameDay, isSameWeek as sameWeek, isSameMonth } from 'date-fns';
import { isSameDay, isSameWeek as sameWeek, isSameMonth, differenceInHours } from 'date-fns';
import { KeepBackupsSettings } from '../../settings/keep-backups-settings';
import { Backup } from '../model/backup';
import { BackupsMap } from '../model/backups-map';
Expand Down Expand Up @@ -42,21 +42,36 @@ function getRecentlyUpdatedBackups(backups: Array<Backup>, recentlyCreatedBackup

function getOutdatedBackups(backups: Array<Backup>, settings: KeepBackupsSettings): Array<Backup> {

const custom: Array<Backup> = getBackupsToKeepInCustomInterval(backups, settings.custom, settings.customInterval);
const daily: Array<Backup> = getBackupsToKeep(backups, settings.daily, isSameDay);
const weekly: Array<Backup> = getBackupsToKeep(backups, settings.weekly, isSameWeek);
const monthly: Array<Backup> = getBackupsToKeep(backups, settings.monthly, isSameMonth);

return backups.filter(backup => {
return !daily.includes(backup)
return !custom.includes(backup)
&& !daily.includes(backup)
&& !weekly.includes(backup)
&& !monthly.includes(backup)
&& backup !== backups[backups.length - 1];
});
}


function getBackupsToKeepInCustomInterval(backups: Array<Backup>, amountToKeep: number,
intervalInHours: number): Array<Backup> {

return backups.slice().reduce((result, backup) => {
if (!result.length
|| !isInCustomInterval(result[result.length - 1].creationDate, backup.creationDate, intervalInHours)) {
result.push(backup);
}
return result;
}, []).reverse().slice(0, amountToKeep);
}


function getBackupsToKeep(backups: Array<Backup>, amountToKeep: number,
isInSameTimespan: (date1: Date, date2: Date) => boolean) {
isInSameTimespan: (date1: Date, date2: Date) => boolean): Array<Backup> {

if (amountToKeep === 0) return [];

Expand Down Expand Up @@ -86,3 +101,9 @@ function isSameWeek(date1: Date, date2: Date) {

return sameWeek(date1, date2, { weekStartsOn: 1 });
}


function isInCustomInterval(date1: Date, date2: Date, intervalInHours: number): boolean {

return differenceInHours(date2, date1, { roundingMethod: 'floor' }) < intervalInHours;
}
2 changes: 2 additions & 0 deletions desktop/src/app/services/settings/keep-backups-settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface KeepBackupsSettings {

custom: number;
customInterval: number;
daily: number;
weekly: number;
monthly: number;
Expand Down
2 changes: 2 additions & 0 deletions desktop/test/hub-integration/image-sync-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('ImageSyncService', () => {
backupDirectoryPath,
isAutoUpdateActive: true,
keepBackups: {
custom: 0,
customInterval: 0,
daily: 0,
weekly: 0,
monthly: 0
Expand Down
Loading

0 comments on commit ec8194e

Please sign in to comment.